-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85ad929
commit 101f482
Showing
10 changed files
with
240 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 86 additions & 9 deletions
95
dotnet/test/AutoGen.Anthropic.Tests/AnthropicClientAgentTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,108 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// AnthropicClientAgentTest.cs | ||
|
||
using AutoGen.Anthropic.DTO; | ||
using AutoGen.Anthropic.Extensions; | ||
using AutoGen.Anthropic.Utils; | ||
using AutoGen.Core; | ||
using AutoGen.Tests; | ||
using Xunit.Abstractions; | ||
using FluentAssertions; | ||
|
||
namespace AutoGen.Anthropic; | ||
namespace AutoGen.Anthropic.Tests; | ||
|
||
public class AnthropicClientAgentTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public AnthropicClientAgentTest(ITestOutputHelper output) => _output = output; | ||
|
||
[ApiKeyFact("ANTHROPIC_API_KEY")] | ||
public async Task AnthropicAgentChatCompletionTestAsync() | ||
{ | ||
var client = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, AnthropicTestUtils.ApiKey); | ||
|
||
var agent = new AnthropicClientAgent( | ||
client, | ||
name: "AnthropicAgent", | ||
AnthropicConstants.Claude3Haiku, | ||
systemMessage: "You are a helpful AI assistant that convert user message to upper case") | ||
.RegisterMessageConnector(); | ||
|
||
var uppCaseMessage = new TextMessage(Role.User, "abcdefg"); | ||
|
||
var reply = await agent.SendAsync(chatHistory: new[] { uppCaseMessage }); | ||
|
||
reply.GetContent().Should().Contain("ABCDEFG"); | ||
reply.From.Should().Be(agent.Name); | ||
} | ||
|
||
[ApiKeyFact("ANTHROPIC_API_KEY")] | ||
public async Task AnthropicAgentTestProcessImageAsync() | ||
{ | ||
var client = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, AnthropicTestUtils.ApiKey); | ||
var agent = new AnthropicClientAgent( | ||
client, | ||
name: "AnthropicAgent", | ||
AnthropicConstants.Claude3Haiku).RegisterMessageConnector(); | ||
|
||
var singleAgentTest = new SingleAgentTest(_output); | ||
await singleAgentTest.UpperCaseTestAsync(agent); | ||
await singleAgentTest.UpperCaseStreamingTestAsync(agent); | ||
var base64Image = await AnthropicTestUtils.Base64FromImageAsync("square.png"); | ||
var imageMessage = new ChatMessage("user", | ||
[new ImageContent { Source = new ImageSource { MediaType = "image/png", Data = base64Image } }]); | ||
|
||
var messages = new IMessage[] { MessageEnvelope.Create(imageMessage) }; | ||
|
||
// test streaming | ||
foreach (var message in messages) | ||
{ | ||
var reply = agent.GenerateStreamingReplyAsync([message]); | ||
|
||
await foreach (var streamingMessage in reply) | ||
{ | ||
streamingMessage.Should().BeOfType<TextMessageUpdate>(); | ||
streamingMessage.As<TextMessageUpdate>().From.Should().Be(agent.Name); | ||
} | ||
} | ||
} | ||
|
||
[ApiKeyFact("ANTHROPIC_API_KEY")] | ||
public async Task AnthropicAgentTestMultiModalAsync() | ||
{ | ||
var client = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, AnthropicTestUtils.ApiKey); | ||
var agent = new AnthropicClientAgent( | ||
client, | ||
name: "AnthropicAgent", | ||
AnthropicConstants.Claude3Haiku) | ||
.RegisterMessageConnector(); | ||
|
||
var image = Path.Combine("images", "square.png"); | ||
var binaryData = BinaryData.FromBytes(await File.ReadAllBytesAsync(image), "image/png"); | ||
var imageMessage = new ImageMessage(Role.User, binaryData); | ||
var textMessage = new TextMessage(Role.User, "What's in this image?"); | ||
var multiModalMessage = new MultiModalMessage(Role.User, [textMessage, imageMessage]); | ||
|
||
var reply = await agent.SendAsync(multiModalMessage); | ||
reply.Should().BeOfType<TextMessage>(); | ||
reply.GetRole().Should().Be(Role.Assistant); | ||
reply.GetContent().Should().NotBeNullOrEmpty(); | ||
reply.From.Should().Be(agent.Name); | ||
} | ||
|
||
[ApiKeyFact("ANTHROPIC_API_KEY")] | ||
public async Task AnthropicAgentTestImageMessageAsync() | ||
{ | ||
var client = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, AnthropicTestUtils.ApiKey); | ||
var agent = new AnthropicClientAgent( | ||
client, | ||
name: "AnthropicAgent", | ||
AnthropicConstants.Claude3Haiku, | ||
systemMessage: "You are a helpful AI assistant that is capable of determining what an image is. Tell me a brief description of the image." | ||
) | ||
.RegisterMessageConnector(); | ||
|
||
var image = Path.Combine("images", "square.png"); | ||
var binaryData = BinaryData.FromBytes(await File.ReadAllBytesAsync(image), "image/png"); | ||
var imageMessage = new ImageMessage(Role.User, binaryData); | ||
|
||
var reply = await agent.SendAsync(imageMessage); | ||
reply.Should().BeOfType<TextMessage>(); | ||
reply.GetRole().Should().Be(Role.Assistant); | ||
reply.GetContent().Should().NotBeNullOrEmpty(); | ||
reply.From.Should().Be(agent.Name); | ||
} | ||
} |
Oops, something went wrong.