Update rag_tools.py
This commit is contained in:
+34
-30
@@ -1,64 +1,68 @@
|
|||||||
"""
|
"""Two tools used by the RAG agent.
|
||||||
Tools for the RAG agent: local semantic search and web search via Tavily.
|
|
||||||
|
- :func:`search_local_kb` – performs a semantic search in the local ChromaDB vector store.
|
||||||
|
- :func:`web_search` – performs a web search via Tavily.
|
||||||
|
|
||||||
|
Both functions are decorated with :func:`langchain.tools.tool` so that they can be used by LangChain
|
||||||
|
agents.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from langchain_community.tools.tavily import TavilySearchResults
|
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from langchain_chroma import Chroma
|
from langchain_ollama import ChatOllama
|
||||||
|
from tavily import TavilyClient
|
||||||
|
|
||||||
|
# The LLM used for generating responses. We keep a single instance.
|
||||||
|
_llm = ChatOllama(model="llama3")
|
||||||
|
|
||||||
|
# Tavily client – the API key is read from the environment by the tavily package.
|
||||||
|
_tavily_client = TavilyClient()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Local KB search tool
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@tool("search_local_kb")
|
@tool("search_local_kb")
|
||||||
|
def search_local_kb(query: str, top_k: int = 3, vectorstore=None) -> str:
|
||||||
def search_local_kb(query: str, top_k: int = 3, vectorstore: Chroma = None) -> str:
|
"""Semantic search in the local knowledge base.
|
||||||
"""Perform a semantic search in the local Chroma vector store.
|
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The user query.
|
The user query.
|
||||||
top_k: int, optional
|
top_k: int, optional
|
||||||
Number of top results to return.
|
Number of documents to return.
|
||||||
vectorstore: Chroma, optional
|
vectorstore: Chroma, optional
|
||||||
The vector store to query. If None, the function will raise an error.
|
The vector store instance. It is passed by the agent.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
str
|
str
|
||||||
Concatenated content of the top results.
|
Concatenated text of the retrieved documents.
|
||||||
"""
|
"""
|
||||||
if vectorstore is None:
|
if vectorstore is None:
|
||||||
raise ValueError("vectorstore must be provided to search_local_kb")
|
raise ValueError("vectorstore must be provided")
|
||||||
retriever = vectorstore.as_retriever(search_kwargs={"k": top_k})
|
retriever = vectorstore.as_retriever(search_kwargs={"k": top_k})
|
||||||
docs = retriever.get_relevant_documents(query)
|
docs = retriever.get_relevant_documents(query)
|
||||||
return "\n\n---\n\n".join(doc.page_content for doc in docs)
|
return "\n\n".join(doc.page_content for doc in docs)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Web search tool using Tavily
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@tool("web_search")
|
@tool("web_search")
|
||||||
|
def web_search(query: str, max_results: int = 3) -> str:
|
||||||
def web_search(query: str, top_k: int = 3) -> str:
|
"""Perform a web search via Tavily.
|
||||||
"""Search the web via Tavily and return a formatted string of results.
|
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The search query.
|
The search query.
|
||||||
top_k: int, optional
|
max_results: int, optional
|
||||||
Number of top results to return.
|
Number of search results to return.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
str
|
str
|
||||||
Formatted search results.
|
Concatenated snippets from the search results.
|
||||||
"""
|
"""
|
||||||
tavily = TavilySearchResults(max_results=top_k)
|
results = _tavily_client.search(query, max_results=max_results)
|
||||||
results = tavily.run(query)
|
snippets = [f"{res.title}\n{res.url}\n{res.content}" for res in results]
|
||||||
formatted = []
|
return "\n\n".join(snippets)
|
||||||
for i, r in enumerate(results, 1):
|
|
||||||
formatted.append(f"{i}. {r.get('title', 'No title')}\n{r.get('url', '')}\n{r.get('content', '')}")
|
# End of rag_tools.py
|
||||||
return "\n\n---\n\n".join(formatted)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user