|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
-
- using LLama.WebAPI.Models;
- using Microsoft;
- using System.Runtime.CompilerServices;
-
- namespace LLama.WebAPI.Services;
-
- public class StatefulChatService : IDisposable
- {
- private readonly ChatSession _session;
- private readonly LLamaContext _context;
- 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)
- {
- var @params = new Common.ModelParams(configuration["ModelPath"])
- {
- ContextSize = 512,
- };
-
- // todo: share weights from a central service
- using var weights = LLamaWeights.LoadFromFile(@params);
-
- _context = new LLamaContext(weights, @params);
-
- _session = new ChatSession(new InteractiveExecutor(_context));
- _session.History.AddMessage(Common.AuthorRole.System, SystemPrompt);
- }
-
- public void Dispose()
- {
- _context?.Dispose();
- }
-
- public async Task<string> Send(SendMessageInput input)
- {
- if (!_continue)
- {
- Console.Write(SystemPrompt);
- _continue = true;
- }
-
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(input.Text);
-
- Console.ForegroundColor = ConsoleColor.White;
- var outputs = _session.ChatAsync(
- new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text),
- new Common.InferenceParams()
- {
- RepeatPenalty = 1.0f,
- AntiPrompts = new string[] { "User:" },
- });
-
- var result = "";
- await foreach (var output in outputs)
- {
- Console.Write(output);
- result += output;
- }
-
- return result;
- }
-
- public async IAsyncEnumerable<string> SendStream(SendMessageInput input)
- {
- if (!_continue)
- {
- Console.Write(SystemPrompt);
- _continue = true;
- }
-
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(input.Text);
-
- Console.ForegroundColor = ConsoleColor.White;
- var outputs = _session.ChatAsync(
- new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text)
- , new Common.InferenceParams()
- {
- RepeatPenalty = 1.0f,
- AntiPrompts = new string[] { "User:" },
- });
-
- await foreach (var output in outputs)
- {
- Console.Write(output);
- yield return output;
- }
- }
- }
|