feat: initialize KwisatzHaderach .NET web application

This commit is contained in:
2025-09-20 15:17:35 +02:00
parent 5f3c31ec3f
commit 6e95b59a3e
11 changed files with 707 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
namespace KwisatzHaderach.Configuration;
public record AIOptions
{
public required string Host { get; init; }
}

View File

@@ -0,0 +1,71 @@
using KwisatzHaderach.Infrastructure.Api;
using KwisatzHaderach.Models;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;
using System.Text.Json;
namespace KwisatzHaderach.Controllers;
[ApiController]
[Route("[controller]")]
public class PromptController : ControllerBase
{
public record StreamChunk(string? Content);
[HttpPost("ask")]
public async IAsyncEnumerable<StreamChunk> GetPromptResponseAsync([FromBody] PromptRequest req,
ILLMClient llmClient,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
var jsonSerializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
HttpResponseMessage? llmResponse = null;
StreamChunk? errorChunk = null;
Exception? capturedException = null;
try
{
llmResponse = await llmClient.GetPromptResponseAsync(req, cancellationToken);
llmResponse.EnsureSuccessStatusCode();
}
catch (OperationCanceledException opEx)
{
capturedException = opEx;
}
catch (Exception ex)
{
errorChunk = new($"[ERROR] Error reading LLM response: {ex.Message}");
capturedException = ex;
}
if (capturedException != null || llmResponse == null)
{
if (errorChunk != null)
{
yield return errorChunk;
}
yield break;
}
using (llmResponse)
{
await using var responseStream = await llmResponse.Content.ReadAsStreamAsync(cancellationToken);
using var reader = new StreamReader(responseStream);
// Read the stream chunk by chunk until the server closes the connection.
char[] buffer = new char[1024]; // Read in chunks of 1KB
int bytesRead;
while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
// Get the string content that was just read.
string textChunk = new string(buffer, 0, bytesRead);
// Wrap the raw string into the StreamChunk object that the React client expects.
yield return new StreamChunk(textChunk);
}
}
yield break;
}
}

View File

@@ -0,0 +1,10 @@
using KwisatzHaderach.Models;
using Refit;
namespace KwisatzHaderach.Infrastructure.Api;
public interface ILLMClient
{
[Post("/ask-stream")]
Task<HttpResponseMessage> GetPromptResponseAsync([Body]PromptRequest req, CancellationToken cancellationToken);
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.9" />
<PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
using System.Text.Json.Serialization;
namespace KwisatzHaderach.Models;
public record PromptRequest(
[property: JsonPropertyName("question")]string Question
);

View File

@@ -0,0 +1,38 @@
using KwisatzHaderach.Configuration;
using KwisatzHaderach.Infrastructure.Api;
using Microsoft.Extensions.Options;
using Refit;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddOpenApi();
builder.Services.Configure<AIOptions>(
builder.Configuration.GetSection("AI")
);
builder.Services.AddRefitClient<ILLMClient>()
.ConfigureHttpClient((provider, client) =>
{
IOptions<AIOptions> aiOptions = provider.GetRequiredService<IOptions<AIOptions>>();
client.BaseAddress = new(aiOptions.Value.Host);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5187",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7056;http://localhost:5187",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AI": {
"Host": "http://127.0.0.1:8000"
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}