ragQuery static method

Future<RAGResult> ragQuery(
  1. String question, {
  2. RAGQueryOptions? options,
})

Query the RAG pipeline with a natural language question.

Retrieves relevant document chunks and generates an AI answer. Publishes SDKRAGEvent.queryStarted before and SDKRAGEvent.queryComplete after the operation.

question - The user's natural language question. options - Optional query parameters (system prompt, token limits, etc.).

Returns a RAGResult with the generated answer, retrieved chunks, and timing.

Throws SDKError.notInitialized if SDK is not initialized. Throws SDKError.generationFailed if the query fails.

Implementation

static Future<RAGResult> ragQuery(
  String question, {
  RAGQueryOptions? options,
}) async {
  if (!RunAnywhere.isSDKInitialized) {
    throw SDKError.notInitialized();
  }

  EventBus.shared.publish(
    SDKRAGEvent.queryStarted(questionLength: question.length),
  );

  try {
    final bridgeResult = DartBridgeRAG.shared.query(
      question,
      systemPrompt: options?.systemPrompt,
      maxTokens: options?.maxTokens ?? 512,
      temperature: options?.temperature ?? 0.7,
      topP: options?.topP ?? 0.9,
      topK: options?.topK ?? 40,
    );

    final result = RAGResult.fromBridge(bridgeResult);

    EventBus.shared.publish(
      SDKRAGEvent.queryComplete(
        answerLength: result.answer.length,
        chunksRetrieved: result.retrievedChunks.length,
        retrievalTimeMs: result.retrievalTimeMs,
        generationTimeMs: result.generationTimeMs,
        totalTimeMs: result.totalTimeMs,
      ),
    );

    return result;
  } catch (e) {
    EventBus.shared.publish(SDKRAGEvent.error(message: e.toString()));
    throw SDKError.generationFailed('RAG query failed: $e');
  }
}