query method
Query the RAG pipeline with named parameters.
Returns a RAGBridgeResult. Use RAGResult.fromBridge in rag_types.dart
to convert to the public RAGResult type.
Implementation
RAGBridgeResult query(
String question, {
String? systemPrompt,
int maxTokens = 512,
double temperature = 0.7,
double topP = 0.9,
int topK = 40,
}) {
_ensurePipeline();
final lib = PlatformLoader.loadCommons();
final queryFn = lib.lookupFunction<RacRagQueryNative, RacRagQueryDart>(
'rac_rag_query');
final freeFn = lib.lookupFunction<RacRagResultFreeNative,
RacRagResultFreeDart>('rac_rag_result_free');
final cQuery = calloc<RacRagQueryStruct>();
final cResult = calloc<RacRagResultStruct>();
try {
cQuery.ref.question = question.toNativeUtf8();
cQuery.ref.systemPrompt =
systemPrompt != null ? systemPrompt.toNativeUtf8() : nullptr;
cQuery.ref.maxTokens = maxTokens;
cQuery.ref.temperature = temperature;
cQuery.ref.topP = topP;
cQuery.ref.topK = topK;
final status = queryFn(_pipeline!, cQuery, cResult);
if (status != RAC_SUCCESS) {
throw Exception('RAG query failed: error $status');
}
final answer = cResult.ref.answer != nullptr
? cResult.ref.answer.toDartString()
: '';
final contextUsed = cResult.ref.contextUsed != nullptr
? cResult.ref.contextUsed.toDartString()
: '';
final chunks = <RAGBridgeSearchResult>[];
for (int i = 0; i < cResult.ref.numChunks; i++) {
final c = cResult.ref.retrievedChunks[i];
final meta =
c.metadataJson != nullptr ? c.metadataJson.toDartString() : null;
chunks.add(RAGBridgeSearchResult(
chunkId: c.chunkId != nullptr ? c.chunkId.toDartString() : '',
text: c.text != nullptr ? c.text.toDartString() : '',
similarityScore: c.similarityScore,
metadataJson: meta?.isEmpty == true ? null : meta,
));
}
final result = RAGBridgeResult(
answer: answer,
retrievedChunks: chunks,
contextUsed: contextUsed,
retrievalTimeMs: cResult.ref.retrievalTimeMs,
generationTimeMs: cResult.ref.generationTimeMs,
totalTimeMs: cResult.ref.totalTimeMs,
);
freeFn(cResult);
return result;
} finally {
calloc.free(cQuery.ref.question);
if (cQuery.ref.systemPrompt != nullptr) {
calloc.free(cQuery.ref.systemPrompt);
}
calloc.free(cQuery);
calloc.free(cResult);
}
}