|
|
|
|
|
|
|
import gradio as gr |
|
import random |
|
import time |
|
import openai |
|
from langchain.chains.summarize import load_summarize_chain |
|
import os |
|
import json |
|
from pprint import pprint |
|
import pinecone |
|
import time |
|
from langchain.chat_models import AzureChatOpenAI |
|
from langchain.vectorstores import Pinecone |
|
from langchain.chains import RetrievalQA, LLMChain |
|
from langchain.memory import ConversationBufferWindowMemory |
|
from typing import Optional |
|
import pandas as pd |
|
from langchain.embeddings import OpenAIEmbeddings |
|
from langchain.prompts import PromptTemplate |
|
from Utility_New import MasterLLM, intent_recognition, entity_recognition_main, describe, compare, RAG, sentiment |
|
|
|
openai.api_type = "azure" |
|
openai.api_base = "https://di-sandbox-gpt4.openai.azure.com/" |
|
openai.api_version = "2023-07-01-preview" |
|
openai.api_key = "69ec3919a7314784be9c4f7414286fba" |
|
|
|
|
|
os.environ['OPENAI_API_KEY']=openai.api_key |
|
os.environ['OPENAI_API_BASE'] = openai.api_base |
|
os.environ['OPENAI_API_VERSION'] = openai.api_version |
|
os.environ['OPENAI_API_TYPE'] = openai.api_type |
|
|
|
|
|
PINECONE_API_KEY = '98bbd113-f65a-403b-b44f-507e60506d46' |
|
|
|
pinecone.init( |
|
api_key=os.environ.get('PINECONE_API_KEY') or '98bbd113-f65a-403b-b44f-507e60506d46', |
|
environment=os.environ.get('PINECONE_ENVIRONMENT') or 'gcp-starter' |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
index_name = 'rag-sbc' |
|
|
|
embedding_model = OpenAIEmbeddings(openai_api_key = os.environ.get('OPENAI_API_KEY'), |
|
deployment="text-embedding-ada-002", |
|
model="text-embedding-ada-002", |
|
openai_api_base=os.environ.get('OPENAI_API_BASE'), |
|
openai_api_type=os.environ.get('OPENAI_API_TYPE')) |
|
|
|
|
|
index = pinecone.Index(index_name) |
|
|
|
vectorstore = Pinecone(index, embedding_model.embed_query, 'text') |
|
|
|
|
|
llm = AzureChatOpenAI(deployment_name = "GPT4_32k", model_name = "gpt-4-32k", temperature=0) |
|
|
|
llm_35 = AzureChatOpenAI(deployment_name = "GPT_35_16k", model_name = "gpt-35-turbo-16k", temperature=0) |
|
|
|
conversational_memory = ConversationBufferWindowMemory( |
|
memory_key='chat_history', |
|
k=10, |
|
return_messages=True) |
|
|
|
session_memory = {} |
|
entities = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = pd.read_csv('SBC_Final_License_Level_Data.csv') |
|
|
|
data.drop(columns=['ID'],inplace=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = data.applymap(lambda x: x.strip() if isinstance(x, str) else x) |
|
LicenseName = data['License Name'].to_list() |
|
|
|
|
|
def final_output(UserPrompt): |
|
final_json = {} |
|
|
|
intent = intent_recognition(UserPrompt) |
|
|
|
|
|
|
|
print('Intent') |
|
print(intent) |
|
print() |
|
|
|
oldPrompt = UserPrompt |
|
|
|
if len(list(entities.keys())) == 0: |
|
PrevEntity = [] |
|
else: |
|
PrevEntity = entities[list(entities.keys())[-1]] |
|
|
|
Prev_Entity = [i.strip() for i in PrevEntity] |
|
Prev_Entity = list(set(Prev_Entity)) |
|
print('Prev_Entity') |
|
print(Prev_Entity) |
|
print() |
|
|
|
if len(session_memory) > 0: |
|
lastUserPrompt = list(session_memory.keys())[-1] |
|
|
|
lastOutput = session_memory[lastUserPrompt] |
|
|
|
|
|
else: |
|
lastUserPrompt = "" |
|
lastOutput = "" |
|
|
|
|
|
print("Last User Prompt") |
|
print(lastUserPrompt) |
|
print("Last Output") |
|
print(lastOutput) |
|
|
|
|
|
UserPrompt, Prev_Entity = sentiment(UserPrompt, Prev_Entity, lastUserPrompt, lastOutput) |
|
|
|
print('New Prev Entity') |
|
print(Prev_Entity) |
|
|
|
print('UserPrompt') |
|
print(UserPrompt) |
|
print() |
|
|
|
entity_main = entity_recognition_main(UserPrompt, LicenseName) |
|
entity_main = list(set(entity_main)) |
|
print('Main Entity') |
|
print(entity_main) |
|
print() |
|
|
|
entities[oldPrompt] = entity_main |
|
|
|
for i in Prev_Entity: |
|
entity_main.append(i) |
|
|
|
entity = entity_main |
|
entity = list(set(entity)) |
|
print('Full Entity') |
|
print(entity) |
|
print() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
filtered_data = data[data['License Name'].isin(entity)] |
|
filtered_data.drop(columns=["Extra Details"],inplace=True) |
|
filtered_data_dict = filtered_data.to_dict(orient='records') |
|
|
|
except: |
|
filtered_data_dict = {} |
|
|
|
c = 0 |
|
|
|
if intent in ['Transfer']: |
|
print(intent) |
|
print() |
|
output = "I have shared our last conversation on your WhatsApp" |
|
return output, intent |
|
|
|
elif len(entity)>0: |
|
for i in entity: |
|
if i in LicenseName: |
|
c+=1 |
|
|
|
if c > 0 and c == len(entity): |
|
if intent in ['Describe'] and len(entity) > 0: |
|
print('Describe') |
|
prompt, prompt_template = describe(entity, filtered_data_dict, UserPrompt) |
|
if len(entity) <=3: |
|
chain = LLMChain(llm=llm_35, prompt=prompt_template,memory = conversational_memory) |
|
else: |
|
chain = LLMChain(llm=llm_35, prompt=prompt_template,memory = conversational_memory) |
|
output = chain({'context':prompt})['text'] |
|
print(output) |
|
|
|
elif intent in ['Compare'] and len(entity) > 0: |
|
print('Compare') |
|
prompt,prompt_template = compare(entity, filtered_data_dict, UserPrompt) |
|
if len(entity) <=3: |
|
chain = LLMChain(llm=llm_35, prompt=prompt_template,memory = conversational_memory) |
|
else: |
|
chain = LLMChain(llm=llm_35, prompt=prompt_template,memory = conversational_memory) |
|
output = chain({'context':prompt})['text'] |
|
print(output) |
|
else: |
|
print("RAG1") |
|
prompt = RAG(UserPrompt,entity) |
|
if len(entity) <=3: |
|
qa = RetrievalQA.from_chain_type(llm=llm_35, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
else: |
|
qa = RetrievalQA.from_chain_type(llm=llm_35, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
output = qa.run(prompt) |
|
print(output) |
|
|
|
|
|
else: |
|
print("RAG2") |
|
prompt = RAG(UserPrompt,entity) |
|
if len(entity) <=3: |
|
qa = RetrievalQA.from_chain_type(llm=llm_35, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
else: |
|
qa = RetrievalQA.from_chain_type(llm=llm, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
output = qa.run(prompt) |
|
print(output) |
|
else: |
|
print("RAG1") |
|
prompt = RAG(UserPrompt,entity) |
|
if len(entity) <=3: |
|
qa = RetrievalQA.from_chain_type(llm=llm_35, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
else: |
|
qa = RetrievalQA.from_chain_type(llm=llm, |
|
chain_type="stuff", |
|
retriever=vectorstore.as_retriever(), |
|
memory = conversational_memory) |
|
output = qa.run(prompt) |
|
print(output) |
|
|
|
session_memory[oldPrompt] = output |
|
|
|
print("1st Output") |
|
print(output) |
|
|
|
return output, intent |
|
|
|
def final_output_formatted(UserPrompt): |
|
|
|
answer,_ = final_output(UserPrompt) |
|
final_prompt_inst = """Your job is to compress the given answer. |
|
You need to make sure the key points of the answer are retained. |
|
Your answers will be short and direct without any explanation. |
|
Remove redundancy in answer. |
|
Give answer in bullets. |
|
You need to analyze the answer and provide human like conversational responses. |
|
Provide hyperlink only when asked where or how to apply. |
|
|
|
User Prompt for your reference: {UserPrompt} |
|
Answer provided: {answer} |
|
|
|
Based on the above instructions, provide the final summarized answer.""" |
|
|
|
prompt_template_final = PromptTemplate(template = final_prompt_inst,input_variables=['UserPrompt','answer'] ) |
|
|
|
chain = LLMChain(llm=llm_35, prompt=prompt_template_final) |
|
|
|
final_text = chain({'UserPrompt':UserPrompt,'answer':answer})['text'] |
|
|
|
return final_text |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data_dict ={} |
|
|
|
|
|
|
|
def add_text(history, text): |
|
history = history + [(text, None)] |
|
return history, gr.update(value="", interactive=True) |
|
|
|
|
|
def bot(history): |
|
User_Prompt = history[-1][0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final_output_show = final_output_formatted(User_Prompt) |
|
|
|
response = final_output_show |
|
data_dict[history[-1][0]] = response |
|
history[-1][1] = "" |
|
for character in response: |
|
history[-1][1] += character |
|
time.sleep(0.05) |
|
yield history |
|
|
|
with gr.Blocks() as demo: |
|
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=0.85): |
|
txt = gr.Textbox( |
|
show_label=False, |
|
placeholder="Enter text and press enter", |
|
).style(container=False) |
|
with gr.Column(scale=0.10): |
|
submit_btn = gr.Button( |
|
'Submit', |
|
variant='primary' |
|
) |
|
with gr.Column(scale=0.10): |
|
clear_btn = gr.Button( |
|
'Clear', |
|
variant='stop' |
|
) |
|
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then( |
|
bot, chatbot, chatbot |
|
) |
|
submit_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then( |
|
bot, chatbot, chatbot |
|
) |
|
clear_btn.click(lambda: None, None, chatbot, queue=False) |
|
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False) |
|
|
|
demo.queue() |
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|
|
|
|
|