-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
130 lines (108 loc) · 3.62 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from typing import Dict, Optional
import chainlit as cl
from chainlit.input_widget import Select, Slider, Switch
from services.chatbot_service import ChatbotService
from services.utils import model_list
workflow_mapping = {
"Simple Chatbot": "simple_chatbot",
"Multi-Step Agent": "multi_step_agent",
"Prompt Optimization": "prompt_optimization",
"Web Search": "web_search",
}
@cl.oauth_callback
def oauth_callback(
provider_id: str,
token: str,
raw_user_data: Dict[str, str],
default_user: cl.User,
) -> Optional[cl.User]:
return default_user
@cl.set_chat_profiles
async def chat_profile():
"""
Chat profile.
"""
return [
cl.ChatProfile(
name="Simple Chatbot",
markdown_description="This is a simple chatbot that can answer questions.",
# icon="./assets/agent_types/chat.png",
),
cl.ChatProfile(
name="Multi-Step Agent",
markdown_description="This is a multi-step agent that can answer questions.",
# icon="./assets/agent_types/layer-icon.png",
),
cl.ChatProfile(
name="Prompt Optimization",
markdown_description="This is a prompt optimization workflow that can optimize prompts.",
# icon="./assets/agent_types/sparkle.png",
),
cl.ChatProfile(
name="Web Search",
markdown_description="This is a web search agent that can search the internet for answers.",
# icon="./assets/agent_types/search.png",
),
]
@cl.on_chat_start
async def on_chat_start():
"""
On chat start.
"""
# Create the chatbot service
app = ChatbotService()
# Set the chatbot service in the user session
cl.user_session.set("app", app)
chat_profile = cl.user_session.get("chat_profile")
await cl.Message(
content=f"You are now chatting with the {chat_profile} workflow."
).send()
# Set the workflow type in the user session
cl.user_session.set("workflow_type", workflow_mapping[chat_profile])
# Setup settings
settings = await cl.ChatSettings(
[
Select(
id="Model",
label="OpenAI - Model",
values=list(model_list),
initial_value="llama-3.1-70b"
if "llama-3.1-70b" in model_list
else model_list[0],
),
Switch(id="Streaming", label="OpenAI - Stream Tokens", initial=True),
Slider(
id="Temperature",
label="OpenAI - Temperature",
initial=1,
min=0,
max=2,
step=0.1,
),
]
).send()
# Update the model variable in the user session
cl.user_session.set("model", settings.get("Model"))
# This receives updates in settings
@cl.on_settings_update
async def update_model(settings):
cl.user_session.set("model", settings["Model"])
@cl.on_message
async def on_message(message: cl.Message):
"""
On message.
"""
# Get the app from the user session
app = cl.user_session.get("app")
# Start a parent step
async with cl.Step(name="Processing Query") as parent_step:
parent_step.input = message.content
# Run the execute_request_workflow logic
result = await app.process_request(
user_input=message.content,
workflow_type=cl.user_session.get("workflow_type"),
)
# Set the output of the parent step
parent_step.output = result
# Send the final result back to the user
await cl.Message(content=parent_step.output).send()