Files
langchain/docs/examples/chains/vector_db_qa.ipynb

2.6 KiB

Vector DB Question/Answering

This example showcases question answering over a vector database.

In [1]:
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores.faiss import FAISS
from langchain.text_splitter import CharacterTextSplitter
from langchain import OpenAI, VectorDBQA
In [3]:
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 = FAISS.from_texts(texts, embeddings)
In [5]:
qa = VectorDBQA.from_llm(llm=OpenAI(), vectorstore=docsearch)
In [4]:
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
Out [4]:
" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator and federal public defender, and from a family of public school educators and police officers. He also said that she has received a broad range of support since she was nominated, from the Fraternal Order of Police to former judges appointed by Democrats and Republicans."
In [ ]: