# import gradio as gr # import json # from typing import List, Dict # from langchain_openai.embeddings import OpenAIEmbeddings # from langchain_chroma import Chroma # from langchain.retrievers.multi_query import MultiQueryRetriever # from langchain.chains import ConversationalRetrievalChain # from langchain.memory import ConversationBufferMemory # from langchain_openai import ChatOpenAI # from langchain.schema import Document # from langchain.chains import LLMChain # from langchain.chains.question_answering import load_qa_chain # from langchain.prompts import PromptTemplate # import os # openai_api_key = os.getenv("OPENAI_API_KEY") # vectorstore = None # llm = None # qa_instance = None # chat_history = [] # def load_embeddings_from_json(json_file_path: str): # with open(json_file_path, 'r') as f: # data = json.load(f) # chunks = [item['chunk'] for item in data] # embeddings = [item['embeddings'] for item in data] # ids = [item.get('id', str(index)) for index, item in enumerate(data)] # return chunks, embeddings, ids # def initialize_chatbot_from_json(json_file_path: str, openai_api_key: str): # global vectorstore, llm, qa_instance # if vectorstore is None: # chunks, embeddings, ids = load_embeddings_from_json(json_file_path) # vectorstore = Chroma( # collection_name="my_collection", # persist_directory=None, # embedding_function=OpenAIEmbeddings(api_key=openai_api_key) # ) # vectorstore._client._add( # collection_id=vectorstore._collection.id, # ids=ids, # embeddings=embeddings, # metadatas=[{"source": "json"} for _ in chunks], # documents=chunks, # ) # if llm is None: # llm = ChatOpenAI(api_key=openai_api_key, temperature=0.5, model="gpt-4o", verbose=True) # retriever = MultiQueryRetriever.from_llm(retriever=vectorstore.as_retriever(), llm=llm) # memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) # _template = """Given the following conversation and a follow up question, rephrase the follow up question to be a # standalone question without changing the content in given question. # Chat History: # {chat_history} # Follow Up Input: {question} # Standalone question:""" # condense_question_prompt_template = PromptTemplate.from_template(_template) # prompt_template = """You are a highly informative and helpful QA System specialized in providing information related to the UPSC Exam but strictly within the 'Context'. Ensure you only answer questions that are relevant to the UPSC Exam. If the question asked is not in 'Context' and not related to the UPSC Exam, do not provide an answer. Always answer in an informative and highly detailed manner, oriented towards the UPSC Exam. Also never just answer the Query, Never tell anything about 'Context'. Dont use unnecessary lines! # Context: # {context} # Question: {question} # Helpful Answer:""" # qa_prompt = PromptTemplate( # template=prompt_template, input_variables=["context", "question"] # ) # question_generator = LLMChain(llm=llm, prompt=condense_question_prompt_template, memory=memory) # doc_chain = load_qa_chain(llm, chain_type="stuff", prompt=qa_prompt) # qa_instance = ConversationalRetrievalChain( # retriever=retriever, # question_generator=question_generator, # combine_docs_chain=doc_chain, # memory=memory) # def answer_query(question: str): # global chat_history # if qa_instance is None: # return [("Please initialize the system first.", "")] # if not question.strip(): # return [("Please enter a question.", "")] # result = qa_instance({"question": question}) # chat_history.append((question, result['answer'])) # return chat_history # with gr.Blocks() as demo: # initialize_chatbot_from_json("embeddings.json", openai_api_key) # chat_history = [] # chatbot = gr.Chatbot(label="Chatbot") # question = gr.Textbox(label="Ask a question", placeholder="Type your question...") # question.submit(answer_query, inputs=[question], outputs=[chatbot]) # if __name__ == "__main__": # demo.launch() import gradio as gr import json from typing import List, Dict from langchain_openai.embeddings import OpenAIEmbeddings from langchain_chroma import Chroma from langchain.retrievers.multi_query import MultiQueryRetriever from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from langchain_openai import ChatOpenAI from langchain.schema import Document from langchain.chains import LLMChain from langchain.chains.question_answering import load_qa_chain from langchain.prompts import PromptTemplate import os openai_api_key = os.getenv("OPENAI_API_KEY") def load_embeddings_from_json(json_file_path: str): with open(json_file_path, 'r') as f: data = json.load(f) chunks = [item['chunk'] for item in data] embeddings = [item['embeddings'] for item in data] ids = [item.get('id', str(index)) for index, item in enumerate(data)] return chunks, embeddings, ids def initialize_chatbot_from_json(json_file_path: str, openai_api_key: str): chunks, embeddings, ids = load_embeddings_from_json(json_file_path) vectorstore = Chroma( collection_name="my_collection", persist_directory=None, embedding_function=OpenAIEmbeddings(api_key=openai_api_key) ) vectorstore._client._add( collection_id=vectorstore._collection.id, ids=ids, embeddings=embeddings, metadatas=[{"source": "json"} for _ in chunks], documents=chunks, ) llm = ChatOpenAI(api_key=openai_api_key, temperature=0.5, model="gpt-4o", verbose=True) retriever = MultiQueryRetriever.from_llm(retriever=vectorstore.as_retriever(), llm=llm) memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) condense_question_prompt_template = PromptTemplate.from_template("""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question without changing the content in given question. Chat History: {chat_history} Follow Up Input: {question} Standalone question:""") qa_prompt = PromptTemplate( template="""You are a highly informative and helpful QA System specialized in providing information related to the UPSC Exam but strictly within the 'Context'. Ensure you only answer questions that are relevant to the UPSC Exam. If the question asked is not in 'Context' and not related to the UPSC Exam, do not provide an answer. Always answer in an informative and highly detailed manner, oriented towards the UPSC Exam. Also never just answer the Query, Never tell anything about 'Context'. Dont use unnecessary lines! Context: {context} Question: {question} Helpful Answer:""", input_variables=["context", "question"] ) question_generator = LLMChain(llm=llm, prompt=condense_question_prompt_template, memory=memory) doc_chain = load_qa_chain(llm, chain_type="stuff", prompt=qa_prompt) qa_instance = ConversationalRetrievalChain( retriever=retriever, question_generator=question_generator, combine_docs_chain=doc_chain, memory=memory ) return qa_instance def answer_query(question: str, chat_history): if not question.strip(): return "Please enter a question.", chat_history qa_instance = initialize_chatbot_from_json("embeddings.json", openai_api_key) result = qa_instance({"question": question, "chat_history": chat_history}) chat_history.append((question, result['answer'])) return "", chat_history with gr.Blocks() as demo: gr.Markdown( """ # AI Book Agent! Ask any UPSC relevant Query from the NCERT. """) chatbot = gr.Chatbot(label="Chatbot") question = gr.Textbox(label="Ask a question", placeholder="Type your question...") # answer_button = gr.Button("Get Answer") question.submit(answer_query, inputs=[question, chatbot], outputs=[question, chatbot]) if __name__ == "__main__": demo.launch()