import os import google.generativeai as genai from langchain_google_genai import ChatGoogleGenerativeAI from langchain_community.vectorstores import FAISS from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough from langchain.prompts import PromptTemplate import json import re from Classes.Helper_Class import DB_Retriever from typing import Optional os.environ["GOOGLE_API_KEY"] = "AIzaSyBoghqvvnMMS4bA61LjQkkPNdIRetqk438" genai.configure(api_key="AIzaSyBoghqvvnMMS4bA61LjQkkPNdIRetqk438") class OWiki: def __init__(self,**kwargs): temperature = kwargs['temperature'] self.summary = kwargs['summary_length'] model = kwargs["model"] self.db_loc = kwargs["db_loc"] self.llm = ChatGoogleGenerativeAI(model=model, temperature=temperature) self.model_embedding = kwargs['model_embeddings'] def get_summary_template(self): prompt = """Generate a summary for the following conversational data in less than {summary} lines.\nText:\n{text}\n\nSummary:""" prompt_template = PromptTemplate(template = prompt,input_variables=['summary','text']) return prompt_template def create_sql_prompt_template(self,schemas): prompt = """Write an SQL query for the following questions whose schemas are as follows.\nSQL Schema:""" for table_name,table_schema in schemas.items(): prompt+= f"Table Name: {table_name}, Schema : {table_schema}\n\n" prompt+= """\n\nQuestion:{question}\n\nAnswer:""" prompt_template = PromptTemplate(template = prompt,input_variables=['question']) return prompt_template def create_prompt_for_OIC_bot(self): template = """You are OIC(Oracle Integration Cloud) Bot.Follow chat instructions and answer the question based only on the following Chat_instructions: 1. Response must contain Question Explaination along with Potential Solution Headings. 2. Response must contain all possible Error Scenarios if applicable along with a Summary Heading containing breif summary at the end. Context: {context} Question: {question} """ prompt = PromptTemplate.from_template(template) return prompt def create_sql_agent(self,question,schemas): prompt_template = self.create_sql_prompt_template(schemas) chain = prompt_template | self.llm | StrOutputParser() response = chain.invoke({"question":question}) response = self.format_llm_response(response) return response def generate_summary(self,text): prompt_template = self.get_summary_template() chain = prompt_template | self.llm | StrOutputParser() response = chain.invoke({"text":text,"summary":self.summary}) return response def format_llm_response(self,text): bold_pattern = r"\*\*(.*?)\*\*" italic_pattern = r"\*(.*?)\*" code_pattern = r"```(.*?)```" text = text.replace('\n', '
') formatted_text = re.sub(code_pattern,"
\\1
",text) formatted_text = re.sub(bold_pattern, "\\1", formatted_text) formatted_text = re.sub(italic_pattern, "\\1", formatted_text) return formatted_text def search_from_db(self, query : str, chat_history : Optional[str] ) -> str : db = DB_Retriever(self.db_loc,self.model_embedding) retriever = db.retrieve(query) prompt = self.create_prompt_for_OIC_bot() chat_history = self.generate_summary(chat_history) retrieval_chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | self.llm | StrOutputParser() ) response = retrieval_chain.invoke(query) # response = self.format_llm_response(response) return response if __name__=="__main__": with open("src/config.json",'r') as f: hyperparameters = json.load(f) a = OWiki(**hyperparameters) # print(a.generate_summary("""User:What is ML?\nBot:Machine learning (ML) is a branch of # and computer science that focuses on the using data and algorithms to enable AI to imitate the way that humans learn, gradually improving its accuracy. # How does machine learning work? # (link resides outside ibm.com) breaks out the learning system of a machine learning algorithm into three main parts.\nUser:How to integrate with Oracle\nUser:Explain what have you explained above\nBot:""")) # print("*"*100) # hyperparameters = {"User":" id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE","User1":" id1 INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE"} # print(a.create_sql_agent("Filter out common values in table 1 and 2 based on id",**hyperparameters)) print(a.search_from_db("What is Machine Learning","You can answer out of context as well"))