Add this skill
npx mdskills install sickn33/azure-ai-projects-dotnetComprehensive SDK reference with complete examples and clear client hierarchy
1---2name: azure-ai-projects-dotnet3description: |4 Azure AI Projects SDK for .NET. High-level client for Azure AI Foundry projects including agents, connections, datasets, deployments, evaluations, and indexes. Use for AI Foundry project management, versioned agents, and orchestration. Triggers: "AI Projects", "AIProjectClient", "Foundry project", "versioned agents", "evaluations", "datasets", "connections", "deployments .NET".5package: Azure.AI.Projects6---78# Azure.AI.Projects (.NET)910High-level SDK for Azure AI Foundry project operations including agents, connections, datasets, deployments, evaluations, and indexes.1112## Installation1314```bash15dotnet add package Azure.AI.Projects16dotnet add package Azure.Identity1718# Optional: For versioned agents with OpenAI extensions19dotnet add package Azure.AI.Projects.OpenAI --prerelease2021# Optional: For low-level agent operations22dotnet add package Azure.AI.Agents.Persistent --prerelease23```2425**Current Versions**: GA v1.1.0, Preview v1.2.0-beta.52627## Environment Variables2829```bash30PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>31MODEL_DEPLOYMENT_NAME=gpt-4o-mini32CONNECTION_NAME=<your-connection-name>33AI_SEARCH_CONNECTION_NAME=<ai-search-connection>34```3536## Authentication3738```csharp39using Azure.Identity;40using Azure.AI.Projects;4142var endpoint = Environment.GetEnvironmentVariable("PROJECT_ENDPOINT");43AIProjectClient projectClient = new AIProjectClient(44 new Uri(endpoint),45 new DefaultAzureCredential());46```4748## Client Hierarchy4950```51AIProjectClient52├── Agents → AIProjectAgentsOperations (versioned agents)53├── Connections → ConnectionsClient54├── Datasets → DatasetsClient55├── Deployments → DeploymentsClient56├── Evaluations → EvaluationsClient57├── Evaluators → EvaluatorsClient58├── Indexes → IndexesClient59├── Telemetry → AIProjectTelemetry60├── OpenAI → ProjectOpenAIClient (preview)61└── GetPersistentAgentsClient() → PersistentAgentsClient62```6364## Core Workflows6566### 1. Get Persistent Agents Client6768```csharp69// Get low-level agents client from project client70PersistentAgentsClient agentsClient = projectClient.GetPersistentAgentsClient();7172// Create agent73PersistentAgent agent = await agentsClient.Administration.CreateAgentAsync(74 model: "gpt-4o-mini",75 name: "Math Tutor",76 instructions: "You are a personal math tutor.");7778// Create thread and run79PersistentAgentThread thread = await agentsClient.Threads.CreateThreadAsync();80await agentsClient.Messages.CreateMessageAsync(thread.Id, MessageRole.User, "Solve 3x + 11 = 14");81ThreadRun run = await agentsClient.Runs.CreateRunAsync(thread.Id, agent.Id);8283// Poll for completion84do85{86 await Task.Delay(500);87 run = await agentsClient.Runs.GetRunAsync(thread.Id, run.Id);88}89while (run.Status == RunStatus.Queued || run.Status == RunStatus.InProgress);9091// Get messages92await foreach (var msg in agentsClient.Messages.GetMessagesAsync(thread.Id))93{94 foreach (var content in msg.ContentItems)95 {96 if (content is MessageTextContent textContent)97 Console.WriteLine(textContent.Text);98 }99}100101// Cleanup102await agentsClient.Threads.DeleteThreadAsync(thread.Id);103await agentsClient.Administration.DeleteAgentAsync(agent.Id);104```105106### 2. Versioned Agents with Tools (Preview)107108```csharp109using Azure.AI.Projects.OpenAI;110111// Create agent with web search tool112PromptAgentDefinition agentDefinition = new(model: "gpt-4o-mini")113{114 Instructions = "You are a helpful assistant that can search the web",115 Tools = {116 ResponseTool.CreateWebSearchTool(117 userLocation: WebSearchToolLocation.CreateApproximateLocation(118 country: "US",119 city: "Seattle",120 region: "Washington"121 )122 ),123 }124};125126AgentVersion agentVersion = await projectClient.Agents.CreateAgentVersionAsync(127 agentName: "myAgent",128 options: new(agentDefinition));129130// Get response client131ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForAgent(agentVersion.Name);132133// Create response134ResponseResult response = responseClient.CreateResponse("What's the weather in Seattle?");135Console.WriteLine(response.GetOutputText());136137// Cleanup138projectClient.Agents.DeleteAgentVersion(agentName: agentVersion.Name, agentVersion: agentVersion.Version);139```140141### 3. Connections142143```csharp144// List all connections145foreach (AIProjectConnection connection in projectClient.Connections.GetConnections())146{147 Console.WriteLine($"{connection.Name}: {connection.ConnectionType}");148}149150// Get specific connection151AIProjectConnection conn = projectClient.Connections.GetConnection(152 connectionName,153 includeCredentials: true);154155// Get default connection156AIProjectConnection defaultConn = projectClient.Connections.GetDefaultConnection(157 includeCredentials: false);158```159160### 4. Deployments161162```csharp163// List all deployments164foreach (AIProjectDeployment deployment in projectClient.Deployments.GetDeployments())165{166 Console.WriteLine($"{deployment.Name}: {deployment.ModelName}");167}168169// Filter by publisher170foreach (var deployment in projectClient.Deployments.GetDeployments(modelPublisher: "Microsoft"))171{172 Console.WriteLine(deployment.Name);173}174175// Get specific deployment176ModelDeployment details = (ModelDeployment)projectClient.Deployments.GetDeployment("gpt-4o-mini");177```178179### 5. Datasets180181```csharp182// Upload single file183FileDataset fileDataset = projectClient.Datasets.UploadFile(184 name: "my-dataset",185 version: "1.0",186 filePath: "data/training.txt",187 connectionName: connectionName);188189// Upload folder190FolderDataset folderDataset = projectClient.Datasets.UploadFolder(191 name: "my-dataset",192 version: "2.0",193 folderPath: "data/training",194 connectionName: connectionName,195 filePattern: new Regex(".*\\.txt"));196197// Get dataset198AIProjectDataset dataset = projectClient.Datasets.GetDataset("my-dataset", "1.0");199200// Delete dataset201projectClient.Datasets.Delete("my-dataset", "1.0");202```203204### 6. Indexes205206```csharp207// Create Azure AI Search index208AzureAISearchIndex searchIndex = new(aiSearchConnectionName, aiSearchIndexName)209{210 Description = "Sample Index"211};212213searchIndex = (AzureAISearchIndex)projectClient.Indexes.CreateOrUpdate(214 name: "my-index",215 version: "1.0",216 index: searchIndex);217218// List indexes219foreach (AIProjectIndex index in projectClient.Indexes.GetIndexes())220{221 Console.WriteLine(index.Name);222}223224// Delete index225projectClient.Indexes.Delete(name: "my-index", version: "1.0");226```227228### 7. Evaluations229230```csharp231// Create evaluation configuration232var evaluatorConfig = new EvaluatorConfiguration(id: EvaluatorIDs.Relevance);233evaluatorConfig.InitParams.Add("deployment_name", BinaryData.FromObjectAsJson("gpt-4o"));234235// Create evaluation236Evaluation evaluation = new Evaluation(237 data: new InputDataset("<dataset_id>"),238 evaluators: new Dictionary<string, EvaluatorConfiguration>239 {240 { "relevance", evaluatorConfig }241 }242)243{244 DisplayName = "Sample Evaluation"245};246247// Run evaluation248Evaluation result = projectClient.Evaluations.Create(evaluation: evaluation);249250// Get evaluation251Evaluation getResult = projectClient.Evaluations.Get(result.Name);252253// List evaluations254foreach (var eval in projectClient.Evaluations.GetAll())255{256 Console.WriteLine($"{eval.DisplayName}: {eval.Status}");257}258```259260### 8. Get Azure OpenAI Chat Client261262```csharp263using Azure.AI.OpenAI;264using OpenAI.Chat;265266ClientConnection connection = projectClient.GetConnection(typeof(AzureOpenAIClient).FullName!);267268if (!connection.TryGetLocatorAsUri(out Uri uri) || uri is null)269 throw new InvalidOperationException("Invalid URI.");270271uri = new Uri($"https://{uri.Host}");272273AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(uri, new DefaultAzureCredential());274ChatClient chatClient = azureOpenAIClient.GetChatClient("gpt-4o-mini");275276ChatCompletion result = chatClient.CompleteChat("List all rainbow colors");277Console.WriteLine(result.Content[0].Text);278```279280## Available Agent Tools281282| Tool | Class | Purpose |283|------|-------|---------|284| Code Interpreter | `CodeInterpreterToolDefinition` | Execute Python code |285| File Search | `FileSearchToolDefinition` | Search uploaded files |286| Function Calling | `FunctionToolDefinition` | Call custom functions |287| Bing Grounding | `BingGroundingToolDefinition` | Web search via Bing |288| Azure AI Search | `AzureAISearchToolDefinition` | Search Azure AI indexes |289| OpenAPI | `OpenApiToolDefinition` | Call external APIs |290| Azure Functions | `AzureFunctionToolDefinition` | Invoke Azure Functions |291| MCP | `MCPToolDefinition` | Model Context Protocol tools |292293## Key Types Reference294295| Type | Purpose |296|------|---------|297| `AIProjectClient` | Main entry point |298| `PersistentAgentsClient` | Low-level agent operations |299| `PromptAgentDefinition` | Versioned agent definition |300| `AgentVersion` | Versioned agent instance |301| `AIProjectConnection` | Connection to Azure resource |302| `AIProjectDeployment` | Model deployment info |303| `AIProjectDataset` | Dataset metadata |304| `AIProjectIndex` | Search index metadata |305| `Evaluation` | Evaluation configuration and results |306307## Best Practices3083091. **Use `DefaultAzureCredential`** for production authentication3102. **Use async methods** (`*Async`) for all I/O operations3113. **Poll with appropriate delays** (500ms recommended) when waiting for runs3124. **Clean up resources** — delete threads, agents, and files when done3135. **Use versioned agents** (via `Azure.AI.Projects.OpenAI`) for production scenarios3146. **Store connection IDs** rather than names for tool configurations3157. **Use `includeCredentials: true`** only when credentials are needed3168. **Handle pagination** — use `AsyncPageable<T>` for listing operations317318## Error Handling319320```csharp321using Azure;322323try324{325 var result = await projectClient.Evaluations.CreateAsync(evaluation);326}327catch (RequestFailedException ex)328{329 Console.WriteLine($"Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");330}331```332333## Related SDKs334335| SDK | Purpose | Install |336|-----|---------|---------|337| `Azure.AI.Projects` | High-level project client (this SDK) | `dotnet add package Azure.AI.Projects` |338| `Azure.AI.Agents.Persistent` | Low-level agent operations | `dotnet add package Azure.AI.Agents.Persistent` |339| `Azure.AI.Projects.OpenAI` | Versioned agents with OpenAI | `dotnet add package Azure.AI.Projects.OpenAI` |340341## Reference Links342343| Resource | URL |344|----------|-----|345| NuGet Package | https://www.nuget.org/packages/Azure.AI.Projects |346| API Reference | https://learn.microsoft.com/dotnet/api/azure.ai.projects |347| GitHub Source | https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/ai/Azure.AI.Projects |348| Samples | https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/ai/Azure.AI.Projects/samples |349
Full transparency — inspect the skill content before installing.