Spaces:
Sleeping
Sleeping
Add speaker selection method, update timeout logic, add agents config changes detection, add naming rule
Browse files- app.py +26 -69
- autogen_utils.py +6 -5
- custom_widgets.py +2 -2
app.py
CHANGED
|
@@ -34,42 +34,25 @@ def get_description_text():
|
|
| 34 |
"""
|
| 35 |
|
| 36 |
|
| 37 |
-
template.main.append(
|
| 38 |
-
pn.pane.Markdown(get_description_text(), sizing_mode="stretch_width")
|
| 39 |
-
)
|
| 40 |
|
| 41 |
txt_model = TextInput(
|
| 42 |
-
name="Model Name",
|
| 43 |
-
placeholder="Enter your model name here...",
|
| 44 |
-
value="gpt-35-turbo",
|
| 45 |
-
sizing_mode="stretch_width",
|
| 46 |
)
|
| 47 |
pwd_openai_key = PasswordInput(
|
| 48 |
-
name="OpenAI API Key",
|
| 49 |
-
placeholder="Enter your OpenAI API Key here...",
|
| 50 |
-
sizing_mode="stretch_width",
|
| 51 |
)
|
| 52 |
pwd_openai_url = PasswordInput(
|
| 53 |
-
name="OpenAI Base Url",
|
| 54 |
-
placeholder="Enter your OpenAI Base Url here...",
|
| 55 |
-
sizing_mode="stretch_width",
|
| 56 |
)
|
| 57 |
pwd_aoai_key = PasswordInput(
|
| 58 |
-
name="Azure OpenAI API Key",
|
| 59 |
-
placeholder="Enter your Azure OpenAI API Key here...",
|
| 60 |
-
sizing_mode="stretch_width",
|
| 61 |
)
|
| 62 |
pwd_aoai_url = PasswordInput(
|
| 63 |
-
name="Azure OpenAI Base Url",
|
| 64 |
-
placeholder="Enter your Azure OpenAI Base Url here...",
|
| 65 |
-
sizing_mode="stretch_width",
|
| 66 |
)
|
| 67 |
file_cfg = pn.widgets.FileInput(filename="OAI_CONFIG_LIST", sizing_mode="stretch_width")
|
| 68 |
-
template.main.append(
|
| 69 |
-
pn.Row(
|
| 70 |
-
txt_model, pwd_openai_key, pwd_openai_url, pwd_aoai_key, pwd_aoai_url, file_cfg
|
| 71 |
-
)
|
| 72 |
-
)
|
| 73 |
|
| 74 |
|
| 75 |
def get_config(tmpfilename="OAI_CONFIG_LIST"):
|
|
@@ -123,9 +106,8 @@ def get_config(tmpfilename="OAI_CONFIG_LIST"):
|
|
| 123 |
|
| 124 |
btn_add = Button(name="+", button_type="success")
|
| 125 |
btn_remove = Button(name="-", button_type="danger")
|
| 126 |
-
switch_code = Switch(
|
| 127 |
-
|
| 128 |
-
)
|
| 129 |
template.main.append(
|
| 130 |
pn.Row(
|
| 131 |
pn.pane.Markdown("## Add or Remove Agents: "),
|
|
@@ -133,6 +115,8 @@ template.main.append(
|
|
| 133 |
btn_remove,
|
| 134 |
pn.pane.Markdown("### Run Code: "),
|
| 135 |
switch_code,
|
|
|
|
|
|
|
| 136 |
)
|
| 137 |
)
|
| 138 |
|
|
@@ -192,8 +176,7 @@ class myGroupChatManager(autogen.GroupChatManager):
|
|
| 192 |
content = autogen.OpenAIWrapper.instantiate(
|
| 193 |
content,
|
| 194 |
message["context"],
|
| 195 |
-
self.llm_config
|
| 196 |
-
and self.llm_config.get("allow_format_str_template", False),
|
| 197 |
)
|
| 198 |
if "function_call" in message:
|
| 199 |
function_call = dict(message["function_call"])
|
|
@@ -240,39 +223,26 @@ def init_groupchat(event, collection_name):
|
|
| 240 |
else False
|
| 241 |
)
|
| 242 |
agent = initialize_agents(
|
| 243 |
-
llm_config,
|
| 244 |
-
agent_name,
|
| 245 |
-
system_msg,
|
| 246 |
-
agent_type,
|
| 247 |
-
retrieve_config,
|
| 248 |
-
code_execution_config,
|
| 249 |
-
)
|
| 250 |
-
agent.register_reply(
|
| 251 |
-
[autogen.Agent, None], reply_func=send_messages, config={"callback": None}
|
| 252 |
)
|
|
|
|
| 253 |
agents.append(agent)
|
| 254 |
if len(agents) >= 3:
|
| 255 |
groupchat = autogen.GroupChat(
|
| 256 |
agents=agents,
|
| 257 |
messages=[],
|
| 258 |
max_round=12,
|
| 259 |
-
speaker_selection_method=
|
| 260 |
allow_repeat_speaker=False,
|
| 261 |
)
|
| 262 |
manager = myGroupChatManager(groupchat=groupchat, llm_config=llm_config)
|
| 263 |
else:
|
| 264 |
manager = None
|
| 265 |
-
return agents, manager
|
| 266 |
|
| 267 |
|
| 268 |
async def agents_chat(init_sender, manager, contents, agents):
|
| 269 |
-
recipient = (
|
| 270 |
-
manager
|
| 271 |
-
if len(agents) > 2
|
| 272 |
-
else agents[1]
|
| 273 |
-
if agents[1] != init_sender
|
| 274 |
-
else agents[0]
|
| 275 |
-
)
|
| 276 |
if isinstance(init_sender, (RetrieveUserProxyAgent, MathUserProxyAgent)):
|
| 277 |
await init_sender.a_initiate_chat(recipient, problem=contents)
|
| 278 |
else:
|
|
@@ -286,12 +256,9 @@ async def reply_chat(contents, user, instance):
|
|
| 286 |
collection_name = f"{int(time.time())}_{random.randint(0, 100000)}"
|
| 287 |
instance.collection_name = collection_name
|
| 288 |
|
| 289 |
-
column_agents_list = [agent[0]
|
| 290 |
-
if (
|
| 291 |
-
|
| 292 |
-
or instance.agents_list != column_agents_list
|
| 293 |
-
):
|
| 294 |
-
agents, manager = init_groupchat(None, collection_name)
|
| 295 |
instance.manager = manager
|
| 296 |
instance.agents = agents
|
| 297 |
instance.agents_list = column_agents_list
|
|
@@ -321,7 +288,7 @@ async def reply_chat(contents, user, instance):
|
|
| 321 |
|
| 322 |
if not init_sender:
|
| 323 |
init_sender = agents[0]
|
| 324 |
-
await generate_code(agents, manager, contents, code_editor)
|
| 325 |
await agents_chat(init_sender, manager, contents, agents)
|
| 326 |
return "The task is done. Please start a new task."
|
| 327 |
|
|
@@ -361,18 +328,10 @@ btn_msg2.on_click(load_message)
|
|
| 361 |
btn_msg3.on_click(load_message)
|
| 362 |
|
| 363 |
|
| 364 |
-
btn_example1 = Button(
|
| 365 |
-
|
| 366 |
-
)
|
| 367 |
-
|
| 368 |
-
name="RAG 2 agents", button_type="primary", sizing_mode="stretch_width"
|
| 369 |
-
)
|
| 370 |
-
btn_example3 = Button(
|
| 371 |
-
name="Software Dev 3 agents", button_type="primary", sizing_mode="stretch_width"
|
| 372 |
-
)
|
| 373 |
-
btn_example4 = Button(
|
| 374 |
-
name="Research 6 agents", button_type="primary", sizing_mode="stretch_width"
|
| 375 |
-
)
|
| 376 |
template.main.append(
|
| 377 |
pn.Row(
|
| 378 |
pn.pane.Markdown("## Agent Examples: ", sizing_mode="stretch_width"),
|
|
@@ -540,9 +499,7 @@ btn_example2.on_click(load_example)
|
|
| 540 |
btn_example3.on_click(load_example)
|
| 541 |
btn_example4.on_click(load_example)
|
| 542 |
|
| 543 |
-
code_editor = CodeEditor(
|
| 544 |
-
value="", sizing_mode="stretch_width", language="python", height=300
|
| 545 |
-
)
|
| 546 |
template.main.append(code_editor)
|
| 547 |
|
| 548 |
template.servable(title=TITLE)
|
|
|
|
| 34 |
"""
|
| 35 |
|
| 36 |
|
| 37 |
+
template.main.append(pn.pane.Markdown(get_description_text(), sizing_mode="stretch_width"))
|
|
|
|
|
|
|
| 38 |
|
| 39 |
txt_model = TextInput(
|
| 40 |
+
name="Model Name", placeholder="Enter your model name here...", value="gpt-35-turbo", sizing_mode="stretch_width"
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
pwd_openai_key = PasswordInput(
|
| 43 |
+
name="OpenAI API Key", placeholder="Enter your OpenAI API Key here...", sizing_mode="stretch_width"
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
pwd_openai_url = PasswordInput(
|
| 46 |
+
name="OpenAI Base Url", placeholder="Enter your OpenAI Base Url here...", sizing_mode="stretch_width"
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
pwd_aoai_key = PasswordInput(
|
| 49 |
+
name="Azure OpenAI API Key", placeholder="Enter your Azure OpenAI API Key here...", sizing_mode="stretch_width"
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
pwd_aoai_url = PasswordInput(
|
| 52 |
+
name="Azure OpenAI Base Url", placeholder="Enter your Azure OpenAI Base Url here...", sizing_mode="stretch_width"
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
file_cfg = pn.widgets.FileInput(filename="OAI_CONFIG_LIST", sizing_mode="stretch_width")
|
| 55 |
+
template.main.append(pn.Row(txt_model, pwd_openai_key, pwd_openai_url, pwd_aoai_key, pwd_aoai_url, file_cfg))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def get_config(tmpfilename="OAI_CONFIG_LIST"):
|
|
|
|
| 106 |
|
| 107 |
btn_add = Button(name="+", button_type="success")
|
| 108 |
btn_remove = Button(name="-", button_type="danger")
|
| 109 |
+
switch_code = Switch(name="Run Code", sizing_mode="fixed", width=50, height=30, align="end")
|
| 110 |
+
select_speaker_method = pn.widgets.Select(name="", options=["round_robin", "auto", "random"], value="round_robin")
|
|
|
|
| 111 |
template.main.append(
|
| 112 |
pn.Row(
|
| 113 |
pn.pane.Markdown("## Add or Remove Agents: "),
|
|
|
|
| 115 |
btn_remove,
|
| 116 |
pn.pane.Markdown("### Run Code: "),
|
| 117 |
switch_code,
|
| 118 |
+
pn.pane.Markdown("### Speaker Selection Method: "),
|
| 119 |
+
select_speaker_method,
|
| 120 |
)
|
| 121 |
)
|
| 122 |
|
|
|
|
| 176 |
content = autogen.OpenAIWrapper.instantiate(
|
| 177 |
content,
|
| 178 |
message["context"],
|
| 179 |
+
self.llm_config and self.llm_config.get("allow_format_str_template", False),
|
|
|
|
| 180 |
)
|
| 181 |
if "function_call" in message:
|
| 182 |
function_call = dict(message["function_call"])
|
|
|
|
| 223 |
else False
|
| 224 |
)
|
| 225 |
agent = initialize_agents(
|
| 226 |
+
llm_config, agent_name, system_msg, agent_type, retrieve_config, code_execution_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
)
|
| 228 |
+
agent.register_reply([autogen.Agent, None], reply_func=send_messages, config={"callback": None})
|
| 229 |
agents.append(agent)
|
| 230 |
if len(agents) >= 3:
|
| 231 |
groupchat = autogen.GroupChat(
|
| 232 |
agents=agents,
|
| 233 |
messages=[],
|
| 234 |
max_round=12,
|
| 235 |
+
speaker_selection_method=select_speaker_method.value,
|
| 236 |
allow_repeat_speaker=False,
|
| 237 |
)
|
| 238 |
manager = myGroupChatManager(groupchat=groupchat, llm_config=llm_config)
|
| 239 |
else:
|
| 240 |
manager = None
|
| 241 |
+
return agents, manager, groupchat
|
| 242 |
|
| 243 |
|
| 244 |
async def agents_chat(init_sender, manager, contents, agents):
|
| 245 |
+
recipient = manager if len(agents) > 2 else agents[1] if agents[1] != init_sender else agents[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
if isinstance(init_sender, (RetrieveUserProxyAgent, MathUserProxyAgent)):
|
| 247 |
await init_sender.a_initiate_chat(recipient, problem=contents)
|
| 248 |
else:
|
|
|
|
| 256 |
collection_name = f"{int(time.time())}_{random.randint(0, 100000)}"
|
| 257 |
instance.collection_name = collection_name
|
| 258 |
|
| 259 |
+
column_agents_list = [[a.value for a in agent[0]] for agent in column_agents]
|
| 260 |
+
if not hasattr(instance, "agent_list") or instance.agents_list != column_agents_list:
|
| 261 |
+
agents, manager, groupchat = init_groupchat(None, collection_name)
|
|
|
|
|
|
|
|
|
|
| 262 |
instance.manager = manager
|
| 263 |
instance.agents = agents
|
| 264 |
instance.agents_list = column_agents_list
|
|
|
|
| 288 |
|
| 289 |
if not init_sender:
|
| 290 |
init_sender = agents[0]
|
| 291 |
+
await generate_code(agents, manager, contents, code_editor, groupchat)
|
| 292 |
await agents_chat(init_sender, manager, contents, agents)
|
| 293 |
return "The task is done. Please start a new task."
|
| 294 |
|
|
|
|
| 328 |
btn_msg3.on_click(load_message)
|
| 329 |
|
| 330 |
|
| 331 |
+
btn_example1 = Button(name="General 2 agents", button_type="primary", sizing_mode="stretch_width")
|
| 332 |
+
btn_example2 = Button(name="RAG 2 agents", button_type="primary", sizing_mode="stretch_width")
|
| 333 |
+
btn_example3 = Button(name="Software Dev 3 agents", button_type="primary", sizing_mode="stretch_width")
|
| 334 |
+
btn_example4 = Button(name="Research 6 agents", button_type="primary", sizing_mode="stretch_width")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
template.main.append(
|
| 336 |
pn.Row(
|
| 337 |
pn.pane.Markdown("## Agent Examples: ", sizing_mode="stretch_width"),
|
|
|
|
| 499 |
btn_example3.on_click(load_example)
|
| 500 |
btn_example4.on_click(load_example)
|
| 501 |
|
| 502 |
+
code_editor = CodeEditor(value="", sizing_mode="stretch_width", language="python", height=300)
|
|
|
|
|
|
|
| 503 |
template.main.append(code_editor)
|
| 504 |
|
| 505 |
template.servable(title=TITLE)
|
autogen_utils.py
CHANGED
|
@@ -182,11 +182,11 @@ async def get_human_input(name, prompt: str, instance=None) -> str:
|
|
| 182 |
while True:
|
| 183 |
if time.time() - ts > TIMEOUT:
|
| 184 |
instance.send(
|
| 185 |
-
f"You didn't provide your feedback in {TIMEOUT} seconds,
|
| 186 |
user=name,
|
| 187 |
respond=False,
|
| 188 |
)
|
| 189 |
-
reply = ""
|
| 190 |
break
|
| 191 |
if get_input_widget.value != "" and get_input_checkbox.value is True:
|
| 192 |
get_input_widget.disabled = True
|
|
@@ -289,7 +289,7 @@ async def format_code(code_to_format: str) -> str:
|
|
| 289 |
return formatted_code
|
| 290 |
|
| 291 |
|
| 292 |
-
async def generate_code(agents, manager, contents, code_editor):
|
| 293 |
code = """import autogen
|
| 294 |
import os
|
| 295 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent
|
|
@@ -397,6 +397,7 @@ agent = UserProxyAgent(
|
|
| 397 |
name="{agent.name}",
|
| 398 |
is_termination_msg=termination_msg,
|
| 399 |
human_input_mode="TERMINATE",
|
|
|
|
| 400 |
default_auto_reply="{DEFAULT_AUTO_REPLY}",
|
| 401 |
max_consecutive_auto_reply=5,
|
| 402 |
code_execution_config={agent._code_execution_config},
|
|
@@ -440,9 +441,9 @@ if not init_sender:
|
|
| 440 |
code += _code
|
| 441 |
|
| 442 |
if manager:
|
| 443 |
-
_code = """
|
| 444 |
groupchat = autogen.GroupChat(
|
| 445 |
-
agents=agents, messages=[], max_round=12, speaker_selection_method="
|
| 446 |
) # todo: auto, sometimes message has no name
|
| 447 |
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
|
| 448 |
|
|
|
|
| 182 |
while True:
|
| 183 |
if time.time() - ts > TIMEOUT:
|
| 184 |
instance.send(
|
| 185 |
+
f"You didn't provide your feedback in {TIMEOUT} seconds, exit.",
|
| 186 |
user=name,
|
| 187 |
respond=False,
|
| 188 |
)
|
| 189 |
+
reply = "exit"
|
| 190 |
break
|
| 191 |
if get_input_widget.value != "" and get_input_checkbox.value is True:
|
| 192 |
get_input_widget.disabled = True
|
|
|
|
| 289 |
return formatted_code
|
| 290 |
|
| 291 |
|
| 292 |
+
async def generate_code(agents, manager, contents, code_editor, groupchat):
|
| 293 |
code = """import autogen
|
| 294 |
import os
|
| 295 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent
|
|
|
|
| 397 |
name="{agent.name}",
|
| 398 |
is_termination_msg=termination_msg,
|
| 399 |
human_input_mode="TERMINATE",
|
| 400 |
+
system_message=\"\"\"{agent.system_message}\"\"\",
|
| 401 |
default_auto_reply="{DEFAULT_AUTO_REPLY}",
|
| 402 |
max_consecutive_auto_reply=5,
|
| 403 |
code_execution_config={agent._code_execution_config},
|
|
|
|
| 441 |
code += _code
|
| 442 |
|
| 443 |
if manager:
|
| 444 |
+
_code = f"""
|
| 445 |
groupchat = autogen.GroupChat(
|
| 446 |
+
agents=agents, messages=[], max_round=12, speaker_selection_method="{groupchat.speaker_selection_method}", allow_repeat_speaker=False
|
| 447 |
) # todo: auto, sometimes message has no name
|
| 448 |
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
|
| 449 |
|
custom_widgets.py
CHANGED
|
@@ -28,14 +28,14 @@ class RowAgentWidget(Viewer):
|
|
| 28 |
self._agent_name = TextInput(
|
| 29 |
name="",
|
| 30 |
value=params.get("value")[0],
|
| 31 |
-
placeholder="Agent Name",
|
| 32 |
min_width=100,
|
| 33 |
sizing_mode="scale_width",
|
| 34 |
)
|
| 35 |
self._system_msg = TextInput(
|
| 36 |
name="",
|
| 37 |
value=params.get("value")[1],
|
| 38 |
-
placeholder="System Message",
|
| 39 |
min_width=400,
|
| 40 |
sizing_mode="scale_width",
|
| 41 |
)
|
|
|
|
| 28 |
self._agent_name = TextInput(
|
| 29 |
name="",
|
| 30 |
value=params.get("value")[0],
|
| 31 |
+
placeholder="Agent Name (can only contain letters, numbers, and underscores))",
|
| 32 |
min_width=100,
|
| 33 |
sizing_mode="scale_width",
|
| 34 |
)
|
| 35 |
self._system_msg = TextInput(
|
| 36 |
name="",
|
| 37 |
value=params.get("value")[1],
|
| 38 |
+
placeholder="System Message, leave empty to use default",
|
| 39 |
min_width=400,
|
| 40 |
sizing_mode="scale_width",
|
| 41 |
)
|