Files
langchain/docs/examples/chains/vector_db_qa.ipynb
T
2022-11-22 06:16:26 -08:00

2.3 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 [4]:
qa = VectorDBQA(llm=OpenAI(), vectorstore=docsearch)
In [5]:
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
Out [5]:
' The President said that Ketanji Brown Jackson is a consensus builder and has received a broad range of support since she was nominated.'
In [ ]: