-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchainlit_agents.py
81 lines (75 loc) · 2.62 KB
/
chainlit_agents.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
from autogen.agentchat import Agent, AssistantAgent, UserProxyAgent
from typing import Dict, Optional, Union, Callable
import chainlit as cl
async def ask_helper(func, **kwargs):
res = await func(**kwargs).send()
while not res:
res = await func(**kwargs).send()
return res
class ChainlitAssistantAgent(AssistantAgent):
"""
Wrapper for AutoGens Assistant Agent
"""
def send(
self,
message: Union[Dict, str],
recipient: Agent,
request_reply: Optional[bool] = None,
silent: Optional[bool] = False,
) -> bool:
cl.run_sync(
cl.Message(
content=f'*Sending message to "{recipient.name}":*\n\n{message}',
author=self.name,
).send()
)
super(ChainlitAssistantAgent, self).send(
message=message,
recipient=recipient,
request_reply=request_reply,
silent=silent,
)
class ChainlitUserProxyAgent(UserProxyAgent):
"""
Wrapper for AutoGens UserProxy Agent. Simplifies the UI by adding CL Actions.
"""
def get_human_input(self, prompt: str) -> str:
if prompt.startswith(
"Provide feedback to chat_manager. Press enter to skip and use auto-reply"
):
res = cl.run_sync(
ask_helper(
cl.AskActionMessage,
content="Continue or provide feedback?",
actions=[
cl.Action( name="continue", value="continue", label="✅ Continue" ),
cl.Action( name="feedback",value="feedback", label="💬 Provide feedback"),
cl.Action( name="exit",value="exit", label="🔚 Exit Conversation" )
],
)
)
if res.get("value") == "continue":
return ""
if res.get("value") == "exit":
return "TERMINATE"
reply = cl.run_sync(ask_helper(cl.AskUserMessage, content=prompt, timeout=60))
return reply["output"].strip()
def send(
self,
message: Union[Dict, str],
recipient: Agent,
request_reply: Optional[bool] = None,
silent: Optional[bool] = False,
):
#cl.run_sync(
#cl.Message(
# content=f'*Sending message to "{recipient.name}"*:\n\n{message}',
# author=self.name,
#).send()
#)
super(ChainlitUserProxyAgent, self).send(
message=message,
recipient=recipient,
request_reply=request_reply,
silent=silent,
)