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 completion example #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
231 changes: 231 additions & 0 deletions Basic_Samples/Completions/structured_output_completion.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
{
"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",
"\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 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": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}