File size: 3,301 Bytes
b9fa07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import re

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
# from langchain.output_parsers import OutputFixingParser
# from langchain.schema import OutputParserException

import pinecone




class model:
    def __init__(self) -> None:
        self.gpt_api = None
        self.pinecone_api = None
        self.model_name = 'text-embedding-ada-002'

        self.instructions = self.getInstructions()

        self.embeddings = None
        self.index = None
        self.vectorstore = None
    
    def setAPIKEY(self, key1, key2):
        self.gpt_api = key1
        self.pinecone_api = key2

        
    
    def initializer(self):
        self.embeddings = OpenAIEmbeddings(
            model=self.model_name,
            openai_api_key=self.gpt_api,
        )

        pinecone.init(
            api_key=self.pinecone_api,
            environment='gcp-starter',
        )
        self.index = pinecone.Index('vectordb') 

        # print("Initialized", self.index)
        # print(self.instructions)


        self.vectorstore = Pinecone(
            index=self.index,
            embedding_function=self.embeddings.embed_query,
            text_key='text',
        )
    
    def getInstructions(self):
        with open("instructions.txt", "r") as f:
            instructions = f.read()
        return instructions

    def get_response(self, query):
        """
        This function takes in an instruction and a query, and returns a response and a list of results.
        instruction: str
        query: str
        Returns: str, list
        """

        results = self.vectorstore.similarity_search(query, k=5)
        llm = ChatOpenAI(
            openai_api_key=self.gpt_api,
            model_name='gpt-3.5-turbo',
            temperature=0.0,
            request_timeout=1000
        )
        qa = RetrievalQA.from_chain_type(
            llm=llm,
            chain_type='stuff',
            retriever=self.vectorstore.as_retriever(),
        )
        response = qa.run(str(self.instructions) + str(query))

        return response



# Main

model_obj = model()


# response.setAPIKEY("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", "ce1dd6d6-6783-44ba-ac68-57cff520df1e")
# response.initializer()


# query="""
# 59 y/o male patient with h/o dyspepsia, esophageal ulcer, and overweight is here for a follow up after a colonoscopy/EGD. Colonoscopy show examined portion of the ileum was normal, a single colonic ulcer in the cecum, erythematous mucosa in the rectum, and internal hemorrhoids. Advise patient to repeat colonoscopy based on pathology for surveillance. EGD show LA grade C esophagitis, esophageal ulcers, gastroparesis, gastritis, and a normal duodenum. Path report show no significant histopathology on esophagus or stomach. Patient advised to repeat EGD in 3 months to check for healing and for surveillance. Results discussed with patient. Significance of findings and importance of surveillance explained to patient. Anti reflux measures including increase physical activity explained to patient. All patient questions were answered. Patient understand and agrees to plan.
# """

# print(response.get_response(query))