mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.Net: Code interpreter tool abstraction and implementation examples (#110)
* Added code interpreter abstraction updates for OpenAI Assistants * Updated Persistent Agents implementation based on latest changes in SDK * Added code interpreter abstraction updates for Azure AI Persistent Agents * Small note for OpenAI responses code interpreter * Small update * Fixes after merge * Addressed PR feedback * Small update * Small fix * Fix after merge
This commit is contained in:
@@ -8,8 +8,11 @@ using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
using OpenAI.Assistants;
|
||||
using OpenAI.Responses;
|
||||
|
||||
#pragma warning disable OPENAI001
|
||||
|
||||
namespace GettingStarted;
|
||||
|
||||
public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
@@ -19,8 +22,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
/// </summary>
|
||||
public enum ChatClientProviders
|
||||
{
|
||||
OpenAI,
|
||||
AzureOpenAI,
|
||||
OpenAIChatCompletion,
|
||||
OpenAIAssistant,
|
||||
OpenAIResponses,
|
||||
OpenAIResponses_InMemoryMessageThread,
|
||||
OpenAIResponses_ConversationIdThread,
|
||||
@@ -30,7 +34,8 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
protected Task<IChatClient> GetChatClientAsync(ChatClientProviders provider, ChatClientAgentOptions options, CancellationToken cancellationToken = default)
|
||||
=> provider switch
|
||||
{
|
||||
ChatClientProviders.OpenAI => GetOpenAIChatClientAsync(),
|
||||
ChatClientProviders.OpenAIChatCompletion => GetOpenAIChatClientAsync(),
|
||||
ChatClientProviders.OpenAIAssistant => GetOpenAIAssistantChatClientAsync(options, cancellationToken),
|
||||
ChatClientProviders.AzureOpenAI => GetAzureOpenAIChatClientAsync(),
|
||||
ChatClientProviders.AzureAIAgentsPersistent => GetAzureAIAgentPersistentClientAsync(options, cancellationToken),
|
||||
ChatClientProviders.OpenAIResponses or
|
||||
@@ -48,6 +53,10 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
_ => null
|
||||
};
|
||||
|
||||
protected OpenAIClient OpenAIClient => new(TestConfiguration.OpenAI.ApiKey);
|
||||
|
||||
protected PersistentAgentsClient AzureAIPersistentAgentsClient => new(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
/// <summary>
|
||||
/// For providers that store the agent and the thread on the server side, this will clean and delete
|
||||
/// any sample agent and thread that was created during this execution.
|
||||
@@ -74,7 +83,7 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
|
||||
private Task<IChatClient> GetOpenAIChatClientAsync()
|
||||
=> Task.FromResult(
|
||||
new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
OpenAIClient
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient());
|
||||
|
||||
@@ -89,17 +98,14 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
|
||||
private Task<IChatClient> GetOpenAIResponsesClientAsync()
|
||||
=> Task.FromResult(
|
||||
new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
OpenAIClient
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient());
|
||||
|
||||
private async Task<IChatClient> GetAzureAIAgentPersistentClientAsync(ChatClientAgentOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a server side agent to work with.
|
||||
var persistentAgentResponse = await persistentAgentsClient.Administration.CreateAgentAsync(
|
||||
var persistentAgentResponse = await AzureAIPersistentAgentsClient.Administration.CreateAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName,
|
||||
name: options.Name,
|
||||
instructions: options.Instructions,
|
||||
@@ -108,7 +114,23 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
var persistentAgent = persistentAgentResponse.Value;
|
||||
|
||||
// Get the chat client to use for the agent.
|
||||
return persistentAgentsClient.AsIChatClient(persistentAgent.Id);
|
||||
return AzureAIPersistentAgentsClient.AsIChatClient(persistentAgent.Id);
|
||||
}
|
||||
|
||||
private async Task<IChatClient> GetOpenAIAssistantChatClientAsync(ChatClientAgentOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
var assistantClient = OpenAIClient.GetAssistantClient();
|
||||
|
||||
Assistant assistant = await assistantClient.CreateAssistantAsync(
|
||||
TestConfiguration.OpenAI.ChatModelId,
|
||||
new()
|
||||
{
|
||||
Name = options.Name,
|
||||
Instructions = options.Instructions
|
||||
},
|
||||
cancellationToken);
|
||||
|
||||
return assistantClient.AsIChatClient(assistant.Id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user