| @@ -5,15 +5,23 @@ using System.Text; | |||
| namespace LLama.Abstractions | |||
| { | |||
| /// <summary> | |||
| /// Transform history to plain text and vice versa. | |||
| /// </summary> | |||
| public interface IHistoryTransform | |||
| { | |||
| /// <summary> | |||
| /// Convert a ChatHistory instance to plain text. | |||
| /// </summary> | |||
| /// <param name="history">The ChatHistory instance</param> | |||
| /// <returns></returns> | |||
| string HistoryToText(ChatHistory history); | |||
| /// <summary> | |||
| /// | |||
| /// Converts plain text to a ChatHistory instance. | |||
| /// </summary> | |||
| /// <param name="history">The existing history.</param> | |||
| /// <param name="role"></param> | |||
| /// <param name="text"></param> | |||
| /// <param name="role">The role for the author.</param> | |||
| /// <param name="text">The chat history as plain text.</param> | |||
| /// <returns>The updated history.</returns> | |||
| ChatHistory TextToHistory(AuthorRole role, string text); | |||
| } | |||
| @@ -6,9 +6,22 @@ using System.Threading; | |||
| namespace LLama.Abstractions | |||
| { | |||
| /// <summary> | |||
| /// A high level interface for LLama models. | |||
| /// </summary> | |||
| public interface ILLamaExecutor | |||
| { | |||
| /// <summary> | |||
| /// The loaded model for this executor. | |||
| /// </summary> | |||
| public LLamaModel Model { get; } | |||
| /// <summary> | |||
| /// Infers a response from the model. | |||
| /// </summary> | |||
| /// <param name="text">Your prompt</param> | |||
| /// <param name="inferenceParams">Any additional parameters</param> | |||
| /// <param name="token">A cancellation token.</param> | |||
| /// <returns></returns> | |||
| IEnumerable<string> Infer(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); | |||
| IAsyncEnumerable<string> InferAsync(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); | |||
| @@ -4,9 +4,22 @@ using System.Text; | |||
| namespace LLama.Abstractions | |||
| { | |||
| /// <summary> | |||
| /// Takes a stream of tokens and transforms them. | |||
| /// </summary> | |||
| public interface ITextStreamTransform | |||
| { | |||
| /// <summary> | |||
| /// Takes a stream of tokens and transforms them, returning a new stream of tokens. | |||
| /// </summary> | |||
| /// <param name="tokens"></param> | |||
| /// <returns></returns> | |||
| IEnumerable<string> Transform(IEnumerable<string> tokens); | |||
| /// <summary> | |||
| /// Takes a stream of tokens and transforms them, returning a new stream of tokens asynchronously. | |||
| /// </summary> | |||
| /// <param name="tokens"></param> | |||
| /// <returns></returns> | |||
| IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens); | |||
| } | |||
| } | |||
| @@ -4,8 +4,22 @@ using System.Text; | |||
| namespace LLama.Abstractions | |||
| { | |||
| /// <summary> | |||
| /// An interface for text transformations. | |||
| /// These can be used to compose a pipeline of text transformations, such as: | |||
| /// - Tokenization | |||
| /// - Lowercasing | |||
| /// - Punctuation removal | |||
| /// - Trimming | |||
| /// - etc. | |||
| /// </summary> | |||
| public interface ITextTransform | |||
| { | |||
| /// <summary> | |||
| /// Takes a string and transforms it. | |||
| /// </summary> | |||
| /// <param name="text"></param> | |||
| /// <returns></returns> | |||
| string Transform(string text); | |||
| } | |||
| } | |||
| @@ -13,6 +13,11 @@ namespace LLama | |||
| { | |||
| public class LLamaTransforms | |||
| { | |||
| /// <summary> | |||
| /// The default history transform. | |||
| /// Uses plain text with the following format: | |||
| /// [Author]: [Message] | |||
| /// </summary> | |||
| public class DefaultHistoryTransform : IHistoryTransform | |||
| { | |||
| private readonly string defaultUserName = "User"; | |||
| @@ -94,13 +99,15 @@ namespace LLama | |||
| { | |||
| } | |||
| /// <inheritdoc /> | |||
| public string Transform(string text) | |||
| { | |||
| return text.Trim(); | |||
| } | |||
| } | |||
| /// <summary> | |||
| /// A no-op text input transform. | |||
| /// </summary> | |||
| public class EmptyTextOutputStreamTransform : ITextStreamTransform | |||
| { | |||
| public IEnumerable<string> Transform(IEnumerable<string> tokens) | |||
| @@ -113,7 +120,9 @@ namespace LLama | |||
| return tokens; | |||
| } | |||
| } | |||
| /// <summary> | |||
| /// A text output transform that removes the keywords from the response. | |||
| /// </summary> | |||
| public class KeywordTextOutputStreamTransform : ITextStreamTransform | |||
| { | |||
| HashSet<string> _keywords; | |||
| @@ -135,7 +144,7 @@ namespace LLama | |||
| _maxKeywordLength = keywords.Select(x => x.Length).Max() + redundancyLength; | |||
| _removeAllMatchedTokens = removeAllMatchedTokens; | |||
| } | |||
| /// <inheritdoc /> | |||
| public IEnumerable<string> Transform(IEnumerable<string> tokens) | |||
| { | |||
| var window = new Queue<string>(); | |||
| @@ -167,7 +176,7 @@ namespace LLama | |||
| yield return window.Dequeue(); | |||
| } | |||
| } | |||
| /// <inheritdoc /> | |||
| public async IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens) | |||
| { | |||
| var window = new Queue<string>(); | |||