### Quickstart Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry-local/includes/sdk-legacy-reference/javascript.md A quickstart example to verify the SDK can start the service and reach the local catalog. ```APIDOC ## JavaScript SDK Quickstart Use this snippet to verify that the SDK can start the service and reach the local catalog. ```js import { FoundryLocalManager } from "foundry-local-sdk"; const manager = new FoundryLocalManager(); await manager.startService(); const catalogModels = await manager.listCatalogModels(); console.log(`Catalog models available: ${catalogModels.length}`); ``` This example prints a non-zero number when the service is running and the catalog is available. ``` -------------------------------- ### Initialize project with minimal configuration Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/fine-tuning/fine-tune-cli.md Use this command to start a project with guided prompts for subscription and environment setup. ```bash azd init --minimal ``` -------------------------------- ### Foundry Local Rust SDK Quickstart Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry-local/includes/sdk-legacy-reference/rust.md A quickstart example to verify the SDK can start the service and read the local catalog. ```APIDOC ## Rust SDK Quickstart Use this snippet to verify that the SDK can start the service and read the local catalog. ```rust use anyhow::Result; use foundry_local::FoundryLocalManager; #[tokio::main] async fn main() -> Result<()> { let mut manager = FoundryLocalManager::builder().bootstrap(true).build().await?; let models = manager.list_catalog_models().await?; println!("Catalog models available: {}", models.len()); Ok(()) } ``` This example prints a nonzero number when the service is running and the catalog is available. ``` -------------------------------- ### Quickstart: Start Service and List Catalog Models Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry-local/includes/sdk-legacy-reference/javascript.md Use this snippet to verify that the SDK can start the Foundry Local service and reach the local catalog. This example prints the number of available catalog models. ```javascript import { FoundryLocalManager } from "foundry-local-sdk"; const manager = new FoundryLocalManager(); await manager.startService(); const catalogModels = await manager.listCatalogModels(); console.log(`Catalog models available: ${catalogModels.length}`); ``` -------------------------------- ### Quickstart: Initialize and List Models Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry-local/includes/sdk-reference/python.md A quickstart example to verify SDK initialization and list available models in the local catalog. ```APIDOC ## Quickstart Use this snippet to verify that the SDK can initialize and read the local catalog. ```python from foundry_local_sdk import Configuration, FoundryLocalManager config = Configuration(app_name="app-name") FoundryLocalManager.initialize(config) manager = FoundryLocalManager.instance models = manager.catalog.list_models() print(f"Catalog models available: {len(models)}") ``` This example prints a non-zero number when the catalog is available. ``` -------------------------------- ### Java Voice Live API Quickstart Setup Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/quickstarts/voice-live-api/java.md This snippet shows the necessary constants and class structure for the Voice Live API quickstart in Java. It includes audio format configurations and utility class setup. ```java private static final String ENV_API_KEY = "AZURE_VOICELIVE_API_KEY"; // Audio format constants (VoiceLive requirements) private static final int SAMPLE_RATE = 24000; // 24kHz as required by VoiceLive private static final int CHANNELS = 1; // Mono private static final int SAMPLE_SIZE_BITS = 16; // 16-bit PCM private static final int CHUNK_SIZE = 1200; // 50ms chunks (24000 * 0.05) private static final int AUDIO_BUFFER_SIZE_MULTIPLIER = 4; // Private constructor to prevent instantiation private ModelQuickstart() { throw new UnsupportedOperationException("Utility class cannot be instantiated"); } /** * Audio packet for playback queue management. * Uses sequence numbers to support interruption handling. */ private static class AudioPlaybackPacket { final int sequenceNumber; final byte[] audioData; AudioPlaybackPacket(int sequenceNumber, byte[] audioData) { this.sequenceNumber = sequenceNumber; this.audioData = audioData; } } /** * Handles real-time audio capture from microphone and playback to speakers. * *

This class manages two separate threads:

* * *

Supports interruption handling where user speech can cancel ongoing assistant responses.

*/ private static class AudioProcessor { private final VoiceLiveSessionAsyncClient session; private final AudioFormat audioFormat; // Audio capture components private TargetDataLine microphone; private final AtomicBoolean isCapturing = new AtomicBoolean(false); // Audio playback components private SourceDataLine speaker; private final BlockingQueue playbackQueue = new LinkedBlockingQueue<>(); private final AtomicBoolean isPlaying = new AtomicBoolean(false); private final AtomicInteger nextSequenceNumber = new AtomicInteger(0); private final AtomicInteger playbackBase = new AtomicInteger(0); AudioProcessor(VoiceLiveSessionAsyncClient session) { this.session = session; this.audioFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, SAMPLE_SIZE_BITS, CHANNELS, CHANNELS * SAMPLE_SIZE_BITS / 8, // frameSize SAMPLE_RATE, false // bigEndian ); } /** * Start capturing audio from microphone */ void startCapture() { if (isCapturing.get()) { return; } try { DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class, audioFormat); if (!AudioSystem.isLineSupported(micInfo)) { throw new UnsupportedOperationException("Microphone not supported with required format"); } microphone = (TargetDataLine) AudioSystem.getLine(micInfo); microphone.open(audioFormat, CHUNK_SIZE * AUDIO_BUFFER_SIZE_MULTIPLIER); microphone.start(); isCapturing.set(true); // Start capture thread Thread captureThread = new Thread(this::captureAudioLoop, "VoiceLive-AudioCapture"); captureThread.setDaemon(true); captureThread.start(); System.out.println("🎤 Microphone capture started"); } catch (LineUnavailableException e) { System.err.println("❌ Failed to start microphone: " + e.getMessage()); throw new RuntimeException("Failed to initialize microphone", e); } } /** * Starts audio playback system. */ void startPlayback() { if (isPlaying.get()) { return; } try { DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, audioFormat); if (!AudioSystem.isLineSupported(speakerInfo)) { throw new UnsupportedOperationException("Speaker not supported with required format"); } speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo); speaker.open(audioFormat, CHUNK_SIZE * AUDIO_BUFFER_SIZE_MULTIPLIER); speaker.start(); isPlaying.set(true); // Start playback thread Thread playbackThread = new Thread(this::playbackAudioLoop, "VoiceLive-AudioPlayback"); playbackThread.setDaemon(true); ``` -------------------------------- ### Set up the root directory for the samples Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/machine-learning/tutorial-develop-feature-set-with-custom-source.md Initializes the environment and dependencies for the tutorial. This process may take approximately 10 minutes to complete. ```python # Set up the root directory for the samples import os # Get the current working directory current_dir = os.getcwd() # Set the root directory to the featurestore_sample folder root_dir = os.path.abspath(os.path.join(current_dir, "../../..")) # Print the root directory print(f"Root directory: {root_dir}") # Change the current working directory to the root directory os.chdir(root_dir) # Verify the current working directory print(f"Current working directory: {os.getcwd()}") ``` -------------------------------- ### Get Project GUID using Azure CLI Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/agents/how-to/tools/toolbox.md Retrieve the project GUID required for uploading files to the account-level Files endpoint. Ensure you have the Azure CLI installed and are logged in. ```bash ARM_TOKEN=$(az account get-access-token --query accessToken -o tsv) PROJECT_GUID=$(curl -s -H "Authorization: Bearer $ARM_TOKEN" \ "https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.CognitiveServices/accounts/{account}/projects/{project}?api-version=2025-06-01" \ | jq -r '.properties.amlWorkspace.internalId') ``` -------------------------------- ### Python Quickstart Responses Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/includes/quickstart-v2-chat.md Example code for receiving model responses in Python. Ensure you have the necessary libraries installed. ```python from typing import List def get_response(input: str) -> str: # Replace with your actual model interaction logic return f"Model response to: {input}" def get_chat_response(messages: List[dict]) -> str: # Replace with your actual chat model interaction logic return "Chat model response" def get_tool_response(tool_code: str) -> str: # Replace with your actual tool response logic return "Tool response" if __name__ == "__main__": user_input = "Hello, AI!" response = get_response(user_input) print(f"Response: {response}") chat_messages = [{"role": "user", "content": "What is Azure AI Foundry?"}] chat_response = get_chat_response(chat_messages) print(f"Chat Response: {chat_response}") tool_code_example = "print(\"Hello\")" tool_response = get_tool_response(tool_code_example) print(f"Tool Response: {tool_response}") ``` -------------------------------- ### Create image-quickstart folder Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/openai/includes/dall-e-dotnet.md Creates a new directory for the project and navigates into it. ```shell mkdir image-quickstart && cd image-quickstart ``` -------------------------------- ### Initialize a new agent project from a sample Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/agents/quickstarts/quickstart-optimize-hosted-agent.md Create a new agent project folder and initialize it using a sample manifest URL. This process downloads the sample and generates necessary configuration files. ```bash mkdir my-agent && cd my-agent azd ai agent init -m https://github.com/microsoft-foundry/foundry-samples/blob/main/samples/python/hosted-agents/bring-your-own/responses/optimization-customer-support/agent.manifest.yaml . ``` -------------------------------- ### Get dictionary examples in Node.js Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/translator/text-translation/how-to/use-rest-api.md This Node.js code snippet shows how to fetch dictionary examples using the Translator REST API with Axios. Ensure you have Axios and UUID libraries installed. ```javascript const axios = require('axios').default; const { v4: uuidv4 } = require('uuid'); let key = ""; let endpoint = "https://api.cognitive.microsofttranslator.com"; // Add your location, also known as region. The default is global. // This is required if using an Azure AI multi-service resource. let location = ""; let params = new URLSearchParams(); params.append("api-version", "3.0"); params.append("from", "en"); params.append("to", "es"); axios({ baseURL: endpoint, url: '/dictionary/examples', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': key, // location required if you're using a multi-service or regional (not global) resource. 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json', 'X-ClientTraceId': uuidv4().toString() }, params: params, data: [{ 'text': 'sunlight', 'translation': 'luz solar' }], responseType: 'json' }).then(function(response){ console.log(JSON.stringify(response.data, null, 4)); }) ``` -------------------------------- ### Set up Node.js project Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/common/transcription-sdk-javascript.md Initialize a new Node.js project and install the necessary Azure SDK packages. ```bash mkdir transcription-quickstart cd transcription-quickstart npm init -y npm install @azure/ai-speech-transcription @azure/identity npm pkg set type=module ``` -------------------------------- ### Complete MCPQuickstart.java code example Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/how-to/voice-live-mcp-server/java.md This is the complete Java code for the MCPQuickstart class, demonstrating how to integrate Voice Live SDK for conversational AI, including handling approvals and tool calls. ```java import com.microsoft.cognitiveservices.speech.*; import com.microsoft.cognitiveservices.speech.audio.*; import com.microsoft.cognitiveservices.speech.conversation.*; import java.util.Scanner; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledFuture; public class MCPQuickstart { private static String speechKey = "YOUR_SPEECH_KEY"; private static String speechRegion = "YOUR_SPEECH_REGION"; // e.g., "eastus" private static String conversationId = "YOUR_CONVERSATION_ID"; // A unique ID for the conversation private static int toolCallCount = 0; private static final int MAX_TOOL_CALLS = 3; public static void main(String[] args) throws Exception { SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion); AudioConfig audioConfig = AudioConfig.fromDefaultMicrophoneInput(); // Use Direct Line Speech for MCP Conversation connection = Conversation.createConversation(audioConfig, speechConfig); // Configure MCP server // For 'deepwiki' server, tool calls execute automatically. // For 'azure_doc' server, you're prompted to approve each tool call. String serverLabel = "azure_doc"; // or "deepwiki" String serverUrl = "YOUR_MCP_SERVER_URL"; // e.g., "https://my-mcp-server.com/api" String allowedTools = "[\"tool1\", \"tool2\"]"; // Optional: specify allowed tools String requireApproval = "always"; // "never", "always", or per-tool dictionary // Construct the server configuration JSON string String serverConfigJson = String.format( "{\"serverLabel\": \"%s\", \"serverUrl\": \"%s\", \"allowedTools\": %s, \"requireApproval\": \"%s\"}", serverLabel, serverUrl, allowedTools, requireApproval ); // Send the server configuration to the conversation connection.send(serverConfigJson); // Subscribe to events connection.synthesis.SpeechStartDetected.addEventListener((s, e) -> { System.out.println("Speech start detected."); }); connection.synthesis.SpeechEndDetected.addEventListener((s, e) -> { System.out.println("Speech end detected."); }); connection.session.addParticipantJoinedListener(e -> { System.out.println("Participant joined: " + e.getParticipantId()); }); connection.session.addParticipantLeftListener(e -> { System.out.println("Participant left: " + e.getParticipantId()); }); connection.conversation.addParticipantReportedListener(e -> { System.out.println("Participant reported: " + e.getParticipantId()); }); connection.conversation.addMessageReceivedListener((s, e) -> { System.out.println("Message received: " + e.getMessage()); // Handle different message types, e.g., tool calls, responses if (e.getMessage().getType() == ConversationMessageContentType.TEXT) { System.out.println("Text message: " + e.getMessage().getText()); } }); connection.conversation.addItemReceivedListener((s, e) -> { System.out.println("Item received: " + e.getItem()); // Handle conversation items, e.g., tool calls, transcriptions if (e.getItem().hasInput() && e.getItem().getInput().hasAudioTranscription()) { if (e.getItem().getInput().getAudioTranscription().isTranscriptionCompleted()) { String transcript = e.getItem().getInput().getAudioTranscription().getTranscription(); System.out.println("Transcription completed: " + transcript); // Example: Handle voice-based approval if (transcript.matches("\\byes\\b")) { // User approved sendResponse(new MCPApprovalResponseRequestItem(conversationId, true)); } else if (transcript.matches("\\b(no|stop|cancel)\\b")) { // User denied sendResponse(new MCPApprovalResponseRequestItem(conversationId, false)); } } } }); // Start the conversation connection.open().get(); System.out.println("Conversation opened. Speak into your microphone."); // Example: Handle approval requests with a system message if requireApproval is 'always' // String systemMessage = "You must ask the user for approval before calling any tools."; // connection.sendSystemMessage(systemMessage); // Example: Detect stalls during MCP tool calls // ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); // Runnable injectSystemMessage = () -> { // if (toolCallCount < MAX_TOOL_CALLS) { // connection.sendSystemMessage("Still waiting for results..."); // toolCallCount++; // } else { // // Auto-deny after max attempts // sendResponse(new MCPApprovalResponseRequestItem(conversationId, false)); // scheduler.shutdown(); // } // }; // ScheduledFuture scheduledFuture = scheduler.scheduleAtFixedRate(injectSystemMessage, 10, 10, TimeUnit.SECONDS); // Keep the application running until interrupted Scanner scanner = new Scanner(System.in); System.out.println("Press Enter to stop the session..."); scanner.nextLine(); // Clean up connection.close().get(); // if (scheduledFuture != null) { // scheduledFuture.cancel(true); // } // scheduler.shutdown(); scanner.close(); System.out.println("Session stopped."); } // Helper method to send a response item (e.g., approval decision) private static void sendResponse(ConversationRequestItem item) { try { Future result = connection.send(item); result.get(); // Wait for the send operation to complete System.out.println("Response sent: " + item.toString()); } catch (Exception e) { System.err.println("Error sending response: " + e.getMessage()); } } } ``` -------------------------------- ### Get dictionary examples in C# Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/translator/text-translation/how-to/use-rest-api.md Use this snippet to retrieve contextual examples for a given term and its translation. Ensure you have the Newtonsoft.Json NuGet package installed. Replace placeholders for your key, location, and the text/translation pair. ```csharp using System; using Newtonsoft.Json; // Install Newtonsoft.Json with NuGet class Program { private static readonly string key = ""; private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com"; // location, also known as region. // required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page. private static readonly string location = ""; static async Task Main(string[] args) { // See examples of terms in context string route = "/dictionary/examples?api-version=3.0&from=en&to=es"; object[] body = new object[] { new { Text = "sunlight", Translation = "luz solar" } } ; var requestBody = JsonConvert.SerializeObject(body); using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { // Build the request. request.Method = HttpMethod.Post; request.RequestUri = new Uri(endpoint + route); request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); request.Headers.Add("Ocp-Apim-Subscription-Key", key); // location required if you're using a multi-service or regional (not global) resource. request.Headers.Add("Ocp-Apim-Subscription-Region", location); // Send the request and get response. HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); // Read response as a string. string result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result); } } } ``` -------------------------------- ### Navigate to Quickstart Folder and Open in VS Code Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/semantic-ranker-rest.md Navigate into the cloned repository's quickstart folder and open it in Visual Studio Code. This prepares your workspace for modifying the REST client files. ```bash cd azure-search-rest-samples/Quickstart-semantic-ranking code . ``` -------------------------------- ### REST API Quickstart Responses Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/includes/quickstart-v2-chat.md Example shell script for interacting with the REST API to get model responses. Replace placeholders with your actual values. ```console curl -X POST "https://YOUR-FOUNDRY-RESOURCE-NAME.cognitiveservices.azure.com/openai/deployments/YOUR-DEPLOYMENT-NAME/chat/completions?api-version=2023-07-01-preview" -H "Content-Type: application/json" -H "Accept: application/json" -H "api-key: YOUR-API-KEY" -d '{ "messages": [{"role": "user", "content": "Hello, AI!"}] }' ``` -------------------------------- ### Navigate to the quickstart folder Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/search-get-started-vector-javascript.md Change your current directory to the quickstart-vector-js folder within the cloned repository. ```bash cd azure-search-javascript-samples/quickstart-vector-js ``` -------------------------------- ### Get started with Speech to Text using AI Foundry Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/get-started-speech-to-text.md This snippet demonstrates the basic setup for using the Speech service with AI Foundry. It requires specific include directives. ```text [!INCLUDE [Microsoft Foundry include](includes/quickstarts/speech-to-text-basics/ai-foundry.md)] ``` -------------------------------- ### Verify Azure AI Projects SDK setup Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/multi-service-resource.md Use this minimal example to confirm authentication and access to your Azure AI resource. Ensure you have installed the necessary SDK packages. ```python # Install the SDK: pip install azure-ai-projects azure-identity from azure.ai.projects import AIProjectClient from azure.identity import DefaultAzureCredential ``` -------------------------------- ### Set up Node.js project and install packages Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/common/llm-speech-sdk-javascript.md Initialize a Node.js project and install the necessary Azure SDK packages for speech transcription and identity management. ```shell mkdir llm-speech-quickstart && cd llm-speech-quickstart npm init -y npm install @azure/ai-speech-transcription @azure/identity ``` -------------------------------- ### Navigate and open project Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/semantic-ranker-python.md Navigate to the specific quickstart folder and open it in Visual Studio Code. This prepares your workspace for the next steps. ```bash cd azure-search-python-samples/Quickstart-Semantic-Ranking code . ``` -------------------------------- ### Async API Requests using Client Libraries Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/language-service/concepts/use-asynchronously.md Provides links to examples for sending asynchronous API requests to the Language service using various client libraries. These examples guide users on how to install the necessary libraries and utilize client objects for asynchronous operations tailored to different programming languages. ```C# See /dotnet/api/overview/azure/ai.textanalytics-readme?preserve-view=true&view=azure-dotnet#async-examples ``` ```Java See /java/api/overview/azure/ai-textanalytics-readme?preserve-view=true&view=azure-java-preview#analyze-multiple-actions ``` ```JavaScript See /javascript/api/overview/azure/ai-text-analytics-readme?preserve-view=true&view=azure-node-preview#analyze-actions ``` ```Python See /python/api/overview/azure/ai-textanalytics-readme?preserve-view=true&view=azure-python-preview#multiple-analysis ``` -------------------------------- ### Start a batch scoring job using Python SDK Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/machine-learning/how-to-use-batch-model-deployments.md Initiates a batch scoring job using the Azure Machine Learning Python SDK. This example assumes the necessary SDK components are installed and authenticated. ```python from azure.ai.ml import MLClient from azure.ai.ml.entities import BatchDeployment, BatchEndpoint, CodeConfiguration, Model from azure.identity import DefaultAzureCredential subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" resource_group = "rg-name" workspace = "workspace-name" ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource group, workspace) # Create a batch endpoint if it doesn't exist endpoint = BatchEndpoint(name="mnist-classifier-endpoint") ml_client.batch_endpoints.begin_create_or_update(endpoint).result() # Create a batch deployment model = Model(path="azureml://registries/azureml/models/mnist-classifier/versions/1") blue_print = CodeConfiguration(code="./src", scoring_script="score.py") deployment = BatchDeployment( name="mnist-classifier", description="Batch deployment for MNIST classifier", endpoint_name="mnist-classifier-endpoint", model=model, code_configuration=blue_print, instance_type="Standard_DS3_v2", instance_count=2, ) ml_client.batch_deployments.begin_create_or_update(deployment).result() # Invoke the batch endpoint job = ml_client.batch_endpoints.invoke( endpoint_name="mnist-classifier-endpoint", deployment_name="mnist-classifier", input={ "path": "azureml://datastores/workspaceblobstore/paths/data/mnist/sample" }, ) print(f"Job name: {job.name}") ``` -------------------------------- ### Run Python Quickstart Application Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/metrics-advisor/includes/quickstarts/python.md Execute the Python quickstart file using the python command. Ensure the file name matches the one in your project. ```console python quickstart-file.py ``` -------------------------------- ### Complete Agent Example with Optimization Config Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/agents/how-to/make-agent-optimizer-ready.md This Python script demonstrates a complete agent setup that leverages optimization configurations for instructions, tools, and skills. It includes example tools for travel policy lookup, budget checking, and flight alternatives. Ensure you have the necessary agent framework and Azure AI libraries installed. ```python import json import logging import os from pathlib import Path from typing import Annotated from agent_framework import Agent, tool from agent_framework.foundry import FoundryChatClient from agent_framework_foundry_hosting import ResponsesHostServer from azure.identity import DefaultAzureCredential from pydantic import Field from azure.ai.agentserver.optimization import load_config, load_skills_from_dir logger = logging.getLogger(__name__) @tool(approval_mode="never_require") def lookup_travel_policy() -> str: """Look up the company travel policy rules and limits.""" return json.dumps({ "company": "Contoso Ltd.", "approval_thresholds": { "auto": 1500, "manager": 3000, "director": 7500, "vp": "above 7500" }, "lodging_per_night": {"domestic": 250, "international": 400}, "airfare": "economy only; business class if flight > 6 hours", "advance_booking_days": 14, }) @tool(approval_mode="never_require") def check_department_budget() -> str: """Check the remaining travel budget for the employee's department.""" return json.dumps({ "department": "Engineering", "total_budget": 50000, "remaining": 14800, }) @tool(approval_mode="never_require") def get_flight_alternatives( destination: Annotated[str, Field(description="The travel destination city")], ) -> str: """Find cheaper flight alternatives for the given destination.""" return json.dumps({ "alternatives": [ {"option": "Flexible dates (±2 days)", "savings": "$200-800"}, {"option": "Nearby alternate airport", "savings": "$100-400"}, ], }) def main(): # Load optimization config from .agent_configs/ config = load_config() # Load skills from local directory if not provided by optimization if not config.skills and config.skills_dir: config.skills.extend(load_skills_from_dir(Path(config.skills_dir))) model = config.model or os.environ.get( "AZURE_AI_MODEL_DEPLOYMENT_NAME", "gpt-4.1-mini" ) instructions = config.compose_instructions() # Apply optimized tool descriptions tools = [lookup_travel_policy, check_department_budget, get_flight_alternatives] config.apply_tool_descriptions(tools) logger.info( "Config source=%s | model=%s | prompt_len=%d | skills=%d", config.source, model, len(instructions), len(config.skills), ) client = FoundryChatClient( project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], model=model, credential=DefaultAzureCredential(), ) agent = Agent( client=client, instructions=instructions, tools=tools, default_options={"store": False}, ) server = ResponsesHostServer(agent) server.run() if __name__ == "__main__": main() ``` -------------------------------- ### Navigate to Quickstart Folder Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/agentic-retrieval-csharp.md Navigate to the agentic retrieval quickstart directory within the cloned repository. ```bash cd azure-search-dotnet-samples/quickstart-agentic-retrieval ``` -------------------------------- ### Create a response with an image and text in JavaScript Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/openai/includes/how-to-responses-content.md This JavaScript example shows how to upload an image and then use the responses API to get an analysis. It utilizes Node.js streams for file handling. Ensure the OpenAI Node.js library is installed. ```javascript import fs from "node:fs"; import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/", apiKey: process.env.AZURE_OPENAI_API_KEY, }); const file = await client.files.create({ file: fs.createReadStream("path_to_your_image.jpg"), purpose: "vision", }); const response = await client.responses.create({ model: "MODEL_NAME", input: [ { role: "user", content: [ { type: "input_text", text: "What is in this image?" }, { type: "input_image", file_id: file.id }, ], }, ], }); console.log(response.output_text) ``` -------------------------------- ### Java Voice Live Function Calling Quickstart Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/how-to/voice-live-function-calling/java.md This Java code provides a comprehensive example of integrating function calling with the Voice Live SDK. It includes necessary imports, audio processing setup, and the core logic for handling function calls. ```java // ------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // ------------------------------------------------------------------------- import com.azure.ai.voicelive.VoiceLiveAsyncClient; import com.azure.ai.voicelive.VoiceLiveClientBuilder; import com.azure.ai.voicelive.VoiceLiveServiceVersion; import com.azure.ai.voicelive.VoiceLiveSessionAsyncClient; import com.azure.ai.voicelive.models.AudioEchoCancellation; import com.azure.ai.voicelive.models.AudioInputTranscriptionOptions; import com.azure.ai.voicelive.models.AudioInputTranscriptionOptionsModel; import com.azure.ai.voicelive.models.AudioNoiseReduction; import com.azure.ai.voicelive.models.AudioNoiseReductionType; import com.azure.ai.voicelive.models.AzureStandardVoice; import com.azure.ai.voicelive.models.ClientEventConversationItemCreate; import com.azure.ai.voicelive.models.ClientEventResponseCreate; import com.azure.ai.voicelive.models.ClientEventSessionUpdate; import com.azure.ai.voicelive.models.FunctionCallOutputItem; import com.azure.ai.voicelive.models.InputAudioFormat; import com.azure.ai.voicelive.models.InteractionModality; import com.azure.ai.voicelive.models.OutputAudioFormat; import com.azure.ai.voicelive.models.ServerEventType; import com.azure.ai.voicelive.models.ServerVadTurnDetection; import com.azure.ai.voicelive.models.SessionUpdate; import com.azure.ai.voicelive.models.ResponseFunctionCallItem; import com.azure.ai.voicelive.models.SessionUpdateConversationItemCreated; import com.azure.ai.voicelive.models.SessionUpdateError; import com.azure.ai.voicelive.models.SessionUpdateResponseAudioDelta; import com.azure.ai.voicelive.models.SessionUpdateResponseFunctionCallArgumentsDone; import com.azure.ai.voicelive.models.SessionUpdateSessionUpdated; import com.azure.ai.voicelive.models.ToolChoiceLiteral; import com.azure.ai.voicelive.models.VoiceLiveFunctionDefinition; import com.azure.ai.voicelive.models.VoiceLiveSessionOptions; import com.azure.core.credential.KeyCredential; import com.azure.core.credential.TokenCredential; import com.azure.core.util.BinaryData; import com.azure.identity.AzureCliCredentialBuilder; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.TargetDataLine; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; /** * Voice assistant with function calling using * VoiceLive SDK for Java. */ public final class FunctionCallingQuickstart { private static final int SAMPLE_RATE = 24000; private static final int CHANNELS = 1; private static final int SAMPLE_SIZE_BITS = 16; private static final int CHUNK_SIZE = 1200; // Available backend functions private static final Map, Map>> FUNCTIONS = new HashMap<>(); static { FUNCTIONS.put( "get_current_time", FunctionCallingQuickstart::getCurrentTime); FUNCTIONS.put( "get_current_weather", FunctionCallingQuickstart::getCurrentWeather); } // Pending function call state private static volatile String pendingName; private static volatile String pendingCallId; private static volatile String pendingItemId; private static volatile String pendingArguments; private FunctionCallingQuickstart() { } // --------------------------------------------------------------- // Audio processor // --------------------------------------------------------------- private static class AudioProcessor { private final VoiceLiveSessionAsyncClient session; private final AudioFormat audioFormat; private TargetDataLine microphone; private SourceDataLine speaker; private final AtomicBoolean isCapturing = new AtomicBoolean(false); private final AtomicBoolean isPlaying = new AtomicBoolean(false); private final BlockingQueue ``` -------------------------------- ### Initialize Project and Dependencies Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/openai/includes/whisper-javascript.md Commands to create a project directory, initialize package.json, and install required SDKs. ```shell mkdir synthesis-quickstart && cd synthesis-quickstart ``` ```shell npm init -y ``` ```console npm install openai ``` ```console npm install @azure/identity ``` -------------------------------- ### Create a response with an image and text in C# Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/openai/includes/how-to-responses-content.md This C# example demonstrates how to upload an image and then use the responses API to get an analysis from a model. It shows both API key and Microsoft Entra ID authentication methods. Ensure the Azure OpenAI SDK for .NET is installed. ```csharp #pragma warning disable OPENAI001 using System.IO; using System.Threading.Tasks; using Azure.Identity; using OpenAI; using OpenAI.Files; using OpenAI.Responses; using System.ClientModel.Primitives; string endpoint = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1"; // API key authentication ResponsesClient openAIClient = new( credential: new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")!), options: new ResponsesClientOptions { Endpoint = new Uri(endpoint) }); OpenAIFileClient fileClient = new( credential: new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")!), options: new OpenAIClientOptions { Endpoint = new Uri(endpoint) }); // Microsoft Entra ID authentication (recommended) BearerTokenPolicy tokenPolicy = new( new DefaultAzureCredential(), "https://ai.azure.com/.default"); ResponsesClient openAIClientEntra = new( authenticationPolicy: tokenPolicy, options: new ResponsesClientOptions { Endpoint = new Uri(endpoint) }); byte[] imageBytes = await File.ReadAllBytesAsync("path_to_your_image.jpg"); OpenAIFile uploadedFile = await fileClient.UploadFileAsync( BinaryData.FromBytes(imageBytes), "path_to_your_image.jpg", FileUploadPurpose.Vision); CreateResponseOptions options = new() { Model = "MODEL_NAME", InputItems = { ResponseItem.CreateUserMessageItem( [ ResponseContentPart.CreateInputTextPart("What is in this image?"), ResponseContentPart.CreateInputImagePart(uploadedFile.Id) ]) } }; ResponseResult response = await openAIClient.CreateResponseAsync(options); Console.WriteLine(response.GetOutputText()) ``` -------------------------------- ### Run MCP Quickstart Sample Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/how-to/voice-live-mcp-server/python.md Execute the Python script for the MCP quickstart. This involves creating the script file, signing into Azure, and running the script to interact with Voice Live. ```shell az login python mcp-quickstart.py ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/agentic-retrieval-csharp.md Install the necessary .NET dependencies for the agentic retrieval quickstart project. ```bash dotnet restore AgenticRetrievalQuickstart.csproj ``` -------------------------------- ### Navigate to the quickstart folder Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/semantic-ranker-csharp.md Change the current directory to the semantic ranking quickstart folder within the cloned repository. ```bash cd azure-search-dotnet-samples/quickstart-semantic-ranking ``` -------------------------------- ### TypeScript example for function calling with Azure AI agents Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/agents/how-to/tools/function-calling.md This sample demonstrates how to create an agent with function tools, handle function calls from the model, and provide function results to get the final response. Ensure you have the necessary Azure Identity and AI Projects packages installed. ```typescript import { DefaultAzureCredential } from "@azure/identity"; import { AIProjectClient } from "@azure/ai-projects"; // Format: "https://resource_name.ai.azure.com/api/projects/project_name" const projectEndpoint = "your_project_endpoint"; /** * Define a function tool for the model to use */ const funcTool = { type: "function" as const, name: "get_horoscope", description: "Get today's horoscope for an astrological sign.", strict: true, parameters: { type: "object", properties: { sign: { type: "string", description: "An astrological sign like Taurus or Aquarius", }, }, required: ["sign"], additionalProperties: false, }, }; /** * Generate a horoscope for the given astrological sign. */ function getHoroscope(sign: string): string { return `${sign}: Next Tuesday you will befriend a baby otter.`; } export async function main(): Promise { // Create AI Project client const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential()); const openai = project.getOpenAIClient(); // Create a conversation for multi-turn interaction const conversation = await openai.conversations.create(); // Create agent with function tools const agent = await project.agents.createVersion("function-tool-agent", { kind: "prompt", model: "gpt-4.1-mini", instructions: "You are a helpful assistant that can use function tools.", tools: [funcTool], }); // Prompt the model with tools defined const response = await openai.responses.create( { input: [ { type: "message", role: "user", content: "What is my horoscope? I am an Aquarius.", }, ], conversation: conversation.id, }, { body: { agent: { name: agent.name, type: "agent_reference" } }, }, ); console.log(`Response output: ${response.output_text}`); // Process function calls const inputList: Array<{ type: "function_call_output"; call_id: string; output: string; }> = []; for (const item of response.output) { if (item.type === "function_call") { if (item.name === "get_horoscope") { // Parse the function arguments const args = JSON.parse(item.arguments); // Execute the function logic for get_horoscope const horoscope = getHoroscope(args.sign); // Provide function call results to the model inputList.push({ type: "function_call_output", call_id: item.call_id, output: JSON.stringify({ horoscope }), }); } } } // Submit function results to get final response const finalResponse = await openai.responses.create( { input: inputList, conversation: conversation.id, }, { body: { agent: { name: agent.name, type: "agent_reference" } }, }, ); // Print the final response console.log(finalResponse.output_text); // Clean up await project.agents.deleteVersion(agent.name, agent.version); await openai.conversations.delete(conversation.id); } main().catch((err) => { console.error("The sample encountered an error:", err); }); ``` -------------------------------- ### Navigate to the quickstart folder Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/search/includes/quickstarts/search-get-started-vector-csharp.md Change your current directory to the vector search quickstart folder within the cloned repository. ```bash cd azure-search-dotnet-samples/quickstart-vector-search ``` -------------------------------- ### Install Python packages as azureuser in setup script Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/machine-learning/how-to-customize-compute-instance.md This script demonstrates how to execute commands as the 'azureuser' user within a root-level setup script. It is specifically used for installing Python packages into the user's environment. ```bash #!/bin/bash sudo -u azureuser -i <<'EOF' pip install EOF ``` -------------------------------- ### Run Python Quickstart Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/ai-services/speech-service/includes/quickstarts/voice-live-api/python.md Execute the Python quickstart script. Use the `--use-token-credential` flag for authentication. ```shell python voice-live-quickstart.py --use-token-credential ``` -------------------------------- ### Install OpenAI SDK for Go Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/includes/model-inference-migration/go.md Install the OpenAI SDK for Go using the go get command. ```bash go get github.com/openai/openai-go/v3 ``` -------------------------------- ### C# Quickstart for Realtime API Source: https://github.com/microsoftdocs/azure-ai-docs/blob/main/articles/foundry/openai/how-to/realtime-audio.md This snippet provides a quickstart guide for using the Realtime API with C#. It is suitable for server-to-server scenarios where low latency is not a requirement. ```csharp using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; using System; using System.IO; using System.Threading.Tasks; public class RealtimeAudio { public static async Task SynthesizeAudioAsync(string text, string outputPath) { var speechConfig = SpeechConfig.FromSubscription("YOUR_SPEECH_KEY", "YOUR_SPEECH_REGION"); speechConfig.SpeechSynthesisVoiceName = "en-US-JennyNeural"; using var synthesizer = new SpeechSynthesizer(speechConfig, null); using var result = await synthesizer.SpeakTextAsync(text); if (result.Reason == ResultReason.SynthesizingAudioCompleted) { await File.WriteAllBytesAsync(outputPath, result.AudioData); Console.WriteLine($"Audio content written out to: {outputPath}"); } else { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); Console.WriteLine($"Speech synthesis failed: {cancellation.Reason}"); if (!string.IsNullOrEmpty(cancellation.ErrorDetails)) { Console.WriteLine($"Error details: {cancellation.ErrorDetails}"); } } } public static async Task Main(string[] args) { string textToSynthesize = "Hello, this is a test of the real-time audio API."; string outputFilePath = "output.wav"; await SynthesizeAudioAsync(textToSynthesize, outputFilePath); } } ```