|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Hosting;
|
|
using AISApp;
|
|
|
|
namespace AISApp
|
|
{
|
|
internal static class ProgramEntry
|
|
{
|
|
// Minimal REST server + static file serving
|
|
private sealed class ChatInput
|
|
{
|
|
[JsonPropertyName("message")] public string? Message { get; set; }
|
|
}
|
|
|
|
public static async Task Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
var app = builder.Build();
|
|
|
|
// Prepare static directory (relative to app base)
|
|
var staticRoot = Path.Combine(builder.Environment.ContentRootPath, "static");
|
|
|
|
if (!Directory.Exists(staticRoot)) Directory.CreateDirectory(staticRoot);
|
|
|
|
// Serve index.html at "/" and other assets from /static
|
|
var fileProvider = new PhysicalFileProvider(staticRoot);
|
|
|
|
var defaultFiles = new DefaultFilesOptions
|
|
{
|
|
FileProvider = fileProvider,
|
|
RequestPath = ""
|
|
};
|
|
defaultFiles.DefaultFileNames.Clear();
|
|
defaultFiles.DefaultFileNames.Add("index.html");
|
|
app.UseDefaultFiles(defaultFiles);
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
FileProvider = fileProvider,
|
|
RequestPath = ""
|
|
});
|
|
|
|
// Load system message from prompt.txt
|
|
string systemMessage = System.IO.File.ReadAllText("prompt.txt");
|
|
|
|
// Single AIS instance for the app
|
|
var ai = new AIS(model: "gemma", safeMode: false, systemMessage: systemMessage);
|
|
|
|
// Minimal chat endpoint
|
|
app.MapPost("/api/chat", async (HttpContext ctx) =>
|
|
{
|
|
try
|
|
{
|
|
var input = await JsonSerializer.DeserializeAsync<ChatInput>(ctx.Request.Body);
|
|
var prompt = input?.Message ?? string.Empty;
|
|
|
|
var result = await ai.ChatAsync(prompt);
|
|
object replyPayload = result is JsonNode jn ? jn : (object)(result?.ToString() ?? "");
|
|
|
|
await ctx.Response.WriteAsJsonAsync(new { ok = true, reply = replyPayload });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
await ctx.Response.WriteAsJsonAsync(new { ok = false, error = ex.Message });
|
|
}
|
|
});
|
|
|
|
await app.RunAsync();
|
|
}
|
|
}
|
|
}
|