mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
6232dd8305
* Point AgentOptions.Instructions to ChatOptions * Update tests and checks * Update xml docs * Removal of agentOptions.Instructions in favor of chatOptions.Instructions * Instructions and tool check consistency * Instructions and tool check consistency * Address comment * Update .github/upgrades/prompts/SemanticKernelToAgentFramework.md Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Address PR Comment * Update latest changes to comply with the PR proposal * Address feedback * Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> * Address instructions * Update declarative to use promptAgent.Instrucitons with chatOptions.Instructions --------- Co-authored-by: westey <164392973+westey-m@users.noreply.github.com> Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
105 lines
4.2 KiB
C#
105 lines
4.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AgentConformance.IntegrationTests;
|
|
using AgentConformance.IntegrationTests.Support;
|
|
using Anthropic;
|
|
using Anthropic.Models.Beta.Messages;
|
|
using Anthropic.Models.Messages;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Extensions.AI;
|
|
using Shared.IntegrationTests;
|
|
|
|
namespace AnthropicChatCompletion.IntegrationTests;
|
|
|
|
public class AnthropicChatCompletionFixture : IChatClientAgentFixture
|
|
{
|
|
// All tests for Anthropic are intended to be ran locally as the CI pipeline for Anthropic is not setup.
|
|
internal const string SkipReason = "Integrations tests for local execution only";
|
|
|
|
private static readonly AnthropicConfiguration s_config = TestConfiguration.LoadSection<AnthropicConfiguration>();
|
|
private readonly bool _useReasoningModel;
|
|
private readonly bool _useBeta;
|
|
|
|
private ChatClientAgent _agent = null!;
|
|
|
|
public AnthropicChatCompletionFixture(bool useReasoningChatModel, bool useBeta)
|
|
{
|
|
this._useReasoningModel = useReasoningChatModel;
|
|
this._useBeta = useBeta;
|
|
}
|
|
|
|
public AIAgent Agent => this._agent;
|
|
|
|
public IChatClient ChatClient => this._agent.ChatClient;
|
|
|
|
public async Task<List<ChatMessage>> GetChatHistoryAsync(AgentThread thread)
|
|
{
|
|
var typedThread = (ChatClientAgentThread)thread;
|
|
|
|
return typedThread.MessageStore is null ? [] : (await typedThread.MessageStore.GetMessagesAsync()).ToList();
|
|
}
|
|
|
|
public Task<ChatClientAgent> CreateChatClientAgentAsync(
|
|
string name = "HelpfulAssistant",
|
|
string instructions = "You are a helpful assistant.",
|
|
IList<AITool>? aiTools = null)
|
|
{
|
|
var anthropicClient = new AnthropicClient() { APIKey = s_config.ApiKey };
|
|
|
|
IChatClient? chatClient = this._useBeta
|
|
? anthropicClient
|
|
.Beta
|
|
.AsIChatClient()
|
|
.AsBuilder()
|
|
.ConfigureOptions(options
|
|
=> options.RawRepresentationFactory = _
|
|
=> new Anthropic.Models.Beta.Messages.MessageCreateParams()
|
|
{
|
|
Model = options.ModelId ?? (this._useReasoningModel ? s_config.ChatReasoningModelId : s_config.ChatModelId),
|
|
MaxTokens = options.MaxOutputTokens ?? 4096,
|
|
Messages = [],
|
|
Thinking = this._useReasoningModel
|
|
? new BetaThinkingConfigParam(new BetaThinkingConfigEnabled(2048))
|
|
: new BetaThinkingConfigParam(new BetaThinkingConfigDisabled())
|
|
}).Build()
|
|
|
|
: anthropicClient
|
|
.AsIChatClient()
|
|
.AsBuilder()
|
|
.ConfigureOptions(options
|
|
=> options.RawRepresentationFactory = _
|
|
=> new Anthropic.Models.Messages.MessageCreateParams()
|
|
{
|
|
Model = options.ModelId ?? (this._useReasoningModel ? s_config.ChatReasoningModelId : s_config.ChatModelId),
|
|
MaxTokens = options.MaxOutputTokens ?? 4096,
|
|
Messages = [],
|
|
Thinking = this._useReasoningModel
|
|
? new ThinkingConfigParam(new ThinkingConfigEnabled(2048))
|
|
: new ThinkingConfigParam(new ThinkingConfigDisabled())
|
|
}).Build();
|
|
|
|
return Task.FromResult(new ChatClientAgent(chatClient, options: new()
|
|
{
|
|
Name = name,
|
|
ChatOptions = new() { Instructions = instructions, Tools = aiTools }
|
|
}));
|
|
}
|
|
|
|
public Task DeleteAgentAsync(ChatClientAgent agent) =>
|
|
// Chat Completion does not require/support deleting agents, so this is a no-op.
|
|
Task.CompletedTask;
|
|
|
|
public Task DeleteThreadAsync(AgentThread thread) =>
|
|
// Chat Completion does not require/support deleting threads, so this is a no-op.
|
|
Task.CompletedTask;
|
|
|
|
public async Task InitializeAsync() =>
|
|
this._agent = await this.CreateChatClientAgentAsync();
|
|
|
|
public Task DisposeAsync() =>
|
|
Task.CompletedTask;
|
|
}
|