mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
6.0 KiB
6.0 KiB
In [1]:
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.faiss import FAISS
from langchain.docstore.document import DocumentIn [2]:
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()In [3]:
docsearch = FAISS.from_texts(texts, embeddings)In [4]:
query = "What did the president say about Justice Breyer"
docs = docsearch.similarity_search(query)In [5]:
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAIIn [6]:
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")In [7]:
docs = [Document(page_content=t) for t in texts[:3]]In [8]:
query = "What did the president say about Justice Breyer"
chain({"input_documents": docs, "question": query}, return_only_outputs=True)Out [8]:
{'output_text': ' The president did not mention Justice Breyer.'}In [9]:
chain = load_qa_chain(OpenAI(temperature=0), chain_type="map_reduce")In [10]:
query = "What did the president say about Justice Breyer"
chain({"input_documents": docs, "question": query}, return_only_outputs=True)Out [10]:
{'output_text': ' The president did not mention Justice Breyer.'}In [11]:
chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine")In [12]:
query = "What did the president say about Justice Breyer"
chain({"input_documents": docs, "question": query}, return_only_outputs=True)Out [12]:
{'output_text': "\n\nThe president did not mention Justice Breyer in his speech to the European Parliament about building a coalition of freedom-loving nations to confront Putin, unifying European allies, countering Russia's lies with truth, and enforcing powerful economic sanctions."}In [ ]: