Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured output in Azure OpenAI #1436

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion authors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,9 @@ joecasson-openai:
ericning-o:
name: "Eric Ning"
website: "https://github.com/ericning-o"
avatar: "https://avatars.githubusercontent.com/u/182030612"
avatar: "https://avatars.githubusercontent.com/u/182030612"

Akshana:
name: "Akshana K"
website: "https://github.com/akshana-aravind"
avatar: "https://avatars.githubusercontent.com/u/149507276"
218 changes: 218 additions & 0 deletions examples/azure/structured_output.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Azure OpenAI Structured output\n",
"\n",
"This notebook shows how to use the structured output capability with the Azure OpenAI service.This differs from the older JSON mode feature, which could generate valid JSON but couldn't strictly enforce adherence to the provided schema.\n",
"\n",
"You can read more about Structured outputs on OpenAI's blog: https://openai.com/index/introducing-structured-outputs-in-the-api/\n",
"\n",
"**NOTE:** only gpt-4o version: 2024-08-06 supports structured outputs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup\n",
"First, we install the necessary dependencies and import the libraries we will be using."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! pip install openai==1.44.0 "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import Dict, List, Optional\n",
"from pydantic import BaseModel, Field\n",
"from openai import AzureOpenAI\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Azure OpenAI Client Setup\n",
"\n",
"\n",
"**Authentication using API key**\n",
"\n",
"Login Azure Portal and you can find this key in \"Keys and Endpoints\" under \"Resource Management\" in the Azure Portal.\n",
"\n",
"Here initializes an AzureOpenAI client using endpoint, API key, and version.\n",
"\n",
"Replace **AZURE_OPENAI_ENDPOINT** and **AZURE_OPENAI_API_KEY** with your key.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = AzureOpenAI(\n",
" azure_endpoint = \"AZURE_OPENAI_ENDPOINT\",\n",
" api_key=\"AZURE_OPENAI_API_KEY\",\n",
" api_version=\"2024-07-01-preview\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pydantic Structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
"from typing import List, Optional\n",
"\n",
"class Customer(BaseModel):\n",
" id: str\n",
" name: str\n",
" email: Optional[str]\n",
" phone: Optional[str]\n",
"\n",
"class Product(BaseModel):\n",
" id: str\n",
" name: str\n",
" price: float\n",
" stock: int\n",
"\n",
"class ShoppingCart(BaseModel):\n",
" customer_details: Customer\n",
" products: List[Product] = Field(\n",
" description=\"List of products added to the shopping cart. Each entry must include product details as per the `Product` class.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here converting the ShoppingCart model into JSON schema.you can use any format.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"manage_shopping_cart\",\n",
" \"description\": \"Manage shopping cart operations, including adding, removing, or viewing products in a customer's shopping cart\",\n",
" \"parameters\": ShoppingCart.model_json_schema(),\n",
" }\n",
" }\n",
"]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Invoking the API with Messages and Tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"shopping_cart_data = \"\"\" \n",
"Customer Details: ID C123, Name John Doe, Email [email protected], Phone +91-9876543210. \n",
"Products: \n",
"1. ID P001, Name Laptop, Price 999.99, Stock 5. \n",
"2. ID P002, Name Wireless Mouse, Price 25.99, Stock 50. \n",
"\"\"\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inside the client.chat.completions.create, with tools=tools, the Pydantic structure is executed strictly, ensuring that the data adheres to the predefined schema without \n",
"\n",
"deviation. This strict enforcement guarantees that any operations related to the shopping cart, such as adding, removing, or viewing products, follow the exact structure \n",
"\n",
"and validation rules defined by the Pydantic models.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are an AI assistant specialized in managing shopping cart operations. Use the supplied tools to assist the user with adding, removing, or viewing products in their shopping cart.\"},\n",
" {\"role\": \"user\", \"content\": f\"\"\"Can you help me manage my shopping cart? Here are the details: {shopping_cart_data}\"\"\"}\n",
"]\n",
"\n",
"response = client.chat.completions.create(\n",
" model=\"example-model\", # replace with your actual model deployment name\n",
" messages=messages,\n",
" tools=tools\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Handling the Response\n",
"\n",
"Prints Response data in a formatted JSON output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if response.choices and response.choices[0].message.tool_calls:\n",
" tool_call = response.choices[0].message.tool_calls[0]\n",
" function_args = tool_call.function.arguments\n",
"\n",
" response_data = json.loads(function_args)\n",
"\n",
" print(json.dumps(response_data, indent=2))\n",
"else:\n",
" print(\"No function call was made in the response.\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
8 changes: 8 additions & 0 deletions registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1606,3 +1606,11 @@
- msingh-openai
tags:
- web-browsing

- title: Structured output in Azure OpenAI
path: examples/azure/structured_output.ipynb
date: 2024-09-30
authors:
- akshana-aravind
tags:
- completions