Browse Source

update webapi example

tags/v0.4.2-preview
xbotter 2 years ago
parent
commit
2fe4ccfb58
No known key found for this signature in database GPG Key ID: D299220A7FE5CF1E
7 changed files with 69 additions and 38 deletions
  1. +2
    -2
      LLama.WebAPI/Controllers/ChatController.cs
  2. +1
    -0
      LLama.WebAPI/LLama.WebAPI.csproj
  3. +2
    -1
      LLama.WebAPI/Program.cs
  4. +0
    -34
      LLama.WebAPI/Services/ChatService.cs
  5. +56
    -0
      LLama.WebAPI/Services/StatefulChatService.cs
  6. +6
    -0
      LLama.WebAPI/Services/StatelessChatService.cs
  7. +2
    -1
      LLama.WebAPI/appsettings.json

+ 2
- 2
LLama.WebAPI/Controllers/ChatController.cs View File

@@ -9,11 +9,11 @@ namespace LLama.WebAPI.Controllers
[Route("[controller]")] [Route("[controller]")]
public class ChatController : ControllerBase public class ChatController : ControllerBase
{ {
private readonly ChatService _service;
private readonly StatefulChatService _service;
private readonly ILogger<ChatController> _logger; private readonly ILogger<ChatController> _logger;


public ChatController(ILogger<ChatController> logger, public ChatController(ILogger<ChatController> logger,
ChatService service)
StatefulChatService service)
{ {
_logger = logger; _logger = logger;
_service = service; _service = service;


+ 1
- 0
LLama.WebAPI/LLama.WebAPI.csproj View File

@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>


<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>




+ 2
- 1
LLama.WebAPI/Program.cs View File

@@ -9,7 +9,8 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();


builder.Services.AddSingleton<ChatService>();
builder.Services.AddSingleton<StatefulChatService>();
builder.Services.AddScoped<StatelessChatService>();


var app = builder.Build(); var app = builder.Build();




+ 0
- 34
LLama.WebAPI/Services/ChatService.cs View File

@@ -1,34 +0,0 @@
using LLama.OldVersion;
using LLama.WebAPI.Models;

namespace LLama.WebAPI.Services;

public class ChatService
{
private readonly ChatSession<LLamaModel> _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<LLamaModel>(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;
}
}

+ 56
- 0
LLama.WebAPI/Services/StatefulChatService.cs View File

@@ -0,0 +1,56 @@

using LLama.WebAPI.Models;
using Microsoft;

namespace LLama.WebAPI.Services;

public class StatefulChatService : IDisposable
{
private readonly ChatSession _session;
private readonly LLamaModel _model;
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.\n\n"
+ "User: ";

public StatefulChatService(IConfiguration configuration)
{
_model = new LLamaModel(new Common.ModelParams(configuration["ModelPath"], contextSize: 512));
_session = new ChatSession(new InteractiveExecutor(_model))
;//.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8));
}

public void Dispose()
{
_model?.Dispose();
}

public string Send(SendMessageInput input)
{
var userInput = input.Text;
if (!_continue)
{
userInput = SystemPrompt + userInput;
Console.Write(SystemPrompt);
_continue = true;
}

Console.ForegroundColor = ConsoleColor.Green;
Console.Write(input.Text);

Console.ForegroundColor = ConsoleColor.White;
var outputs = _session.Chat(userInput, new Common.InferenceParams()
{
RepeatPenalty = 1.0f,
AntiPrompts = new string[] { "User:" },
});
var result = "";
foreach (var output in outputs)
{
Console.Write(output);
result += output;
}

return result;
}
}

+ 6
- 0
LLama.WebAPI/Services/StatelessChatService.cs View File

@@ -0,0 +1,6 @@
namespace LLama.WebAPI.Services
{
public class StatelessChatService
{
}
}

+ 2
- 1
LLama.WebAPI/appsettings.json View File

@@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*"
"AllowedHosts": "*",
"ModelPath": "..\\..\\LLamaModel\\ggml-model-f32-q4_0.bin"
} }

Loading…
Cancel
Save