| |
| from components.memory import autochatmemory |
| from components.memory import formatted_documentretrival |
|
|
| |
| from components.promptengine import promptenginefinalize |
|
|
| |
| from components.LLM import chatopenai, chatopenairetry, chatopenaistream, calculatetokens |
|
|
| |
| |
| import gradio as gr |
| |
| from components.configs import pagetitle, chatlabel, chatfirstmsg, pagesubtitle, inputboxlabel, inputboxplaceholder, restartbuttonlabel, sendbuttonlabel |
| |
| from components.configs import Fun_examples, Standard_examples, Followup_examples, Languages_examples |
|
|
| |
| |
| from components.frontend_logger import log_interaction_to_database |
| |
| import datetime |
| import uuid |
|
|
|
|
| |
| conversation_id= "" |
| user_message = "" |
| chatmemory = [] |
| history = [] |
|
|
| |
| def usermessagepush(user_message, history): |
| return "", history + [[user_message, None]], user_message |
| |
|
|
| |
| def handle_openaistream(content): |
| yield content |
|
|
| def chainexecutor(user_message, history, conversation_id, chatmemory): |
| if chatmemory == []: |
| conversation_id=str(uuid.uuid4()) |
| print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}:Session ID set to: {conversation_id}") |
| print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}:Assisting the user in session id {conversation_id} for the query:\n {user_message}") |
| print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}:State of my memory is\n {chatmemory}") |
| |
| relevantcontents = formatted_documentretrival(user_message) |
| |
| if chatmemory != []: |
| lastresponse= history [-2][1] |
| print(lastresponse) |
| relevantcontents += formatted_documentretrival(lastresponse, docnumber=1) |
| print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}:Fetched relevant contents:\n{relevantcontents}") |
| |
| finalprompt = promptenginefinalize(chatmemory,relevantcontents,user_message) |
| |
| stream = chatopenaistream(finalprompt, handle_openaistream) |
| |
| history[-1][1] = "" |
| for content in stream: |
| history[-1][1] += ''.join(content) |
| yield gr.update(value=""), history, conversation_id, chatmemory |
| |
| |
| assistant_response = history[-1][1] |
| usedtokens=calculatetokens(finalprompt,assistant_response) |
| print(f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}:Generated a response: \n {assistant_response} \n estimated spending: {usedtokens} tokens") |
| |
| chatmemory = autochatmemory(chatmemory,relevantcontents,user_message, assistant_response) |
| |
| log_interaction_to_database(user_message, response=assistant_response, conversation_id=conversation_id, usedtokens=usedtokens) |
| return gr.update(value=""), history, conversation_id, chatmemory |
|
|
| |
| block = gr.Blocks(theme=gr.themes.Soft(spacing_size="sm",radius_size="none")) |
| with block as DrusoGPTUI: |
| conversation_id = gr.State(conversation_id) |
| chatmemory = gr.State(chatmemory) |
| user_message = gr.State(user_message) |
| with gr.Row(): |
| gr.Markdown(pagetitle) |
| with gr.Row(): |
| gr.HTML(pagesubtitle) |
| with gr.Row(): |
| chatbot = gr.Chatbot( |
| label=chatlabel, |
| show_label=True, |
| value=[(None, chatfirstmsg)] |
| ).style(height=280) |
| with gr.Row(): |
| msg = gr.Textbox( |
| label=inputboxlabel, |
| placeholder=inputboxplaceholder, |
| lines=1, |
| ) |
| with gr.Row(): |
| submit = gr.Button(value=sendbuttonlabel, variant="primary") |
| with gr.Row(): |
| with gr.Tab("Standard"): |
| gr.Examples(Standard_examples, msg, cache_examples=False) |
| with gr.Tab("Fun"): |
| gr.Examples(Fun_examples, msg, cache_examples=False) |
| with gr.Tab("Followup"): |
| gr.Examples(Followup_examples, msg, cache_examples=False) |
| with gr.Tab("Languages"): |
| gr.Examples(Languages_examples, msg, cache_examples=False) |
| |
| |
| with gr.Row(): |
| clear = gr.Button(restartbuttonlabel, variant="secondary") |
| msg.submit(usermessagepush, [msg, chatbot], [msg, chatbot, user_message], queue=False).then(chainexecutor, [user_message, chatbot, conversation_id, chatmemory], [msg, chatbot, conversation_id, chatmemory], queue=True) |
| clear.click(lambda: None, None, chatbot, queue=False) |
| submit.click(usermessagepush, [msg, chatbot], [msg, chatbot, user_message], queue=False).then(chainexecutor, [user_message, chatbot, conversation_id, chatmemory], [msg, chatbot, conversation_id, chatmemory], queue=True) |
| if __name__ == "__main__": |
| DrusoGPTUI.queue() |
| DrusoGPTUI.launch(debug=True) |