Skip to content

Commit 733112a

Browse files
committed
responses: token counting
1 parent c4c6393 commit 733112a

File tree

13 files changed

+68
-17
lines changed

13 files changed

+68
-17
lines changed

src/LlmTornado/Chat/ConversationCompression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public async Task<List<ChatMessage>> Compress(List<ChatMessage> messages, string
112112
Task<string>[] summaryTasks = chunks.Select(chunk => SummarizeChunk(api, chunk, options, context, token)).ToArray();
113113
string[] summaries = await Task.WhenAll(summaryTasks);
114114

115-
List<ChatMessage> conversation = new List<ChatMessage>();
115+
List<ChatMessage> conversation = [];
116116

117117
foreach (string summary in summaries)
118118
{
@@ -203,8 +203,8 @@ private async Task<string> SummarizeChunk(TornadoApi api, List<ChatMessage> chun
203203

204204
public class ConversationContent
205205
{
206-
public List<ChatMessage> SystemMessages { get; set; } = new List<ChatMessage>();
207-
public List<ChatMessage> MessagesToCompress { get; set; } = new List<ChatMessage>();
206+
public List<ChatMessage> SystemMessages { get; set; } = [];
207+
public List<ChatMessage> MessagesToCompress { get; set; } = [];
208208

209209
/// <summary>
210210
/// Get the content of the conversation based on the compression options

src/LlmTornado/Chat/Vendors/Cohere/VendorCohereChatRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public VendorCohereChatRequest(ChatRequest request, IEndpointProvider provider)
9494
if (request.Tools is { Count: > 0 })
9595
{
9696
// Optimize: single loop instead of Where().Select().ToList() to avoid intermediate enumerable allocations
97-
List<VendorCohereTool> tools = new List<VendorCohereTool>();
97+
List<VendorCohereTool> tools = [];
9898
foreach (var t in request.Tools)
9999
{
100100
if (t.Function is not null)

src/LlmTornado/Chat/Vendors/Mistral/MistralChatMessageConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal class MistralChatMessageConverter : JsonConverter<ChatMessage>
3232
{
3333
StringBuilder reasoningBuilder = new StringBuilder();
3434
StringBuilder textBuilder = new StringBuilder();
35-
List<ChatMessagePart> parts = new List<ChatMessagePart>();
35+
List<ChatMessagePart> parts = [];
3636

3737
foreach (JToken block in contentArr)
3838
{
@@ -170,7 +170,7 @@ private static ChatMessageRoles ParseRole(string? role)
170170
return null;
171171
}
172172

173-
List<ToolCall> toolCalls = new List<ToolCall>();
173+
List<ToolCall> toolCalls = [];
174174

175175
foreach (JToken tc in toolCallsArray)
176176
{

src/LlmTornado/Chat/Vendors/Zai/VendorZaiChatRequestData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public VendorZaiChatRequestData(ChatRequest request) : base(request)
8282
// Add built-in tools from vendor extensions
8383
if (request.VendorExtensions?.Zai?.BuiltInTools is { Count: > 0 })
8484
{
85-
Tools ??= new List<VendorZaiTool>();
85+
Tools ??= [];
8686
Tools.AddRange(request.VendorExtensions.Zai.BuiltInTools.Select(ConvertBuiltInTool));
8787
}
8888
}
8989

9090
private static List<VendorZaiTool> ConvertTools(List<Tool> tools)
9191
{
92-
List<VendorZaiTool> zaiTools = new();
92+
List<VendorZaiTool> zaiTools = [];
9393

9494
foreach (Tool tool in tools)
9595
{

src/LlmTornado/Code/DiffMatchPatch/PatchList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private static bool IsHeaderlessHeader(string line)
187187

188188
private static Patch? ParseHeaderlessPatch(IReadOnlyList<string> lines, ref int index)
189189
{
190-
List<string> body = new();
190+
List<string> body = [];
191191

192192
while (index < lines.Count)
193193
{

src/LlmTornado/Infra/Clr.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal static class Clr
6767

6868
private static MethodInfo? GetCachedMethod(Type type, string methodName, BindingFlags bindingFlags, Type[]? parameterTypes)
6969
{
70-
Type[] paramTypes = parameterTypes ?? Array.Empty<Type>();
70+
Type[] paramTypes = parameterTypes ?? [];
7171
return methodInfoCache.GetOrAdd((type, methodName, paramTypes), key =>
7272
key.Type.GetMethod(key.MethodName, bindingFlags, null, key.ParameterTypes, null));
7373
}

src/LlmTornado/Ocr/Models/Mistral/OcrModelMistral.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class OcrModelMistral : BaseVendorModelProvider
1717
/// OCR 3 (mistral-ocr-2512) - Released December 2024.
1818
/// Features: table_format (markdown/html), extract_header, extract_footer, hyperlinks output.
1919
/// </summary>
20-
public static readonly OcrModel ModelOcr2512 = new OcrModel("mistral-ocr-2512", LLmProviders.Mistral, new List<string> { "mistral-ocr-latest" });
20+
public static readonly OcrModel ModelOcr2512 = new OcrModel("mistral-ocr-2512", LLmProviders.Mistral, ["mistral-ocr-latest"]);
2121

2222
/// <summary>
2323
/// <inheritdoc cref="ModelOcr2512"/>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Newtonsoft.Json;
2+
3+
namespace LlmTornado.Responses;
4+
5+
/// <summary>
6+
/// Result of the input token counting endpoint (POST /responses/input_tokens).
7+
/// Returns the number of input tokens the request would consume.
8+
/// </summary>
9+
public class ResponseInputTokensResult
10+
{
11+
/// <summary>
12+
/// The object type. Always "response.input_tokens".
13+
/// </summary>
14+
[JsonProperty("object")]
15+
public string Object { get; set; } = "response.input_tokens";
16+
17+
/// <summary>
18+
/// The number of input tokens the request would use.
19+
/// </summary>
20+
[JsonProperty("input_tokens")]
21+
public int InputTokens { get; set; }
22+
}

src/LlmTornado/Responses/ResponsesEndpoint.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,35 @@ public async Task<HttpCallResult<ResponseCompactResult>> CompactResponseSafe(Res
284284
return await HttpPost<ResponseCompactResult>(provider, Endpoint, url: GetUrl(provider, "/compact"), postData: body, ct: request.CancellationToken).ConfigureAwait(false);
285285
}
286286

287+
/// <summary>
288+
/// Returns the number of input tokens a response request would consume, without actually generating a response.
289+
/// </summary>
290+
/// <param name="request">The request to count tokens for.</param>
291+
/// <param name="cancellationToken">Cancellation token.</param>
292+
public async Task<ResponseInputTokensResult> CountInputTokens(ResponseRequest request, CancellationToken cancellationToken = default)
293+
{
294+
HttpCallResult<ResponseInputTokensResult> data = await CountInputTokensSafe(request, cancellationToken).ConfigureAwait(false);
295+
296+
if (!data.Ok)
297+
{
298+
throw data.Exception;
299+
}
300+
301+
return data.Data;
302+
}
303+
304+
/// <summary>
305+
/// Returns the number of input tokens a response request would consume, without actually generating a response.
306+
/// </summary>
307+
/// <param name="request">The request to count tokens for.</param>
308+
/// <param name="cancellationToken">Cancellation token.</param>
309+
public async Task<HttpCallResult<ResponseInputTokensResult>> CountInputTokensSafe(ResponseRequest request, CancellationToken cancellationToken = default)
310+
{
311+
IEndpointProvider provider = Api.GetProvider(request.Model ?? ChatModel.OpenAi.Gpt35.Turbo);
312+
TornadoRequestContent requestBody = request.Serialize(provider);
313+
return await HttpPost<ResponseInputTokensResult>(provider, Endpoint, url: GetUrl(provider, "/input_tokens"), postData: requestBody.Body, model: request.Model, ct: cancellationToken).ConfigureAwait(false);
314+
}
315+
287316
/// <summary>
288317
/// Creates a responses API request.
289318
/// </summary>

src/LlmTornado/Skills/CreateSkillRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class CreateSkillRequest
3030
/// </summary>
3131
public CreateSkillRequest()
3232
{
33-
Files = Array.Empty<FileUploadRequest>();
33+
Files = [];
3434
}
3535

3636
/// <summary>
@@ -41,7 +41,7 @@ public CreateSkillRequest()
4141
public CreateSkillRequest(string displayTitle, FileUploadRequest[]? files = null)
4242
{
4343
DisplayTitle = displayTitle;
44-
Files = files ?? Array.Empty<FileUploadRequest>();
44+
Files = files ?? [];
4545
}
4646

4747
/// <summary>

0 commit comments

Comments
 (0)