prepareAskWith function

Future<RagAskPrompt> prepareAskWith(
  1. RagSearchTool tool,
  2. String question, {
  3. RagPromptBuilder buildPrompt = defaultAskPrompt,
})

Searches with tool (its defaultTopK passages) and builds the prompt for question.

MobileRag.prepareAsk and CollectionRag.prepareAsk call this with a new tool each time, so citation ids start at 1 for every question.

Implementation

Future<RagAskPrompt> prepareAskWith(
  RagSearchTool tool,
  String question, {
  RagPromptBuilder buildPrompt = defaultAskPrompt,
}) async {
  if (question.trim().isEmpty) {
    throw ArgumentError.value(question, 'question', 'must not be empty');
  }
  final json = await tool.call({'query': question});
  if (json['error'] case final String error) throw ArgumentError(error);
  final passages = [
    for (final r in json['results'] as List)
      RagPassage.fromLlmJson(r as Map<String, Object?>),
  ];
  return RagAskPrompt(
    question: question,
    prompt: passages.isEmpty ? '' : buildPrompt(question, passages),
    passages: passages,
  );
}