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.
Install Anthropic.SDK via the NuGet package manager:
PM> Install-Package Anthropic.SDK
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.
The AnthropicClient
can optionally take an IHttpClientFactory
, which allows you to control elements such as retries and timeouts.
To start using the Claude AI API, simply create an instance of the AnthropicClient
class.
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);
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);
}
Pull requests are welcome. If you're planning to make a major change, please open an issue first to discuss your proposed changes.
This project is licensed under the MIT License.