From b911b2548b698363b48cec012d035803985fa2f5 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 15 Jun 2023 22:06:47 +0200 Subject: [PATCH 1/2] move interfaces into abstractions folder --- LLama/{ => Abstractions}/IHistoryTransform.cs | 2 +- LLama/{ => Abstractions}/ILLamaExecutor.cs | 2 +- LLama/{ => Abstractions}/ITextStreamTransform.cs | 2 +- LLama/{ => Abstractions}/ITextTransform.cs | 2 +- LLama/ChatSession.cs | 3 ++- LLama/LLamaExecutorBase.cs | 3 ++- LLama/LLamaStatelessExecutor.cs | 3 ++- LLama/LLamaTransforms.cs | 3 ++- 8 files changed, 12 insertions(+), 8 deletions(-) rename LLama/{ => Abstractions}/IHistoryTransform.cs (94%) rename LLama/{ => Abstractions}/ILLamaExecutor.cs (94%) rename LLama/{ => Abstractions}/ITextStreamTransform.cs (90%) rename LLama/{ => Abstractions}/ITextTransform.cs (84%) diff --git a/LLama/IHistoryTransform.cs b/LLama/Abstractions/IHistoryTransform.cs similarity index 94% rename from LLama/IHistoryTransform.cs rename to LLama/Abstractions/IHistoryTransform.cs index 6da20da3..0768369a 100644 --- a/LLama/IHistoryTransform.cs +++ b/LLama/Abstractions/IHistoryTransform.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.Text; -namespace LLama +namespace LLama.Abstractions { public interface IHistoryTransform { diff --git a/LLama/ILLamaExecutor.cs b/LLama/Abstractions/ILLamaExecutor.cs similarity index 94% rename from LLama/ILLamaExecutor.cs rename to LLama/Abstractions/ILLamaExecutor.cs index b8585d7c..485e1e44 100644 --- a/LLama/ILLamaExecutor.cs +++ b/LLama/Abstractions/ILLamaExecutor.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Text; using System.Threading; -namespace LLama +namespace LLama.Abstractions { public interface ILLamaExecutor { diff --git a/LLama/ITextStreamTransform.cs b/LLama/Abstractions/ITextStreamTransform.cs similarity index 90% rename from LLama/ITextStreamTransform.cs rename to LLama/Abstractions/ITextStreamTransform.cs index e700d972..7e1bbdf8 100644 --- a/LLama/ITextStreamTransform.cs +++ b/LLama/Abstractions/ITextStreamTransform.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace LLama +namespace LLama.Abstractions { public interface ITextStreamTransform { diff --git a/LLama/ITextTransform.cs b/LLama/Abstractions/ITextTransform.cs similarity index 84% rename from LLama/ITextTransform.cs rename to LLama/Abstractions/ITextTransform.cs index a7945ce9..731a5129 100644 --- a/LLama/ITextTransform.cs +++ b/LLama/Abstractions/ITextTransform.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace LLama +namespace LLama.Abstractions { public interface ITextTransform { 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/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index 715f6b2d..a77704fc 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..f9957b46 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; From 65925eac4f9bc92390540286d021263dfa79bc06 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 15 Jun 2023 22:23:58 +0200 Subject: [PATCH 2/2] Added documentation for the interfaces --- LLama/Abstractions/IHistoryTransform.cs | 16 ++++++++++++---- LLama/Abstractions/ILLamaExecutor.cs | 13 +++++++++++++ LLama/Abstractions/ITextStreamTransform.cs | 13 +++++++++++++ LLama/Abstractions/ITextTransform.cs | 14 ++++++++++++++ LLama/LLamaTransforms.cs | 19 ++++++++++++++----- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/LLama/Abstractions/IHistoryTransform.cs b/LLama/Abstractions/IHistoryTransform.cs index 0768369a..75a78bc5 100644 --- a/LLama/Abstractions/IHistoryTransform.cs +++ b/LLama/Abstractions/IHistoryTransform.cs @@ -5,15 +5,23 @@ 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 existing history. - /// - /// + /// 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 index 485e1e44..b1712ce5 100644 --- a/LLama/Abstractions/ILLamaExecutor.cs +++ b/LLama/Abstractions/ILLamaExecutor.cs @@ -6,9 +6,22 @@ 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 index 7e1bbdf8..af083564 100644 --- a/LLama/Abstractions/ITextStreamTransform.cs +++ b/LLama/Abstractions/ITextStreamTransform.cs @@ -4,9 +4,22 @@ 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 index 731a5129..c165e807 100644 --- a/LLama/Abstractions/ITextTransform.cs +++ b/LLama/Abstractions/ITextTransform.cs @@ -4,8 +4,22 @@ 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/LLamaTransforms.cs b/LLama/LLamaTransforms.cs index f9957b46..1a2dccd3 100644 --- a/LLama/LLamaTransforms.cs +++ b/LLama/LLamaTransforms.cs @@ -13,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"; @@ -94,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) @@ -113,7 +120,9 @@ namespace LLama return tokens; } } - + /// + /// A text output transform that removes the keywords from the response. + /// public class KeywordTextOutputStreamTransform : ITextStreamTransform { HashSet _keywords; @@ -135,7 +144,7 @@ namespace LLama _maxKeywordLength = keywords.Select(x => x.Length).Max() + redundancyLength; _removeAllMatchedTokens = removeAllMatchedTokens; } - + /// public IEnumerable Transform(IEnumerable tokens) { var window = new Queue(); @@ -167,7 +176,7 @@ namespace LLama yield return window.Dequeue(); } } - + /// public async IAsyncEnumerable TransformAsync(IAsyncEnumerable tokens) { var window = new Queue();