cmagganas commited on
Commit
36f943d
β€’
1 Parent(s): 2558e7c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##########################################################################
2
+ #
3
+ #
4
+ # Waiting on https://github.com/microsoft/autogen/issues/527 to be solved
5
+ #
6
+ #
7
+ ##########################################################################
8
+
9
+
10
+ from typing import Dict, Optional, Union
11
+
12
+ from autogen import Agent, AssistantAgent, UserProxyAgent, config_list_from_json
13
+ import chainlit as cl
14
+
15
+ from dotenv import load_dotenv
16
+ import os
17
+
18
+ # Load environment variables from .env file
19
+ load_dotenv()
20
+ TASK = "Plot a chart of NVDA stock price change YTD and save it on disk."
21
+
22
+
23
+ async def ask_helper(func, **kwargs):
24
+ res = await func(**kwargs).send()
25
+ while not res:
26
+ res = await func(**kwargs).send()
27
+ return res
28
+
29
+
30
+ class ChainlitAssistantAgent(AssistantAgent):
31
+ async def a_send(
32
+ self,
33
+ message: Union[Dict, str],
34
+ recipient: Agent,
35
+ request_reply: Optional[bool] = None,
36
+ silent: Optional[bool] = False,
37
+ ) -> bool:
38
+ await cl.Message(
39
+ content=f'*Sending message to "{recipient.name}":*\n\n{message}',
40
+ author="AssistantAgent",
41
+ ).send()
42
+ await super(ChainlitAssistantAgent, self).a_send(
43
+ message=message,
44
+ recipient=recipient,
45
+ request_reply=request_reply,
46
+ silent=silent,
47
+ )
48
+
49
+
50
+ class ChainlitUserProxyAgent(UserProxyAgent):
51
+ async def get_human_input(self, prompt: str) -> str:
52
+ if prompt.startswith(
53
+ "Provide feedback to assistant. Press enter to skip and use auto-reply"
54
+ ):
55
+ res = await ask_helper(
56
+ cl.AskActionMessage,
57
+ content="Continue or provide feedback?",
58
+ actions=[
59
+ cl.Action(
60
+ name="continue", value="continue", label="βœ… Continue"
61
+ ),
62
+ cl.Action(
63
+ name="feedback",
64
+ value="feedback",
65
+ label="πŸ’¬ Provide feedback",
66
+ ),
67
+ cl.Action(
68
+ name="exit",
69
+ value="exit",
70
+ label="πŸ”š Exit Conversation"
71
+ ),
72
+ ],
73
+ )
74
+ if res.get("value") == "continue":
75
+ return ""
76
+ if res.get("value") == "exit":
77
+ return "exit"
78
+
79
+ reply = await ask_helper(
80
+ cl.AskUserMessage, content=prompt, timeout=60)
81
+
82
+ return reply["content"].strip()
83
+
84
+ async def a_send(
85
+ self,
86
+ message: Union[Dict, str],
87
+ recipient: Agent,
88
+ request_reply: Optional[bool] = None,
89
+ silent: Optional[bool] = False,
90
+ ):
91
+ await cl.Message(
92
+ content=f'*Sending message to "{recipient.name}"*:\n\n{message}',
93
+ author="UserProxyAgent",
94
+ ).send()
95
+ await super(ChainlitUserProxyAgent, self).a_send(
96
+ message=message,
97
+ recipient=recipient,
98
+ request_reply=request_reply,
99
+ silent=silent,
100
+ )
101
+
102
+
103
+ @cl.on_chat_start
104
+ async def on_chat_start():
105
+ config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
106
+ assistant = ChainlitAssistantAgent(
107
+ "assistant", llm_config={"config_list": config_list}
108
+ )
109
+ user_proxy = ChainlitUserProxyAgent(
110
+ "user_proxy",
111
+ code_execution_config={
112
+ "work_dir": "workspace",
113
+ "use_docker": False,
114
+ },
115
+ )
116
+ await cl.Message(content=f"Starting agents on task: {TASK}...").send()
117
+ await user_proxy.a_initiate_chat(
118
+ assistant,
119
+ message=TASK,
120
+ )