File size: 1,353 Bytes
3369d9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2927735
 
 
3369d9f
2927735
 
3369d9f
2927735
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS 
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI

class openai_chain():
    def __init__(self, inp_dir='output_reports/reports_1/faiss_index') -> None:
        self.inp_dir = inp_dir
        pass

    def get_response(self, query, k=3, type="map_reduce", model_name="gpt-3.5-turbo"):
        # Initialize OPENAI embeddings
        embedding = OpenAIEmbeddings()

        # Load Database for required PDF
        db = FAISS.load_local(self.inp_dir, embedding)

        # Get relevant docs
        docs = db.similarity_search(query, k=k)

        # Create Chain
        chain = load_qa_chain(ChatOpenAI(model=model_name), chain_type=type)

        # Get Response
        response = chain.run(input_documents=docs, question=query)

        return response

    def get_response_from_drive(self, query, database, k=3, type="stuff", model_name="gpt-3.5-turbo"):
        # Get relevant docs
        docs = database.similarity_search(query, k=k)

        # Create chain
        chain = load_qa_chain(ChatOpenAI(model=model_name), chain_type=type)

        #Get Response
        response = chain.run(input_documents=docs, question=query)
        
        return response