diff --git a/LLama/Abstractions/IHistoryTransform.cs b/LLama/Abstractions/IHistoryTransform.cs new file mode 100644 index 00000000..75a78bc5 --- /dev/null +++ b/LLama/Abstractions/IHistoryTransform.cs @@ -0,0 +1,28 @@ +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Text; + +namespace LLama.Abstractions +{ + /// + /// Transform history to plain text and vice versa. + /// + public interface IHistoryTransform + { + /// + /// Convert a ChatHistory instance to plain text. + /// + /// The ChatHistory instance + /// + string HistoryToText(ChatHistory history); + + /// + /// Converts plain text to a ChatHistory instance. + /// + /// The role for the author. + /// The chat history as plain text. + /// The updated history. + ChatHistory TextToHistory(AuthorRole role, string text); + } +} diff --git a/LLama/Abstractions/ILLamaExecutor.cs b/LLama/Abstractions/ILLamaExecutor.cs new file mode 100644 index 00000000..b1712ce5 --- /dev/null +++ b/LLama/Abstractions/ILLamaExecutor.cs @@ -0,0 +1,29 @@ +using LLama.Common; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace LLama.Abstractions +{ + /// + /// A high level interface for LLama models. + /// + public interface ILLamaExecutor + { + /// + /// The loaded model for this executor. + /// + public LLamaModel Model { get; } + /// + /// Infers a response from the model. + /// + /// Your prompt + /// Any additional parameters + /// A cancellation token. + /// + IEnumerable Infer(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); + + IAsyncEnumerable InferAsync(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); + } +} diff --git a/LLama/Abstractions/ITextStreamTransform.cs b/LLama/Abstractions/ITextStreamTransform.cs new file mode 100644 index 00000000..af083564 --- /dev/null +++ b/LLama/Abstractions/ITextStreamTransform.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LLama.Abstractions +{ + /// + /// Takes a stream of tokens and transforms them. + /// + public interface ITextStreamTransform + { + /// + /// Takes a stream of tokens and transforms them, returning a new stream of tokens. + /// + /// + /// + IEnumerable Transform(IEnumerable tokens); + /// + /// Takes a stream of tokens and transforms them, returning a new stream of tokens asynchronously. + /// + /// + /// + IAsyncEnumerable TransformAsync(IAsyncEnumerable tokens); + } +} diff --git a/LLama/Abstractions/ITextTransform.cs b/LLama/Abstractions/ITextTransform.cs new file mode 100644 index 00000000..c165e807 --- /dev/null +++ b/LLama/Abstractions/ITextTransform.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LLama.Abstractions +{ + /// + /// An interface for text transformations. + /// These can be used to compose a pipeline of text transformations, such as: + /// - Tokenization + /// - Lowercasing + /// - Punctuation removal + /// - Trimming + /// - etc. + /// + public interface ITextTransform + { + /// + /// Takes a string and transforms it. + /// + /// + /// + string Transform(string text); + } +} diff --git a/LLama/ChatSession.cs b/LLama/ChatSession.cs index 3ff36fc1..552a37cc 100644 --- a/LLama/ChatSession.cs +++ b/LLama/ChatSession.cs @@ -1,4 +1,5 @@ -using LLama.Common; +using LLama.Abstractions; +using LLama.Common; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; diff --git a/LLama/IHistoryTransform.cs b/LLama/IHistoryTransform.cs deleted file mode 100644 index 6da20da3..00000000 --- a/LLama/IHistoryTransform.cs +++ /dev/null @@ -1,20 +0,0 @@ -using LLama.Common; -using System; -using System.Collections.Generic; -using System.Text; - -namespace LLama -{ - public interface IHistoryTransform - { - string HistoryToText(ChatHistory history); - /// - /// - /// - /// The existing history. - /// - /// - /// The updated history. - ChatHistory TextToHistory(AuthorRole role, string text); - } -} diff --git a/LLama/ILLamaExecutor.cs b/LLama/ILLamaExecutor.cs deleted file mode 100644 index b8585d7c..00000000 --- a/LLama/ILLamaExecutor.cs +++ /dev/null @@ -1,16 +0,0 @@ -using LLama.Common; -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; - -namespace LLama -{ - public interface ILLamaExecutor - { - public LLamaModel Model { get; } - IEnumerable Infer(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); - - IAsyncEnumerable InferAsync(string text, InferenceParams? inferenceParams = null, CancellationToken token = default); - } -} diff --git a/LLama/ITextStreamTransform.cs b/LLama/ITextStreamTransform.cs deleted file mode 100644 index e700d972..00000000 --- a/LLama/ITextStreamTransform.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace LLama -{ - public interface ITextStreamTransform - { - IEnumerable Transform(IEnumerable tokens); - IAsyncEnumerable TransformAsync(IAsyncEnumerable tokens); - } -} diff --git a/LLama/ITextTransform.cs b/LLama/ITextTransform.cs deleted file mode 100644 index a7945ce9..00000000 --- a/LLama/ITextTransform.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace LLama -{ - public interface ITextTransform - { - string Transform(string text); - } -} diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index 98a8218f..800d9118 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -1,4 +1,5 @@ -using LLama.Common; +using LLama.Abstractions; +using LLama.Common; using LLama.Exceptions; using LLama.Native; using System; diff --git a/LLama/LLamaStatelessExecutor.cs b/LLama/LLamaStatelessExecutor.cs index 98a573eb..2008bf9c 100644 --- a/LLama/LLamaStatelessExecutor.cs +++ b/LLama/LLamaStatelessExecutor.cs @@ -1,4 +1,5 @@ -using LLama.Common; +using LLama.Abstractions; +using LLama.Common; using LLama.Native; using System; using System.Collections.Generic; diff --git a/LLama/LLamaTransforms.cs b/LLama/LLamaTransforms.cs index 6d349761..1a2dccd3 100644 --- a/LLama/LLamaTransforms.cs +++ b/LLama/LLamaTransforms.cs @@ -1,4 +1,5 @@ -using LLama.Common; +using LLama.Abstractions; +using LLama.Common; using Microsoft.VisualBasic; using System; using System.Collections.Generic; @@ -12,6 +13,11 @@ namespace LLama { public class LLamaTransforms { + /// + /// The default history transform. + /// Uses plain text with the following format: + /// [Author]: [Message] + /// public class DefaultHistoryTransform : IHistoryTransform { private readonly string defaultUserName = "User"; @@ -93,13 +99,15 @@ namespace LLama { } - + /// public string Transform(string text) { return text.Trim(); } } - + /// + /// A no-op text input transform. + /// public class EmptyTextOutputStreamTransform : ITextStreamTransform { public IEnumerable Transform(IEnumerable tokens) @@ -112,7 +120,9 @@ namespace LLama return tokens; } } - + /// + /// A text output transform that removes the keywords from the response. + /// public class KeywordTextOutputStreamTransform : ITextStreamTransform { HashSet _keywords; @@ -134,7 +144,7 @@ namespace LLama _maxKeywordLength = keywords.Select(x => x.Length).Max() + redundancyLength; _removeAllMatchedTokens = removeAllMatchedTokens; } - + /// public IEnumerable Transform(IEnumerable tokens) { var window = new Queue(); @@ -166,7 +176,7 @@ namespace LLama yield return window.Dequeue(); } } - + /// public async IAsyncEnumerable TransformAsync(IAsyncEnumerable tokens) { var window = new Queue();