Update src/tools.py
This commit is contained in:
+18
-16
@@ -1,22 +1,24 @@
|
|||||||
"""Agent tools for interacting with the knowledge base.
|
"""Tools for the RAG agent.
|
||||||
|
|
||||||
This module defines two tools that can be used by the LangChain agent:
|
This module defines two tools that the agent can call:
|
||||||
|
|
||||||
* ``search_knowledge_base`` – performs a semantic search in the Qdrant vector store.
|
* ``search_knowledge_base`` – performs a semantic search in the Qdrant vector store.
|
||||||
* ``add_to_knowledge_base`` – adds a new document (title + content) to the store.
|
* ``add_to_knowledge_base`` – adds a document to the store.
|
||||||
|
|
||||||
Both tools are decorated with ``@tool`` from ``langchain.tools`` so that they can be
|
Both tools are decorated with ``@tool`` from ``langchain.tools`` so that the LLM can invoke them.
|
||||||
exposed to the agent.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import List, Dict, Any
|
from typing import List, Dict, Any
|
||||||
|
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
|
|
||||||
|
# Import the knowledge base implementation.
|
||||||
from .vector_store import KnowledgeBase
|
from .vector_store import KnowledgeBase
|
||||||
|
|
||||||
# Create a single global knowledge base instance that all tools will use.
|
# Create a global knowledge base instance. In a real application you might
|
||||||
# In a real deployment you might want to inject this via dependency injection.
|
# want to inject this via a dependency injection container.
|
||||||
kb = KnowledgeBase()
|
kb = KnowledgeBase()
|
||||||
|
|
||||||
@tool("search_knowledge_base")
|
@tool("search_knowledge_base")
|
||||||
@@ -26,32 +28,32 @@ def search_knowledge_base(query: str, max_results: int = 5) -> List[Dict[str, An
|
|||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The search query.
|
Search query.
|
||||||
max_results: int, optional
|
max_results: int
|
||||||
Number of top results to return. Defaults to 5.
|
Number of results to return.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
List[Dict[str, Any]]
|
list[dict]
|
||||||
A list of dictionaries containing ``content``, ``title``, ``chunk_index`` and ``score``.
|
List of dictionaries with ``content``, ``title`` and ``chunk_index``.
|
||||||
"""
|
"""
|
||||||
return kb.search(query, max_results)
|
return kb.search(query, limit=max_results)
|
||||||
|
|
||||||
@tool("add_to_knowledge_base")
|
@tool("add_to_knowledge_base")
|
||||||
def add_to_knowledge_base(content: str, title: str) -> str:
|
def add_to_knowledge_base(content: str, title: str) -> str:
|
||||||
"""Add a new document to the knowledge base.
|
"""Add a document to the knowledge base.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
content: str
|
content: str
|
||||||
Full text of the document.
|
Full text of the document.
|
||||||
title: str
|
title: str
|
||||||
Title or name of the document.
|
Title or identifier for the document.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
str
|
str
|
||||||
Confirmation message.
|
Confirmation message.
|
||||||
"""
|
"""
|
||||||
kb.add_document(content, title)
|
kb.add_document(title=title, content=content)
|
||||||
return f"Document '{title}' added to the knowledge base."
|
return f"Document '{title}' added to the knowledge base."
|
||||||
Reference in New Issue
Block a user