Skip to content

Commit

Permalink
[Add] Function calling support for Autobuild
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnashed committed Apr 5, 2024
1 parent c6f6707 commit 900fdd6
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OAI_CONFIG_LIST_sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"api_type": "azure",
"api_version": "2024-02-15-preview"
}
]
]
60 changes: 58 additions & 2 deletions autogen/agentchat/contrib/agent_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ class AgentBuilder:
# Only return the list of agent names.
"""

AGENT_FUNCTION_MAP_PROMPT = """Consider the following function.
Function Name: {function_name}
Function Description: {function_description}
The agent details are given in the format: {format_agent_details}
Which one of the following agents should be able to execute this function?
{agent_details}
Hint:
# Only respond with the name of the agent that is most suited to execute the function and nothing else.
"""

def __init__(
self,
config_file_or_env: Optional[str] = "OAI_CONFIG_LIST",
Expand Down Expand Up @@ -338,6 +351,7 @@ def build(
self,
building_task: str,
default_llm_config: Dict,
list_of_functions: Optional[List[Dict]] = None,
coding: Optional[bool] = None,
code_execution_config: Optional[Dict] = None,
use_oai_assistant: Optional[bool] = False,
Expand Down Expand Up @@ -461,7 +475,7 @@ def build(
}
)

return self._build_agents(use_oai_assistant, **kwargs)
return self._build_agents(use_oai_assistant, list_of_functions, **kwargs)

def build_from_library(
self,
Expand Down Expand Up @@ -631,7 +645,7 @@ def build_from_library(
return self._build_agents(use_oai_assistant, **kwargs)

def _build_agents(
self, use_oai_assistant: Optional[bool] = False, **kwargs
self, use_oai_assistant: Optional[bool] = False, list_of_functions: Optional[List[Dict]] = None, **kwargs
) -> Tuple[List[autogen.ConversableAgent], Dict]:
"""
Build agents with generated configs.
Expand Down Expand Up @@ -680,6 +694,48 @@ def _build_agents(
+ agent_list
)

agent_details = []

for agent in agent_list[1:]:
agent_details.append({"name": agent.name, "description": agent.description})

config_list = autogen.config_list_from_json(
self.config_file_or_env,
file_location=self.config_file_location,
filter_dict={"model": [self.builder_model]},
)

build_manager = autogen.OpenAIWrapper(config_list=config_list)

for func in list_of_functions:
resp = (
build_manager.create(
messages=[
{
"role": "user",
"content": self.AGENT_FUNCTION_MAP_PROMPT.format(
function_name=func["name"],
function_description=func["description"],
format_agent_details='[{"name": "agent_name", "description": "agent description"}, ...]',
agent_details=str(json.dumps(agent_details)),
),
}
]
)
.choices[0]
.message.content
)

autogen.agentchat.register_function(
func["function"],
caller=self.agent_procs_assign[resp][0],
executor=agent_list[0],
name=func["name"],
description=func["description"],
)

print(f"Function {func['name']} is registered to agent {resp}.")

return agent_list, self.cached_configs.copy()

def save(self, filepath: Optional[str] = None) -> str:
Expand Down
120 changes: 120 additions & 0 deletions notebook/autobuild_function_calling.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'autogen.agentchat.contrib.agent_builder'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mautogen\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mautogen\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39magentchat\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mcontrib\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39magent_builder\u001b[39;00m \u001b[39mimport\u001b[39;00m AgentBuilder\n\u001b[1;32m 4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mIPython\u001b[39;00m \u001b[39mimport\u001b[39;00m get_ipython\n\u001b[1;32m 5\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtyping_extensions\u001b[39;00m \u001b[39mimport\u001b[39;00m Annotated\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'autogen.agentchat.contrib.agent_builder'"
]
}
],
"source": [
"import autogen\n",
"from autogen.agentchat.contrib.agent_builder import AgentBuilder\n",
"\n",
"from IPython import get_ipython\n",
"from typing_extensions import Annotated\n",
"\n",
"\n",
"config_file_or_env = \"/workspaces/autogen/OAI_CONFIG_LIST.json\"\n",
"\n",
"\n",
"def exec_python(cell: Annotated[str, \"Valid Python cell to execute.\"]) -> str:\n",
" ipython = get_ipython()\n",
" result = ipython.run_cell(cell)\n",
" log = str(result.result)\n",
" if result.error_before_exec is not None:\n",
" log += f\"\\n{result.error_before_exec}\"\n",
" if result.error_in_exec is not None:\n",
" log += f\"\\n{result.error_in_exec}\"\n",
" return log\n",
"\n",
"\n",
"config_list = autogen.config_list_from_json(config_file_or_env, filter_dict={\"model\": [\"gpt-4-1106-preview\", \"gpt-4\"]})\n",
"\n",
"\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"timeout\": 120,\n",
"}\n",
"\n",
"def start_task(execution_task: str, agent_list: list):\n",
" group_chat = autogen.GroupChat(agents=agent_list, messages=[], max_round=12)\n",
" manager = autogen.GroupChatManager(groupchat=group_chat, llm_config={\"config_list\": config_list})\n",
" agent_list[0].initiate_chat(manager, message=execution_task)\n",
"\n",
"\n",
"builder = AgentBuilder(\n",
" config_file_or_env=config_file_or_env, builder_model=\"gpt-4-1106-preview\", agent_model=\"gpt-4-1106-preview\"\n",
")\n",
"\n",
"building_task = \"Draw two agents chatting with each other with an example dialog using matplotlib. Don't add plt.show().\"\n",
"\n",
"list_of_functions = [\n",
" {\"name\": \"exec_python\", \"description\": \"run cell in ipython and return the execution result.\", \"function\": exec_python}\n",
"]\n",
"\n",
"agent_list, agent_configs = builder.build(building_task, llm_config, list_of_functions)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"start_task(\n",
" execution_task=building_task,\n",
" agent_list=agent_list\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder.clear_all_agents(recycle_endpoint=True)\n",
"\n",
"saved_path = builder.save()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 900fdd6

Please sign in to comment.