Browse Source

Make text transform interfaces have explicit copy operation

tags/0.11.0
eublefar 1 year ago
parent
commit
6f76d77350
5 changed files with 48 additions and 6 deletions
  1. +6
    -0
      LLama/Abstractions/IHistoryTransform.cs
  2. +6
    -0
      LLama/Abstractions/ITextStreamTransform.cs
  3. +6
    -0
      LLama/Abstractions/ITextTransform.cs
  4. +6
    -6
      LLama/ChatSession.cs
  5. +24
    -0
      LLama/LLamaTransforms.cs

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

@@ -21,5 +21,11 @@ namespace LLama.Abstractions
/// <param name="text">The chat history as plain text.</param> /// <param name="text">The chat history as plain text.</param>
/// <returns>The updated history.</returns> /// <returns>The updated history.</returns>
ChatHistory TextToHistory(AuthorRole role, string text); ChatHistory TextToHistory(AuthorRole role, string text);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
IHistoryTransform Clone();
} }
} }

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

@@ -13,5 +13,11 @@ namespace LLama.Abstractions
/// <param name="tokens"></param> /// <param name="tokens"></param>
/// <returns></returns> /// <returns></returns>
IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens); IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextStreamTransform Clone();
} }
} }

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

@@ -17,5 +17,11 @@
/// <param name="text"></param> /// <param name="text"></param>
/// <returns></returns> /// <returns></returns>
string Transform(string text); string Transform(string text);

/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextTransform Clone();
} }
} }

+ 6
- 6
LLama/ChatSession.cs View File

@@ -189,9 +189,9 @@ public class ChatSession
} }
Executor.Context.LoadState(state.ContextState); Executor.Context.LoadState(state.ContextState);
History = new ChatHistory(state.History); History = new ChatHistory(state.History);
InputTransformPipeline = state.InputTransformPipeline.ToList();
OutputTransform = state.OutputTransform;
HistoryTransform = state.HistoryTransform;
InputTransformPipeline = state.InputTransformPipeline.Select(t => t.Clone()).ToList();
OutputTransform = state.OutputTransform.Clone();
HistoryTransform = state.HistoryTransform.Clone();
} }


/// <summary> /// <summary>
@@ -634,8 +634,8 @@ public record SessionState
ContextState = contextState; ContextState = contextState;
ExecutorState = executorState; ExecutorState = executorState;
History = history.Messages.ToArray(); History = history.Messages.ToArray();
InputTransformPipeline = inputTransformPipeline.ToArray();
OutputTransform = outputTransform;
HistoryTransform = historyTransform;
InputTransformPipeline = inputTransformPipeline.Select(t => t.Clone()).ToArray();
OutputTransform = outputTransform.Clone();
HistoryTransform = historyTransform.Clone();
} }
} }

+ 24
- 0
LLama/LLamaTransforms.cs View File

@@ -47,6 +47,12 @@ namespace LLama
_isInstructMode = isInstructMode; _isInstructMode = isInstructMode;
} }


/// <inheritdoc />
public IHistoryTransform Clone()
{
return new DefaultHistoryTransform(_userName, _assistantName, _systemName, _unknownName, _isInstructMode);
}

/// <inheritdoc /> /// <inheritdoc />
public virtual string HistoryToText(ChatHistory history) public virtual string HistoryToText(ChatHistory history)
{ {
@@ -116,6 +122,12 @@ namespace LLama
{ {
return text.Trim(); return text.Trim();
} }

/// <inheritdoc />
public ITextTransform Clone()
{
return new NaiveTextInputTransform();
}
} }


/// <summary> /// <summary>
@@ -129,6 +141,12 @@ namespace LLama
{ {
return tokens; return tokens;
} }

/// <inheritdoc />
public ITextStreamTransform Clone()
{
return new EmptyTextOutputStreamTransform();
}
} }


/// <summary> /// <summary>
@@ -157,6 +175,12 @@ namespace LLama
_removeAllMatchedTokens = removeAllMatchedTokens; _removeAllMatchedTokens = removeAllMatchedTokens;
} }


/// <inheritdoc />
public ITextStreamTransform Clone()
{
return new KeywordTextOutputStreamTransform(_keywords, _maxKeywordLength, _removeAllMatchedTokens);
}

/// <inheritdoc /> /// <inheritdoc />
public async IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens) public async IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens)
{ {


Loading…
Cancel
Save