Chris Alexiuk
commited on
Commit
·
88f663f
1
Parent(s):
7540eba
Update app.py
Browse files
app.py
CHANGED
@@ -9,14 +9,20 @@ from chainlit.input_widget import Select, Switch, Slider #importing chainlit set
|
|
9 |
# You only need the api key inserted here if it's not in your .env file
|
10 |
#openai.api_key = "YOUR_API_KEY"
|
11 |
|
|
|
12 |
system_template = """
|
13 |
You are a helpful assistant who always speaks in a pleasant tone!
|
14 |
"""
|
15 |
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
@cl.on_chat_start # marks a function that will be executed at the start of a user session
|
19 |
async def start_chat():
|
|
|
|
|
20 |
settings = await cl.ChatSettings(
|
21 |
[
|
22 |
Select(
|
@@ -33,29 +39,57 @@ async def start_chat():
|
|
33 |
min=0,
|
34 |
max=2,
|
35 |
step=0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
)
|
37 |
]
|
38 |
).send()
|
39 |
|
40 |
-
|
|
|
|
|
41 |
async def setup_agent(settings):
|
42 |
print("on_settings_update", settings)
|
43 |
|
44 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
45 |
async def main(message: str):
|
46 |
-
|
47 |
|
48 |
-
prompt =
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
msg = cl.Message(content=""
|
53 |
|
|
|
54 |
async for stream_resp in await openai.ChatCompletion.acreate(
|
55 |
-
|
56 |
):
|
57 |
token = stream_resp.choices[0]["delta"].get("content", "")
|
58 |
await msg.stream_token(token)
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
await msg.send()
|
|
|
9 |
# You only need the api key inserted here if it's not in your .env file
|
10 |
#openai.api_key = "YOUR_API_KEY"
|
11 |
|
12 |
+
# ChatOpenAI Templates
|
13 |
system_template = """
|
14 |
You are a helpful assistant who always speaks in a pleasant tone!
|
15 |
"""
|
16 |
|
17 |
+
user_template = """
|
18 |
+
{input}
|
19 |
+
Think through your response step by step.
|
20 |
+
"""
|
21 |
|
22 |
@cl.on_chat_start # marks a function that will be executed at the start of a user session
|
23 |
async def start_chat():
|
24 |
+
|
25 |
+
# allows users to dynamically select their settings for generation
|
26 |
settings = await cl.ChatSettings(
|
27 |
[
|
28 |
Select(
|
|
|
39 |
min=0,
|
40 |
max=2,
|
41 |
step=0.1,
|
42 |
+
),
|
43 |
+
Slider(
|
44 |
+
id="Max Tokens",
|
45 |
+
label="OpenAI - Max Tokens",
|
46 |
+
initial=250,
|
47 |
+
min=100,
|
48 |
+
max=500,
|
49 |
+
step=10
|
50 |
)
|
51 |
]
|
52 |
).send()
|
53 |
|
54 |
+
cl.user_session("settings", settings)
|
55 |
+
|
56 |
+
@cl.on_settings_update # marks a function that "logs" settings update
|
57 |
async def setup_agent(settings):
|
58 |
print("on_settings_update", settings)
|
59 |
|
60 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
61 |
async def main(message: str):
|
62 |
+
settings = cl.user_session.get("settings")
|
63 |
|
64 |
+
prompt = Prompt(
|
65 |
+
provider=ChatOpenAI.id,
|
66 |
+
messages=[
|
67 |
+
PromptMessage(
|
68 |
+
role="system",
|
69 |
+
template=system_template,
|
70 |
+
),
|
71 |
+
PromptMessage(
|
72 |
+
role="user",
|
73 |
+
template=user_template,
|
74 |
+
formatted=template.format(input=message)
|
75 |
+
)
|
76 |
+
],
|
77 |
+
settings = settings
|
78 |
+
inputs = {"input" : message}
|
79 |
+
)
|
80 |
|
81 |
+
msg = cl.Message(content="")
|
82 |
|
83 |
+
# Call OpenAI
|
84 |
async for stream_resp in await openai.ChatCompletion.acreate(
|
85 |
+
messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
|
86 |
):
|
87 |
token = stream_resp.choices[0]["delta"].get("content", "")
|
88 |
await msg.stream_token(token)
|
89 |
|
90 |
+
# Update the prompt object with the completion
|
91 |
+
prompt.completion = msg.content
|
92 |
+
msg.prompt = prompt
|
93 |
+
|
94 |
+
# Send and close the message stream
|
95 |
await msg.send()
|