mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
90226526fa
* Remove unneeded model from extensions * Add noop justification
36 lines
1.8 KiB
C#
36 lines
1.8 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to create and use a simple AI agent with Azure Foundry Agents as the backend.
|
|
|
|
using Azure.AI.Agents;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
|
|
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
|
|
var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
|
|
|
const string JokerInstructions = "You are good at telling jokes.";
|
|
const string JokerName = "JokerAgent";
|
|
|
|
// Get a client to create/retrieve server side agents with.
|
|
var agentsClient = new AgentsClient(new Uri(endpoint), new AzureCliCredential());
|
|
|
|
// Define the agent you want to create.
|
|
var agentDefinition = new PromptAgentDefinition(model: deploymentName) { Instructions = JokerInstructions };
|
|
|
|
// You can create a server side agent with the Azure.AI.Agents SDK.
|
|
var agentVersion = agentsClient.CreateAgentVersion(agentName: JokerName, definition: agentDefinition).Value;
|
|
|
|
// You can retrieve an already created server side agent as an AIAgent.
|
|
AIAgent existingAgent = await agentsClient.GetAIAgentAsync(agentVersion.Name);
|
|
|
|
// You can also create a server side persistent agent and return it as an AIAgent directly.
|
|
var createdAgent = agentsClient.CreateAIAgent(name: JokerName, model: deploymentName, instructions: JokerInstructions);
|
|
|
|
// You can then invoke the agent like any other AIAgent.
|
|
AgentThread thread = existingAgent.GetNewThread();
|
|
Console.WriteLine(await existingAgent.RunAsync("Tell me a joke about a pirate.", thread));
|
|
|
|
// Cleanup by agent name (removes both agent versions created by existingAgent + createdAgent).
|
|
await agentsClient.DeleteAgentAsync(agentVersion.Name);
|