mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Add some OpenAI and Foundry extension methods (#225)
* Add some OpenAI specific extensions * Update samples and extension methods * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add extension methods for creating agents using the Assistant API * Add orchestration sample * Add orchestration sample * Sample for the Foundry alignment document * Address code review feedback * Rename provider samples * Sample showing how to get an AI agent for Foundry SDK * Add OpenAI chat completion based implementation of AIAgent * Split OpenAI client extension methods by client type * Remove OpenAIClient extension methods * Rename AsRunnableAgent * Fix XML comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace Custom;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use a custom <see cref="OpenAIChatClientAgent"/>.
|
||||
/// </summary>
|
||||
public sealed class Custom_OpenAIChatClientAgent(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
/// <summary>
|
||||
/// This will create an instance of <see cref="MyOpenAIChatClientAgent"/> and run it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunCustomChatClientAgent()
|
||||
{
|
||||
var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey).GetChatClient(TestConfiguration.OpenAI.ChatModelId);
|
||||
|
||||
var agent = new MyOpenAIChatClientAgent(chatClient);
|
||||
|
||||
var chatMessage = new UserChatMessage("Tell me a joke about a pirate.");
|
||||
var chatCompletion = await agent.RunAsync(chatMessage);
|
||||
|
||||
Console.WriteLine(chatCompletion.Content.Last().Text);
|
||||
}
|
||||
}
|
||||
|
||||
public class MyOpenAIChatClientAgent : OpenAIChatClientAgent
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
public MyOpenAIChatClientAgent(ChatClient client, ILoggerFactory? loggerFactory = null) :
|
||||
base(client, instructions: JokerInstructions, name: JokerName, loggerFactory: loggerFactory)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<RootNamespace>GettingStarted</RootNamespace>
|
||||
<OutputType>Library</OutputType>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
<NoWarn>$(NoWarn);CA1707;CA1716;IDE0009;IDE1006;OPENAI001;</NoWarn>
|
||||
<NoWarn>$(NoWarn);CA1707;CA1716;IDE0009;IDE1006; OPENAI001;</NoWarn>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InjectSharedSamples>true</InjectSharedSamples>
|
||||
<InjectSharedThrow>true</InjectSharedThrow>
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Azure.AI.Agents.Persistent;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.Orchestration;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
|
||||
namespace Orchestration;
|
||||
|
||||
/// <summary>
|
||||
/// Demonstrates how to use the <see cref="SequentialOrchestration"/> for
|
||||
/// executing multiple Foundry agents in sequence.
|
||||
/// </summary>
|
||||
public class SequentialOrchestration_Foundry_Agents(ITestOutputHelper output) : OrchestrationSample(output)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task RunOrchestrationAsync(bool streamedResponse)
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
var model = TestConfiguration.OpenAI.ChatModelId;
|
||||
|
||||
// Define the agents
|
||||
AIAgent analystAgent =
|
||||
await persistentAgentsClient.CreateAIAgentAsync(
|
||||
model,
|
||||
name: "Analyst",
|
||||
instructions:
|
||||
"""
|
||||
You are a marketing analyst. Given a product description, identify:
|
||||
- Key features
|
||||
- Target audience
|
||||
- Unique selling points
|
||||
""",
|
||||
description: "A agent that extracts key concepts from a product description.");
|
||||
AIAgent writerAgent =
|
||||
await persistentAgentsClient.CreateAIAgentAsync(
|
||||
model,
|
||||
name: "copywriter",
|
||||
instructions:
|
||||
"""
|
||||
You are a marketing copywriter. Given a block of text describing features, audience, and USPs,
|
||||
compose a compelling marketing copy (like a newsletter section) that highlights these points.
|
||||
Output should be short (around 150 words), output just the copy as a single text block.
|
||||
""",
|
||||
description: "An agent that writes a marketing copy based on the extracted concepts.");
|
||||
AIAgent editorAgent =
|
||||
await persistentAgentsClient.CreateAIAgentAsync(
|
||||
model,
|
||||
name: "editor",
|
||||
instructions:
|
||||
"""
|
||||
You are an editor. Given the draft copy, correct grammar, improve clarity, ensure consistent tone,
|
||||
give format and make it polished. Output the final improved copy as a single text block.
|
||||
""",
|
||||
description: "An agent that formats and proofreads the marketing copy.");
|
||||
|
||||
// Create a monitor to capturing agent responses (via ResponseCallback)
|
||||
// to display at the end of this sample. (optional)
|
||||
// NOTE: Create your own callback to capture responses in your application or service.
|
||||
OrchestrationMonitor monitor = new();
|
||||
// Define the orchestration
|
||||
SequentialOrchestration orchestration =
|
||||
new(analystAgent, writerAgent, editorAgent)
|
||||
{
|
||||
LoggerFactory = this.LoggerFactory,
|
||||
ResponseCallback = monitor.ResponseCallback,
|
||||
StreamingResponseCallback = streamedResponse ? monitor.StreamingResultCallback : null,
|
||||
};
|
||||
|
||||
// Run the orchestration
|
||||
string input = "An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours";
|
||||
Console.WriteLine($"\n# INPUT: {input}\n");
|
||||
AgentRunResponse result = await orchestration.RunAsync(input);
|
||||
Console.WriteLine($"\n# RESULT: {result}");
|
||||
|
||||
this.DisplayHistory(monitor.History);
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(editorAgent.Id);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(writerAgent.Id);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(analystAgent.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.Orchestration;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
|
||||
namespace Orchestration;
|
||||
|
||||
/// <summary>
|
||||
/// Demonstrates how to use the <see cref="SequentialOrchestration"/> for
|
||||
/// executing multiple heterogeneous agents in sequence.
|
||||
/// </summary>
|
||||
public class SequentialOrchestration_Multi_Agent(ITestOutputHelper output) : OrchestrationSample(output)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task RunOrchestrationAsync(bool streamedResponse)
|
||||
{
|
||||
var openAIClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey);
|
||||
var model = TestConfiguration.OpenAI.ChatModelId;
|
||||
|
||||
// Define the agents
|
||||
AIAgent analystAgent =
|
||||
openAIClient.GetChatClient(model).CreateAIAgent(
|
||||
name: "Analyst",
|
||||
instructions:
|
||||
"""
|
||||
You are a marketing analyst. Given a product description, identify:
|
||||
- Key features
|
||||
- Target audience
|
||||
- Unique selling points
|
||||
""",
|
||||
description: "A agent that extracts key concepts from a product description.");
|
||||
AIAgent writerAgent =
|
||||
openAIClient.GetOpenAIResponseClient(model).CreateAIAgent(
|
||||
name: "copywriter",
|
||||
instructions:
|
||||
"""
|
||||
You are a marketing copywriter. Given a block of text describing features, audience, and USPs,
|
||||
compose a compelling marketing copy (like a newsletter section) that highlights these points.
|
||||
Output should be short (around 150 words), output just the copy as a single text block.
|
||||
""",
|
||||
description: "An agent that writes a marketing copy based on the extracted concepts.");
|
||||
AIAgent editorAgent =
|
||||
openAIClient.GetAssistantClient().CreateAIAgent(
|
||||
model,
|
||||
name: "editor",
|
||||
instructions:
|
||||
"""
|
||||
You are an editor. Given the draft copy, correct grammar, improve clarity, ensure consistent tone,
|
||||
give format and make it polished. Output the final improved copy as a single text block.
|
||||
""",
|
||||
description: "An agent that formats and proofreads the marketing copy.");
|
||||
|
||||
// Create a monitor to capturing agent responses (via ResponseCallback)
|
||||
// to display at the end of this sample. (optional)
|
||||
// NOTE: Create your own callback to capture responses in your application or service.
|
||||
OrchestrationMonitor monitor = new();
|
||||
// Define the orchestration
|
||||
SequentialOrchestration orchestration =
|
||||
new(analystAgent, writerAgent, editorAgent)
|
||||
{
|
||||
LoggerFactory = this.LoggerFactory,
|
||||
ResponseCallback = monitor.ResponseCallback,
|
||||
StreamingResponseCallback = streamedResponse ? monitor.StreamingResultCallback : null,
|
||||
};
|
||||
|
||||
// Run the orchestration
|
||||
string input = "An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours";
|
||||
Console.WriteLine($"\n# INPUT: {input}\n");
|
||||
AgentRunResponse result = await orchestration.RunAsync(input);
|
||||
Console.WriteLine($"\n# RESULT: {result}");
|
||||
|
||||
this.DisplayHistory(monitor.History);
|
||||
|
||||
// Cleanup
|
||||
var assistantClient = openAIClient.GetAssistantClient();
|
||||
await assistantClient.DeleteAssistantAsync(editorAgent.Id);
|
||||
// Need to know how to get the assistant thread ID to delete the thread (issue #260)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Azure.AI.Agents.Persistent;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Shows how to use <see cref="AIAgent"/> with Azure AI Persistent Agents.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Running "az login" command in terminal is required for authentication with Azure AI service.
|
||||
/// </remarks>
|
||||
public sealed class AIAgent_With_AzureAIAgentsPersistent(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task GetWithAzureAIAgentsPersistent()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a service side persistent agent.
|
||||
var persistentAgent = await persistentAgentsClient.Administration.CreateAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName!,
|
||||
name: JokerName,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Get a server side agent.
|
||||
AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(persistentAgent.Value.Id);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Threads.DeleteThreadAsync(thread.Id);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateWithAzureAIAgentsPersistent()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a server side persistent agent.
|
||||
AIAgent agent = await persistentAgentsClient.CreateAIAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName!,
|
||||
name: JokerName,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Threads.DeleteThreadAsync(thread.Id);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id);
|
||||
}
|
||||
}
|
||||
+7
-9
@@ -3,16 +3,16 @@
|
||||
using System.ClientModel;
|
||||
using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with Azure OpenAI Chat Completion.
|
||||
/// </summary>
|
||||
public sealed class ChatClientAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
|
||||
public sealed class AIAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
@@ -20,16 +20,14 @@ public sealed class ChatClientAgent_With_AzureOpenAIChatCompletion(ITestOutputHe
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletion()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = ((TestConfiguration.AzureOpenAI.ApiKey is null)
|
||||
// Get the OpenAI client to use for the agent.
|
||||
var openAIClient = (TestConfiguration.AzureOpenAI.ApiKey is null)
|
||||
// Use Azure CLI credentials if API key is not provided.
|
||||
? new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new AzureCliCredential())
|
||||
: new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey)))
|
||||
.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName)
|
||||
.AsIChatClient();
|
||||
: new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey));
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent = new(chatClient, JokerInstructions, JokerName);
|
||||
// Create the agent
|
||||
AIAgent agent = openAIClient.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName).CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
+15
-22
@@ -1,6 +1,5 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
@@ -10,36 +9,29 @@ using OpenAI;
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Assistants.
|
||||
/// End-to-end sample showing how to use <see cref="AIAgent"/> with OpenAI Assistants.
|
||||
/// </summary>
|
||||
public sealed class ChatClientAgent_With_OpenAIAssistant(ITestOutputHelper output) : AgentSample(output)
|
||||
public sealed class AIAgent_With_OpenAIAssistant(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithOpenAIAssistant()
|
||||
public async Task RunWithAssistant()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var openAIClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey);
|
||||
var assistantClient = openAIClient.GetAssistantClient();
|
||||
|
||||
// Create a server side agent to work with.
|
||||
var assistantCreateResult = await assistantClient.CreateAssistantAsync(
|
||||
TestConfiguration.OpenAI.ChatModelId,
|
||||
new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions
|
||||
});
|
||||
|
||||
var assistantId = assistantCreateResult.Value.Id;
|
||||
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = assistantClient.AsIChatClient(assistantId);
|
||||
|
||||
// Define the agent.
|
||||
ChatClientAgent agent = new(chatClient);
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = openAIClient
|
||||
.GetAssistantClient()
|
||||
.CreateAIAgent(
|
||||
TestConfiguration.OpenAI.ChatModelId,
|
||||
options: new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
});
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
@@ -59,7 +51,8 @@ public sealed class ChatClientAgent_With_OpenAIAssistant(ITestOutputHelper outpu
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
var assistantClient = openAIClient.GetAssistantClient();
|
||||
await assistantClient.DeleteThreadAsync(thread.Id);
|
||||
await assistantClient.DeleteAssistantAsync(assistantId);
|
||||
await assistantClient.DeleteAssistantAsync(agent.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="AIAgent"/> with OpenAI Chat Completion and Responses.
|
||||
/// </summary>
|
||||
public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletion()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response.Messages.Last().Text);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionReturnChatCompletion()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
var chatCompletion = response.AsChatCompletion();
|
||||
|
||||
Console.WriteLine(chatCompletion.Content.Last().Text);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionWithOpenAIChatMessage()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
|
||||
// Use the OpenAI.Chat message types directly
|
||||
var chatMessage = new UserChatMessage(input);
|
||||
var chatCompletion = await agent.RunAsync(chatMessage, thread);
|
||||
|
||||
Console.WriteLine(chatCompletion.Content.Last().Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-16
@@ -11,7 +11,7 @@ namespace Providers;
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
|
||||
/// </summary>
|
||||
public sealed class ChatClientAgent_With_OpenAIResponsesChatCompletion(ITestOutputHelper output) : AgentSample(output)
|
||||
public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
@@ -20,15 +20,12 @@ public sealed class ChatClientAgent_With_OpenAIResponsesChatCompletion(ITestOutp
|
||||
/// This will use the conversation id to reference the thread state on the server side.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionServiceManagedThread()
|
||||
public async Task RunWithResponses()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent = new(chatClient, JokerInstructions, JokerName);
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation based on the type.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
@@ -52,16 +49,12 @@ public sealed class ChatClientAgent_With_OpenAIResponsesChatCompletion(ITestOutp
|
||||
/// This will use in-memory messages to store the thread state.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionInMemoryThread()
|
||||
public async Task RunWithResponsesAndStoreOutputDisabled()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent =
|
||||
new(chatClient, options: new()
|
||||
.CreateAIAgent(options: new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Azure.AI.Agents.Persistent;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Shows how to use <see cref="ChatClientAgent"/> with Azure AI Persistent Agents.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Running "az login" command in terminal is required for authentication with Azure AI service.
|
||||
/// </remarks>
|
||||
public sealed class ChatClientAgent_With_AzureAIAgentsPersistent(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithAzureAIAgentsPersistent()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a server side persistent agent.
|
||||
var createPersistentAgentResponse = await persistentAgentsClient.Administration.CreateAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName,
|
||||
name: JokerName,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Get a local proxy for the agent to work with.
|
||||
AIAgent agent = await persistentAgentsClient.GetRunnableAgentAsync(createPersistentAgentResponse.Value.Id);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Threads.DeleteThreadAsync(thread.Id);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(createPersistentAgentResponse.Value.Id);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
|
||||
/// </summary>
|
||||
public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletion()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent = new(chatClient, JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user