-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (92 loc) · 3.21 KB
/
Program.cs
File metadata and controls
95 lines (92 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Text.Json;
using System.Text.Json.Serialization;
using ClaudeCodeProxy;
using ClaudeCodeProxy.Configuration;
using ClaudeCodeProxy.Infrastructure;
using ClaudeCodeProxy.Middleware;
using ClaudeCodeProxy.Models;
using ClaudeCodeProxy.Services;
using LlmTornado;
using LlmTornado.Code.Vendor;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using Serilog.Settings.Configuration;
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
var configurationAssemblies = new[]
{
typeof(ConsoleLoggerConfigurationExtensions).Assembly,
typeof(FileLoggerConfigurationExtensions).Assembly,
};
var builder = WebApplication.CreateSlimBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.UseDebugSerilog(
builder.Configuration,
out var serilogger,
new ConfigurationReaderOptions(configurationAssemblies)
);
builder.Services.AddProblemDetails();
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
builder.Services.AddSingleton<ModelMappingService>();
builder.Services.AddSingleton<RateLimitHandlerService>();
builder.Services.AddSingleton<ITokenCountService, NaiveTokenCountService>();
builder.Services.AddSingleton<AnthropicProxyService>();
builder
.Services.AddOptionsFromRootAndValidateOnStart<Options>(builder.Configuration)
.Validate(
o =>
{
var res = o.TryParse(out var parsed);
if (res != null)
serilogger.Information(res.ToLogString());
return res != null;
},
"Invalid options configuration"
);
builder.Services.AddSingleton(sp => sp.GetOptions<Options>().Parse());
builder.Services.AddOpenApi();
var app = builder.Build();
// Add traffic logging middleware to capture ALL HTTP traffic
app.UseTrafficLogging();
app.MapGet(
"/",
() => Results.Ok("Anthropic Proxy for LlmTornado, proudly powered by ASP.NET Core 10.0")
);
app.MapPost(
"/v1/messages",
static async (
[FromBody] MessagesRequest request,
AnthropicProxyService proxyService,
RateLimitHandlerService rateLimitHandler
) =>
{
try
{
return request.Stream ?? false
? TypedResults.ServerSentEvents(proxyService.CreateStreamingResponse(request))
: Results.Json(
await proxyService.CreateCompletion(request),
AppJsonSerializerContext.Default.MessagesResponse
);
}
catch (Exception ex) when (rateLimitHandler.IsRateLimitError(ex))
{
return rateLimitHandler.CreateRateLimitResult(ex);
}
}
);
app.MapPost(
"/v1/messages/count_tokens",
static (TokenCountRequest request, ITokenCountService proxyService) =>
{
var response = proxyService.CountTokens(request);
return Results.Json(response, AppJsonSerializerContext.Default.TokenCountResponse);
}
);
app.Run();