Structured output
OpenAI offers functionality for defining a structure of the messages
generated by LLMs, AG2 enables this functionality by propagating
response_format
, in the LLM configuration for your agents, to the
underlying client. This is currently only supported by OpenAI.
For more info on structured output, please check here
Set your API Endpoint
The
config_list_from_json
function loads a list of configurations from an environment variable or
a json file. Structured Output is supported by OpenAI’s models from
gpt-4-0613 and gpt-3.5-turbo-0613.
Example: math reasoning
Using structured output, we can enforce chain-of-thought reasoning in the model to output an answer in a structured, step-by-step way.
Define the reasoning model
First we will define the math reasoning model. This model will indirectly force the LLM to solve the posed math problems iteratively through math reasoning steps.
Applying the Response Format
The response_format
is added to the LLM configuration and then this
configuration is applied to the agent.
Define chat actors
Now we can define the agents that will solve the posed math problem. We
will keep this example simple; we will use a UserProxyAgent
to input
the math problem and an AssistantAgent
to solve it.
The AssistantAgent
will be constrained to solving the math problem
step-by-step by using the MathReasoning
response format we defined
above.
Start the chat
Let’s now start the chat and prompt the assistant to solve a simple
equation. The assistant agent should return a response solving the
equation using a step-by-step MathReasoning
model.
Formatting a response
When defining a response_format
, you have the flexibility to customize
how the output is parsed and presented, making it more user-friendly. To
demonstrate this, we’ll add a format
method to our MathReasoning
model. This method will define the logic for transforming the raw JSON
response into a more human-readable and accessible format.
Define the reasoning model
Let’s redefine the MathReasoning
model to include a format
method.
This method will allow the underlying client to parse the return value
from the LLM into a more human-readable format. If the format
method
is not defined, the client will default to returning the model’s JSON
representation, as demonstrated in the previous example.
Define chat actors and start the chat
The rest of the process is the same as in the previous example: define the actors and start the chat.
Observe how the Math_solver agent now communicates using the format we
have defined in our MathReasoning.format
method.
Normal function calling still works alongside structured output, so your agent can have a response format while still calling tools.