32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
"""
|
|
Tools for the RAG agent.
|
|
|
|
search_knowledge_base and add_to_knowledge_base are implemented using VectorStore.
|
|
"""
|
|
from typing import List, Dict
|
|
from langchain.tools import tool
|
|
from vector_store import VectorStore
|
|
|
|
# Instantiate a global store
|
|
store = VectorStore()
|
|
|
|
@tool
|
|
def search_knowledge_base(query: str, max_results: int = 5) -> str:
|
|
"""Semantic search in the knowledge base."""
|
|
results = store.similarity_search(query, k=max_results)
|
|
if not results:
|
|
return "No relevant documents found."
|
|
out_lines = []
|
|
for i, r in enumerate(results, 1):
|
|
out_lines.append(f"{i}. {r['content'][:200]}... (source: {r['metadata'].get('title', 'unknown')})")
|
|
return "\n".join(out_lines)
|
|
|
|
@tool
|
|
def add_to_knowledge_base(content: str, title: str = "document") -> str:
|
|
"""Add a document to the knowledge base.
|
|
|
|
The content is split into chunks and stored with metadata.
|
|
"""
|
|
store.add_documents([content], [{"title": title}])
|
|
return f"Document '{title}' added to knowledge base."
|