response_format, in the LLM configuration for your
agents, to the underlying client.
Structured outputs are available for a number of Model Providers, see
the Supported model providers section below. In this example we will
use OpenAI as the model provider.
Install
ag2:Note: If you have been usingFor more information, please refer to the installation guide.autogenorag2, all you need to do is upgrade it using:orasautogen, andag2are aliases for the same PyPI package.
Supported model providers
AG2 has structured output support for following model providers:-
OpenAI (
openai) -
Anthropic (
anthropic) -
Google Gemini (
google) -
Ollama (
ollama)
Set your API Endpoint
TheLLMConfig.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.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 aUserProxyAgent 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.
The response_format is added to the LLM configuration and then this
configuration is applied to the agent.
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-stepMathReasoning model.
Formatting a response
When defining aresponse_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 theMathReasoning 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 ourMathReasoning.format method.