diff --git a/authors.yaml b/authors.yaml index 265d1d40b2..bbe51cc348 100644 --- a/authors.yaml +++ b/authors.yaml @@ -176,4 +176,9 @@ joecasson-openai: ericning-o: name: "Eric Ning" website: "https://github.com/ericning-o" - avatar: "https://avatars.githubusercontent.com/u/182030612" \ No newline at end of file + avatar: "https://avatars.githubusercontent.com/u/182030612" + +Akshana: + name: "Akshana K" + website: "https://github.com/akshana-aravind" + avatar: "https://avatars.githubusercontent.com/u/149507276" \ No newline at end of file diff --git a/examples/azure/structured_output.ipynb b/examples/azure/structured_output.ipynb new file mode 100644 index 0000000000..8c3fd47ad5 --- /dev/null +++ b/examples/azure/structured_output.ipynb @@ -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 johndoe@example.com, 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 +} diff --git a/registry.yaml b/registry.yaml index 33d712fbbb..5ed60d1c7f 100644 --- a/registry.yaml +++ b/registry.yaml @@ -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 \ No newline at end of file