""" Utility tools for the LangChain agent. Two tools are exposed: * ``search_knowledge_base`` – semantic search in a Qdrant collection. * ``add_to_knowledge_base`` – add a document to the same collection. Both use the :mod:`qdrant_store` module defined in this repository. """ from __future__ import annotations import json from typing import List, Dict from langchain.tools import tool from langchain_core.documents import Document # Import the singleton store instance from qdrant_store.py from .qdrant_store import store # --------------------------------------------------------------------------- @tool def search_knowledge_base(query: str, max_results: int = 5) -> str: """Return a formatted string with the top *max_results* documents. The function performs a semantic similarity search using Qdrant and returns a human‑readable list. If no results are found an explanatory message is returned. """ docs: List[Document] = store.similarity_search(query, k=max_results) if not docs: return "No relevant documents found." lines: List[str] = [] for i, doc in enumerate(docs, start=1): title = doc.metadata.get("title", f"doc{i}") lines.append(f"{i}. {title}:\n{doc.page_content[:200]}{'...' if len(doc.page_content)>200 else ''}") return "\n\n".join(lines) # --------------------------------------------------------------------------- @tool def add_to_knowledge_base(content: str, title: str = "document") -> str: """Add a document to the knowledge base. The function creates a :class:`langchain_core.documents.Document` with the supplied ``content`` and optional ``title`` metadata. It then delegates to :func:`qdrant_store.store.add_documents`. """ doc = Document(page_content=content, metadata={"title": title}) store.add_documents([doc]) return f"Added '{title}' to the knowledge base." __all__ = ["search_knowledge_base", "add_to_knowledge_base"]