update app.py with o1-mini
Browse files
app.py
CHANGED
@@ -1,54 +1,65 @@
|
|
1 |
-
# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
|
2 |
-
|
3 |
-
# OpenAI Chat completion
|
4 |
import os
|
5 |
-
from openai import AsyncOpenAI
|
6 |
-
import chainlit as cl
|
7 |
-
from chainlit.prompt import Prompt, PromptMessage
|
8 |
-
from chainlit.playground.providers import ChatOpenAI
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
"""
|
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 |
settings = {
|
25 |
-
"model": "
|
26 |
-
"temperature": 0,
|
27 |
-
"max_tokens": 500,
|
28 |
-
"top_p": 1,
|
29 |
-
"frequency_penalty": 0,
|
30 |
-
"presence_penalty": 0,
|
31 |
}
|
32 |
|
33 |
cl.user_session.set("settings", settings)
|
34 |
|
35 |
-
|
36 |
-
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
37 |
async def main(message: cl.Message):
|
38 |
settings = cl.user_session.get("settings")
|
39 |
-
|
40 |
client = AsyncOpenAI()
|
41 |
|
42 |
-
print(message.content)
|
43 |
-
|
44 |
prompt = Prompt(
|
45 |
provider=ChatOpenAI.id,
|
46 |
messages=[
|
47 |
-
PromptMessage(
|
48 |
-
role="system",
|
49 |
-
template=system_template,
|
50 |
-
formatted=system_template,
|
51 |
-
),
|
52 |
PromptMessage(
|
53 |
role="user",
|
54 |
template=user_template,
|
@@ -59,22 +70,27 @@ async def main(message: cl.Message):
|
|
59 |
settings=settings,
|
60 |
)
|
61 |
|
62 |
-
print([m.to_openai() for m in prompt.messages])
|
63 |
-
|
64 |
msg = cl.Message(content="")
|
65 |
|
66 |
-
# Call OpenAI
|
67 |
async for stream_resp in await client.chat.completions.create(
|
68 |
-
messages=[m.to_openai() for m in prompt.messages],
|
|
|
|
|
69 |
):
|
70 |
token = stream_resp.choices[0].delta.content
|
71 |
-
if not
|
72 |
-
token
|
73 |
-
await msg.stream_token(token)
|
74 |
|
75 |
# Update the prompt object with the completion
|
76 |
prompt.completion = msg.content
|
77 |
msg.prompt = prompt
|
78 |
|
79 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
await msg.send()
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from openai import AsyncOpenAI
|
3 |
+
import chainlit as cl
|
4 |
+
from chainlit.prompt import Prompt, PromptMessage
|
5 |
+
from chainlit.playground.providers import ChatOpenAI
|
6 |
from dotenv import load_dotenv
|
7 |
|
8 |
load_dotenv()
|
9 |
|
10 |
+
# Combined template for o1-mini
|
11 |
+
user_template = """You are an empathetic and insightful vibe check assistant. Your role is to:
|
12 |
+
1. Help users reflect on their current emotional state
|
13 |
+
2. Provide supportive and constructive feedback
|
14 |
+
3. Suggest practical ways to maintain or improve their vibe
|
15 |
+
4. Keep responses balanced between professional and friendly
|
16 |
+
5. Always maintain a supportive and non-judgmental tone
|
17 |
+
|
18 |
+
Frame your responses in these sections:
|
19 |
+
- Current Vibe Analysis
|
20 |
+
- Key Observations
|
21 |
+
- Supportive Suggestions
|
22 |
+
- Positive Reinforcement
|
23 |
+
|
24 |
+
User message: {input}
|
25 |
+
|
26 |
+
Please consider:
|
27 |
+
- The emotional undertones in the response
|
28 |
+
- Any patterns in behavior or thinking
|
29 |
+
- Potential areas for positive growth
|
30 |
+
- Immediate actionable steps
|
31 |
+
|
32 |
+
Think through your response step by step and structure it clearly.
|
33 |
"""
|
34 |
|
35 |
+
@cl.on_chat_start
|
|
|
36 |
async def start_chat():
|
37 |
+
# Welcome message with vibe check introduction
|
38 |
+
await cl.Message(
|
39 |
+
content="π Welcome to your Vibe Check! I'm here to help you reflect on your current state "
|
40 |
+
"and provide supportive insights. Feel free to share how you're feeling or respond to any "
|
41 |
+
"of these questions:\n\n"
|
42 |
+
"1. How would you describe your energy level right now?\n"
|
43 |
+
"2. What's the strongest emotion you're experiencing?\n"
|
44 |
+
"3. What's one thing that's influencing your mood today?\n"
|
45 |
+
"4. How connected do you feel to your goals right now?\n"
|
46 |
+
"5. What's one small thing you could do to improve your vibe?\n"
|
47 |
+
).send()
|
48 |
+
|
49 |
settings = {
|
50 |
+
"model": "o1-mini", # Only including the model parameter
|
|
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
|
53 |
cl.user_session.set("settings", settings)
|
54 |
|
55 |
+
@cl.on_message
|
|
|
56 |
async def main(message: cl.Message):
|
57 |
settings = cl.user_session.get("settings")
|
|
|
58 |
client = AsyncOpenAI()
|
59 |
|
|
|
|
|
60 |
prompt = Prompt(
|
61 |
provider=ChatOpenAI.id,
|
62 |
messages=[
|
|
|
|
|
|
|
|
|
|
|
63 |
PromptMessage(
|
64 |
role="user",
|
65 |
template=user_template,
|
|
|
70 |
settings=settings,
|
71 |
)
|
72 |
|
|
|
|
|
73 |
msg = cl.Message(content="")
|
74 |
|
|
|
75 |
async for stream_resp in await client.chat.completions.create(
|
76 |
+
messages=[m.to_openai() for m in prompt.messages],
|
77 |
+
stream=True,
|
78 |
+
**settings
|
79 |
):
|
80 |
token = stream_resp.choices[0].delta.content
|
81 |
+
if token is not None:
|
82 |
+
await msg.stream_token(token)
|
|
|
83 |
|
84 |
# Update the prompt object with the completion
|
85 |
prompt.completion = msg.content
|
86 |
msg.prompt = prompt
|
87 |
|
88 |
+
# Add supportive emoji based on content
|
89 |
+
if any(word in msg.content.lower() for word in ['good', 'great', 'excellent', 'positive']):
|
90 |
+
await msg.stream_token(" β¨")
|
91 |
+
elif any(word in msg.content.lower() for word in ['challenging', 'difficult', 'hard']):
|
92 |
+
await msg.stream_token(" πͺ")
|
93 |
+
else:
|
94 |
+
await msg.stream_token(" π")
|
95 |
+
|
96 |
await msg.send()
|