Currency Calculator: Task Solving with Provided Tools as Functions
AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participation through multi-agent conversation. Please find documentation about this feature here.
In this notebook, we demonstrate how to use AssistantAgent
and
UserProxyAgent
to make function calls with the new feature of OpenAI
models (in model version 0613). A specified prompt and function configs
must be passed to AssistantAgent
to initialize the agent. The
corresponding functions must be passed to UserProxyAgent
, which will
execute any function calls made by AssistantAgent
. Besides this
requirement of matching descriptions with functions, we recommend
checking the system message in the AssistantAgent
to ensure the
instructions align with the function call descriptions.
Requirements
AutoGen requires Python>=3.9
. To run this notebook example, please
install pyautogen
:
Set your API Endpoint
The
config_list_from_json
function loads a list of configurations from an environment variable or
a json file.
It first looks for environment variable “OAI_CONFIG_LIST” which needs to be a valid json string. If that variable is not found, it then looks for a json file named “OAI_CONFIG_LIST”. It filters the configs by tags (you can filter by other keys as well). Only the configs with matching tags are kept in the list based on the filter condition.
The config list looks like the following:
You can set the value of config_list in any way you prefer. Please refer to this notebook for full code examples of the different methods.
Making Function Calls
In this example, we demonstrate function call execution with
AssistantAgent
and UserProxyAgent
. With the default system prompt of
AssistantAgent
, we allow the LLM assistant to perform tasks with code,
and the UserProxyAgent
would extract code blocks from the LLM response
and execute them. With the new “function_call” feature, we define
functions and specify the description of the function in the OpenAI
config for the AssistantAgent
. Then we register the functions in
UserProxyAgent
.
The decorator @chatbot.register_for_llm()
reads the annotated
signature of the function currency_calculator
and generates the
following JSON schema used by OpenAI API to suggest calling the
function. We can check the JSON schema generated as follows:
The decorator @user_proxy.register_for_execution()
maps the name of
the function to be proposed by OpenAI API to the actual implementation.
The function mapped is wrapped since we also automatically handle
serialization of the output of function as follows:
-
string are untouched, and
-
objects of the Pydantic BaseModel type are serialized to JSON.
We can check the correctness of function map by using ._origin
property of the wrapped function as follows:
Finally, we can use this function to accurately calculate exchange amounts:
Pydantic models
We can also use Pydantic Base models to rewrite the function as follows: