-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from tghamm/feature/2.0.0
Feature/2.0.0
- Loading branch information
Showing
16 changed files
with
646 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Anthropic.SDK.Constants; | ||
using Anthropic.SDK.Messaging; | ||
|
||
namespace Anthropic.SDK.Tests | ||
{ | ||
[TestClass] | ||
public class Messages | ||
{ | ||
[TestMethod] | ||
public async Task TestBasicClaude3Message() | ||
{ | ||
var client = new AnthropicClient(); | ||
var messages = new List<Message>(); | ||
messages.Add(new Message() | ||
{ | ||
Role = RoleType.User, | ||
Content = "Write me a sonnet about the Statue of Liberty" | ||
}); | ||
var parameters = new MessageParameters() | ||
{ | ||
Messages = messages, | ||
MaxTokens = 512, | ||
Model = AnthropicModels.Claude3Sonnet, | ||
Stream = false, | ||
Temperature = 1.0m, | ||
}; | ||
var res = await client.Messages.GetClaudeMessageAsync(parameters); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestBasicClaude3ImageMessage() | ||
{ | ||
string resourceName = "Anthropic.SDK.Tests.Red_Apple.jpg"; | ||
|
||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
|
||
await using Stream stream = assembly.GetManifestResourceStream(resourceName); | ||
byte[] imageBytes; | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
await stream.CopyToAsync(memoryStream); | ||
imageBytes = memoryStream.ToArray(); | ||
} | ||
|
||
string base64String = Convert.ToBase64String(imageBytes); | ||
|
||
var client = new AnthropicClient(); | ||
|
||
var messages = new List<Message>(); | ||
messages.Add(new Message() | ||
{ | ||
Role = RoleType.User, | ||
Content = new dynamic[] | ||
{ | ||
new ImageContent() | ||
{ | ||
Source = new ImageSource() | ||
{ | ||
MediaType = "image/jpeg", | ||
Data = base64String | ||
} | ||
}, | ||
new TextContent() | ||
{ | ||
Text = "What is this a picture of?" | ||
} | ||
} | ||
}); | ||
var parameters = new MessageParameters() | ||
{ | ||
Messages = messages, | ||
MaxTokens = 512, | ||
Model = AnthropicModels.Claude3Opus, | ||
Stream = false, | ||
Temperature = 1.0m, | ||
}; | ||
var res = await client.Messages.GetClaudeMessageAsync(parameters); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestStreamingClaude3ImageMessage() | ||
{ | ||
string resourceName = "Anthropic.SDK.Tests.Red_Apple.jpg"; | ||
|
||
// Get the current assembly | ||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
|
||
// Get a stream to the embedded resource | ||
await using Stream stream = assembly.GetManifestResourceStream(resourceName); | ||
// Read the stream into a byte array | ||
byte[] imageBytes; | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
await stream.CopyToAsync(memoryStream); | ||
imageBytes = memoryStream.ToArray(); | ||
} | ||
|
||
// Convert the byte array to a base64 string | ||
string base64String = Convert.ToBase64String(imageBytes); | ||
|
||
var client = new AnthropicClient(); | ||
var messages = new List<Message>(); | ||
messages.Add(new Message() | ||
{ | ||
Role = RoleType.User, | ||
Content = new dynamic[] | ||
{ | ||
new ImageContent() | ||
{ | ||
Source = new ImageSource() | ||
{ | ||
MediaType = "image/jpeg", | ||
Data = base64String | ||
} | ||
}, | ||
new TextContent() | ||
{ | ||
Text = "What is this a picture of?" | ||
} | ||
} | ||
}); | ||
var parameters = new MessageParameters() | ||
{ | ||
Messages = messages, | ||
MaxTokens = 512, | ||
Model = AnthropicModels.Claude3Opus, | ||
Stream = true, | ||
Temperature = 1.0m, | ||
}; | ||
var outputs = new List<MessageResponse>(); | ||
await foreach (var res in client.Messages.StreamClaudeMessageAsync(parameters)) | ||
{ | ||
if (res.Delta != null) | ||
{ | ||
Debug.Write(res.Delta.Text); | ||
} | ||
|
||
outputs.Add(res); | ||
} | ||
Debug.WriteLine(string.Empty); | ||
Debug.WriteLine($@"Used Tokens - Input:{outputs.First().StreamStartMessage.Usage.InputTokens}. | ||
Output: {outputs.Last().Usage.OutputTokens}"); | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.