Update rag_tools.py
This commit is contained in:
+53
-30
@@ -1,57 +1,80 @@
|
|||||||
"""Tools for the RAG agent: local KB search and web search via Tavily."""
|
"""Tools for the RAG agent.
|
||||||
|
|
||||||
from typing import List, Dict
|
This module defines two LangChain tools:
|
||||||
|
|
||||||
|
* ``search_local_kb`` – semantic search in the local ChromaDB vector store.
|
||||||
|
* ``web_search`` – real‑time web search using Tavily.
|
||||||
|
|
||||||
|
Both tools return a string containing the retrieved information.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from langchain_ollama import ChatOllama
|
from langchain_ollama import ChatOllama
|
||||||
from langchain_chroma import Chroma
|
|
||||||
from langchain_tavily import TavilySearchResults
|
from langchain_tavily import TavilySearchResults
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
|
|
||||||
# --- Local KB search tool -----------------------------------------------------
|
# The LLM used for summarising or formatting responses
|
||||||
|
llm = ChatOllama(model="llama3")
|
||||||
|
|
||||||
|
# Tavily client – the API key is read from the environment by the package
|
||||||
|
# (requires a .env file or the TAVILY_API_KEY environment variable).
|
||||||
|
search = TavilySearchResults()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Local knowledge base search tool
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
@tool("search_local_kb")
|
@tool("search_local_kb")
|
||||||
def search_local_kb(query: str, top_k: int = 3, vectorstore: Chroma = None) -> List[Dict]:
|
|
||||||
"""Perform a semantic search in the local Chroma vector store.
|
def search_local_kb(query: str, top_k: int = 3) -> str:
|
||||||
|
"""Perform a semantic search in the local ChromaDB vector store.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The user query.
|
The user question.
|
||||||
top_k: int
|
top_k: int, optional
|
||||||
Number of top results to return.
|
Number of top documents to return. Defaults to 3.
|
||||||
vectorstore: Chroma
|
|
||||||
The vector store to search.
|
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
List[Dict]
|
str
|
||||||
List of dictionaries containing ``content`` and ``metadata``.
|
A formatted string containing the retrieved passages.
|
||||||
"""
|
"""
|
||||||
if vectorstore is None:
|
# The vectorstore is expected to be loaded globally – the agent will
|
||||||
raise ValueError("vectorstore must be provided")
|
# provide it via the tool context. We simply call the retriever.
|
||||||
retriever = vectorstore.as_retriever(search_kwargs={"k": top_k})
|
retriever = globals().get("vectorstore_retriever")
|
||||||
docs = retriever.get_relevant_documents(query)
|
if retriever is None:
|
||||||
return [{"content": doc.page_content, "metadata": doc.metadata} for doc in docs]
|
raise RuntimeError("Vector store retriever not configured for the tool.")
|
||||||
|
|
||||||
# --- Web search tool ---------------------------------------------------------
|
docs = retriever.get_relevant_documents(query, k=top_k)
|
||||||
|
# Concatenate the documents into a single string.
|
||||||
|
passages = "\n\n".join(doc.page_content for doc in docs)
|
||||||
|
return passages
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Web search tool
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
@tool("web_search")
|
@tool("web_search")
|
||||||
def web_search(query: str, top_k: int = 3) -> List[Dict]:
|
|
||||||
"""Search the web using Tavily.
|
def web_search(query: str) -> str:
|
||||||
|
"""Search the web using Tavily and return the top results.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The user query.
|
The user question.
|
||||||
top_k: int
|
|
||||||
Number of top results to return.
|
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
List[Dict]
|
str
|
||||||
List of dictionaries containing ``title``, ``url`` and ``content``.
|
A formatted string containing the search results.
|
||||||
"""
|
"""
|
||||||
tavily = TavilySearchResults(max_results=top_k)
|
results = search.run(query)
|
||||||
results = tavily.run(query)
|
# TavilySearchResults returns a list of dicts with keys: title, url, content
|
||||||
# Tavily returns a list of dicts with keys: title, url, content
|
formatted = []
|
||||||
return results
|
for r in results:
|
||||||
|
formatted.append(f"Title: {r.get('title', 'N/A')}\nURL: {r.get('url', 'N/A')}\nSnippet: {r.get('content', 'N/A')}\n")
|
||||||
|
return "\n\n".join(formatted)
|
||||||
|
|
||||||
|
# End of rag_tools.py
|
||||||
|
|||||||
Reference in New Issue
Block a user