-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major update for the initial version
- Loading branch information
1 parent
6be72d4
commit 16436e1
Showing
39 changed files
with
1,553 additions
and
831 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Anthropic.Playground.SampleModels; | ||
|
||
public class WeatherInput | ||
{ | ||
[JsonPropertyName("location")] | ||
public string Location { get; set; } | ||
|
||
[JsonPropertyName("unit")] | ||
public string? Unit { get; set; } | ||
} |
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,92 +1,108 @@ | ||
using Anthropic.ObjectModels; | ||
using Anthropic.ObjectModels.ResponseModels; | ||
using Anthropic.ObjectModels.SharedModels; | ||
using Anthropic.Services; | ||
|
||
namespace Anthropic.Playground.TestHelpers; | ||
|
||
/// <summary> | ||
/// Helper class for testing chat functionalities of the Anthropic SDK. | ||
/// </summary> | ||
internal static class ChatTestHelper | ||
{ | ||
public static async Task RunSimpleChatTest(IAnthropicService sdk) | ||
/// <summary> | ||
/// Runs a simple chat completion test using the Anthropic SDK. | ||
/// </summary> | ||
/// <param name="anthropicService">The Anthropic service instance.</param> | ||
public static async Task RunChatCompletionTest(IAnthropicService anthropicService) | ||
{ | ||
Console.WriteLine("Messsaging Testing is starting:"); | ||
|
||
Console.WriteLine("Starting Chat Completion Test:"); | ||
try | ||
{ | ||
Console.WriteLine("Chat Completion Test:", ConsoleColor.DarkCyan); | ||
var completionResult = await sdk.Messages.Create(new() | ||
Console.WriteLine("Sending chat completion request..."); | ||
var chatCompletionResult = await anthropicService.Messages.Create(new() | ||
{ | ||
Messages = new List<Message> | ||
{ | ||
Messages = | ||
[ | ||
Message.FromUser("Hello there."), | ||
Message.FromAssistant("Hi, I'm Claude. How can I help you?"), | ||
Message.FromUser("Can you explain LLMs in plain English?") | ||
}, | ||
MaxTokens = 50, | ||
], | ||
MaxTokens = 200, | ||
Model = "claude-3-5-sonnet-20240620" | ||
}); | ||
|
||
if (completionResult.Successful) | ||
if (chatCompletionResult.Successful) | ||
{ | ||
Console.WriteLine(completionResult.Content.First().Text); | ||
Console.WriteLine("Chat Completion Response:"); | ||
Console.WriteLine(chatCompletionResult.ToString()); | ||
} | ||
else | ||
{ | ||
if (completionResult.Error == null) | ||
if (chatCompletionResult.Error == null) | ||
{ | ||
throw new("Unknown Error"); | ||
throw new("Unknown Error Occurred"); | ||
} | ||
|
||
Console.WriteLine($"{completionResult.HttpStatusCode}: {completionResult.Error.Message}"); | ||
Console.WriteLine($"Error: {chatCompletionResult.HttpStatusCode}: {chatCompletionResult.Error.Message}"); | ||
} | ||
} | ||
catch (Exception e) | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(e); | ||
Console.WriteLine($"Exception occurred: {ex}"); | ||
throw; | ||
} | ||
Console.WriteLine("---- 0 ----"); | ||
|
||
} | ||
|
||
public static async Task RunSimpleCompletionStreamTest(IAnthropicService sdk) | ||
/// <summary> | ||
/// Runs a simple chat completion stream test using the Anthropic SDK. | ||
/// </summary> | ||
/// <param name="anthropicService">The Anthropic service instance.</param> | ||
public static async Task RunChatCompletionStreamTest(IAnthropicService anthropicService) | ||
{ | ||
Console.WriteLine("Chat Completion Stream Testing is starting:", ConsoleColor.Cyan); | ||
Console.WriteLine("Starting Chat Completion Stream Test:"); | ||
try | ||
{ | ||
Console.WriteLine("Chat Completion Stream Test:", ConsoleColor.DarkCyan); | ||
var completionResult = sdk.Messages.CreateAsStream(new() | ||
Console.WriteLine("Initiating chat completion stream..."); | ||
var chatCompletionStream = anthropicService.Messages.CreateAsStream(new() | ||
{ | ||
Messages = new List<Message> | ||
{ | ||
Messages = | ||
[ | ||
Message.FromUser("Hello there."), | ||
Message.FromAssistant("Hi, I'm Claude. How can I help you?"), | ||
Message.FromUser("Tell me really long story about how a purple color became human?(in Turkish)") | ||
}, | ||
MaxTokens = 1024, | ||
Message.FromUser("Tell me a really long story about how the color purple became human.") | ||
], | ||
MaxTokens = 10, | ||
Model = "claude-3-5-sonnet-20240620" | ||
}); | ||
|
||
await foreach (var completion in completionResult) | ||
await foreach (var streamResponse in chatCompletionStream) | ||
{ | ||
if (completion.Successful) | ||
if (streamResponse.IsMessageResponse()) | ||
{ | ||
Console.Write(completion?.Content?.First().Text); | ||
var messageCompletion = streamResponse.As<MessageResponse>(); | ||
Console.Write(messageCompletion.ToString()); | ||
} | ||
else | ||
else if (streamResponse.IsError()) | ||
{ | ||
if (completion.Error == null) | ||
{ | ||
throw new("Unknown Error"); | ||
} | ||
|
||
Console.WriteLine($"{completion.HttpStatusCode}: {completion.Error.Message}"); | ||
var errorResponse = streamResponse.As<BaseResponse>(); | ||
Console.WriteLine($"Error: {errorResponse.HttpStatusCode}: {errorResponse.Error?.Message}"); | ||
} | ||
else if (streamResponse.IsPingResponse()) | ||
{ | ||
// Optionally handle ping responses | ||
// Console.WriteLine("Ping received"); | ||
} | ||
} | ||
|
||
Console.WriteLine(""); | ||
Console.WriteLine("Complete"); | ||
Console.WriteLine("\nStream completed"); | ||
} | ||
catch (Exception e) | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(e); | ||
Console.WriteLine($"Exception occurred: {ex}"); | ||
throw; | ||
} | ||
Console.WriteLine("---- 0 ----"); | ||
} | ||
} | ||
} |
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,95 @@ | ||
using Anthropic.Extensions; | ||
using Anthropic.ObjectModels.ResponseModels; | ||
using Anthropic.ObjectModels.SharedModels; | ||
using Anthropic.Playground.SampleModels; | ||
using Anthropic.Services; | ||
|
||
namespace Anthropic.Playground.TestHelpers; | ||
|
||
/// <summary> | ||
/// Helper class for testing chat functionalities with tool use in the Anthropic SDK. | ||
/// </summary> | ||
internal static class ChatToolUseTestHelper | ||
{ | ||
/// <summary> | ||
/// Runs a chat completion stream test with tool use using the Anthropic SDK. | ||
/// </summary> | ||
/// <param name="anthropicService">The Anthropic service instance.</param> | ||
public static async Task RunChatCompletionWithToolUseTest(IAnthropicService anthropicService) | ||
{ | ||
Console.WriteLine("Starting Chat Completion Stream Test with Tool Use:"); | ||
try | ||
{ | ||
Console.WriteLine("Initiating chat completion stream..."); | ||
var chatCompletionStream = anthropicService.Messages.CreateAsStream(new() | ||
{ | ||
Messages = [Message.FromUser("What is the weather like in San Francisco?")], | ||
MaxTokens = 2024, | ||
Model = "claude-3-5-sonnet-20240620", | ||
Tools = | ||
[ | ||
new() | ||
{ | ||
Name = "get_weather", | ||
Description = "Get the current weather in a given location", | ||
InputSchema = PropertyDefinition.DefineObject(new Dictionary<string, PropertyDefinition> | ||
{ | ||
{ "location", PropertyDefinition.DefineString("The city and state, e.g. San Francisco, CA") } | ||
}, ["location"], null, null, null) | ||
} | ||
] | ||
// ToolChoice = new() { Type = "any" } | ||
}); | ||
|
||
await foreach (var streamResponse in chatCompletionStream) | ||
{ | ||
if (streamResponse.IsMessageResponse()) | ||
{ | ||
var messageResponse = streamResponse.As<MessageResponse>(); | ||
var content = messageResponse.Content?.FirstOrDefault(); | ||
if (content == null) | ||
{ | ||
continue; | ||
} | ||
switch (content.Type) | ||
{ | ||
case "text": | ||
Console.Write(content.Text); | ||
break; | ||
case "tool_use" when content.Name == "get_weather": | ||
if (content.TryGetInputAs<WeatherInput>(out var weatherInput)) | ||
{ | ||
Console.WriteLine($"\nWeather request for \"{weatherInput.Location}\" in \"{weatherInput.Unit}\""); | ||
} | ||
|
||
break; | ||
case "tool_use": | ||
throw new InvalidOperationException($"Unknown tool use: {content.Name}"); | ||
default: | ||
Console.WriteLine($"\nUnknown content type: {content.Type}"); | ||
break; | ||
} | ||
} | ||
else if (streamResponse.IsError()) | ||
{ | ||
var errorResponse = streamResponse.As<BaseResponse>(); | ||
Console.WriteLine($"Error: {errorResponse.HttpStatusCode}: {errorResponse.Error?.Message}"); | ||
} | ||
else if (streamResponse.IsPingResponse()) | ||
{ | ||
// Optionally handle ping responses | ||
// Console.WriteLine("Ping received"); | ||
} | ||
} | ||
|
||
Console.WriteLine("\nStream completed"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Exception occurred: {ex}"); | ||
throw; | ||
} | ||
|
||
Console.WriteLine("---- 0 ----"); | ||
} | ||
} |
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,50 +1,57 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>Latest</LangVersion> | ||
<Copyright>Betalgo Up Ltd.</Copyright> | ||
<PackageProjectUrl>https://www.anthropic.com/</PackageProjectUrl> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Title>Anthropic SDK by Betalgo</Title> | ||
<Version>0.0.1</Version> | ||
<Authors>Tolga Kayhan, Betalgo</Authors> | ||
<Company>Betalgo Up Ltd.</Company> | ||
<Product>Anthropic Claude dotnet SDK</Product> | ||
<Description>Dotnet SDK for Anthropic Claude</Description> | ||
<RepositoryUrl>https://github.com/betalgo/anthropic/</RepositoryUrl> | ||
<PackageTags>Anthropic,Claude,ai,betalgo,NLP</PackageTags> | ||
<PackageId>Betalgo.Ranul.Anthropic</PackageId> | ||
<PackageReadmeFile>Readme.md</PackageReadmeFile> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<RepositoryUrl>https://github.com/betalgo/anthropic.git</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>Latest</LangVersion> | ||
<Copyright>Betalgo Up Ltd.</Copyright> | ||
<PackageProjectUrl>https://www.anthropic.com/</PackageProjectUrl> | ||
<PackageIcon>Ranul-Anthropic-Icon.png</PackageIcon> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Title>Anthropic Claude C# .NET library by Ranul Betalgo</Title> | ||
<Version>8.0.0</Version> | ||
<Authors>Tolga Kayhan, Betalgo, Ranul</Authors> | ||
<Company>Betalgo Up Ltd.</Company> | ||
<Product>Anthropic Claude C# .NET library by Ranul Betalgo</Product> | ||
<Description>Anthropic Claude C# .NET library by Ranul Betalgo</Description> | ||
<RepositoryUrl>https://github.com/betalgo/anthropic/</RepositoryUrl> | ||
<PackageTags>Anthropic,Claude,ai,betalgo,NLP,Ranul</PackageTags> | ||
<PackageId>Betalgo.Ranul.Anthropic</PackageId> | ||
<PackageReadmeFile>Readme.md</PackageReadmeFile> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<RepositoryUrl>https://github.com/betalgo/anthropic.git</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Interfaces\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Remove="Interfaces\**" /> | ||
<EmbeddedResource Remove="Interfaces\**" /> | ||
<None Remove="Interfaces\**" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\Readme.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
|
||
<None Include="Ranul-Anthropic-Icon.png"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> | ||
<PackageReference Include="System.Text.Json" Version="8.0.4" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> | ||
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</Project> |
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.