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.
This commit is contained in:
parent
7139281e4d
commit
2abc2ecfca
|
@ -1,14 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Import Project="..\LLama\LLamaSharp.Runtime.targets" />
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
public class SendMessageInput
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public string Text { get; set; } = "";
|
||||
}
|
||||
|
||||
public class HistoryInput
|
||||
{
|
||||
public List<HistoryItem> Messages { get; set; }
|
||||
public List<HistoryItem> 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; } = "";
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ builder.Services.AddSingleton<StatefulChatService>();
|
|||
builder.Services.AddScoped<StatelessChatService>();
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseRouting();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
@ -23,6 +24,9 @@ if (app.Environment.IsDevelopment())
|
|||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
_ = endpoints.MapControllers();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
|
|
@ -9,13 +9,14 @@ public class StatefulChatService : IDisposable
|
|||
{
|
||||
private readonly ChatSession _session;
|
||||
private readonly LLamaContext _context;
|
||||
private readonly ILogger<StatefulChatService> _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<StatefulChatService> 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<string> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue