-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (83 loc) · 3.73 KB
/
Program.cs
File metadata and controls
98 lines (83 loc) · 3.73 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
96
97
98
using InvoiceAI.Services;
var builder = WebApplication.CreateBuilder(args);
// ── Configuration ─────────────────────────────────────────────────────────────
builder.Configuration
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
// Map ANTHROPIC_API_KEY env var → Anthropic:ApiKey config key
var anthropicApiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY");
if (!string.IsNullOrEmpty(anthropicApiKey))
builder.Configuration["Anthropic:ApiKey"] = anthropicApiKey;
// ── Services ──────────────────────────────────────────────────────────────────
builder.Services.AddControllers()
.AddNewtonsoftJson(opts =>
{
opts.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
opts.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new()
{
Title = "Invoice AI API",
Version = "v1",
Description = """
Demonstrates training a base LLM to read invoices and receipts
using few-shot learning and prompt engineering.
## Quickstart
1. **POST /api/training/run** – run the training pipeline
2. **POST /api/demo/run/train-001** – extract a sample invoice
3. **POST /api/invoice/extract** – extract your own invoice text
"""
});
// Include XML comments
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
c.IncludeXmlComments(xmlPath);
});
// HTTP client for Anthropic API
builder.Services.AddHttpClient("anthropic", client =>
{
client.Timeout = TimeSpan.FromSeconds(120);
client.BaseAddress = new Uri("https://api.anthropic.com");
});
// Core services
builder.Services.AddSingleton<IInvoiceExtractionService, AnthropicInvoiceExtractionService>();
builder.Services.AddSingleton<ModelTrainingService>();
builder.Services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
var app = builder.Build();
// ── Pipeline ──────────────────────────────────────────────────────────────────
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Invoice AI v1");
c.RoutePrefix = string.Empty; // serve Swagger UI at root
c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.List);
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
// ── Auto-train on startup (loads few-shot context into memory) ────────────────
var logger = app.Services.GetRequiredService<ILogger<Program>>();
try
{
logger.LogInformation("Auto-training model on startup with sample invoice dataset...");
var trainingService = app.Services.GetRequiredService<ModelTrainingService>();
await trainingService.TrainAsync();
logger.LogInformation("Startup training complete. Invoice AI is ready.");
}
catch (Exception ex)
{
logger.LogWarning(ex, "Startup training failed (API calls will still work with base model)");
}
app.Run();