How to query the vector database in LangChain AgentExecutor, invoice before summarizing the 'Final Answer' after all tools have been called? #25723
Replies: 1 comment
-
Hey there, @chenkuncloud! I'm here to assist you with any bugs, questions, or contributions while you wait for a human maintainer. Let me know how I can help! To query the vector database in LangChain's Here is an example of how you can set this up: from langchain.chains import RetrievalQA
from langchain_chroma import Chroma
from langchain_openai import OpenAI, OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from langchain.agents import AgentType, Tool, initialize_agent
# Initialize the language model
llm = OpenAI(temperature=0)
# Load and split documents
from langchain_community.document_loaders import TextLoader
loader = TextLoader("path_to_your_document.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
# Create embeddings and vector store
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings, collection_name="your_collection_name")
# Create the RetrievalQA tool
retrieval_qa_tool = RetrievalQA.from_chain_type(
llm=llm, chain_type="stuff", retriever=docsearch.as_retriever()
)
# Define the tool for the agent
tools = [
Tool(
name="Document QA System",
func=retrieval_qa_tool.run,
description="useful for answering questions based on the ingested documents. Input should be a fully formed question.",
return_direct=True,
),
]
# Initialize the agent
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
# Run the agent with a query
query = "What is the capital of France?"
answer = agent.run(query)
# Print the answer
print(answer) This code initializes a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Beta Was this translation helpful? Give feedback.
All reactions