Update rag_tools.py
This commit is contained in:
+37
-65
@@ -2,90 +2,62 @@
|
|||||||
|
|
||||||
Two tools are provided:
|
Two tools are provided:
|
||||||
|
|
||||||
* :func:`search_local_kb` – semantic search in the local ChromaDB vector store.
|
* ``search_local_kb`` – semantic search in the local ChromaDB store.
|
||||||
* :func:`web_search` – real‑time web search via Tavily.
|
* ``web_search`` – real‑time web search via Tavily.
|
||||||
|
|
||||||
Both functions are decorated with :func:`langchain.tools.tool` so that LangChain
|
|
||||||
can expose them to the agent.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from langchain.chains import RetrievalQA
|
|
||||||
from langchain.chroma import Chroma
|
|
||||||
from langchain_ollama import ChatOllama
|
from langchain_ollama import ChatOllama
|
||||||
from langchain_tavily import TavilySearchResults
|
from langchain_tavily import TavilySearchResults
|
||||||
|
from langchain_chroma import Chroma
|
||||||
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||||
|
from langchain.docstore.document import Document
|
||||||
|
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
|
|
||||||
# Load environment variables (TAVILY_API_KEY)
|
# The LLM used for generating answers. Using the same model as the embeddings
|
||||||
from dotenv import load_dotenv
|
# keeps the pipeline consistent.
|
||||||
|
_llm = ChatOllama(model="llama3")
|
||||||
|
|
||||||
|
# Tavily client – the API key is read from the environment variable
|
||||||
|
# ``TAVILY_API_KEY`` by the TavilySearchResults class.
|
||||||
|
_tavily = TavilySearchResults()
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Local KB search
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@tool("search_local_kb")
|
@tool("search_local_kb")
|
||||||
def search_local_kb(query: str, top_k: int = 3, vectorstore: Chroma | None = None) -> str:
|
def search_local_kb(query: str, top_k: int = 3, vectorstore: Chroma = None) -> str:
|
||||||
"""Return the best answer from the local ChromaDB store.
|
"""Semantic search in the local ChromaDB vector store.
|
||||||
|
|
||||||
Parameters
|
Args:
|
||||||
----------
|
query: User question.
|
||||||
query: str
|
top_k: Number of results to return.
|
||||||
The user question.
|
vectorstore: The Chroma instance to query.
|
||||||
top_k: int
|
|
||||||
Number of documents to retrieve.
|
|
||||||
vectorstore: Chroma | None
|
|
||||||
If ``None`` the function will attempt to load the default store.
|
|
||||||
|
|
||||||
Returns
|
Returns:
|
||||||
-------
|
A string containing the concatenated top results.
|
||||||
str
|
|
||||||
The answer prefixed with the source identifier.
|
|
||||||
"""
|
"""
|
||||||
if vectorstore is None:
|
if vectorstore is None:
|
||||||
# Load the default store
|
raise ValueError("vectorstore must be provided")
|
||||||
from vectorstore import create_vectorstore
|
|
||||||
vectorstore = create_vectorstore()
|
|
||||||
|
|
||||||
retriever = vectorstore.as_retriever(search_kwargs={"k": top_k})
|
retriever = vectorstore.as_retriever(search_kwargs={"k": top_k})
|
||||||
qa_chain = RetrievalQA.from_chain_type(
|
docs: List[Document] = retriever.invoke(query)
|
||||||
llm=ChatOllama(model="llama3", temperature=0),
|
return "\n\n".join(doc.page_content for doc in docs)
|
||||||
chain_type="stuff",
|
|
||||||
retriever=retriever,
|
|
||||||
)
|
|
||||||
result = qa_chain.run(query)
|
|
||||||
return f"[chromadb] {result}"
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Web search via Tavily
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@tool("web_search")
|
@tool("web_search")
|
||||||
def web_search(query: str, top_k: int = 3) -> str:
|
def web_search(query: str, top_k: int = 3) -> str:
|
||||||
"""Return an answer based on a Tavily web search.
|
"""Search the web using Tavily.
|
||||||
|
|
||||||
Parameters
|
Args:
|
||||||
----------
|
query: User question.
|
||||||
query: str
|
top_k: Number of results to return.
|
||||||
The user question.
|
|
||||||
top_k: int
|
|
||||||
Number of search results to retrieve.
|
|
||||||
|
|
||||||
Returns
|
Returns:
|
||||||
-------
|
Concatenated snippets from the search results.
|
||||||
str
|
|
||||||
The answer prefixed with the source identifier.
|
|
||||||
"""
|
"""
|
||||||
api_key = os.getenv("TAVILY_API_KEY")
|
results = _tavily.run(query, max_results=top_k)
|
||||||
if not api_key:
|
# TavilySearchResults returns a list of dicts with keys like 'title',
|
||||||
raise RuntimeError("TAVILY_API_KEY environment variable is not set")
|
# 'content', 'url'. We return the content for simplicity.
|
||||||
tavily = TavilySearchResults(api_key=api_key, top_k=top_k)
|
return "\n\n".join(r.get("content", "") for r in results)
|
||||||
results = tavily.run(query)
|
|
||||||
snippets = "\n\n".join(item.get("content", "") for item in results)
|
*** End of File ***
|
||||||
llm = ChatOllama(model="llama3", temperature=0)
|
|
||||||
prompt = f"Answer the question based on the following web snippets:\n{snippets}\n\nQuestion: {query}\nAnswer:" # noqa: E501
|
|
||||||
answer = llm.invoke(prompt).content
|
|
||||||
return f"[tavily] {answer}"
|
|
||||||
Reference in New Issue
Block a user