This commit is contained in:
Roger Barreto
2025-11-07 19:57:27 +00:00
Unverified
parent 9586a3ea53
commit f5d6056074
4 changed files with 24 additions and 18 deletions
@@ -10,6 +10,8 @@ using Azure.Identity;
using Microsoft.Agents.AI;
using SampleApp;
#pragma warning disable CA5399
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
var deploymentName = "gpt-5"; // Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
@@ -20,7 +22,9 @@ const string AssistantName = "StructuredOutputAssistant";
var agentsClient = new AgentsClient(new Uri(endpoint), new AzureCliCredential());
// Create ChatClientAgent directly
ChatClientAgent agent = await agentsClient.CreateAIAgentAsync(model: deploymentName, new ChatClientAgentOptions(name: AssistantName, instructions: AssistantInstructions));
ChatClientAgent agent = await agentsClient.CreateAIAgentAsync(
model: deploymentName,
new ChatClientAgentOptions(name: AssistantName, instructions: AssistantInstructions));
// Set PersonInfo as the type parameter of RunAsync method to specify the expected structured output from the agent and invoke the agent with some unstructured input.
AgentRunResponse<PersonInfo> response = await agent.RunAsync<PersonInfo>("Please provide information about John Smith, who is a 35-year-old software engineer.");
@@ -16,14 +16,7 @@ const string JokerName = "JokerAgent";
// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
var agentsClient = new AgentsClient(new Uri(endpoint), new AzureCliCredential());
// Define the agent you want to create. (Prompt Agent in this case)
var agentDefinition = new PromptAgentDefinition(model: deploymentName) { Instructions = JokerInstructions };
// Create a server side agent version with the Azure.AI.Agents SDK client.
var agentVersion = agentsClient.CreateAgentVersion(agentName: JokerName, definition: agentDefinition);
// Retrieve an AIAgent for the created server side agent version.
AIAgent agent = agentsClient.GetAIAgent(agentVersion);
AIAgent agent = await agentsClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: JokerInstructions);
// Start a new thread for the agent conversation.
AgentThread thread = agent.GetNewThread();
@@ -26,14 +26,20 @@ VectorStore vectorStore = new InMemoryVectorStore();
// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
var agentsClient = new AgentsClient(new Uri(endpoint), new AzureCliCredential());
// Define the agent you want to create. (Prompt Agent in this case)
var agentDefinition = new PromptAgentDefinition(model: deploymentName) { Instructions = JokerInstructions };
// Create a server side agent version with the Azure.AI.Agents SDK client.
var agentVersion = agentsClient.CreateAgentVersion(agentName: JokerName, definition: agentDefinition);
// Retrieve an AIAgent for the created server side agent version.
AIAgent agent = agentsClient.GetAIAgent(agentVersion);
AIAgent agent = await agentsClient.CreateAIAgentAsync(
deploymentName,
new ChatClientAgentOptions
{
Instructions = JokerInstructions,
Name = JokerName,
ChatMessageStoreFactory = ctx =>
{
// Create a new chat message store for this agent that stores the messages in a vector store.
// Each thread must get its own copy of the VectorChatMessageStore, since the store
// also contains the id that the thread is stored under.
return new VectorChatMessageStore(vectorStore, ctx.SerializedState, ctx.JsonSerializerOptions);
}
});
// Start a new thread for the agent conversation.
AgentThread thread = agent.GetNewThread();
@@ -97,9 +97,12 @@ internal sealed class AzureAIAgentChatClient : DelegatingChatClient
private ChatOptions GetConversationEnabledChatOptions(ChatOptions? chatOptions, string conversationId)
{
// Ignore all the chatOptions provided per-request and use by default the one provided at the agent creation.
// Start with a clone of the base chat options defined for the agent, if any.
ChatOptions conversationChatOptions = this._chatOptions?.Clone() ?? new();
// Ignore per-request all options that can't be overriden.
conversationChatOptions.Instructions = null;
// Preserve the original RawRepresentationFactory
var originalFactory = chatOptions?.RawRepresentationFactory;