From 21c36cbf803cd69e4a2bc9c541a5dda80cb54807 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Thu, 11 May 2023 21:45:34 -0500 Subject: [PATCH] Added WebAPI. --- LLama.Examples/Program.cs | 2 +- LLama.WebAPI/Controllers/ChatController.cs | 28 ++++++++++++++++++ LLama.WebAPI/LLama.WebAPI.csproj | 17 +++++++++++ LLama.WebAPI/Models/SendMessageInput.cs | 6 ++++ LLama.WebAPI/Program.cs | 27 ++++++++++++++++++ LLama.WebAPI/Services/ChatService.cs | 33 ++++++++++++++++++++++ LLama.WebAPI/appsettings.Development.json | 8 ++++++ LLama.WebAPI/appsettings.json | 9 ++++++ LLama/LLamaSharp.csproj | 4 +-- LLamaSharp.sln | 14 +++++++++ 10 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 LLama.WebAPI/Controllers/ChatController.cs create mode 100644 LLama.WebAPI/LLama.WebAPI.csproj create mode 100644 LLama.WebAPI/Models/SendMessageInput.cs create mode 100644 LLama.WebAPI/Program.cs create mode 100644 LLama.WebAPI/Services/ChatService.cs create mode 100644 LLama.WebAPI/appsettings.Development.json create mode 100644 LLama.WebAPI/appsettings.json diff --git a/LLama.Examples/Program.cs b/LLama.Examples/Program.cs index 494186a6..7bed702c 100644 --- a/LLama.Examples/Program.cs +++ b/LLama.Examples/Program.cs @@ -6,7 +6,7 @@ int choice = 3; if(choice == 0) { - ChatSession chat = new(@"D:\development\llama\weights\LLaMA\7B\ggml-model-q4_0.bin", @"D:\development\llama\llama.cpp\prompts\chat-with-bob.txt", new string[] { "User:" }); + ChatSession chat = new(@"C:\Users\haipi\Source\repos\ggml-model-q4_0.bin", @"C:\Users\haipi\Source\repos\SciSharp\LLamaSharp\LLama.Examples\Assets\chat-with-bob.txt", new string[] { "User:" }); chat.Run(); } else if(choice == 1) diff --git a/LLama.WebAPI/Controllers/ChatController.cs b/LLama.WebAPI/Controllers/ChatController.cs new file mode 100644 index 00000000..cf64a9e5 --- /dev/null +++ b/LLama.WebAPI/Controllers/ChatController.cs @@ -0,0 +1,28 @@ +using LLama.WebAPI.Models; +using LLama.WebAPI.Services; +using Microsoft.AspNetCore.Mvc; +using System; + +namespace LLama.WebAPI.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ChatController : ControllerBase + { + private readonly ChatService _service; + private readonly ILogger _logger; + + public ChatController(ILogger logger, + ChatService service) + { + _logger = logger; + _service = service; + } + + [HttpPost("Send")] + public string SendMessage([FromBody] SendMessageInput input) + { + return _service.Send(input); + } + } +} \ No newline at end of file diff --git a/LLama.WebAPI/LLama.WebAPI.csproj b/LLama.WebAPI/LLama.WebAPI.csproj new file mode 100644 index 00000000..a977b885 --- /dev/null +++ b/LLama.WebAPI/LLama.WebAPI.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/LLama.WebAPI/Models/SendMessageInput.cs b/LLama.WebAPI/Models/SendMessageInput.cs new file mode 100644 index 00000000..741b631f --- /dev/null +++ b/LLama.WebAPI/Models/SendMessageInput.cs @@ -0,0 +1,6 @@ +namespace LLama.WebAPI.Models; + +public class SendMessageInput +{ + public string Text { get; set; } +} diff --git a/LLama.WebAPI/Program.cs b/LLama.WebAPI/Program.cs new file mode 100644 index 00000000..33e8bf81 --- /dev/null +++ b/LLama.WebAPI/Program.cs @@ -0,0 +1,27 @@ +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(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/LLama.WebAPI/Services/ChatService.cs b/LLama.WebAPI/Services/ChatService.cs new file mode 100644 index 00000000..d4cc3cf5 --- /dev/null +++ b/LLama.WebAPI/Services/ChatService.cs @@ -0,0 +1,33 @@ +using LLama.WebAPI.Models; + +namespace LLama.WebAPI.Services; + +public class ChatService +{ + private readonly ChatSession _session; + + public ChatService() + { + LLamaModel model = new(new LLamaParams(model: @"ggml-model-q4_0.bin", n_ctx: 512, interactive: true, repeat_penalty: 1.0f, verbose_prompt: false)); + _session = new ChatSession(model) + .WithPromptFile(@"Assets\chat-with-bob.txt") + .WithAntiprompt(new string[] { "User:" }); + } + + public string Send(SendMessageInput input) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine(input.Text); + + Console.ForegroundColor = ConsoleColor.White; + var outputs = _session.Chat(input.Text); + var result = ""; + foreach (var output in outputs) + { + Console.Write(output); + result += output; + } + + return result; + } +} diff --git a/LLama.WebAPI/appsettings.Development.json b/LLama.WebAPI/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/LLama.WebAPI/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/LLama.WebAPI/appsettings.json b/LLama.WebAPI/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/LLama.WebAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/LLama/LLamaSharp.csproj b/LLama/LLamaSharp.csproj index 8c46129a..39a10c38 100644 --- a/LLama/LLamaSharp.csproj +++ b/LLama/LLamaSharp.csproj @@ -8,7 +8,7 @@ AnyCPU;x64 True - 0.1.1 + 0.2.0 Yaohui Liu, Haiping Chen SciSharp STACK true @@ -26,7 +26,7 @@ MIT packages AnyCPU;x64 - LLama + LLamaSharp Debug;Release;GPU diff --git a/LLamaSharp.sln b/LLamaSharp.sln index 3b6d6a7a..a8a5ad58 100644 --- a/LLamaSharp.sln +++ b/LLamaSharp.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp", "LLama\LLamaSh EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFDemo", "WPFDemo\WPFDemo.csproj", "{1E952A70-B720-4F76-9856-EC3B4259A80B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLama.WebAPI", "LLama.WebAPI\LLama.WebAPI.csproj", "{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -69,6 +71,18 @@ Global {1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|Any CPU.Build.0 = Release|Any CPU {1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.ActiveCfg = Release|Any CPU {1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.Build.0 = Release|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.ActiveCfg = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.Build.0 = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|Any CPU.ActiveCfg = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|Any CPU.Build.0 = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|x64.ActiveCfg = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|x64.Build.0 = Debug|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|Any CPU.Build.0 = Release|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|x64.ActiveCfg = Release|Any CPU + {D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE