docs: documentation for v0.4.0 [skip ci]

This commit is contained in:
Stevan Freeborn
2025-01-03 02:56:41 +00:00
parent e08c94ec57
commit 4a7d2c39be
14 changed files with 918 additions and 59 deletions
+28 -34
View File
@@ -146,8 +146,33 @@ var client = new AnthropicApiClient(apiKey, new HttpClient());
<h5>Note</h5>
<p>The following examples assume that you have already created an instance of the <code>AnthropicApiClient</code> class named <code>client</code>. You can also find these snippets in the examples directory.</p>
</div>
<h3 id="count-message-tokens">Count Message Tokens</h3>
<p>The <code>AnthropicApiClient</code> exposes a method named <code>CountMessageTokensAsync</code> that can be used to count the number of tokens in a message. The method requires a <code>CountMessageTokensRequest</code> instance as a parameter.</p>
<pre><code class="lang-csharp">using AnthropicClient;
using AnthropicClient.Models;
var response = await client.CountMessageTokensAsync(new CountMessageTokensRequest(
AnthropicModels.Claude3Haiku,
[
new(
MessageRole.User,
[new TextContent(&quot;Please write a haiku about the ocean.&quot;)]
)
]
));
if (response.IsFailure)
{
Console.WriteLine(&quot;Failed to count message tokens&quot;);
Console.WriteLine(&quot;Error Type: {0}&quot;, response.Error.Error.Type);
Console.WriteLine(&quot;Error Message: {0}&quot;, response.Error.Error.Message);
return;
}
Console.WriteLine(&quot;Token Count: {0}&quot;, response.Value.InputTokens);
</code></pre>
<h3 id="create-a-message">Create a message</h3>
<p>The <code>AnthropicApiClient</code> exposes a single method named <code>CreateMessageAsync</code> that can be used to create a message. The method requires a <code>MessageRequest</code> or a <code>StreamMessageRequest</code> instance as a parameter. The <code>MessageRequest</code> class is used to create a message whose response is not streamed and the <code>StreamMessageRequest</code> class is used to create a message whose response is streamed. The <code>MessageRequest</code> instance's properties can be set to configure how the message is created.</p>
<p>The <code>AnthropicApiClient</code> exposes a method named <code>CreateMessageAsync</code> that can be used to create a message. The method requires a <code>MessageRequest</code> or a <code>StreamMessageRequest</code> instance as a parameter. The <code>MessageRequest</code> class is used to create a message whose response is not streamed and the <code>StreamMessageRequest</code> class is used to create a message whose response is streamed. The <code>MessageRequest</code> instance's properties can be set to configure how the message is created.</p>
<h4 id="non-streaming">Non-Streaming</h4>
<pre><code class="lang-csharp">using AnthropicClient;
using AnthropicClient.Models;
@@ -674,20 +699,7 @@ foreach (var content in response.Value.Content)
}
</code></pre>
<h3 id="prompt-caching">Prompt Caching</h3>
<p>Anthropic has recently introduced a feature called <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching">Prompt Caching</a> that allows you to cache all or part of the prompt you send to the model. This can be used to improve the performance of your application by reducing latency and token usage. This feature is covered in depth in <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching">Anthropic's API Documentation</a>.</p>
<div class="NOTE">
<h5>Note</h5>
<p>This feature is in beta and requires you to set an <code>anthropic-beta</code> header on your requests to use it.
The value of the header should be <code>prompt-caching-2024-07-31</code>.</p>
</div>
<p>When using this library you can opt-in to prompt caching by adding the required header to the <code>HttpClient</code> instance you provide to the <code>AnthropicApiClient</code> constructor.</p>
<pre><code class="lang-csharp">using AnthropicClient;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(&quot;anthropic-beta&quot;, &quot;prompt-caching-2024-07-31&quot;);
var client = new AnthropicApiClient(apiKey, httpClient);
</code></pre>
<p>Anthropic provides a feature called <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching">Prompt Caching</a> that allows you to cache all or part of the prompt you send to the model. This can be used to improve the performance of your application by reducing latency and token usage. This feature is covered in depth in <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching">Anthropic's API Documentation</a>.</p>
<p>Prompt caching can be used to cache all parts of the prompt including system messages, user messages, and tools. You should refer to the <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching">Anthropic API Documentation</a> for specifics on limitations and requirements for using prompt caching. This library aims to make using prompt caching convenient and give you complete control over what parts of the prompt are cached. Currently there is only one type of cache control available - <code>EphemeralCacheControl</code>.</p>
<h4 id="caching-system-messages">Caching System Messages</h4>
<p>System messages can be cached by providing a <code>List&lt;TextContent&gt;</code> as the <code>systemMessages</code> parameter in the <code>MessageRequest</code> or <code>StreamMessageRequest</code> constructor and having one or more of the <code>TextContent</code> instances have the <code>CacheControl</code> property set.</p>
@@ -813,20 +825,7 @@ foreach (var content in response.Value.Content)
}
</code></pre>
<h3 id="pdf-support">PDF Support</h3>
<p>Anthropic has recently introduced a feature called <a href="https://docs.anthropic.com/en/docs/build-with-claude/pdf-support">PDF Support</a> that allows Claude to support PDF input and understand both text and visual content within documents. . This feature is covered in depth in <a href="https://docs.anthropic.com/en/docs/build-with-claude/pdf-support">Anthropic's API Documentation</a>.</p>
<div class="NOTE">
<h5>Note</h5>
<p>This feature is in beta and requires you to set an <code>anthropic-beta</code> header on your requests to use it.
The value of the header should be <code>pdfs-2024-09-25</code>.</p>
</div>
<p>When using this library you can opt-in to PDF support by adding the required header to the <code>HttpClient</code> instance you provide to the <code>AnthropicApiClient</code> constructor.</p>
<pre><code class="lang-csharp">using AnthropicClient;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(&quot;anthropic-beta&quot;, &quot;pdfs-2024-09-25&quot;);
var client = new AnthropicApiClient(apiKey, httpClient);
</code></pre>
<p>Anthropic provides a feature called <a href="https://docs.anthropic.com/en/docs/build-with-claude/pdf-support">PDF Support</a> that allows Claude to support PDF input and understand both text and visual content within documents. This feature is covered in depth in <a href="https://docs.anthropic.com/en/docs/build-with-claude/pdf-support">Anthropic's API Documentation</a>.</p>
<p>PDF support can be used to provide a PDF document as input to the model. This can be used to provide additional context to the model or to ask for additional information from the model. This library aims to make using PDF support convenient by allowing you to provide the PDF document you want Anthropic's models to consider for use when creating a message.</p>
<h4 id="pdf-document">PDF Document</h4>
<p>You can provide a PDF document by providing its base64 encoded content as a <code>DocumentContent</code> instance in the list of messages in the <code>MessageRequest</code> or <code>StreamMessageRequest</code> constructor.</p>
@@ -841,11 +840,6 @@ var request = new MessageRequest(
]
);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(&quot;anthropic-beta&quot;, &quot;pdfs-2024-09-25&quot;);
var client = new AnthropicApiClient(apiKey, httpClient);
var response = await client.CreateMessageAsync(request);
if (response.IsSuccess is false)