feat: add support for count tokens endpoint

This commit is contained in:
Stevan Freeborn
2025-01-01 20:13:01 -06:00
parent 31a7097cdc
commit 944822060c
8 changed files with 297 additions and 39 deletions
+30 -4
View File
@@ -26,6 +26,13 @@ public interface IAnthropicApiClient
/// <param name="request">The message request to create.</param>
/// <returns>An asynchronous enumerable that yields the response event by event.</returns>
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request);
/// <summary>
/// Counts the tokens in a message asynchronously.
/// </summary>
/// <param name="request">The count message tokens request.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="TokenCountResponse"/>.</returns>
Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request);
}
/// <inheritdoc cref="IAnthropicApiClient"/>
@@ -34,6 +41,7 @@ public class AnthropicApiClient : IAnthropicApiClient
private const string BaseUrl = "https://api.anthropic.com/v1/";
private const string ApiKeyHeader = "x-api-key";
private const string MessagesEndpoint = "messages";
private const string CountTokensEndpoint = "messages/count_tokens";
private const string JsonContentType = "application/json";
private const string EventPrefix = "event:";
private const string DataPrefix = "data:";
@@ -71,7 +79,7 @@ public class AnthropicApiClient : IAnthropicApiClient
/// <inheritdoc />
public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request)
{
var response = await SendRequestAsync(request);
var response = await SendRequestAsync(MessagesEndpoint, request);
var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync();
@@ -94,7 +102,7 @@ public class AnthropicApiClient : IAnthropicApiClient
/// <inheritdoc />
public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request)
{
var response = await SendRequestAsync(request);
var response = await SendRequestAsync(MessagesEndpoint, request);
if (response.IsSuccessStatusCode is false)
{
@@ -274,11 +282,29 @@ public class AnthropicApiClient : IAnthropicApiClient
return new ToolCall(tool, toolUse);
}
private async Task<HttpResponseMessage> SendRequestAsync(BaseMessageRequest request)
/// <inheritdoc />
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request)
{
var response = await SendRequestAsync(CountTokensEndpoint, request);
var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode is false)
{
var error = Deserialize<AnthropicError>(responseContent) ?? new AnthropicError();
return AnthropicResult<TokenCountResponse>.Failure(error, anthropicHeaders);
}
var msgResponse = Deserialize<TokenCountResponse>(responseContent) ?? new TokenCountResponse();
return AnthropicResult<TokenCountResponse>.Success(msgResponse, anthropicHeaders);
}
private async Task<HttpResponseMessage> SendRequestAsync<T>(string endpoint, T request)
{
var requestJson = Serialize(request);
var requestContent = new StringContent(requestJson, Encoding.UTF8, JsonContentType);
return await _httpClient.PostAsync(MessagesEndpoint, requestContent);
return await _httpClient.PostAsync(endpoint, requestContent);
}
private string Serialize<T>(T obj) => JsonSerializer.Serialize(obj, JsonSerializationOptions.DefaultOptions);
@@ -69,21 +69,4 @@ public static class AnthropicModels
/// The Claude 3.5 Haiku model.
/// </summary>
public const string Claude35HaikuLatest = "claude-3-5-haiku-latest";
internal static bool IsValidModel(string modelId) => modelId is
Claude3Opus or
Claude3Opus20241022 or
Claude3OpusLatest or
Claude3Sonnet or
Claude3Sonnet20240229 or
Claude35Sonnet or
Claude35Sonnet20240620 or
Claude35Sonnet20241022 or
Claude35SonnetLatest or
Claude3Haiku or
Claude3Haiku20240307 or
Claude35Haiku20241022 or
Claude35HaikuLatest;
}
@@ -0,0 +1,72 @@
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a request to count the number of tokens in a message.
/// </summary>
public class CountMessageTokensRequest
{
/// <summary>
/// Gets the model ID to be used for the request.
/// </summary>
public string Model { get; init; } = string.Empty;
/// <summary>
/// Gets the messages to count the number of tokens in.
/// </summary>
public List<Message> Messages { get; init; } = [];
/// <summary>
/// Gets the tool choice mode to use for the request.
/// </summary>
[JsonPropertyName("tool_choice")]
public ToolChoice? ToolChoice { get; init; } = null;
/// <summary>
/// Gets the tools to use for the request.
/// </summary>
public List<Tool>? Tools { get; init; } = null;
/// <summary>
/// Gets the system prompt to use for the request.
/// </summary>
[JsonPropertyName("system")]
public List<TextContent>? SystemPrompt { get; init; } = null;
/// <summary>
/// Initializes a new instance of the <see cref="CountMessageTokensRequest"/> class.
/// </summary>
/// <param name="model">The model ID to use for the request.</param>
/// <param name="messages">The messages to count the number of tokens in.</param>
/// <param name="toolChoice">The tool choice mode to use for the request.</param>
/// <param name="tools">The tools to use for the request.</param>
/// <param name="systemPrompt">The system prompt to use for the request.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="model"/> or <paramref name="messages"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="messages"/> is empty.</exception>
/// <returns>A new instance of the <see cref="CountMessageTokensRequest"/> class.</returns>
public CountMessageTokensRequest(
string model,
List<Message> messages,
ToolChoice? toolChoice = null,
List<Tool>? tools = null,
List<TextContent>? systemPrompt = null
)
{
ArgumentValidator.ThrowIfNull(model, nameof(model));
ArgumentValidator.ThrowIfNull(messages, nameof(messages));
if (messages.Count < 1)
{
throw new ArgumentException("Messages must contain at least one message");
}
Model = model;
Messages = messages;
ToolChoice = toolChoice;
Tools = tools;
SystemPrompt = systemPrompt;
}
}
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a response to a token count request.
/// </summary>
public class TokenCountResponse
{
/// <summary>
/// The number of input tokens counted.
/// </summary>
[JsonPropertyName("input_tokens")]
public int InputTokens { get; init; }
}