langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Get the source document info with result

Open Sadaf-Syeda opened this issue 2 years ago • 1 comments

System Info

ValueError: run not supported when there is not exactly one output key. Got ['result', 'source_documents' ]

Who can help?

No response

Information

  • [X] The official example notebooks/scripts
  • [ ] My own modified scripts

Related Components

  • [X] LLMs/Chat Models
  • [X] Embedding Models
  • [X] Prompts / Prompt Templates / Prompt Selectors
  • [ ] Output Parsers
  • [X] Document Loaders
  • [X] Vector Stores / Retrievers
  • [X] Memory
  • [X] Agents / Agent Executors
  • [X] Tools / Toolkits
  • [X] Chains
  • [ ] Callbacks/Tracing
  • [ ] Async

Reproduction

qa = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type = "stuff", retriever=db.as_retriever(), return_source_documents=True )

agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)#, output_keys=['result','source_documents']) agent_chain = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True, memory=memory)# return_intermediate_steps=True)#, output_keys=['result','source_documents']

#)

Expected behavior

Returns the answer and source doc as well

Sadaf-Syeda avatar May 26 '23 11:05 Sadaf-Syeda

Same error message here when using a VectorDBQAWithSourcesChain as tool in combination with an agent.

Using the following workaround I got it to work (adding the lambda).

tool_data_source = Tool(
                    name = data_source_name,
                    func=lambda question: chain({"question": question}, return_only_outputs=False),
                    description="data set description",
                    return_direct=True, # Finish the chain directly after using this tool.
                )

It returns the 'answer' 'question, and 'sources' nested in the 'output' key

mick-net avatar Jun 16 '23 16:06 mick-net

faced same issue work with

result = chain(query)

result = chain.run(query) throws error

SDcodehub avatar Jun 17 '23 12:06 SDcodehub

It seems you have to call qa directly with qa({"query": query}) see https://python.langchain.com/docs/modules/chains/popular/vector_db_qa.

paluchasz avatar Jul 17 '23 11:07 paluchasz

Hi, @Sadaf-Syeda. I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you opened this issue regarding a ValueError that occurs when running a script without exactly one output key. Vermaat suggested a workaround by adding a lambda function to the script, which resolved the issue for them. SDcodehub also faced the same issue and mentioned that using result = chain(query) or result = chain.run(query) throws an error. Paluchasz suggested calling qa({"query": query}) directly to resolve the issue.

Before we close this issue, we wanted to check if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your understanding.

dosubot[bot] avatar Oct 16 '23 16:10 dosubot[bot]