.ipynb
.pdf
VectorDB Question Answering with Sources
 Contents 
Chain Type
VectorDB Question Answering with Sources#
This notebook goes over how to do question-answering with sources over a vector database. It does this by using the VectorDBQAWithSourcesChain, which does the lookup of the documents from a vector database.
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings.cohere import CohereEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch
from langchain.vectorstores import Chroma
with open('../../state_of_the_union.txt') as f:
    state_of_the_union = f.read()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_text(state_of_the_union)
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": f"{i}-pl"} for i in range(len(texts))])
Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.
Exiting: Cleaning up .chroma directory
from langchain.chains import VectorDBQAWithSourcesChain
from langchain import OpenAI
chain = VectorDBQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type="stuff", vectorstore=docsearch)
chain({"question": "What did the president say about Justice Breyer"}, return_only_outputs=True)
{'answer': ' The president thanked Justice Breyer for his service and mentioned his legacy of excellence.\n',
 'sources': '30-pl'}
Chain Type#