mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-10-30 23:29:54 +00:00 
			
		
		
		
	Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
		
			
				
	
	
		
			24 lines
		
	
	
		
			937 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			24 lines
		
	
	
		
			937 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| We can also perform document QA and return the sources that were used to answer the question. To do this we'll just need to make sure each document has a "source" key in the metadata, and we'll use the `load_qa_with_sources` helper to construct our chain:
 | |
| 
 | |
| ```python
 | |
| docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))])
 | |
| query = "What did the president say about Justice Breyer"
 | |
| docs = docsearch.similarity_search(query)
 | |
| ```
 | |
| 
 | |
| ```python
 | |
| from langchain.chains.qa_with_sources import load_qa_with_sources_chain
 | |
| 
 | |
| chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type="stuff")
 | |
| query = "What did the president say about Justice Breyer"
 | |
| chain({"input_documents": docs, "question": query}, return_only_outputs=True)
 | |
| ```
 | |
| 
 | |
| <CodeOutputBlock lang="python">
 | |
| 
 | |
| ```
 | |
|     {'output_text': ' The president thanked Justice Breyer for his service.\nSOURCES: 30-pl'}
 | |
| ```
 | |
| 
 | |
| </CodeOutputBlock>
 |