From 8987cda79e016d389e881995dae042d3438295fe Mon Sep 17 00:00:00 2001 From: Duggempudi-BC <138879239+Duggempudi-BC@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:47:33 +0530 Subject: [PATCH] Chainlit Conversational UI (#133) * made changes in function_agent.py file * Chainlit UI added --------- Co-authored-by: THEAVINASHREDDY Co-authored-by: Dominic <34897716+shroominic@users.noreply.github.com> --- frontend/chainlitui.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 frontend/chainlitui.py diff --git a/frontend/chainlitui.py b/frontend/chainlitui.py new file mode 100644 index 00000000..e66cd820 --- /dev/null +++ b/frontend/chainlitui.py @@ -0,0 +1,64 @@ +import chainlit as cl +from codeinterpreterapi import CodeInterpreterSession +from codeinterpreterapi import File as CIFile + +UPLOADED_FILES = [] + +@cl.action_callback("upload_file") +async def on_action(action): + files = None + + # Wait for the user to upload a file + while files is None: + files = await cl.AskFileMessage( + content="Please upload a text file to begin!", accept=["text/csv"] + ).send() + # Decode the file + text_file = files[0] + text = text_file.content.decode("utf-8") + + UPLOADED_FILES.append(text_file) + + # Let the user know that the system is ready + await cl.Message( + content=f"`{text_file.name}` uploaded, it contains {len(text)} characters!" + ).send() + await action.remove() + +@cl.on_chat_start +async def start_chat(): + actions = [ + cl.Action(name="upload_file", value="example_value", description="Upload file") + ] + + await cl.Message( + content="Hello, How can I assist you today", actions=actions + ).send() + +@cl.on_message +async def run_conversation(user_message: str): + session = CodeInterpreterSession() + await session.astart() + + files = [CIFile(name=it.name, content=it.content) for it in UPLOADED_FILES] + + + response = await session.agenerate_response(user_message, files=files) + elements = [ + cl.Image( + content=file.content, + name=f"code-interpreter-image-{file.name}", + display="inline", + ) + for file in response.files + ] + actions = [ + cl.Action(name="upload_file", value="example_value", description="Upload file") + ] + await cl.Message( + content=response.content, + elements=elements, + actions=actions, + ).send() + + await session.astop() \ No newline at end of file