from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores.faiss import FAISS from langchain.docstore.document import Document from langchain.prompts import PromptTemplate from langchain.chains.question_answering import load_qa_chain from langchain.llms import OpenAI def test_document(filename, query): with open(filename) as f: article = f.read() text_splitter = CharacterTextSplitter(chunk_size=2000, chunk_overlap=0) texts = text_splitter.split_text(article) embeddings = OpenAIEmbeddings() docsearch = FAISS.from_texts(texts, embeddings) chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff") docs = docsearch.similarity_search(query) out = chain.run(input_documents=docs, question=query) print(out) document = "Summary-of-Benefits-original.txt" print("document", document) query = "What is my copay?" query = "What is my copay for a mamography?" test_document(document, query) query = "What is my copay for a mamography?" document = "summary-of-benefits-paragraphs.txt" print("document", document) test_document(document, query)