Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
// agentVersion.Version = <versionNumber>,
// agentVersion.Name = <agentName>

// You can retrieve an AIAgent for an already created server side agent version.
// You can use an AIAgent with an already created server side agent version.
AIAgent existingJokerAgent = aiProjectClient.AsAIAgent(createdAgentVersion);

// You can also create another AIAgent version by providing the same name with a different definition.
AIAgent newJokerAgent = aiProjectClient.CreateAIAgent(name: JokerName, model: deploymentName, instructions: "You are extremely hilarious at telling jokes.");
AIAgent newJokerAgent = await aiProjectClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: "You are extremely hilarious at telling jokes.");

// You can also get the AIAgent latest version just providing its name.
AIAgent jokerAgentLatest = aiProjectClient.GetAIAgent(name: JokerName);
AIAgent jokerAgentLatest = await aiProjectClient.GetAIAgentAsync(name: JokerName);
var latestAgentVersion = jokerAgentLatest.GetService<AgentVersion>()!;

// The AIAgent version can be accessed via the GetService method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
// agentVersion.Version = <versionNumber>,
// agentVersion.Name = <agentName>

// You can retrieve an AIAgent for an already created server side agent version.
AIAgent existingJokerAgent = aiProjectClient.GetAIAgent(createdAgentVersion);
// You can use an AIAgent with an already created server side agent version.
AIAgent existingJokerAgent = aiProjectClient.AsAIAgent(createdAgentVersion);

// You can also create another AIAgent version by providing the same name with a different definition/instruction.
AIAgent newJokerAgent = aiProjectClient.CreateAIAgent(name: JokerName, model: deploymentName, instructions: "You are extremely hilarious at telling jokes.");
AIAgent newJokerAgent = await aiProjectClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: "You are extremely hilarious at telling jokes.");

// You can also get the AIAgent latest version by just providing its name.
AIAgent jokerAgentLatest = aiProjectClient.GetAIAgent(name: JokerName);
AIAgent jokerAgentLatest = await aiProjectClient.GetAIAgentAsync(name: JokerName);
AgentVersion latestAgentVersion = jokerAgentLatest.GetService<AgentVersion>()!;

// The AIAgent version can be accessed via the GetService method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
// You can create a server side agent version with the Azure.AI.Agents SDK client below.
AgentVersion agentVersion = aiProjectClient.Agents.CreateAgentVersion(agentName: JokerName, options);

// You can retrieve an AIAgent for a already created server side agent version.
AIAgent jokerAgent = aiProjectClient.GetAIAgent(agentVersion);
// You can use an AIAgent with an already created server side agent version.
AIAgent jokerAgent = aiProjectClient.AsAIAgent(agentVersion);

// Invoke the agent with streaming support.
await foreach (AgentResponseUpdate update in jokerAgent.RunStreamingAsync("Tell me a joke about a pirate."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
// Create a server side agent version with the Azure.AI.Agents SDK client.
AgentVersion agentVersion = aiProjectClient.Agents.CreateAgentVersion(agentName: JokerName, options);

// Retrieve an AIAgent for the created server side agent version.
AIAgent jokerAgent = aiProjectClient.GetAIAgent(agentVersion);
// Use an AIAgent with an already created server side agent version.
AIAgent jokerAgent = aiProjectClient.AsAIAgent(agentVersion);

// Invoke the agent with a multi-turn conversation, where the context is preserved in the thread object.
AgentThread thread = await jokerAgent.GetNewThreadAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Console.WriteLine($"Occupation: {response.Result.Occupation}");

// Create the ChatClientAgent with the specified name, instructions, and expected structured output the agent should produce.
ChatClientAgent agentWithPersonInfo = aiProjectClient.CreateAIAgent(
ChatClientAgent agentWithPersonInfo = await aiProjectClient.CreateAIAgentAsync(
model: deploymentName,
new ChatClientAgentOptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential());

// Define the agent you want to create. (Prompt Agent in this case)
AIAgent agent = aiProjectClient.CreateAIAgent(name: JokerName, model: deploymentName, instructions: JokerInstructions)
AIAgent agent = (await aiProjectClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: JokerInstructions))
.AsBuilder()
.UseOpenTelemetry(sourceName: sourceName)
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// This sample shows how to use dependency injection to register an AIAgent and use it from a hosted service with a user input chat loop.

using System.ClientModel;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
Expand All @@ -14,16 +15,27 @@
const string JokerInstructions = "You are good at telling jokes.";
const string JokerName = "JokerAgent";

AIProjectClient aIProjectClient = new(new Uri(endpoint), new AzureCliCredential());

// Create a new agent if one doesn't exist already.
ChatClientAgent agent;
try
{
agent = await aIProjectClient.GetAIAgentAsync(name: JokerName);
}
catch (ClientResultException ex) when (ex.Status == 404)
{
agent = await aIProjectClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: JokerInstructions);
}

// Create a host builder that we will register services with and then run.
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

// Add the agents client to the service collection.
builder.Services.AddSingleton((sp) => new AIProjectClient(new Uri(endpoint), new AzureCliCredential()));
builder.Services.AddSingleton((sp) => aIProjectClient);

// Add the AI agent to the service collection.
builder.Services.AddSingleton<AIAgent>((sp)
=> sp.GetRequiredService<AIProjectClient>()
.CreateAIAgent(name: JokerName, model: deploymentName, instructions: JokerInstructions));
builder.Services.AddSingleton<AIAgent>((sp) => agent);

// Add a sample service that will use the agent to respond to user input.
builder.Services.AddHostedService<SampleService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Console.WriteLine($"Creating the agent '{agentName}' ...");

// Define the agent you want to create. (Prompt Agent in this case)
AIAgent agent = aiProjectClient.CreateAIAgent(
AIAgent agent = await aiProjectClient.CreateAIAgentAsync(
name: agentName,
model: deploymentName,
instructions: "You answer questions related to GitHub repositories only.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential());

// Define the agent you want to create. (Prompt Agent in this case)
AIAgent agent = aiProjectClient.CreateAIAgent(name: VisionName, model: deploymentName, instructions: VisionInstructions);
AIAgent agent = await aiProjectClient.CreateAIAgentAsync(name: VisionName, model: deploymentName, instructions: VisionInstructions);

ChatMessage message = new(ChatRole.User, [
new TextContent("What do you see in this image?"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ static string GetWeather([Description("The location to get the weather for.")] s

// Create the weather agent with function tools.
AITool weatherTool = AIFunctionFactory.Create(GetWeather);
AIAgent weatherAgent = aiProjectClient.CreateAIAgent(
AIAgent weatherAgent = await aiProjectClient.CreateAIAgentAsync(
name: WeatherName,
model: deploymentName,
instructions: WeatherInstructions,
tools: [weatherTool]);

// Create the main agent, and provide the weather agent as a function tool.
AIAgent agent = aiProjectClient.CreateAIAgent(
AIAgent agent = await aiProjectClient.CreateAIAgentAsync(
name: MainName,
model: deploymentName,
instructions: MainInstructions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static string GetDateTime()
AITool getWeatherTool = AIFunctionFactory.Create(GetWeather, name: nameof(GetWeather));

// Define the agent you want to create. (Prompt Agent in this case)
AIAgent originalAgent = aiProjectClient.CreateAIAgent(
AIAgent originalAgent = await aiProjectClient.CreateAIAgentAsync(
name: AssistantName,
model: deploymentName,
instructions: AssistantInstructions,
Expand Down Expand Up @@ -69,7 +69,7 @@ static string GetDateTime()
// Special per-request middleware agent.
Console.WriteLine("\n\n=== Example 4: Middleware with human in the loop function approval ===");

AIAgent humanInTheLoopAgent = aiProjectClient.CreateAIAgent(
AIAgent humanInTheLoopAgent = await aiProjectClient.CreateAIAgentAsync(
name: "HumanInTheLoopAgent",
model: deploymentName,
instructions: "You are an Human in the loop testing AI assistant that helps people find information.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

// Define the agent with plugin tools
// Define the agent you want to create. (Prompt Agent in this case)
AIAgent agent = aiProjectClient.CreateAIAgent(
AIAgent agent = await aiProjectClient.CreateAIAgentAsync(
name: AssistantName,
model: deploymentName,
instructions: AssistantInstructions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static async Task Main(string[] args)

string workflowInput = GetWorkflowInput(args);

AIAgent agent = aiProjectClient.GetAIAgent(agentVersion);
AIAgent agent = aiProjectClient.AsAIAgent(agentVersion);

AgentThread thread = await agent.GetNewThreadAsync();

Expand Down
Loading
Loading