-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (111 loc) · 3.65 KB
/
Copy pathmain.py
File metadata and controls
133 lines (111 loc) · 3.65 KB
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
131
132
133
import logging
import typing
import chainlit as cl
from chainlit import ChatSettings, input_widget
from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.base import BaseCheckpointSaver
from langgraph.checkpoint.memory import MemorySaver
from mem0 import Memory
from app import logging_config, utils
from app.agent import Agent
from app.utils import load_chat_model
logging_config.configure()
logger = logging.getLogger(__name__)
def get_settings() -> ChatSettings:
return cl.ChatSettings(
[
input_widget.Select(
id="Model",
label="LLM - Model",
values=[
"openai:gpt-4o",
"anthropic:claude-3-5-sonnet-20241022",
"openai:gpt-4o-mini",
],
initial_index=0,
),
input_widget.Slider(
id="MaximumTokens",
label="Model - Maximum Token",
initial=1024,
min=64,
max=4096,
step=64,
),
input_widget.Slider(
id="Temperature",
label="Model - Temperature",
initial=0.2,
min=0,
max=2,
step=0.1,
),
]
)
@cl.cache
def get_checkpoint() -> BaseCheckpointSaver[str]:
return MemorySaver()
@cl.cache
def get_memory() -> Memory:
return utils.get_memory()
@cl.on_settings_update
async def setup_agent(chat_settings: dict[str, typing.Any]) -> None:
logger.info("Setting up agent with following settings:\n %s", chat_settings)
llm_model = load_chat_model(
fully_specified_name=chat_settings["Model"],
temperature=chat_settings["Temperature"],
max_tokens=chat_settings["MaximumTokens"],
)
checkpoint_memory = cl.user_session.get(
"checkpoint_memory", default=get_checkpoint()
)
cl.user_session.set("model", chat_settings["Model"])
memory = cl.user_session.get("memory", default=get_memory())
agent = Agent(llm_model, checkpoint_memory, memory)
cl.user_session.set("agent", agent)
@cl.on_chat_start
async def on_chat_start() -> None:
cl.user_session.set("memory", get_memory())
chat_settings = await get_settings().send()
await setup_agent(chat_settings)
@cl.set_starters
async def set_starters():
return [
cl.Starter(
label="Shipping times",
message="What is the estimated shipping date from now if I make the order?",
),
cl.Starter(
label="Order history",
message="What is my order history?",
),
cl.Starter(
label="Open Account",
message="I would like to open an account.",
),
cl.Starter(
label="Book appointment",
message="I'd to book an appointment.",
),
]
@cl.on_message
async def main(message: cl.Message) -> None:
agent = typing.cast(Agent, cl.user_session.get("agent"))
chat_model = typing.cast(str, cl.user_session.get("model"))
memory = typing.cast(Memory, cl.user_session.get("memory", default=get_memory()))
user_id = "123"
cb = cl.AsyncLangchainCallbackHandler()
config = RunnableConfig(
configurable=dict(
thread_id=message.thread_id,
user_id=user_id,
memory_store=memory,
model=chat_model,
),
callbacks=[cb],
)
response = cl.Message(content="")
async for event in agent.stream(message.content, config):
# Send a response back to the user
await response.stream_token(event)
await response.send()