Universal.Anthropic.Client 1.0.0

dotnet add package Universal.Anthropic.Client --version 1.0.0                
NuGet\Install-Package Universal.Anthropic.Client -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Universal.Anthropic.Client" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Universal.Anthropic.Client --version 1.0.0                
#r "nuget: Universal.Anthropic.Client, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Universal.Anthropic.Client as a Cake Addin
#addin nuget:?package=Universal.Anthropic.Client&version=1.0.0

// Install Universal.Anthropic.Client as a Cake Tool
#tool nuget:?package=Universal.Anthropic.Client&version=1.0.0                

Universal.Anthropic.Client

Universal.Anthropic.Client is a C# library for interacting with the Anthropic API. It provides a simple and efficient way to create messages and handle streaming responses from Anthropic's AI models.

Features

  • Easy-to-use client for Anthropic API
  • Support for creating messages
  • Streaming support for real-time responses
  • Customizable API version
  • Built-in error handling and deserialization

Usage

Initializing the Client

var client = new AnthropicClient("YOUR_API_KEY");

Creating a Message

var request = new MessageRequest
{
    Model = "claude-3-sonnet-20240320",
    Messages = new List<Message>
    {
        new Message(Roles.User, "Hello, how are you?")
    }
};

var response = await client.CreateMessageAsync(request);
Console.WriteLine(response.Content[0].Text);

Streaming a Message

var streamingRequest = new MessageRequest
{
    Model = "claude-3-sonnet-20240320",
    Messages = new List<Message>
    {
        new Message(Roles.User, "Tell me a story about a brave knight.")
    },
    Stream = true
};

var streamingResponse = client.CreateMessageStreamingAsync(streamingRequest);

streamingResponse.Updated += (sender, args) =>
{
    Console.WriteLine("Response updated: " + streamingResponse.Value.Content);
};

await streamingResponse.Task; // Wait for completion

Using Tools

The Anthropic API supports the use of tools, allowing the AI to perform specific actions or retrieve information. Here's an example of how to use a tool for weather checking:

// Define a weather tool
var weatherTool = new Tool
{
    Name = "get_weather",
    Description = "Get the current weather in a given location",
    InputSchema = new JsonSchema
    {
        Type = "object",
        Properties = new Dictionary<string, JsonSchema>()
        {
            ["location"] = new JsonSchema()
            {
                Type = "string",
                Description = "The city and state, e.g. San Francisco, CA"
            }
        },
        Required = new[] { "location" }
    }
};

// Create a message request with the tool
var request = new MessageRequest()
{
    Model = Models.Claude35Sonnet,
    Messages = new List<Message>
    {
        new Message(Roles.User, "What's the weather like in San Francisco?")
    },
    System = "You are an assistant for Red Marble AI. When asked about weather, always use the get_weather tool to provide accurate information.",
    MaxTokens = 8192,
    Tools = new List<Tool> { weatherTool },
    ToolChoice = new AutoToolChoice()
};

// Send the request
var response = await anthropicClient.CreateMessageAsync(request);

// Check if the tool was used
var toolUseBlock = response.Content.FirstOrDefault(c => c is ToolUseContentBlock) as ToolUseContentBlock;
if (toolUseBlock != null)
{
    Console.WriteLine($"Tool used: {toolUseBlock.Name}");
    Console.WriteLine($"Tool input: {toolUseBlock.Input}");
}

In this example, we define a get_weather tool with an input schema for location. We then include this tool in our message request, along with a system message instructing the AI to use the weather tool for weather-related questions. The ToolChoice is set to AutoToolChoice, allowing the model to decide when to use the tool.

After receiving the response, you can check if the tool was used by looking for a ToolUseContentBlock in the response content.

Key Classes

  • AnthropicClient: The main client for interacting with the Anthropic API.
  • MessageRequest: Represents a request to create a message.
  • MessageResponse: Represents the response from creating a message.
  • StreamingMessageResponse: Represents a streaming response for real-time updates.

Error Handling

The client throws HttpException for non-successful status codes. Make sure to handle these exceptions in your code.

Customization

You can customize the API version used by setting the AnthropicVersion property on the client:

client.AnthropicVersion = "2023-06-01";

For more detailed information about the Anthropic API, please refer to the official Anthropic documentation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 101 9/18/2024

Initial release.