From 2abc2ecfca12f8465bd4aa9840a279a83aca7348 Mon Sep 17 00:00:00 2001 From: Scot McConnaughay Date: Mon, 18 Dec 2023 00:38:05 -0700 Subject: [PATCH] Fixed WebAPI Upgraded to .NET8.0 Fixed logging (removed Console replaced with Ilogger) Fixed warnings (Null Strings, etc). The application now returns data from the back-end chat services. --- LLama.WebAPI/LLama.WebAPI.csproj | 52 ++++++++-------- LLama.WebAPI/Models/SendMessageInput.cs | 8 +-- LLama.WebAPI/Program.cs | 60 ++++++++++--------- LLama.WebAPI/Services/StatefulChatService.cs | 27 ++++----- LLama.WebAPI/Services/StatelessChatService.cs | 2 +- LLama.WebAPI/appsettings.Development.json | 16 ++--- 6 files changed, 84 insertions(+), 81 deletions(-) diff --git a/LLama.WebAPI/LLama.WebAPI.csproj b/LLama.WebAPI/LLama.WebAPI.csproj index a8c1179c..0946c30c 100644 --- a/LLama.WebAPI/LLama.WebAPI.csproj +++ b/LLama.WebAPI/LLama.WebAPI.csproj @@ -1,25 +1,27 @@ - - - - net6.0 - enable - enable - - - - - - - - - - - - - - true - PreserveNewest - Never - - - + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + true + PreserveNewest + Never + + + \ No newline at end of file diff --git a/LLama.WebAPI/Models/SendMessageInput.cs b/LLama.WebAPI/Models/SendMessageInput.cs index 11152ff8..dfee21b6 100644 --- a/LLama.WebAPI/Models/SendMessageInput.cs +++ b/LLama.WebAPI/Models/SendMessageInput.cs @@ -2,15 +2,15 @@ public class SendMessageInput { - public string Text { get; set; } + public string Text { get; set; } = ""; } public class HistoryInput { - public List Messages { get; set; } + public List Messages { get; set; } = []; public class HistoryItem { - public string Role { get; set; } - public string Content { get; set; } + public string Role { get; set; } = "User"; + public string Content { get; set; } = ""; } } \ No newline at end of file diff --git a/LLama.WebAPI/Program.cs b/LLama.WebAPI/Program.cs index 3f2de200..2d9d1123 100644 --- a/LLama.WebAPI/Program.cs +++ b/LLama.WebAPI/Program.cs @@ -1,28 +1,32 @@ -using LLama.WebAPI.Services; - -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. - -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - -builder.Services.AddSingleton(); -builder.Services.AddScoped(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); +using LLama.WebAPI.Services; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddSingleton(); +builder.Services.AddScoped(); + +var app = builder.Build(); +app.UseRouting(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.UseEndpoints(endpoints => +{ + _ = endpoints.MapControllers(); +}); + +app.Run(); diff --git a/LLama.WebAPI/Services/StatefulChatService.cs b/LLama.WebAPI/Services/StatefulChatService.cs index f45c98ee..ae2401c9 100644 --- a/LLama.WebAPI/Services/StatefulChatService.cs +++ b/LLama.WebAPI/Services/StatefulChatService.cs @@ -9,13 +9,14 @@ public class StatefulChatService : IDisposable { private readonly ChatSession _session; private readonly LLamaContext _context; + private readonly ILogger _logger; private bool _continue = false; private const string SystemPrompt = "Transcript of a dialog, where the User interacts with an Assistant. Assistant is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision."; - public StatefulChatService(IConfiguration configuration) + public StatefulChatService(IConfiguration configuration, ILogger logger) { - var @params = new Common.ModelParams(configuration["ModelPath"]) + var @params = new Common.ModelParams(configuration["ModelPath"]!) { ContextSize = 512, }; @@ -23,6 +24,7 @@ public class StatefulChatService : IDisposable // todo: share weights from a central service using var weights = LLamaWeights.LoadFromFile(@params); + _logger = logger; _context = new LLamaContext(weights, @params); _session = new ChatSession(new InteractiveExecutor(_context)); @@ -36,16 +38,13 @@ public class StatefulChatService : IDisposable public async Task Send(SendMessageInput input) { + if (!_continue) { - Console.Write(SystemPrompt); + _logger.LogInformation("Prompt: {text}", SystemPrompt); _continue = true; } - - Console.ForegroundColor = ConsoleColor.Green; - Console.Write(input.Text); - - Console.ForegroundColor = ConsoleColor.White; + _logger.LogInformation("Input: {text}", input.Text); var outputs = _session.ChatAsync( new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text), new Common.InferenceParams() @@ -57,7 +56,7 @@ public class StatefulChatService : IDisposable var result = ""; await foreach (var output in outputs) { - Console.Write(output); + _logger.LogInformation("Message: {output}", output); result += output; } @@ -68,16 +67,14 @@ public class StatefulChatService : IDisposable { if (!_continue) { - Console.Write(SystemPrompt); + _logger.LogInformation(SystemPrompt); _continue = true; } - Console.ForegroundColor = ConsoleColor.Green; - Console.Write(input.Text); + _logger.LogInformation(input.Text); - Console.ForegroundColor = ConsoleColor.White; var outputs = _session.ChatAsync( - new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text) + new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text!) , new Common.InferenceParams() { RepeatPenalty = 1.0f, @@ -86,7 +83,7 @@ public class StatefulChatService : IDisposable await foreach (var output in outputs) { - Console.Write(output); + _logger.LogInformation(output); yield return output; } } diff --git a/LLama.WebAPI/Services/StatelessChatService.cs b/LLama.WebAPI/Services/StatelessChatService.cs index 71da775f..3520c29b 100644 --- a/LLama.WebAPI/Services/StatelessChatService.cs +++ b/LLama.WebAPI/Services/StatelessChatService.cs @@ -12,7 +12,7 @@ namespace LLama.WebAPI.Services public StatelessChatService(IConfiguration configuration) { - var @params = new Common.ModelParams(configuration["ModelPath"]) + var @params = new Common.ModelParams(configuration["ModelPath"]!) { ContextSize = 512, }; diff --git a/LLama.WebAPI/appsettings.Development.json b/LLama.WebAPI/appsettings.Development.json index 0c208ae9..ff66ba6b 100644 --- a/LLama.WebAPI/appsettings.Development.json +++ b/LLama.WebAPI/appsettings.Development.json @@ -1,8 +1,8 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +}