User Defined Functions
User defined functions allow you to define Python functions in your AutoGen program and then provide these to be used by your executor. This allows you to provide your agents with tools without using traditional tool calling APIs. Currently only Python is supported for this feature.
There are several steps involved:
- Define the function
- Provide the function to the executor
- Explain to the code writing agent how to use the function
Define the function
If the function does not require any external imports or dependencies then you can simply use the function. For example:
This would be a valid standalone function.
If the function requires external imports or dependencies then you can
use the @with_requirements
decorator to specify the requirements. For
example:
If you wanted to rename pandas
to pd
or import DataFrame
directly
you could do the following:
Provide the function to the executor
Functions can be loaded into the executor in its constructor. For example:
Before we get an agent involved, we can sanity check that when the agent writes code that looks like this the executor will be able to handle it.
And we can try the function that required a dependency and import too.
Limitations
- Only Python is supported currently
- The function must not depend on any globals or external state as it is loaded as source code
Explain to the code writing agent how to use the function
Now that the function is available to be called by the executor, you can explain to the code writing agent how to use the function. This step is very important as by default it will not know about it.
There is a utility function that you can use to generate a default prompt that describes the available functions and how to use them. This function can have its template overridden to provide a custom message, or you can use a different prompt all together.
For example, you could extend the system message from the page about local execution with a new section that describes the functions available.
Then you can use this system message for your code writing agent.
Now, we can setup the code execution agent using the local command line executor we defined earlier.
Then, we can start the conversation and get the agent to process the dataframe we provided.
We can see the summary of the calculation: