Anthropic.SDK 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Anthropic.SDK --version 1.2.0                
NuGet\Install-Package Anthropic.SDK -Version 1.2.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="Anthropic.SDK" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Anthropic.SDK --version 1.2.0                
#r "nuget: Anthropic.SDK, 1.2.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 Anthropic.SDK as a Cake Addin
#addin nuget:?package=Anthropic.SDK&version=1.2.0

// Install Anthropic.SDK as a Cake Tool
#tool nuget:?package=Anthropic.SDK&version=1.2.0                

Anthropic.SDK

.NET

Anthropic.SDK is an unofficial C# client designed for interacting with the Claude AI API. This powerful interface simplifies the integration of the Claude AI into your C# applications. It targets netstandard2.0, and .net6.0.

Table of Contents

Installation

Install Anthropic.SDK via the NuGet package manager:

PM> Install-Package Anthropic.SDK

API Keys

You can load the API Key from an environment variable named ANTHROPIC_API_KEY by default. Alternatively, you can supply it as a string to the AnthropicClient constructor.

IHttpClientFactory

The AnthropicClient can optionally take an IHttpClientFactory, which allows you to control elements such as retries and timeouts.

Usage

To start using the Claude AI API, simply create an instance of the AnthropicClient class.

Examples

Non-Streaming Call

Here's an example of a non-streaming call to the Claude AI API:

var client = new AnthropicClient();
var prompt = AnthropicSignals.HumanSignal + "Write me a sonnet about Joe Biden." + 
         AnthropicSignals.AssistantSignal;

var parameters = new SamplingParameters()
{
    // required    
    Model = AnthropicModels.Claude_v2_0
    Prompt = prompt,
    MaxTokensToSample = 512,

    //optional
    Temperature = 1,
    Top_k = 1,
    Top_p = 1
    StopSequences = new[] { AnthropicSignals.HumanSignal },
    Stream = false
};

var response = await client.Completions.GetClaudeCompletionAsync(parameters);

Streaming Call

The following is an example of a streaming call to the Claude AI API:

var client = new AnthropicClient();
var prompt = AnthropicSignals.HumanSignal + "Write me a sonnet about Joe Biden." + 
         AnthropicSignals.AssistantSignal;

var parameters = new SamplingParameters()
{
    MaxTokensToSample = 512,
    Prompt = prompt,
    Temperature = 0.0m,
    StopSequences = new[] { AnthropicSignals.HumanSignal },
    Stream = true,
    Model = AnthropicModels.Claude_v2
};

await foreach (var res in client.Completions.StreamClaudeCompletionAsync(parameters))
{
    Console.Write(res.Completion);
}

Token Counts

Token calculation does not use the typical cl100k_base that most OpenAI models use, and instead uses it's own byte-pair encoding. A simple extension method has been added to accurately calculate the number of tokens used by both a prompt and a response. See an example below.

var client = new AnthropicClient();
var prompt = AnthropicSignals.HumanSignal + "Write me a sonnet about Joe Biden." + 
         AnthropicSignals.AssistantSignal;

var parameters = new SamplingParameters()
{
    // required    
    Model = AnthropicModels.Claude_v2_0
    Prompt = prompt,
    MaxTokensToSample = 512,

    //optional
    Temperature = 1,
    Top_k = 1,
    Top_p = 1
    StopSequences = new[] { AnthropicSignals.HumanSignal },
    Stream = false
};

var response = await client.Completions.GetClaudeCompletionAsync(parameters);
Console.WriteLine($@"Tokens Used: Input - {prompt.GetClaudeTokenCount()}. Output - {response.Completion.GetClaudeTokenCount()}.");

Contributing

Pull requests are welcome. If you're planning to make a major change, please open an issue first to discuss your proposed changes.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on Anthropic.SDK:

Package Downloads
OBotService

OBase Framework

BotSharp.Plugin.AnthropicAI

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Anthropic.SDK:

Repository Stars
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
Version Downloads Last updated
4.3.1 870 11/13/2024
4.3.0 2,159 10/30/2024
4.2.0 358 10/26/2024
4.1.1 7,192 8/30/2024
4.1.0 5,210 8/18/2024
4.0.0 2,552 8/2/2024
3.3.0 8,588 7/23/2024
3.2.3 40,930 6/21/2024
3.2.2 2,412 6/16/2024
3.2.1 6,606 4/25/2024
3.2.0 8,744 4/24/2024
3.1.0 1,014 4/17/2024
3.0.1 1,400 4/12/2024
3.0.0 221 4/11/2024
2.0.1 10,807 3/17/2024
2.0.0 1,690 3/5/2024
1.3.0 2,212 11/21/2023
1.2.0 8,121 8/19/2023
1.1.2 1,835 8/9/2023
1.1.1 1,087 7/24/2023
1.1.0 1,015 7/16/2023
1.0.0 1,244 7/1/2023

Adds token counter extension method helper.