Structured output in Azure OpenAI
In this series, we have examined the basics of Azure Open AI, using the chat completions API, streaming responses, and finally, single and multi-tool calling. In today’s article, we will examine how to return structured output from the LLM response. We will first examine structured output without function calling and then update the earlier multi-function calling example to output JSON instead of text.
Structured outputs tell an LLM to follow the schema represented by the response_format
parameter of a request to the LLM. We can use Pydantic to build the schema.
Take a look at this example.
|
|
First, we must import the BaseModel
from pydantic
and define the structured output schema as a pydantic
base model.
|
|
We must change the client.chat.completions.create
to client.beta.chat.completins.parse
and add response_format
as a parameter. If you access response.choices[0].message.parsed
, you will extract the information returned by the model.
|
|
Accessing response.choices[0].message.content
retrieves the JSON output for the extracted information.
If we need to modify the earlier example of a travel planner program that suggests clothes to wear based on the weather at a location, we repeat what we did in the above example.
First, add a pydantic
BaseModel
class.
|
|
And, change the get_model_response
function to replace client.chat.completions.create
with client.beta.chat.completions.parse
and add response_format
as an argument with Suggestion
as the value. When you run this program, the model generates structured output as specified in the response format.
|
|
Structured output can be very useful for multi-step workflows. In the next part of the series, we will see parallel tool calling in Azure OpenAI.
Share on: