Browse Source

Added documentation for the interfaces

tags/v0.4.0
Marcel 2 years ago
parent
commit
65925eac4f
5 changed files with 66 additions and 9 deletions
  1. +12
    -4
      LLama/Abstractions/IHistoryTransform.cs
  2. +13
    -0
      LLama/Abstractions/ILLamaExecutor.cs
  3. +13
    -0
      LLama/Abstractions/ITextStreamTransform.cs
  4. +14
    -0
      LLama/Abstractions/ITextTransform.cs
  5. +14
    -5
      LLama/LLamaTransforms.cs

+ 12
- 4
LLama/Abstractions/IHistoryTransform.cs View File

@@ -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);
}


+ 13
- 0
LLama/Abstractions/ILLamaExecutor.cs View File

@@ -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);


+ 13
- 0
LLama/Abstractions/ITextStreamTransform.cs View File

@@ -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);
}
}

+ 14
- 0
LLama/Abstractions/ITextTransform.cs View File

@@ -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);
}
}

+ 14
- 5
LLama/LLamaTransforms.cs View File

@@ -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>();


Loading…
Cancel
Save