antoinerossupedu commited on
Commit
e753cba
·
1 Parent(s): 870789d
Files changed (1) hide show
  1. app.py +29 -43
app.py CHANGED
@@ -13,37 +13,23 @@ from autogen import Agent, AssistantAgent, UserProxyAgent, config_list_from_json
13
  import chainlit as cl
14
 
15
  # Edit the URL Here
16
- URL = "https://sapes5.sapdevcenter.com/sap/opu/odata/sap/ZPDCDS_SRV/SEPMRA_I_Product_E"
17
-
18
- use_docker="python:3"
19
 
20
  WELCOME_MESSAGE = f"""Datascience Agent Team 👾
21
  \n\n
22
  What can we do for you today?
23
  """
24
 
 
25
  CONTEXT = f"""Access the XML data from the following link: {URL}. Utilize the libraries 'urllib.request' and 'xml.etree.ElementTree' for sending a GET request and parsing the XML data, respectively.
26
 
27
- The XML data uses three namespaces:
28
- 1. Default namespace: 'http://www.w3.org/2005/Atom'
29
- 2. 'd': 'http://schemas.microsoft.com/ado/2007/08/dataservices'
30
- 3. 'm': 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'
31
-
32
- The product details are nested within 'entry' -> 'content' -> 'm:properties' tags. Inside 'm:properties', each product detail is stored in a 'd:TagName' format.
33
-
34
- To access details of a specific product, iterate through all 'entry' tags, and for each entry, navigate to the 'd:Product' tag within 'm:properties' to check the product ID. If the product ID matches the desired ID, extract all child tags within 'm:properties' to get the product details.
35
-
36
- For queries like 'show the products from supplier 100000076', iterate through all 'entry' tags, and for each entry, navigate to the 'd:Supplier' tag within 'm:properties' to check the supplier ID. If the supplier ID matches the desired ID, extract and accumulate the 'd:Product' tags within 'm:properties' from all matching entries to list all products from that supplier.
37
-
38
  Ensure to always check the length of the context to avoid hitting the context limit. Do not express gratitude in responses. If "Thank you" or "You're welcome" are said in the conversation, send a final response. Your final response is just "TERMINATE", do not add other sentences."""
39
 
40
  # Agents
41
  USER_PROXY_NAME = "Query Agent"
42
- CODING_PLANNER = "SAP ABAP Code Planner"
43
- CODING_RUNNER = "AP DATA&AI Engineer"
44
- DATA_ANALYZER = "SAP Analysis Agent"
45
- GROUPCHAT = "Groupchat"
46
- MANAGER = "Manager"
47
 
48
  async def ask_helper(func, **kwargs):
49
  res = await func(**kwargs).send()
@@ -99,7 +85,7 @@ class ChainlitUserProxyAgent(UserProxyAgent):
99
  if res.get("value") == "exit":
100
  return "exit"
101
 
102
- reply = cl.run_sync(ask_helper(cl.AskUserMessage, content=prompt, timeout=200))
103
 
104
  return reply["content"].strip()
105
 
@@ -128,21 +114,25 @@ async def on_chat_start():
128
  try:
129
  config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
130
  coding_assistant = ChainlitAssistantAgent(
131
- name="SAP_ABAP_Code_Planner", llm_config={"config_list": config_list},
132
  system_message="""Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type.
133
  The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the SAP_DATA_and_AI Engineer.
134
  Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the SAP_DATA_and_AI Engineer.
135
  If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code.""" + CONTEXT
136
  )
137
- # coding_runner = ChainlitUserProxyAgent(
138
- # name="SAP_DATA_and_AI_Engineer", llm_config={"config_list": config_list}, human_input_mode="NEVER",
139
- # code_execution_config=False
140
- # system_message="""A Coding Engineer. Use python to run code. Interact with the SAP_ABAP_Code_Planner to run code. Report the result.
141
- # You are an AI model capable of executing code."""
142
- # )
 
 
 
 
143
  analysis_agent = ChainlitAssistantAgent(
144
- name="SAP_Analysis_Agent", llm_config={"config_list": config_list},
145
- system_message="""Analysis agent. You analyse the data outputted by SAP_DATA_and_AI_Engineer when necessary. Be concise and always summarize the data when possible.
146
  Communicate with the Query_Agent when the data is analyzed."""
147
  )
148
  user_proxy = ChainlitUserProxyAgent(
@@ -150,19 +140,15 @@ async def on_chat_start():
150
  # human_input_mode="TERMINATE",
151
  # max_consecutive_auto_reply=3,
152
  # is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
153
- code_execution_config={
154
- "last_n_messages": 3,
155
- "use_docker": False,
156
- },
157
- system_message="""Manager. Administrate the agents on a plan, Only run code on Python. Communicate with the SAP_ABAP_Code_Planner to plan the code.
158
- Communicate with the SAP_Analysis_Agent when we want to analyse the data. When there is code from SAP_ABAP_Code_Planner, we run the code in Python.
159
- Reply TERMINATE at the end of your sentence if the task has been solved at full satisfaction.
160
  Otherwise, reply CONTINUE, or the reason why the task is not solved yet."""
161
  )
162
 
163
  cl.user_session.set(USER_PROXY_NAME, user_proxy)
164
  cl.user_session.set(CODING_PLANNER, coding_assistant)
165
- # cl.user_session.set(CODING_RUNNER, coding_runner)
166
  cl.user_session.set(DATA_ANALYZER, analysis_agent)
167
 
168
  await cl.Message(content=WELCOME_MESSAGE, author="Query_Agent").send()
@@ -173,15 +159,15 @@ async def on_chat_start():
173
 
174
  @cl.on_message
175
  async def run_conversation(message: cl.Message):
176
- #try:
177
  TASK = message.content
178
  print("Task: ", TASK)
179
  coding_assistant = cl.user_session.get(CODING_PLANNER)
180
  user_proxy = cl.user_session.get(USER_PROXY_NAME)
181
- # coding_runner = cl.user_session.get(CODING_RUNNER)
182
  analysis_agent = cl.user_session.get(DATA_ANALYZER)
183
 
184
- groupchat = autogen.GroupChat(agents=[user_proxy, coding_assistant, analysis_agent], messages=[], max_round=50)
185
  manager = autogen.GroupChatManager(groupchat=groupchat)
186
 
187
  print("GC messages: ", len(groupchat.messages))
@@ -192,6 +178,6 @@ async def run_conversation(message: cl.Message):
192
  else:
193
  await cl.make_async(user_proxy.send)( manager, message=TASK, )
194
 
195
- # except Exception as e:
196
- # print("Error: ", e)
197
- # pass
 
13
  import chainlit as cl
14
 
15
  # Edit the URL Here
16
+ URL = "https://www.w3schools.com/xml/simple.xml"
 
 
17
 
18
  WELCOME_MESSAGE = f"""Datascience Agent Team 👾
19
  \n\n
20
  What can we do for you today?
21
  """
22
 
23
+ # We plug a context to the Code Planner so that it understands the File Format. This saves us money from tokens since read is cheaper than write.
24
  CONTEXT = f"""Access the XML data from the following link: {URL}. Utilize the libraries 'urllib.request' and 'xml.etree.ElementTree' for sending a GET request and parsing the XML data, respectively.
25
 
 
 
 
 
 
 
 
 
 
 
 
26
  Ensure to always check the length of the context to avoid hitting the context limit. Do not express gratitude in responses. If "Thank you" or "You're welcome" are said in the conversation, send a final response. Your final response is just "TERMINATE", do not add other sentences."""
27
 
28
  # Agents
29
  USER_PROXY_NAME = "Query Agent"
30
+ CODING_PLANNER = "Code Planner"
31
+ CODING_RUNNER = "Code Runner"
32
+ DATA_ANALYZER = "Analysis Agent"
 
 
33
 
34
  async def ask_helper(func, **kwargs):
35
  res = await func(**kwargs).send()
 
85
  if res.get("value") == "exit":
86
  return "exit"
87
 
88
+ reply = cl.run_sync(ask_helper(cl.AskUserMessage, content=prompt, timeout=60))
89
 
90
  return reply["content"].strip()
91
 
 
114
  try:
115
  config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
116
  coding_assistant = ChainlitAssistantAgent(
117
+ name="Code_Planner", llm_config={"config_list": config_list},
118
  system_message="""Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type.
119
  The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the SAP_DATA_and_AI Engineer.
120
  Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the SAP_DATA_and_AI Engineer.
121
  If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code.""" + CONTEXT
122
  )
123
+ coding_runner = ChainlitUserProxyAgent(
124
+ name="Code_Runner", llm_config={"config_list": config_list}, human_input_mode="NEVER",
125
+ code_execution_config={
126
+ "last_n_messages": 3,
127
+ "work_dir": "workspace",
128
+ "use_docker": True,
129
+ },
130
+ system_message="""A Coding Engineer. Use python to run code. Interact with the Code_Planner to run code. Report the result.
131
+ You are an AI model capable of executing code."""
132
+ )
133
  analysis_agent = ChainlitAssistantAgent(
134
+ name="Analysis_Agent", llm_config={"config_list": config_list},
135
+ system_message="""Analysis agent. You analyse the data outputted by Code_Runner when necessary. Be concise and always summarize the data when possible.
136
  Communicate with the Query_Agent when the data is analyzed."""
137
  )
138
  user_proxy = ChainlitUserProxyAgent(
 
140
  # human_input_mode="TERMINATE",
141
  # max_consecutive_auto_reply=3,
142
  # is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
143
+ code_execution_config=False,
144
+ system_message="""Manager. Administrate the agents on a plan. Communicate with the Code_Planner to plan the code.
145
+ Communicate with the Analysis_Agent when we want to analyse the data. Reply TERMINATE at the end of your sentence if the task has been solved at full satisfaction.
 
 
 
 
146
  Otherwise, reply CONTINUE, or the reason why the task is not solved yet."""
147
  )
148
 
149
  cl.user_session.set(USER_PROXY_NAME, user_proxy)
150
  cl.user_session.set(CODING_PLANNER, coding_assistant)
151
+ cl.user_session.set(CODING_RUNNER, coding_runner)
152
  cl.user_session.set(DATA_ANALYZER, analysis_agent)
153
 
154
  await cl.Message(content=WELCOME_MESSAGE, author="Query_Agent").send()
 
159
 
160
  @cl.on_message
161
  async def run_conversation(message: cl.Message):
162
+ try:
163
  TASK = message.content
164
  print("Task: ", TASK)
165
  coding_assistant = cl.user_session.get(CODING_PLANNER)
166
  user_proxy = cl.user_session.get(USER_PROXY_NAME)
167
+ coding_runner = cl.user_session.get(CODING_RUNNER)
168
  analysis_agent = cl.user_session.get(DATA_ANALYZER)
169
 
170
+ groupchat = autogen.GroupChat(agents=[user_proxy, coding_assistant, coding_runner, analysis_agent], messages=[], max_round=50)
171
  manager = autogen.GroupChatManager(groupchat=groupchat)
172
 
173
  print("GC messages: ", len(groupchat.messages))
 
178
  else:
179
  await cl.make_async(user_proxy.send)( manager, message=TASK, )
180
 
181
+ except Exception as e:
182
+ print("Error: ", e)
183
+ pass