Skip to content

An unofficial C#/.NET SDK for accessing the Anthropic Claude API

License

Notifications You must be signed in to change notification settings

msih-apify/Anthropic.SDK

 
 

Repository files navigation

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 = $"\n\nHuman:Write me a sonnet about Joe Biden.\n\nAssistant:";
var parameters = new SamplingParameters()
{
    MaxTokensToSample = 512,
    Prompt = prompt,
    Temperature = 0.0f,
    StopSequences = new[] { "\n\nHuman:" },
    Stream = false,
    Model = "claude-1.3"
};

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 = $"\n\nHuman:Write me a sonnet about Joe Biden.\n\nAssistant:";
var parameters = new SamplingParameters()
{
    MaxTokensToSample = 512,
    Prompt = prompt,
    Temperature = 0.0f,
    StopSequences = new[] { "\n\nHuman:" },
    Stream = true,
    Model = "claude-1.3"
};

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

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.

About

An unofficial C#/.NET SDK for accessing the Anthropic Claude API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%