### Image Generation Quickstart (Python) Source: https://context7_llms A quickstart guide to generating images using Azure OpenAI REST APIs with Python. It covers prerequisites, setup, and retrieving API keys and endpoints. ```APIDOC ## Image Generation Quickstart (Python) This guide demonstrates how to generate images using the Azure OpenAI REST APIs with Python. ### Prerequisites - An Azure subscription. - Python 3.8 or later. - Installed Python libraries: `os`, `requests`, `json`. - An Azure OpenAI resource deployed with a `gpt-image-1`-series or `dalle3` model. ### Setup Retrieve your Azure OpenAI resource's endpoint and API key from the Azure portal under 'Keys and Endpoint'. **Endpoint Example:** `https://docs-test-001.openai.azure.com/` **API Key:** An access key generated for your resource. ``` -------------------------------- ### Azure OpenAI Webhook Setup Source: https://context7_llms This section details how to set up a webhook server to receive real-time notifications about Azure OpenAI API events. It includes prerequisites, server setup, and an example Flask application for processing events. ```APIDOC ## POST /webhook ### Description Webhook endpoint to receive and process OpenAI events. This endpoint listens for POST requests containing event data from Azure OpenAI. ### Method POST ### Endpoint /webhook ### Parameters #### Query Parameters None #### Request Body This endpoint expects a request body containing event data, which is then processed and verified using the `OPENAI_WEBHOOK_SECRET`. ### Request Example ```json { "event_data": "..." } ``` ### Response #### Success Response (200) - **status** (string) - Indicates the success or failure of processing the webhook event. #### Response Example ```json { "status": "success" } ``` ### Prerequisites - Install Python packages: `pip install flask openai websockets requests` - Set environment variable: `OPENAI_WEBHOOK_SECRET` ``` -------------------------------- ### Start Hosted Agent Deployment with Azure CLI Source: https://context7_llms This command starts a hosted agent deployment in Azure AI Foundry, making it available for requests. It can be used to start a new deployment or restart a stopped agent. Key parameters include account name, project name, agent name, and agent version. ```bash az cognitiveservices agent start \ --account-name myAccount \ --project-name myProject \ --name myAgent \ --agent-version 1 \ --min-replicas 1 \ --max-replicas 2 ``` -------------------------------- ### Vision Fine-tuning with Image Data (JSONL Format) Source: https://context7_llms This snippet demonstrates the JSONL format for vision fine-tuning, allowing images to be included as publicly accessible URLs or base64 encoded data URIs within training data. It supports JPEG, PNG, and WEBP image formats in RGB or RGBA mode. Each example can contain up to 64 images, and the training file has a limit of 50,000 image-containing examples. ```json { "messages": [ { "role": "system", "content": "You are a helpful AI assistant." }, { "role": "user", "content": "Describe the image?" }, { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": "https://raw.githubusercontent.com/MicrosoftDocs/azure-ai-docs/main/articles/ai-services/openai/media/how-to/generated-seattle.png" } } ] }, { "role": "assistant", "content": "The image appears to be a watercolor painting of a city skyline, featuring tall buildings and a recognizable structure often associated with Seattle, like the Space Needle. The artwork uses soft colors and brushstrokes to create a somewhat abstract and artistic representation of the cityscape" } ] } ``` -------------------------------- ### Install Azure OpenAI Package for .NET Source: https://context7_llms This snippet shows how to add the Azure OpenAI package to a .NET project using the dotnet CLI. This is a prerequisite for using Azure OpenAI services in your application. ```bash dotnet add package OpenAI ``` -------------------------------- ### Instrument OpenAI Agents SDK with OpenTelemetry Source: https://context7_llms This code example shows how to instrument the OpenAI Agents SDK using OpenTelemetry for tracing. This enables the collection of detailed telemetry data about agent behavior, which can be sent to Application Insights via Foundry. ```python import os from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from azure.monitor.opentelemetry import AzureMonitorTraceExporter from openai_agents import Agent, Assistant, UserMessage # Configure OpenTelemetry with Azure Monitor exporter exporter = AzureMonitorTraceExporter.from_connection_string( os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING") ) provider = TracerProvider( resource=Resource.create({"service.name": "my-openai-agent"}) ) provider.add_span_processor(BatchSpanProcessor(exporter)) trace.set_tracer_provider(provider) tracer = trace.get_tracer(__name__) # Initialize OpenAI Agent (conceptual) # Assume agent setup and configuration is done here # agent = Agent(...) # Example of using the tracer around agent execution with tracer.start_as_current_span("agent_execution") as span: # Simulate agent interaction # response = agent.run(UserMessage("What is the weather?")) # print(response) span.set_attribute("user.query", "What is the weather?") # span.set_attribute("agent.response", response) pass print("Agent execution traced.") ``` -------------------------------- ### Tool Calling Training Data Format Source: https://context7_llms Example of training data format for fine-tuning models with tool calling capabilities. The data uses the chat completions format and includes 'tool_calls' in the assistant's response, along with the 'tools' definition. ```json { "messages": [ { "role": "user", "content": "What is the weather in San Francisco?" }, { "role": "assistant", "tool_calls": [ { "id": "call_1", "type": "function", "function": { "name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}" } } ] } ], "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and country/region, eg. San Francisco, USA" }, "format": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location", "format"] } } } ] } ``` -------------------------------- ### Install Azure Identity Package for .NET Source: https://context7_llms This snippet shows how to add the Azure Identity package to a .NET project using the dotnet CLI. This package is required for using Microsoft Entra ID authentication with Azure services. ```bash dotnet add package Azure.Identity ``` -------------------------------- ### Reinforcement Fine-Tuning Data Format Source: https://context7_llms Example of training data format for Reinforcement Fine-Tuning (RFT). RFT requires JSONL format with a 'messages' array, where the final message has a 'user' role. Additional fields can be included for grader use. ```json { "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Explain the concept of reinforcement learning." }, { "role": "assistant", "content": "Reinforcement learning is a type of machine learning where an agent learns to make decisions by taking actions in an environment to maximize a cumulative reward." }, { "role": "user", "content": "What are the key components of reinforcement learning?", "grader_data": { "difficulty": "medium" } } ] } ``` -------------------------------- ### Create Block Lists with Azure OpenAI API Source: https://context7_llms This code example shows how to create custom block lists using the Azure OpenAI API. Block lists are used to filter specific terms for content moderation. Prerequisites include an Azure subscription and an Azure OpenAI resource. ```bash # Example using cURL to create a block list (replace placeholders) curl POST https://YOUR_AZURE_OPENAI_ENDPOINT/openai/deployments/YOUR_DEPLOYMENT_NAME/blocklists?api-version=2023-05-15 \ -H "Content-Type: application/json" \ -H "api-key: YOUR_API_KEY" \ -d '{ "name": "my_custom_blocklist", "description": "A list of terms to block", "items": ["term1", "term2", "term3"] }' ``` -------------------------------- ### Manage Hosted Agent Lifecycle Source: https://context7_llms This section provides instructions on how to manage the lifecycle of deployed hosted agents in Foundry Agent Service, including starting, stopping, and potentially updating or deleting them. ```APIDOC ## az cognitiveservices agent start ### Description Starts a hosted agent deployment to make it available for requests. This command can be used to start a new deployment or restart a stopped agent. ### Method CLI Command ### Endpoint N/A (CLI Command) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None **Arguments:** - **--account-name** (`-a`) (string) - Required - Microsoft Foundry account name. - **--project-name** (string) - Required - AI project name. - **--name** (`-n`) (string) - Required - Hosted agent name. - **--agent-version** (integer) - Required - Agent version to start. - **--min-replicas** (integer) - Optional - Minimum replicas (default: 1). - **--max-replicas** (integer) - Optional - Maximum replicas (default: 1). ### Request Example ```bash az cognitiveservices agent start \ --account-name myAccount \ --project-name myProject \ --name myAgent \ --agent-version 1 \ --min-replicas 1 \ --max-replicas 2 ``` ### Response #### Success Response State transitions: Stopped → Starting → Started (success) or Failed (error) #### Response Example N/A (CLI Output) --- ## az cognitiveservices agent stop ### Description Stops a running agent to pause processing and reduce costs. The agent version remains available for restarting later. ### Method CLI Command ### Endpoint N/A (CLI Command) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None **Arguments:** - **--account-name** (`-a`) (string) - Required - Microsoft Foundry account name. - **--project-name** (string) - Required - AI project name. - **--name** (`-n`) (string) - Required - Hosted agent name. - **--agent-version** (integer) - Required - Agent version to stop. ### Request Example ```bash az cognitiveservices agent stop \ --account-name myAccount \ --project-name myProject \ --name myAgent \ --agent-version 1 ``` ### Response #### Success Response State transitions: Running → Stopping → Stopped (success) #### Response Example N/A (CLI Output) ``` -------------------------------- ### Python Code-Based Evaluator Example: Answer Length Source: https://context7_llms Demonstrates a Python code-based evaluator to calculate the length of an answer. This example shows a class structure that can be extended for custom evaluation metrics without relying on a large language model. ```python class AnswerLengthEvaluator: def __init__(self): pass # A class is made callable by implementing th ``` -------------------------------- ### Install Azure Cognitive Services CLI Extension Source: https://context7_llms This command installs or upgrades the Azure Cognitive Services CLI extension, which is required for managing hosted agents. Ensure you have Azure CLI version 2.80 or later installed before running this command. ```bash az extension add --name cognitiveservices --upgrade ``` -------------------------------- ### Configure AI-Assisted Evaluators with AzureOpenAIModelConfiguration (Python) Source: https://context7_llms This Python code snippet demonstrates how to configure AI-assisted evaluators using AzureOpenAIModelConfiguration. It loads environment variables for authentication and sets up the model configuration, which is a prerequisite for using these evaluators. Ensure you have the 'azure-ai-evaluation' and 'python-dotenv' libraries installed. ```python import os from azure.ai.evaluation import AzureOpenAIModelConfiguration from dotenv import load_dotenv load_dotenv() model_co ``` -------------------------------- ### Embeddings API - REST Example Source: https://context7_llms This section details how to make a REST API call to generate embeddings. It includes information on the endpoint, required headers, and the request body structure. ```APIDOC ## POST /openai/v1/embeddings ### Description This endpoint generates an embedding vector for a given piece of text. Embeddings are numerical representations of text that capture semantic meaning, useful for machine learning tasks and similarity searches. ### Method POST ### Endpoint `https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/embeddings` ### Parameters #### Path Parameters None #### Query Parameters - **api-version** (string) - Optional - The explicit Microsoft Foundry Models API version to use for this request. Defaults to 'v1'. #### Request Body - **input** (string or array of strings) - Required - The text(s) to generate embeddings for. - **model** (string) - Optional - The ID of the model to use for embedding generation. Defaults to 'text-embedding-3-small'. - **encoding_format** (string) - Optional - The format of the embeddings. Supported values are 'float' and 'base64'. Defaults to 'float'. ### Request Example ```json { "input": "This is a test string", "model": "text-embedding-3-small" } ``` ### Response #### Success Response (200) - **object** (string) - The type of object returned, should be 'list'. - **data** (array) - A list of embedding objects. - **object** (string) - The type of object, should be 'embedding'. - **embedding** (array of floats) - The embedding vector. - **index** (integer) - The index of the input text in the original request. - **model** (string) - The model used for generating embeddings. - **usage** (object) - Information about the token usage. - **prompt_tokens** (integer) - The number of tokens in the input prompt. - **total_tokens** (integer) - The total number of tokens processed. #### Response Example ```json { "object": "list", "data": [ { "object": "embedding", "embedding": [ 0.006927707, -0.004317977, -0.017044493, // ... more floats ], "index": 0 } ], "model": "text-embedding-3-small", "usage": { "prompt_tokens": 5, "total_tokens": 5 } } ``` ``` -------------------------------- ### Generate Text Responses with Foundry Models Source: https://context7_llms This section details how to use the Responses API to generate text responses from Foundry Models like Microsoft AI, DeepSeek, and Grok. It outlines the prerequisites and provides code examples for making API calls. ```APIDOC ## POST /api/projects/{YOUR_PROJECT_NAME}/completions ### Description Generates text responses from deployed Foundry Models using the Responses API. ### Method POST ### Endpoint `https://YOUR-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR_PROJECT_NAME/completions` ### Parameters #### Path Parameters - **YOUR_PROJECT_NAME** (string) - Required - The name of your Foundry project. - **YOUR-RESOURCE-NAME** (string) - Required - The name of your Azure AI Foundry resource. #### Query Parameters None #### Request Body - **model** (string) - Required - The name of the deployed Foundry Model (e.g., `MAI-DS-R1`). - **prompt** (string) - Required - The input text prompt for the model. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. - **temperature** (number) - Optional - Controls randomness. Lower values make output more focused and deterministic. ### Request Example ```json { "model": "MAI-DS-R1", "prompt": "Explain the concept of large language models.", "max_tokens": 150, "temperature": 0.7 } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the completion. - **object** (string) - Type of object returned (e.g., `text_completion`). - **created** (integer) - Timestamp of creation. - **model** (string) - The model used for completion. - **choices** (array) - An array of completion choices. - **text** (string) - The generated text response. - **index** (integer) - Index of the choice. - **finish_reason** (string) - The reason the model stopped generating tokens (e.g., `stop`, `length`). - **usage** (object) - Usage statistics for the request. - **prompt_tokens** (integer) - Number of tokens in the prompt. - **completion_tokens** (integer) - Number of tokens in the completion. - **total_tokens** (integer) - Total tokens used. #### Response Example ```json { "id": "cmpl-xxxxxxxxxxxxxxxxxxxx", "object": "text_completion", "created": 1677652721, "model": "MAI-DS-R1", "choices": [ { "text": "\n\nLarge language models (LLMs) are a type of artificial intelligence model trained on vast amounts of text data. They are designed to understand, generate, and manipulate human language.", "index": 0, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 30, "total_tokens": 40 } } ``` ``` -------------------------------- ### Azure OpenAI v1 API - Chat Completions Source: https://context7_llms This section details the POST endpoint for creating chat completions using the Azure OpenAI v1 API. It includes information on required parameters, authentication methods, and request/response examples. ```APIDOC ## POST /openai/v1/chat/completions ### Description Creates a chat completion using the Azure OpenAI v1 API. ### Method POST ### Endpoint `https://{your-resource-name}.openai.azure.com/openai/v1/chat/completions` ### Parameters #### Path Parameters - **endpoint** (string) - Required - URL of the supported Azure OpenAI endpoints (e.g., https://aoairesource.openai.azure.com). #### Query Parameters - **api-version** (string) - Optional - The explicit Microsoft Foundry Models API version to use for this request. Defaults to `v1`. #### Request Body - **audio** (object) - Optional - Parameters for audio output. Required when audio output is requested with modalities: ["audio"]. - **format** (enum) - Optional - Specifies the output audio format. Must be one of `wav`, `mp3`, `flac`, `opus`, or `pcm16`. ### Request Example ```json { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me about Azure OpenAI."} ], "max_tokens": 150, "temperature": 0.7 } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the completion. - **object** (string) - The type of object returned, e.g., `chat.completion`. - **created** (integer) - The timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **choices** (array) - A list of completion choices. - **index** (integer) - The index of the choice. - **message** (object) - The message content. - **role** (string) - The role of the message sender (e.g., `assistant`). - **content** (string) - The text content of the message. - **finish_reason** (string) - The reason the completion finished (e.g., `stop`, `length`). - **usage** (object) - Information about token usage. - **prompt_tokens** (integer) - The number of tokens in the prompt. - **completion_tokens** (integer) - The number of tokens in the completion. - **total_tokens** (integer) - The total number of tokens used. #### Response Example ```json { "id": "chatcmpl-12345", "object": "chat.completion", "created": 1677652288, "model": "gpt-4", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Azure OpenAI provides access to advanced language models like GPT-4 and GPT-3.5-Turbo." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` -------------------------------- ### Generate Text Response using Responses API with API Key Source: https://context7_llms This Python code snippet shows how to generate a text response using the Azure OpenAI Responses API with an API key. It initializes an OpenAI client with the API key and base URL, then calls the `responses.create` method with a specified model and input text. The output is printed as a JSON string. It's important to handle API keys securely, for example, by storing them in Azure Key Vault. ```python import os from openai import OpenAI client = OpenAI( api_key=os.getenv("AZURE_OPENAI_API_KEY"), base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/", ) response = client.responses.create( model="gpt-4.1-nano", # Replace with your model deployment name input="This is a test.", ) print(response.model_dump_json(indent=2)) ``` -------------------------------- ### Start Deep Research Task using cURL Source: https://context7_llms Initiates a deep research task by sending a request to the Azure OpenAI Responses API. It specifies the 'o3-deep-research' model and includes web search and code interpreter tools. The input provides a detailed research query about the economic impact of semaglutide. ```shell curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \ -d '{ "model": "o3-deep-research", "background": true, "tools": [ { "type": "web_search_preview" }, { "type": "code_interpreter", "container": {"type": "auto"} } ], "input": "Research the economic impact of semaglutide on global healthcare systems. Include specific figures, trends, statistics, and measurable outcomes. Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical earnings reports. Include inline citations and return all source metadata. Be analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or finan" }' ``` -------------------------------- ### Initialize Foundry Starter Template with Azure Developer CLI Source: https://context7_llms Initializes a new project using the Foundry starter template and configures it with the agent-with-foundry-tools sample. This command-line interface (CLI) tool prompts for an environment name, which is used to determine the resource group name. ```bash azd init -t https://github.com/Azure-Samples/azd-ai-starter-basic ``` -------------------------------- ### Set up langchain-azure-ai tracer for LangChain and LangGraph Source: https://context7_llms This snippet demonstrates how to integrate the `langchain-azure-ai` tracer for LangChain and LangGraph applications. This allows for detailed tracing of LLM calls and agent interactions within these frameworks, sending telemetry to Application Insights. ```python from langchain_azure_ai import AzureAIConnection from langchain_core.runnables import RunnableSequence # Initialize Azure AI connection (replace with your actual details) connection = AzureAIConnection( resource_name="YOUR_AZURE_AI_RESOURCE_NAME", endpoint="YOUR_AZURE_AI_ENDPOINT", api_key="YOUR_AZURE_AI_API_KEY" ) # Assume you have a LangChain or LangGraph runnable defined # For example, a simple chain: llm = connection.llm chain: RunnableSequence = llm | (lambda x: x.upper()) # Example chain # The langchain-azure-ai tracer automatically instruments compatible runnables # when the connection is established. result = chain.invoke("hello world") print(result) ``` -------------------------------- ### Deploy a Hosted Agent Source: https://context7_llms This section details how to deploy a containerized agent to Foundry Agent Service. It includes prerequisites, required permissions, and local testing instructions using a hosting adapter that exposes the agent as a REST API. ```APIDOC ## POST /responses ### Description Deploys a containerized agent to Foundry Agent Service. Use hosted agents when you need to run custom agent code built with frameworks like LangGraph, Microsoft Agent Framework, or your own implementation. ### Method POST ### Endpoint `http://localhost:8088/responses` ### Parameters #### Query Parameters None #### Request Body - **input** (object) - Required - The input for the agent. - **messages** (array) - Required - An array of message objects. - **role** (string) - Required - The role of the message sender (e.g., 'user', 'assistant'). - **content** (string) - Required - The content of the message. ### Request Example ```json { "input": { "messages": [ { "role": "user", "content": "Where is Seattle?" } ] } } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the response. - **object** (string) - The type of object returned (e.g., 'response'). - **output** (array) - An array of output objects from the agent. - **type** (string) - The type of output (e.g., 'message'). - **role** (string) - The role of the sender of the output message. - **content** (string) - The content of the output message. - **status** (string) - The status of the agent's response. #### Response Example ```json { "id": "resp_abc123", "object": "response", "output": [ { "type": "message", "role": "assistant", "content": "Seattle is a major city in the Pacific Northwest region of the United States..." } ], "status": "completed" } ``` ``` -------------------------------- ### Microsoft Foundry Playgrounds Source: https://context7_llms Information about Microsoft Foundry Playgrounds, which offer a zero-setup environment for rapid prototyping, API exploration, and technical validation of AI models before production deployment. ```APIDOC ## Azure OpenAI Playgrounds ### Description Microsoft Foundry Playgrounds provide an on-demand, zero-setup environment for rapid prototyping, API exploration, and technical validation with state-of-the-art models. They support various model types and offer features like "Open in VS Code" for seamless integration. ### Key Features - **AgentOps support**: For evaluations and tracing in the Agents playground. - **Open in VS Code**: Automatically imports endpoint and key from Foundry to VS Code for code samples. - **Images playground 2.0**: Supports models like `gpt-image-1`, `Stable Diffusion 3.5 Large`, and `FLUX.1-Kontext-pro`. - **Video playground**: For models such as Azure OpenAI Sora-2. - **Audio playground**: For models like `gpt-4o-audio-preview`, `gpt-4o-transcribe`, and `gpt-4o-mini-tts`. ### Usage Access the playgrounds directly through the Microsoft Foundry portal for quick experimentation with different AI models and capabilities. ``` -------------------------------- ### Deploy Fine-Tuned Model using Python SDK Source: https://context7_llms This Python code snippet demonstrates how to deploy a fine-tuned Azure OpenAI model for inferencing. It requires authentication token, subscription ID, resource group, and resource name. The UI does not support cross-region deployment, but the Python SDK and REST API do. ```python import json import os import requests token = os.getenv("") subscription = "" resource_group = "" resource_name = "" model_deployment_name = "gpt-4.1-mini-ft" # custom deployment name that you will use to ref ``` -------------------------------- ### Connect Azure AI Search Index to Foundry Agents (C# SDK) Source: https://context7_llms This snippet shows how to connect an Azure AI Search index to a Microsoft Foundry agent using the C# SDK. Prerequisites include an existing search index and a configured Foundry agent environment. The Azure AI Search tool enables grounded agent responses with citations. ```csharp using Azure.AI.Projects.AISearch; // Assuming you have an existing agent and Azure AI Search index configured // Replace with your actual index name and other parameters var searchTool = new AISearchTool( indexName: "your-search-index-name", description: "Search tool for proprietary documents." // Add other parameters as needed, e.g., searchFields, contentFields, etc. ); // Add the search tool to your agent's tools // agent.Tools.Add(searchTool); Console.WriteLine("Azure AI Search tool configured."); ``` -------------------------------- ### POST /openai/v1/chat/completions Source: https://context7_llms Creates a chat completion using the Azure OpenAI API. Supports various parameters for audio output and authentication. ```APIDOC ## POST /openai/v1/chat/completions ### Description Creates a chat completion using the Azure OpenAI API. This endpoint allows for text generation based on provided prompts and configurations. It also supports audio output parameters and various authentication methods. ### Method POST ### Endpoint `{endpoint}/openai/v1/chat/completions` ### Parameters #### Path Parameters - **endpoint** (string) - Required - Supported Azure OpenAI endpoints (protocol and hostname, for example: https://aoairesource.openai.azure.com. Replace "aoairesource" with your Azure OpenAI resource name). #### Query Parameters - **api-version** (string) - Optional - The explicit Microsoft Foundry Models API version to use for this request. Defaults to 'v1' if not specified. #### Request Body - **audio** (object) - Optional - Parameters for audio output. Required when audio output is requested with modalities: ["audio"]. - **format** (enum) - Optional - Specifies the output audio format. Must be one of 'wav', 'mp3', 'flac', 'opus', or 'pcm16'. ### Request Example ```json { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"} ], "temperature": 0.7, "max_tokens": 800, "audio": { "format": "mp3" } } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the completion. - **object** (string) - The type of object returned, e.g., 'chat.completion'. - **created** (integer) - The timestamp of the completion. - **model** (string) - The model used for the completion. - **choices** (array) - A list of completion choices. - **usage** (object) - Usage statistics for the completion. #### Response Example ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-3.5-turbo", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 15, "total_tokens": 25 } } ``` ``` -------------------------------- ### Connect Azure AI Search Index to Foundry Agents (Python SDK) Source: https://context7_llms This snippet demonstrates how to connect an Azure AI Search index to a Microsoft Foundry agent using the Python SDK. It assumes you have an existing search index and a Foundry agent environment set up. The tool retrieves indexed documents to ground agent responses with inline citations. ```python from azure.ai.projects.ai_search import AISearchTool # Assuming you have an existing agent and Azure AI Search index configured # Replace with your actual index name and other parameters search_tool = AISearchTool( index_name="your-search-index-name", description="Search tool for proprietary documents.", # Add other parameters as needed, e.g., search_fields, content_fields, etc. ) # Add the search tool to your agent's tools # agent.add_tool(search_tool) print("Azure AI Search tool configured.") ``` -------------------------------- ### Chat Completions API Usage with Azure OpenAI Reasoning Models (C#) Source: https://context7_llms This C# code demonstrates how to use the chat completions API with Azure OpenAI reasoning models. It requires the Azure.Identity and OpenAI NuGet packages. The code sets up authentication using DefaultAzureCredential and configures the ChatClient with a specific model and endpoint. It also defines ChatCompletionOptions, including the maximum output token count. ```csharp using Azure.Identity; using OpenAI; using OpenAI.Chat; using System.ClientModel.Primitives; #pragma warning disable OPENAI001 //currently required for token based authentication BearerTokenPolicy tokenPolicy = new( new DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"); ChatClient client = new( model: "o4-mini", authenticationPolicy: tokenPolicy, options: new OpenAIClientOptions() { Endpoint = new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1") } ); ChatCompletionOptions options = new ChatCompletionOptions { MaxOutputTokenCount = 100000 }; ``` -------------------------------- ### Image Generation Models Overview Source: https://context7_llms This section provides an overview of different image generation models available through Azure OpenAI, including their input/output modalities and formats. ```APIDOC ## Image Generation Models Overview This section details the capabilities of various image generation models offered by Azure OpenAI. ### Models - **GPT-Image-1.5**: Accepts text + image inputs; outputs images only in base64 (no URL option). - **GPT-Image-1**: Accepts text + image inputs; outputs images only in base64 (no URL option). - **GPT-Image-1-Mini**: Accepts text + image inputs; outputs images only in base64 (no URL option). - **DALL·E 3**: Accepts text (primary) input; limited image editing inputs (with mask). Outputs as URL or base64. ``` -------------------------------- ### Connect Azure AI Search Index to Foundry Agents (JavaScript/TypeScript SDK) Source: https://context7_llms This snippet illustrates connecting an Azure AI Search index to a Microsoft Foundry agent using the JavaScript/TypeScript SDK. It requires an existing search index and a Foundry agent environment. The Azure AI Search tool retrieves indexed documents for grounded responses with citations. ```javascript import { AISearchTool } from "@azure/ai-projects/ai-search"; // Assuming you have an existing agent and Azure AI Search index configured // Replace with your actual index name and other parameters const searchTool = new AISearchTool({ indexName: "your-search-index-name", description: "Search tool for proprietary documents." // Add other parameters as needed, e.g., searchFields, contentFields, etc. }); // Add the search tool to your agent's tools // agent.addTool(searchTool); console.log("Azure AI Search tool configured."); ``` -------------------------------- ### Video Generation with Sora Source: https://context7_llms This section details how to generate video clips using the Sora model via the Azure OpenAI service. It covers creating a job, monitoring its status, and retrieving the generated video. ```APIDOC ## POST /openai/v1/video/generations ### Description Generates video clips from text instructions and/or image or video inputs using the Sora model. ### Method POST ### Endpoint `https://{RESOURCE_NAME}.openai.azure.com/openai/v1/video/generations` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **prompt** (string) - Required - Text description of the desired video. - **image_input** (string, optional) - URL or base64 encoded string of an input image. - **video_input** (string, optional) - URL or base64 encoded string of an input video. ### Request Example ```json { "prompt": "A futuristic cityscape with flying cars.", "image_input": "https://example.com/input.jpg" } ``` ### Response #### Success Response (200) - **job_id** (string) - The ID of the video generation job. - **status** (string) - The current status of the job (e.g., "running", "succeeded", "failed"). #### Response Example ```json { "job_id": "job_12345abcde", "status": "running" } ``` ### Error Handling - **400 Bad Request**: Invalid input parameters. - **401 Unauthorized**: Authentication failed. - **500 Internal Server Error**: Server error during video generation. ``` -------------------------------- ### OpenAI SDK Source: https://context7_llms The OpenAI SDK allows integration with the latest OpenAI models and features, providing access to the full OpenAI API surface. Foundry direct models are accessible via the Chat Completions API. ```APIDOC ## POST /openai/v1/chat/completions ### Description Utilize the latest OpenAI SDK models and features, including Foundry direct models, through the Chat Completions API. ### Method POST ### Endpoint `https://.openai.azure.com/openai/v1/chat/completions` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **model** (string) - Required - The name of the deployment to use for this chat completion. - **messages** (array) - Required - A list of messages comprising the conversation so far. - **role** (string) - Required - The role of the author of this message (e.g., 'system', 'user', or 'assistant'). - **content** (string) - Required - The content of the message. - **temperature** (number) - Optional - Defaults to 1. Specifies the sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the completion. ### Request Example ```json { "model": "gpt-4", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"} ], "temperature": 0.7, "max_tokens": 100 } ``` ### Response #### Success Response (200) - **choices** (array) - A list of chat completion choices. - **message** (object) - Contains the role and content of the assistant's message. - **role** (string) - The role of the assistant. - **content** (string) - The content of the assistant's message. - **finish_reason** (string) - The reason the model stopped generating tokens (e.g., 'stop', 'length'). - **usage** (object) - Usage statistics for the request. - **prompt_tokens** (integer) - Number of tokens in the prompt. - **completion_tokens** (integer) - Number of tokens in the completion. - **total_tokens** (integer) - Total tokens used. #### Response Example ```json { "choices": [ { "message": { "role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 15, "completion_tokens": 12, "total_tokens": 27 } } ``` ``` -------------------------------- ### Define and Parse Structured Output with Pydantic (Python) Source: https://context7_llms Demonstrates how to use Pydantic models in Python to define a schema for structured outputs from an Azure OpenAI model. It shows setting up the OpenAI client with Azure authentication and parsing the model's response according to the defined `CalendarEvent` schema. ```python import os from pydantic import BaseModel from openai import OpenAI from azure.identity import DefaultAzureCredential, get_bearer_token_provider token_provider = get_bearer_token_provider( DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" ) client = OpenAI( base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/", api_key=token_provider, ) class CalendarEvent(BaseModel): name: str date: str participants: list[str] completion = client.beta.chat.completions.parse( model="MODEL_DEPLOYMENT_NAME", # replace with the model deployment name of your gpt-4o 2024-08-06 deployment messages=[ {"role": "system", "content": ""} ] ) ``` -------------------------------- ### Test Hosted Agent Locally with REST API Source: https://context7_llms This snippet demonstrates how to test a containerized agent locally using a hosting adapter. The adapter exposes the agent as a REST API, allowing you to send requests and receive responses. It requires the agent to be running locally. ```http POST http://localhost:8088/responses Content-Type: application/json { "input": { "messages": [ { "role": "user", "content": "Where is Seattle?" } ] } } ``` -------------------------------- ### Set up Flask Webhook Server for Azure OpenAI Source: https://context7_llms This Python code sets up a Flask application to act as a webhook listener for Azure OpenAI events. It requires the 'flask', 'openai', 'websockets', and 'requests' Python packages. The server listens for POST requests on the '/webhook' endpoint, unwraps, and verifies incoming event messages using a webhook secret. ```python from flask import Flask, request, Response from openai import OpenAI, InvalidWebhookSignatureError import os import logging app = Flask(__name__) # Configure logging for Azure App Service logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) client = OpenAI( # api-key parameter is required, but If you are only using the client for webhooks the key can be a placeholder string api_key=os.environ.get("OPENAI_API_KEY", "placeholder-key-for-webhooks-only"), webhook_secret=os.environ["OPENAI_WEBHOOK_SECRET"] # This will be created later ) @app.route("/webhook", methods=["POST"]) def webhook(): """Webhook endpoint to receive and process OpenAI events.""" try: # Unwrap and verify the message using the webhook secret event = cli ```