Skip to content

Commit 6557918

Browse files
authored
Merge pull request #56 from tghamm/feature/4.3.0
Version Bump, Update Readme
2 parents 82d8129 + 4c58a92 commit 6557918

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

Anthropic.SDK/Anthropic.SDK.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
<PackageTags>Claude, AI, ML, API, Anthropic</PackageTags>
1515
<Title>Claude API</Title>
1616
<PackageReleaseNotes>
17-
Support for Batching, Improved Rate Limit Error Handling
17+
Implements IChatClient from Microsoft.Extensions.AI.Abstractions
1818
</PackageReleaseNotes>
1919
<PackageId>Anthropic.SDK</PackageId>
20-
<Version>4.2.0</Version>
21-
<AssemblyVersion>4.2.0.0</AssemblyVersion>
22-
<FileVersion>4.2.0.0</FileVersion>
20+
<Version>4.3.0</Version>
21+
<AssemblyVersion>4.3.0.0</AssemblyVersion>
22+
<FileVersion>4.3.0.0</FileVersion>
2323
<GenerateDocumentationFile>True</GenerateDocumentationFile>
2424
<PackageReadmeFile>README.md</PackageReadmeFile>
2525
<PackageIcon>icon.png</PackageIcon>

README.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Anthropic.SDK is an unofficial C# client designed for interacting with the Claud
1313
- [Examples](#examples)
1414
- [Non-Streaming Call](#non-streaming-call)
1515
- [Streaming Call](#streaming-call)
16+
- [IChatClient](#ichatclient)
1617
- [Prompt Caching](#prompt-caching)
1718
- [Batching](#batching)
1819
- [Tools](#tools)
@@ -37,7 +38,24 @@ The `AnthropicClient` can optionally take a custom `HttpClient` in the `Anthropi
3738

3839
## Usage
3940

40-
To start using the Claude AI API, simply create an instance of the `AnthropicClient` class.
41+
There are two ways to start using the `AnthropicClient`. The first is to simply new up an instance of the `AnthropicClient` and start using it, the second is to use the messaging client with the new `Microsoft.Extensions.AI.Abstractions` builder.
42+
Brief examples of each are below.
43+
44+
Option 1:
45+
46+
```csharp
47+
var client = new AnthropicClient();
48+
```
49+
50+
Option 2:
51+
52+
```csharp
53+
IChatClient client = new ChatClientBuilder()
54+
.UseFunctionInvocation() //optional
55+
.Use(new AnthropicClient().Messages);
56+
```
57+
58+
Both support all the core features of the `AnthropicClient's` Messaging and Tooling capabilities, but the latter will be fully featured in .NET 9 and provide built in telemetry and DI and make it easier to choose which SDK you are using.
4159

4260
## Examples
4361

@@ -149,6 +167,98 @@ Console.WriteLine($@"Used Tokens - Input:{outputs.First().StreamStartMessage.Usa
149167
Output: {outputs.Last().Usage.OutputTokens}");
150168
```
151169

170+
### IChatClient
171+
172+
The `AnthropicClient` has support for the new `IChatClient` from Microsoft and offers a slightly different mechanism for using the `AnthropicClient`. Below are a few examples.
173+
174+
```csharp
175+
//function calling
176+
IChatClient client = new ChatClientBuilder()
177+
.UseFunctionInvocation()
178+
.Use(new AnthropicClient().Messages);
179+
180+
ChatOptions options = new()
181+
{
182+
ModelId = AnthropicModels.Claude3Haiku,
183+
MaxOutputTokens = 512,
184+
Tools = [AIFunctionFactory.Create((string personName) => personName switch {
185+
"Alice" => "25",
186+
_ => "40"
187+
}, "GetPersonAge", "Gets the age of the person whose name is specified.")]
188+
};
189+
190+
var res = await client.CompleteAsync("How old is Alice?", options);
191+
192+
Assert.IsTrue(
193+
res.Message.Text?.Contains("25") is true,
194+
res.Message.Text);
195+
196+
//non-streaming
197+
IChatClient client = new AnthropicClient().Messages;
198+
199+
ChatOptions options = new()
200+
{
201+
ModelId = AnthropicModels.Claude_v2_1,
202+
MaxOutputTokens = 512,
203+
Temperature = 1.0f,
204+
};
205+
206+
var res = await client.CompleteAsync("Write a sonnet about the Statue of Liberty. The response must include the word green.", options);
207+
208+
Assert.IsTrue(res.Message.Text?.Contains("green") is true, res.Message.Text);
209+
210+
//streaming call
211+
IChatClient client = new AnthropicClient().Messages;
212+
213+
ChatOptions options = new()
214+
{
215+
ModelId = AnthropicModels.Claude_v2_1,
216+
MaxOutputTokens = 512,
217+
Temperature = 1.0f,
218+
};
219+
220+
StringBuilder sb = new();
221+
await foreach (var res in client.CompleteStreamingAsync("Write a sonnet about the Statue of Liberty. The response must include the word green.", options))
222+
{
223+
sb.Append(res);
224+
}
225+
226+
Assert.IsTrue(sb.ToString().Contains("green") is true, sb.ToString());
227+
228+
//Image call
229+
string resourceName = "Anthropic.SDK.Tests.Red_Apple.jpg";
230+
231+
Assembly assembly = Assembly.GetExecutingAssembly();
232+
233+
await using Stream stream = assembly.GetManifestResourceStream(resourceName)!;
234+
byte[] imageBytes;
235+
using (var memoryStream = new MemoryStream())
236+
{
237+
await stream.CopyToAsync(memoryStream);
238+
imageBytes = memoryStream.ToArray();
239+
}
240+
241+
IChatClient client = new AnthropicClient().Messages;
242+
243+
var res = await client.CompleteAsync(
244+
[
245+
new ChatMessage(ChatRole.User,
246+
[
247+
new ImageContent(imageBytes, "image/jpeg"),
248+
new TextContent("What is this a picture of?"),
249+
])
250+
], new()
251+
{
252+
ModelId = AnthropicModels.Claude3Opus,
253+
MaxOutputTokens = 512,
254+
Temperature = 0f,
255+
});
256+
257+
Assert.IsTrue(res.Message.Text?.Contains("apple", StringComparison.OrdinalIgnoreCase) is true, res.Message.Text);
258+
259+
```
260+
Please see the unit tests for even more examples.
261+
152262
### Prompt Caching
153263

154264
The `AnthropicClient` supports prompt caching of system messages, user messages (including images), assistant messages, tool_results, and tools in accordance with model limitations. Because the `AnthropicClient` does not have it's own tokenizer, you must ensure yourself that when enabling prompt caching, you are providing enough context to the qualifying model for it to cache or nothing will be cached. Check out the documentation on Anthropic's website for specific model limitations and requirements.

0 commit comments

Comments
 (0)