Vector store agent with sources #4187
-
Hi! For example, I'm trying to use a I've tried these two main concepts from your documentation with a few different variants including switching the chain type and providing parameters to the chain. For 1. I get an error that the tool cannot return more than one parameter. For 2. I can't manage to get the sources that are accessed in the vectorstore. Any help would be much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
So the tricky part is that the We can work around this by wrapping the from langchain.agents import (
AgentType,
initialize_agent,
Tool,
)
from langchain.chains import RetrievalQAWithSourcesChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
text = TextLoader(
"../state_of_the_union_2023.txt"
).load()
docs = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=0,
).split_documents(text)
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
)
qa_chain = RetrievalQAWithSourcesChain.from_chain_type(
llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo"),
chain_type="stuff",
retriever=vectorstore.as_retriever(),
)
def run_qa_chain(question):
results = qa_chain({"question":question},return_only_outputs=True)
return str(results) Then we setup our agent to use this function as a agent_exe = initialize_agent(
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo"),
tools=[
Tool(
name="State of the Union 2023 QA System",
func=run_qa_chain,
description="Useful for when you need to answer questions about "
"the most recent state of the union address. Input "
"should be a fully formed question.",
return_direct=True,
),
],
verbose=True,
) We can set Hope that helps put you in the right direction! |
Beta Was this translation helpful? Give feedback.
-
@oddrationale I am getting the following error when using your solution but with two different tools: code: def load_qa_chain(docsearch):
llm = OpenAI(
client=OpenAI,
model='text-davinci-003',
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()],
temperature=0,
max_tokens=1024,
verbose=True
)
FindallQAWithSourcesChain = RetrievalQAWithSourcesChain.from_chain_type(
llm=llm,
chain_type='refine',
retriever=docsearch.as_retriever(),
verbose=True,
)
return FindallQAWithSourcesChain
def run_qa_chain():
results = st.session_state['qa_chain']({"question": st.session_state['user_input'][-1]}, return_only_outputs=True)
return str(results)
def load_agent(companies):
with st.spinner('Loading model...'):
# Pandas agent
PandasAgent = create_pandas_dataframe_agent(
llm=ChatOpenAI(temperature=0, model='gpt-3.5-turbo-0613'),
df=companies,
verbose=True,
agent_type=AgentType.OPENAI_FUNCTIONS,
callback_manager=CallbackManager(handlers=[StreamingStdOutCallbackHandler()])
)
tools = [
Tool(
name="Findall QA with Sources System",
func=run_qa_chain(),
description=f"useful for when you need to answer questions about {st.session_state['thematic']['term']}. Will return text in natural language.",
return_direct=True
),
Tool(
name="Pandas Agent",
func=PandasAgent.run,
description="useful for when you need to query Pandas DataFrames about companies. Will return a Pandas DataFrame.",
return_direct=True
),
]
agent = initialize_agent(
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo"),
tools=tools,
verbose=True
)
return agent
Any idea why is that? |
Beta Was this translation helpful? Give feedback.
So the tricky part is that the
RetrievalQAwithSourcesChain
chain does not receive and return a single input and output. But on the other hand,Tool
s require a single string input and single string output.We can work around this by wrapping the
RetrievalQAwithSourcesChain
in a function that takes a single string input and single string output. For example, we can return theanswer
andsource
as a string.