File size: 848 Bytes
ba67ed2
 
 
 
 
 
 
835b2d9
ba67ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
import os
os.environ["OPENAI_API_KEY"]

loader = PyPDFLoader("2023_GPT4All_Technical_Report.pdf")
pages = loader.load_and_split()
# print(len(pages), pages)

embeddings = OpenAIEmbeddings()
# Create DB
# db = FAISS.from_documents(pages, embeddings)

# Save the DB in your local
# db.save_local("faiss_index")

# load the DB 
new_db = FAISS.load_local("faiss_index", embeddings)

# Init LLM
llm = ChatOpenAI()
qa_chain = RetrievalQA.from_chain_type(llm, retriever=new_db.as_retriever())

def ask(user_query):
    res=qa_chain({"query": user_query})
    return res["result"]