Skip to content

Commit f3b803d

Browse files
authored
Merge pull request #55 from PabloNunes/main
Adding csharp starter samples
2 parents 00221e0 + 70ef57e commit f3b803d

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# C# Getting Started Sample
2+
3+
This sample demonstrates how to use the AI Foundry platform with C#/.NET. It provides basic examples for authentication, accessing data, and performing common operations with the Foundry APIs.
4+
5+
## Prerequisites
6+
7+
- [.NET SDK](https://dotnet.microsoft.com/download) (version 9.0 or later recommended)
8+
- Visual Studio 22 or Visual Studio Code
9+
10+
## Install Dependencies
11+
12+
13+
## Setup
14+
15+
1. Clone this repository
16+
2. Set up your Azure Ai Foundry account and secrets at the .env file
17+
18+
19+
## Running the Sample
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
AZURE_AI_ENDPOINT=https://your.services.ai.azure.com/api/projects/project
2+
AZURE_AI_MODEL=your_model_name
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// <create_filesearch_agent>
2+
3+
// Placeholder for Filesearch agent
4+
5+
// </create_filesearch_agent>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Azure;
2+
using Azure.AI.Agents.Persistent;
3+
using Azure.Core;
4+
using Azure.Identity;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
9+
10+
namespace AiAgentsTests
11+
{
12+
13+
// <create_and_run_agent>
14+
public class AgentService
15+
{
16+
public static void MathAgent()
17+
{
18+
var endpointUrl = Evironment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
19+
var modelName = Evironment.GetEnvironmentVariable("AZURE_OPENAI_MODEL_NAME");
20+
21+
var endpoint = new Uri(endpointUrl);
22+
var credential = new DefaultAzureCredential();
23+
var model = modelName;
24+
25+
var agentClient = new PersistentAgentsClient(
26+
endpoint,
27+
credential);
28+
29+
// Create a new agent with the specified model, name, and instructions
30+
PersistentAgent agent = agentClient.CreateAgent(
31+
model: model,
32+
name: "Math Tutor",
33+
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
34+
tools: [new CodeInterpreterToolDefinition()]);
35+
36+
// Create a new thread for the agent to run in
37+
PersistentAgentThread thread = agentClient.CreateThread();
38+
39+
// Create a new run for the agent in the thread
40+
ThreadMessage message = agentClient.CreateMessage(
41+
thread.Id,
42+
MessageRole.User,
43+
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
44+
45+
// Create a new run for the agent in the thread with additional instructions
46+
ThreadRun run = agentClient.CreateRun(
47+
thread.Id,
48+
agent.Id,
49+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
50+
51+
// Wait for the run to complete
52+
// This is a blocking call, so it will wait until the run is completed
53+
do
54+
{
55+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
56+
run = agentClient.GetRun(thread.Id, run.Id);
57+
}
58+
while (run.Status == RunStatus.Queued
59+
|| run.Status == RunStatus.InProgress);
60+
61+
// Show the run results
62+
PageableList<ThreadMessage> messages
63+
= agentClient.GetMessages(
64+
threadId: thread.Id, order: ListSortOrder.Ascending);
65+
66+
// Print the messages in the thread
67+
foreach (ThreadMessage threadMessage in messages)
68+
{
69+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
70+
foreach (MessageContent contentItem in threadMessage.ContentItems)
71+
{
72+
if (contentItem is MessageTextContent textItem)
73+
{
74+
Console.Write(textItem.Text);
75+
}
76+
else if (contentItem is MessageImageFileContent imageFileItem)
77+
{
78+
Console.Write($"<image from ID: {imageFileItem.FileId}");
79+
}
80+
Console.WriteLine();
81+
}
82+
}
83+
84+
// Delete the thread and agent after use
85+
agentClient.DeleteThread(threadId: thread.Id);
86+
agentClient.DeleteAgent(agentId: agent.Id);
87+
}
88+
}
89+
// </create_and_run_agent>
90+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// See https://aka.ms/new-console-template for more information
2+
// Console.WriteLine("Hello, World!");
3+
4+
5+
using System;
6+
using Azure;
7+
using Azure.Core;
8+
using Azure.Identity;
9+
using Azure.ResourceManager;
10+
using Azure.ResourceManager.Resources;
11+
using Azure.ResourceManager.Models;
12+
using Azure.ResourceManager.CognitiveServices;
13+
using Azure.ResourceManager.CognitiveServices.Models;
14+
15+
16+
namespace AiAgentsTests
17+
{
18+
19+
// This is an app that creates a resource group, a Azure AI Foundry project.
20+
// It uses the Azure SDK for .NET to interact with Azure resources.
21+
// <create_project>
22+
public class CreateRGAzureAI
23+
{
24+
public static void CreateRG(string[] args)
25+
{
26+
27+
string resourceGroupName = "AzureAIFactoryNET";
28+
string foundryResourceName = "FoundryProjectNET";
29+
30+
AzureLocation location = AzureLocation.EastUS;
31+
32+
var credential = new DefaultAzureCredential();
33+
var armClient = new ArmClient(credential);
34+
35+
// Create a resource group and assign roles to it
36+
ResourceGroupResource resourceGroup = CreateRG(resourceGroupName, location, armClient).Result;
37+
38+
Console.WriteLine($"Created resource group: {resourceGroup.Data.Name}");
39+
Console.WriteLine($"Resource group ID: {resourceGroup.Data.Id}");
40+
41+
// Create a Foundry project
42+
CreateFoundryAIProject(resourceGroup, foundryResourceName, location).Wait();
43+
}
44+
public static async Task<ResourceGroupResource> CreateRG(string resourceGroupName, AzureLocation location, ArmClient armClient)
45+
46+
{
47+
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
48+
ResourceGroupCollection resourceGroupCollection = subscription.GetResourceGroups();
49+
50+
ResourceGroupData resourceGroupData = new ResourceGroupData(location);
51+
ResourceGroupResource rg = (await resourceGroupCollection.CreateOrUpdateAsync(WaitUntil.Completed, resourceGroupName, resourceGroupData)).Value;
52+
53+
return rg;
54+
}
55+
56+
public static async Task CreateFoundryAIProject(ResourceGroupResource resourceGroupResource, string foundryResourceName, AzureLocation location)
57+
{
58+
CognitiveServicesAccountCollection accountCollection = resourceGroupResource.GetCognitiveServicesAccounts();
59+
60+
var parameters = new CognitiveServicesAccountData(location)
61+
{
62+
Kind = "AIServices",
63+
Sku = new CognitiveServicesSku("S0"),
64+
Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned),
65+
};
66+
67+
await accountCollection.CreateOrUpdateAsync(
68+
WaitUntil.Completed,
69+
foundryResourceName,
70+
parameters);
71+
}
72+
// </create_project>
73+
}
74+
}
75+
76+
77+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Azure;
2+
using Azure.Identity;
3+
using Azure.AI.Inference;
4+
using Azure.Core;
5+
using Azure.Core.Pipeline;
6+
7+
8+
namespace AiAgentsTests
9+
{
10+
//<chat_completion>
11+
public class SimpleInference
12+
{
13+
public static void Main(string[] args)
14+
{
15+
16+
var endpointUrl = Evironment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
17+
var modelName = Evironment.GetEnvironmentVariable("AZURE_OPENAI_MODEL_NAME");
18+
19+
var endpoint = new Uri(endpointUrl);
20+
var credential = new DefaultAzureCredential();
21+
var model = modelName;
22+
23+
AzureAIInferenceClientOptions clientOptions = new AzureAIInferenceClientOptions();
24+
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(credential, new string[] { "https://cognitiveservices.azure.com/.default" });
25+
clientOptions.AddPolicy(tokenPolicy, HttpPipelinePosition.PerRetry);
26+
27+
28+
var client = new ChatCompletionsClient(
29+
endpoint,
30+
credential,
31+
clientOptions
32+
);
33+
34+
var requestOptions = new ChatCompletionsOptions()
35+
{
36+
Messages =
37+
{
38+
new ChatRequestSystemMessage("You are a helpful assistant."),
39+
new ChatRequestUserMessage("How many feet are in a mile?"),
40+
},
41+
};
42+
43+
Response<ChatCompletions> response = client.Complete(requestOptions);
44+
Console.WriteLine(response.Value.Content);
45+
46+
}
47+
}
48+
// </chat_completion>
49+
}

0 commit comments

Comments
 (0)