knowledgeBaseTool function

Tool knowledgeBaseTool(
  1. RagPipeline rag, {
  2. String name = 'search_knowledge_base',
  3. String description = 'Search the local knowledge base for passages relevant to a query. ' 'Use it whenever the answer might be in the stored documents. ' 'Returns numbered passages with their document ids.',
  4. int maxResults = 5,
})

A tool that searches a RagPipeline's collection, so a model can look things up in on-device data instead of guessing.

The result is the retrieved chunks with their source ids, which is what a model needs to answer and cite.

Implementation

Tool knowledgeBaseTool(
  RagPipeline rag, {
  String name = 'search_knowledge_base',
  String description =
      'Search the local knowledge base for passages relevant to a query. '
      'Use it whenever the answer might be in the stored documents. '
      'Returns numbered passages with their document ids.',
  int maxResults = 5,
}) => Tool(
  name: name,
  description: description,
  schema: {
    'type': 'object',
    'properties': {
      'query': {
        'type': 'string',
        'description': 'What to look for, in the user\'s own words.',
      },
      'limit': {
        'type': 'integer',
        'description': 'How many passages to return (default 5).',
      },
    },
    'required': ['query'],
  },
  run: (input) async {
    final query = input['query'];
    if (query is! String || query.trim().isEmpty) {
      throw const LlmException('`query` must be a non-empty string');
    }
    final requested = input['limit'];
    final limit = requested is int && requested > 0
        ? (requested < maxResults ? requested : maxResults)
        : maxResults;
    final sources = await rag.retrieve(query, k: limit);
    if (sources.isEmpty) return 'No matching passages.';
    return sources
        .map(
          (s) =>
              '[${s.number}] (${s.documentId ?? s.id}) '
              '${s.text.replaceAll('\n', ' ')}',
        )
        .join('\n');
  },
);