Browse Source

Merge pull request #26 from mlof/document-interfaces

Document interfaces
tags/v0.4.0
Rinne GitHub 2 years ago
parent
commit
08e668a313
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 129 additions and 68 deletions
  1. +28
    -0
      LLama/Abstractions/IHistoryTransform.cs
  2. +29
    -0
      LLama/Abstractions/ILLamaExecutor.cs
  3. +25
    -0
      LLama/Abstractions/ITextStreamTransform.cs
  4. +25
    -0
      LLama/Abstractions/ITextTransform.cs
  5. +2
    -1
      LLama/ChatSession.cs
  6. +0
    -20
      LLama/IHistoryTransform.cs
  7. +0
    -16
      LLama/ILLamaExecutor.cs
  8. +0
    -12
      LLama/ITextStreamTransform.cs
  9. +0
    -11
      LLama/ITextTransform.cs
  10. +2
    -1
      LLama/LLamaExecutorBase.cs
  11. +2
    -1
      LLama/LLamaStatelessExecutor.cs
  12. +16
    -6
      LLama/LLamaTransforms.cs

+ 28
- 0
LLama/Abstractions/IHistoryTransform.cs View File

@@ -0,0 +1,28 @@
using LLama.Common;
using System;
using System.Collections.Generic;
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="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);
}
}

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

@@ -0,0 +1,29 @@
using LLama.Common;
using System;
using System.Collections.Generic;
using System.Text;
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);
}
}

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

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
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);
}
}

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

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
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);
}
}

+ 2
- 1
LLama/ChatSession.cs View File

@@ -1,4 +1,5 @@
using LLama.Common;
using LLama.Abstractions;
using LLama.Common;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;


+ 0
- 20
LLama/IHistoryTransform.cs View File

@@ -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);
/// <summary>
///
/// </summary>
/// <param name="history">The existing history.</param>
/// <param name="role"></param>
/// <param name="text"></param>
/// <returns>The updated history.</returns>
ChatHistory TextToHistory(AuthorRole role, string text);
}
}

+ 0
- 16
LLama/ILLamaExecutor.cs View File

@@ -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<string> Infer(string text, InferenceParams? inferenceParams = null, CancellationToken token = default);

IAsyncEnumerable<string> InferAsync(string text, InferenceParams? inferenceParams = null, CancellationToken token = default);
}
}

+ 0
- 12
LLama/ITextStreamTransform.cs View File

@@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LLama
{
public interface ITextStreamTransform
{
IEnumerable<string> Transform(IEnumerable<string> tokens);
IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens);
}
}

+ 0
- 11
LLama/ITextTransform.cs View File

@@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LLama
{
public interface ITextTransform
{
string Transform(string text);
}
}

+ 2
- 1
LLama/LLamaExecutorBase.cs View File

@@ -1,4 +1,5 @@
using LLama.Common;
using LLama.Abstractions;
using LLama.Common;
using LLama.Exceptions;
using LLama.Native;
using System;


+ 2
- 1
LLama/LLamaStatelessExecutor.cs View File

@@ -1,4 +1,5 @@
using LLama.Common;
using LLama.Abstractions;
using LLama.Common;
using LLama.Native;
using System;
using System.Collections.Generic;


+ 16
- 6
LLama/LLamaTransforms.cs View File

@@ -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
{
/// <summary>
/// The default history transform.
/// Uses plain text with the following format:
/// [Author]: [Message]
/// </summary>
public class DefaultHistoryTransform : IHistoryTransform
{
private readonly string defaultUserName = "User";
@@ -93,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)
@@ -112,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;
@@ -134,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>();
@@ -166,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