feat: initialize KwisatzHaderach .NET web application
This commit is contained in:
6
src/backend/KwisatzHaderach/Configuration/AIOptions.cs
Normal file
6
src/backend/KwisatzHaderach/Configuration/AIOptions.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace KwisatzHaderach.Configuration;
|
||||
|
||||
public record AIOptions
|
||||
{
|
||||
public required string Host { get; init; }
|
||||
}
|
||||
71
src/backend/KwisatzHaderach/Controllers/PromptController.cs
Normal file
71
src/backend/KwisatzHaderach/Controllers/PromptController.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
10
src/backend/KwisatzHaderach/Infrastructure/Api/ILLMClient.cs
Normal file
10
src/backend/KwisatzHaderach/Infrastructure/Api/ILLMClient.cs
Normal 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);
|
||||
}
|
||||
14
src/backend/KwisatzHaderach/KwisatzHaderach.csproj
Normal file
14
src/backend/KwisatzHaderach/KwisatzHaderach.csproj
Normal 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>
|
||||
7
src/backend/KwisatzHaderach/Models/PromptRequest.cs
Normal file
7
src/backend/KwisatzHaderach/Models/PromptRequest.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace KwisatzHaderach.Models;
|
||||
|
||||
public record PromptRequest(
|
||||
[property: JsonPropertyName("question")]string Question
|
||||
);
|
||||
38
src/backend/KwisatzHaderach/Program.cs
Normal file
38
src/backend/KwisatzHaderach/Program.cs
Normal 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();
|
||||
23
src/backend/KwisatzHaderach/Properties/launchSettings.json
Normal file
23
src/backend/KwisatzHaderach/Properties/launchSettings.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/backend/KwisatzHaderach/appsettings.Development.json
Normal file
11
src/backend/KwisatzHaderach/appsettings.Development.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AI": {
|
||||
"Host": "http://127.0.0.1:8000"
|
||||
}
|
||||
}
|
||||
9
src/backend/KwisatzHaderach/appsettings.json
Normal file
9
src/backend/KwisatzHaderach/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user