mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
624709e5d1
* 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>
59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Microsoft.Extensions.AI.Agents;
|
|
using Microsoft.Shared.Samples;
|
|
using OpenAI;
|
|
|
|
#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
|
|
|
|
namespace Providers;
|
|
|
|
/// <summary>
|
|
/// End-to-end sample showing how to use <see cref="AIAgent"/> with OpenAI Assistants.
|
|
/// </summary>
|
|
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 RunWithAssistant()
|
|
{
|
|
// Get a client to create server side agents with.
|
|
var openAIClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey);
|
|
|
|
// 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();
|
|
|
|
// 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);
|
|
}
|
|
|
|
// Cleanup
|
|
var assistantClient = openAIClient.GetAssistantClient();
|
|
await assistantClient.DeleteThreadAsync(thread.Id);
|
|
await assistantClient.DeleteAssistantAsync(agent.Id);
|
|
}
|
|
}
|