Models-Phi-8B/PHI.Data.Generator/RLHF.Generator.Api/Program.cs

296 lines
9.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using Scalar.AspNetCore;
using RLHF.Generator.Api.Contracts;
using RLHF.Generator.Generation;
using RLHF.Generator.Keywords;
using RLHF.Generator.LmStudio;
using RLHF.Generator.Persistence;
using RLHF.Generator.TrainingData;
var builder = WebApplication.CreateBuilder(args);
string connectionString = builder.Configuration.GetConnectionString("RLHFGenerator")
?? throw new InvalidOperationException("Connection string 'RLHFGenerator' was not found.");
builder.Services.AddDbContext<RlhfGeneratorDbContext>(options =>
options.UseSqlServer(connectionString));
LmStudioOptions lmStudioOptions;
try
{
lmStudioOptions = builder.Configuration
.GetSection(LmStudioOptions.SectionName)
.Get<LmStudioOptions>()
?? throw new InvalidOperationException();
}
catch (Exception exception)
{
throw new InvalidOperationException(
$"Configuration section '{LmStudioOptions.SectionName}' is missing or invalid.",
exception);
}
builder.Services.AddSingleton(lmStudioOptions);
builder.Services.AddHttpClient<LmStudioClient>((serviceProvider, httpClient) =>
httpClient.Timeout = TimeSpan.FromSeconds(
serviceProvider.GetRequiredService<LmStudioOptions>().TimeoutSeconds));
GenerationOptions generationOptions;
try
{
generationOptions = builder.Configuration
.GetSection(GenerationOptions.SectionName)
.Get<GenerationOptions>()
?? throw new InvalidOperationException();
}
catch (Exception exception)
{
throw new InvalidOperationException(
$"Configuration section '{GenerationOptions.SectionName}' is missing or invalid.",
exception);
}
builder.Services.AddSingleton(generationOptions);
builder.Services.AddSingleton(
new TrainingDataReader(Path.Combine(AppContext.BaseDirectory, "Data")));
builder.Services.AddScoped<TrainingGenerationService>();
builder.Services.AddScoped<TrainingItemResetService>();
builder.Services.AddScoped<KeywordLoadService>();
builder.Services.AddScoped<TrainingItemKeywordBackfillService>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc(
"v1",
new OpenApiInfo
{
Title = "RLHF Generator API",
Version = "v1",
Description = "Generates balanced RLHF training data from category and pattern files using SQL Server and LM Studio."
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint(
"/swagger/v1/swagger.json",
"RLHF Generator API v1");
options.RoutePrefix = "swagger";
options.DocumentTitle = "RLHF Generator API - Swagger";
});
app.MapScalarApiReference(options =>
{
options
.WithTitle("RLHF Generator API")
.WithOpenApiRoutePattern(
"/swagger/{documentName}/swagger.json");
});
}
app.MapGet("/", () => "RLHF Generator API")
.WithName("GetApiRoot")
.WithSummary("Get API status")
.WithDescription("Returns a simple response confirming that the RLHF Generator API is running.")
.WithTags("System")
.Produces<string>(StatusCodes.Status200OK, "text/plain");
app.MapPost(
"/api/lm-studio/complete",
async (CompleteTextRequest? request, LmStudioClient client, CancellationToken cancellationToken) =>
{
if (request is null
|| string.IsNullOrWhiteSpace(request.Text)
|| request.IsFragment is null)
{
return Results.BadRequest();
}
if (!SentenceTenses.TryNormalize(request.Tense, out string? normalizedTense))
{
return Results.BadRequest();
}
bool hasProtectedMedicalTerm = request.ProtectedMedicalTerm is not null;
bool hasAllowedMedicalForms = request.AllowedMedicalForms is not null;
if (hasProtectedMedicalTerm != hasAllowedMedicalForms)
{
return Results.BadRequest(
"ProtectedMedicalTerm and AllowedMedicalForms must either both be provided or both be omitted.");
}
string? protectedMedicalTerm = null;
IReadOnlyList<string>? allowedMedicalForms = null;
if (hasProtectedMedicalTerm)
{
protectedMedicalTerm = request.ProtectedMedicalTerm!.Trim();
if (protectedMedicalTerm.Length == 0)
{
return Results.BadRequest("ProtectedMedicalTerm cannot be empty or whitespace.");
}
if (request.AllowedMedicalForms!.Count == 0)
{
return Results.BadRequest("AllowedMedicalForms must contain at least one form.");
}
string[] trimmedForms = new string[request.AllowedMedicalForms.Count];
HashSet<string> uniqueForms = new(StringComparer.OrdinalIgnoreCase);
for (int index = 0; index < request.AllowedMedicalForms.Count; index++)
{
string? form = request.AllowedMedicalForms[index];
if (string.IsNullOrWhiteSpace(form))
{
return Results.BadRequest(
$"AllowedMedicalForms[{index}] cannot be null, empty, or whitespace.");
}
string trimmedForm = form.Trim();
if (!uniqueForms.Add(trimmedForm))
{
return Results.BadRequest(
$"AllowedMedicalForms contains duplicate form '{trimmedForm}'.");
}
trimmedForms[index] = trimmedForm;
}
if (!string.Equals(
protectedMedicalTerm,
trimmedForms[0],
StringComparison.OrdinalIgnoreCase))
{
return Results.BadRequest(
"ProtectedMedicalTerm must exactly match the first AllowedMedicalForms value.");
}
allowedMedicalForms = trimmedForms;
}
string completion = await client.CompleteAsync(
request.Text,
normalizedTense,
request.IsFragment.Value,
protectedMedicalTerm: protectedMedicalTerm,
allowedMedicalForms: allowedMedicalForms,
cancellationToken: cancellationToken);
return Results.Text(completion, "text/plain");
})
.WithName("CompleteTextWithLmStudio")
.WithSummary("Complete text with LM Studio")
.WithDescription("Returns completed text without writing to the database. Tense is optional and supports past, present, and future. isFragment is required: true means a notes-field fragment with an omitted singular subject; false means the input contains its explicit subject.")
.WithTags("LM Studio")
.Accepts<CompleteTextRequest>("application/json")
.Produces<string>(StatusCodes.Status200OK, "text/plain")
.Produces(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status500InternalServerError);
app.MapPost(
"/api/keywords/load",
async (KeywordLoadService service, CancellationToken cancellationToken) =>
{
KeywordLoadResult result = await service.LoadAsync(cancellationToken);
return Results.Ok(new KeywordLoadResponse(
result.SourceKeywordCount,
result.SourceVariantCount,
result.ExistingKeywordCount,
result.ExistingVariantCount,
result.InsertedKeywordCount,
result.InsertedVariantCount));
})
.WithName("LoadKeywords")
.WithSummary("Load keyword catalog")
.WithDescription("Additively and idempotently loads missing keywords and variants from category files. Does not backfill TrainingItem rows.")
.WithTags("Keywords")
.Produces<KeywordLoadResponse>(StatusCodes.Status200OK, "application/json")
.Produces(StatusCodes.Status500InternalServerError);
app.MapPost(
"/api/training-items/backfill-keywords",
async (
TrainingItemKeywordBackfillService service,
CancellationToken cancellationToken,
bool dryRun = true,
int batchSize = TrainingItemKeywordBackfillService.DefaultBatchSize) =>
{
if (batchSize is < 1 or > TrainingItemKeywordBackfillService.MaximumBatchSize)
{
return Results.BadRequest(
$"batchSize must be between 1 and {TrainingItemKeywordBackfillService.MaximumBatchSize}.");
}
try
{
TrainingItemKeywordBackfillResult result = await service.BackfillAsync(
dryRun,
batchSize,
cancellationToken);
return Results.Ok(new TrainingItemKeywordBackfillResponse(
result.DryRun,
result.TotalTrainingItemCount,
result.AlreadyAssignedCount,
result.ProcessedCount,
result.UniqueMatchCount,
result.UpdatedCount,
result.UnmatchedCount,
result.AmbiguousCount));
}
catch (KeywordsNotLoadedException exception)
{
return Results.Conflict(exception.Message);
}
})
.WithName("BackfillTrainingItemKeywords")
.WithSummary("Backfill training-item keywords")
.WithDescription("Matches existing TrainingItem.Value text and assigns KeywordId only for one unique concept. Unmatched and ambiguous rows remain null, dry-run is the default, and generated text is not modified.")
.WithTags("Keywords")
.Produces<TrainingItemKeywordBackfillResponse>(StatusCodes.Status200OK, "application/json")
.Produces(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status409Conflict)
.Produces(StatusCodes.Status500InternalServerError);
app.MapPost(
"/api/generation/run",
async (TrainingGenerationService service, CancellationToken cancellationToken) =>
{
GenerationRunResult result = await service.GenerateAsync(cancellationToken);
return Results.Ok(new GenerationRunResponse(
result.RunId,
result.Status,
result.RowsCommittedThisRequest,
result.TotalCommittedRows,
result.TotalExpectedRows,
result.Resumed));
})
.WithName("RunTrainingGeneration")
.WithSummary("Generate training items")
.WithDescription("Starts or resumes compatible deterministic generation. Committed batches remain durable, and the HTTP request remains open until generation succeeds, is cancelled, or fails.")
.WithTags("Training Generation")
.Produces<GenerationRunResponse>(StatusCodes.Status200OK, "application/json")
.Produces(StatusCodes.Status500InternalServerError);
app.MapPost(
"/api/training-items/reset",
async (TrainingItemResetService service, CancellationToken cancellationToken) =>
{
await service.ResetAsync(cancellationToken);
return Results.Ok(new TrainingItemsResetResponse(true));
})
.WithName("ResetTrainingItems")
.WithSummary("Reset training items")
.WithDescription("Truncates TrainingItem, resets its identity, and clears generation-run checkpoints. Category data is not changed.")
.WithTags("Training Generation")
.Produces<TrainingItemsResetResponse>(StatusCodes.Status200OK, "application/json")
.Produces(StatusCodes.Status500InternalServerError);
app.Run();
public partial class Program;