Skip to content

Commit 21c36cb

Browse files
committed
Added WebAPI.
1 parent e8ba691 commit 21c36cb

File tree

10 files changed

+145
-3
lines changed

10 files changed

+145
-3
lines changed

LLama.Examples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
if(choice == 0)
88
{
9-
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:" });
9+
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:" });
1010
chat.Run();
1111
}
1212
else if(choice == 1)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using LLama.WebAPI.Models;
2+
using LLama.WebAPI.Services;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
6+
namespace LLama.WebAPI.Controllers
7+
{
8+
[ApiController]
9+
[Route("[controller]")]
10+
public class ChatController : ControllerBase
11+
{
12+
private readonly ChatService _service;
13+
private readonly ILogger<ChatController> _logger;
14+
15+
public ChatController(ILogger<ChatController> logger,
16+
ChatService service)
17+
{
18+
_logger = logger;
19+
_service = service;
20+
}
21+
22+
[HttpPost("Send")]
23+
public string SendMessage([FromBody] SendMessageInput input)
24+
{
25+
return _service.Send(input);
26+
}
27+
}
28+
}

LLama.WebAPI/LLama.WebAPI.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\LLama\LLamaSharp.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace LLama.WebAPI.Models;
2+
3+
public class SendMessageInput
4+
{
5+
public string Text { get; set; }
6+
}

LLama.WebAPI/Program.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using LLama.WebAPI.Services;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
7+
builder.Services.AddControllers();
8+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
9+
builder.Services.AddEndpointsApiExplorer();
10+
builder.Services.AddSwaggerGen();
11+
12+
builder.Services.AddSingleton<ChatService>();
13+
14+
var app = builder.Build();
15+
16+
// Configure the HTTP request pipeline.
17+
if (app.Environment.IsDevelopment())
18+
{
19+
app.UseSwagger();
20+
app.UseSwaggerUI();
21+
}
22+
23+
app.UseAuthorization();
24+
25+
app.MapControllers();
26+
27+
app.Run();

LLama.WebAPI/Services/ChatService.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using LLama.WebAPI.Models;
2+
3+
namespace LLama.WebAPI.Services;
4+
5+
public class ChatService
6+
{
7+
private readonly ChatSession<LLamaModel> _session;
8+
9+
public ChatService()
10+
{
11+
LLamaModel model = new(new LLamaParams(model: @"ggml-model-q4_0.bin", n_ctx: 512, interactive: true, repeat_penalty: 1.0f, verbose_prompt: false));
12+
_session = new ChatSession<LLamaModel>(model)
13+
.WithPromptFile(@"Assets\chat-with-bob.txt")
14+
.WithAntiprompt(new string[] { "User:" });
15+
}
16+
17+
public string Send(SendMessageInput input)
18+
{
19+
Console.ForegroundColor = ConsoleColor.Green;
20+
Console.WriteLine(input.Text);
21+
22+
Console.ForegroundColor = ConsoleColor.White;
23+
var outputs = _session.Chat(input.Text);
24+
var result = "";
25+
foreach (var output in outputs)
26+
{
27+
Console.Write(output);
28+
result += output;
29+
}
30+
31+
return result;
32+
}
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

LLama.WebAPI/appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

LLama/LLamaSharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Platforms>AnyCPU;x64</Platforms>
99
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
1010

11-
<Version>0.1.1</Version>
11+
<Version>0.2.0</Version>
1212
<Authors>Yaohui Liu, Haiping Chen</Authors>
1313
<Company>SciSharp STACK</Company>
1414
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -26,7 +26,7 @@
2626
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2727
<PackageOutputPath>packages</PackageOutputPath>
2828
<Platforms>AnyCPU;x64</Platforms>
29-
<PackageId>LLama</PackageId>
29+
<PackageId>LLamaSharp</PackageId>
3030
<Configurations>Debug;Release;GPU</Configurations>
3131
</PropertyGroup>
3232

LLamaSharp.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LLamaSharp", "LLama\LLamaSh
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFDemo", "WPFDemo\WPFDemo.csproj", "{1E952A70-B720-4F76-9856-EC3B4259A80B}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LLama.WebAPI", "LLama.WebAPI\LLama.WebAPI.csproj", "{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,18 @@ Global
6971
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|Any CPU.Build.0 = Release|Any CPU
7072
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.ActiveCfg = Release|Any CPU
7173
{1E952A70-B720-4F76-9856-EC3B4259A80B}.Release|x64.Build.0 = Release|Any CPU
74+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.ActiveCfg = Debug|Any CPU
77+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Debug|x64.Build.0 = Debug|Any CPU
78+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
79+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|Any CPU.Build.0 = Debug|Any CPU
80+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|x64.ActiveCfg = Debug|Any CPU
81+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.GPU|x64.Build.0 = Debug|Any CPU
82+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|Any CPU.Build.0 = Release|Any CPU
84+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|x64.ActiveCfg = Release|Any CPU
85+
{D3CEC57A-9027-4DA4-AAAC-612A1EB50ADF}.Release|x64.Build.0 = Release|Any CPU
7286
EndGlobalSection
7387
GlobalSection(SolutionProperties) = preSolution
7488
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)