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))