File size: 1,362 Bytes
36ac265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from llm_api import openai_chatbot_chain
import chainlit as cl

#|--------------------------------------------------------------------------|
#|                            On Boarding                                   |
#|--------------------------------------------------------------------------|
@cl.on_chat_start
async def on_chat_start():
    cl.user_session.set(
        "message_history",
        [{"role": "system", "content": "You are a helpful assistant."}],
    )
    app_user = cl.user_session.get("user")
    await cl.Message(f"Hello User").send()

#|--------------------------------------------------------------------------|
#|                               Chat                                       |
#|--------------------------------------------------------------------------|
@cl.on_message
async def main(user_input: cl.Message):
    message_history = cl.user_session.get("message_history")
    message_history.append({"role": "user", "content": user_input.content})

    llm_output = cl.Message(content="")
    await llm_output.send()

    stream = await openai_chatbot_chain(message_history)

    async for part in stream:
        if token := part.choices[0].delta.content or "":
            await llm_output.stream_token(token)

    message_history.append({"role": "assistant", "content": llm_output.content})
    await llm_output.update()