mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-03-29 13:51:37 +00:00
27 lines
997 B
C#
27 lines
997 B
C#
using AIStudio.Tools.Rust;
|
|
|
|
namespace AIStudio.Tools.Services;
|
|
|
|
public sealed partial class RustService
|
|
{
|
|
public async Task<TokenCountInfo?> GetTokenCount(string text)
|
|
{
|
|
try
|
|
{
|
|
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
|
var payload = new { text };
|
|
var response = await this.http.PostAsJsonAsync("/system/tokenizer/count", payload, this.jsonRustSerializerOptions, cts.Token);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadFromJsonAsync<TokenCountInfo>(this.jsonRustSerializerOptions, cancellationToken: cts.Token);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if(this.logger is not null)
|
|
this.logger.LogError(e, "Error while getting token count from Rust service.");
|
|
else
|
|
Console.WriteLine($"Error while getting token count from Rust service: '{e}'.");
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |