Skip to content

Commit

Permalink
Chainlit Conversational UI (#133)
Browse files Browse the repository at this point in the history
* made changes in function_agent.py file

* Chainlit UI added

---------

Co-authored-by: THEAVINASHREDDY <avinashreddy1233@gmail.com>
Co-authored-by: Dominic <34897716+shroominic@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 12, 2023
1 parent 4666afb commit 8987cda
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions frontend/chainlitui.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8987cda

Please sign in to comment.