likhonsheikhdev commited on
Commit
36ac265
·
verified ·
1 Parent(s): e7f0b1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llm_api import openai_chatbot_chain
2
+ import chainlit as cl
3
+
4
+ #|--------------------------------------------------------------------------|
5
+ #| On Boarding |
6
+ #|--------------------------------------------------------------------------|
7
+ @cl.on_chat_start
8
+ async def on_chat_start():
9
+ cl.user_session.set(
10
+ "message_history",
11
+ [{"role": "system", "content": "You are a helpful assistant."}],
12
+ )
13
+ app_user = cl.user_session.get("user")
14
+ await cl.Message(f"Hello User").send()
15
+
16
+ #|--------------------------------------------------------------------------|
17
+ #| Chat |
18
+ #|--------------------------------------------------------------------------|
19
+ @cl.on_message
20
+ async def main(user_input: cl.Message):
21
+ message_history = cl.user_session.get("message_history")
22
+ message_history.append({"role": "user", "content": user_input.content})
23
+
24
+ llm_output = cl.Message(content="")
25
+ await llm_output.send()
26
+
27
+ stream = await openai_chatbot_chain(message_history)
28
+
29
+ async for part in stream:
30
+ if token := part.choices[0].delta.content or "":
31
+ await llm_output.stream_token(token)
32
+
33
+ message_history.append({"role": "assistant", "content": llm_output.content})
34
+ await llm_output.update()