Make text transform interfaces have explicit copy operation

This commit is contained in:
eublefar 2024-03-17 12:37:02 +01:00
parent 5f3803d23c
commit 6f76d77350
5 changed files with 48 additions and 6 deletions

View File

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

View File

@ -13,5 +13,11 @@ namespace LLama.Abstractions
/// <param name="tokens"></param>
/// <returns></returns>
IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens);
/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextStreamTransform Clone();
}
}

View File

@ -17,5 +17,11 @@
/// <param name="text"></param>
/// <returns></returns>
string Transform(string text);
/// <summary>
/// Copy the transform.
/// </summary>
/// <returns></returns>
ITextTransform Clone();
}
}

View File

@ -189,9 +189,9 @@ public class ChatSession
}
Executor.Context.LoadState(state.ContextState);
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>
@ -634,8 +634,8 @@ public record SessionState
ContextState = contextState;
ExecutorState = executorState;
History = history.Messages.ToArray();
InputTransformPipeline = inputTransformPipeline.ToArray();
OutputTransform = outputTransform;
HistoryTransform = historyTransform;
InputTransformPipeline = inputTransformPipeline.Select(t => t.Clone()).ToArray();
OutputTransform = outputTransform.Clone();
HistoryTransform = historyTransform.Clone();
}
}

View File

@ -47,6 +47,12 @@ namespace LLama
_isInstructMode = isInstructMode;
}
/// <inheritdoc />
public IHistoryTransform Clone()
{
return new DefaultHistoryTransform(_userName, _assistantName, _systemName, _unknownName, _isInstructMode);
}
/// <inheritdoc />
public virtual string HistoryToText(ChatHistory history)
{
@ -116,6 +122,12 @@ namespace LLama
{
return text.Trim();
}
/// <inheritdoc />
public ITextTransform Clone()
{
return new NaiveTextInputTransform();
}
}
/// <summary>
@ -129,6 +141,12 @@ namespace LLama
{
return tokens;
}
/// <inheritdoc />
public ITextStreamTransform Clone()
{
return new EmptyTextOutputStreamTransform();
}
}
/// <summary>
@ -157,6 +175,12 @@ namespace LLama
_removeAllMatchedTokens = removeAllMatchedTokens;
}
/// <inheritdoc />
public ITextStreamTransform Clone()
{
return new KeywordTextOutputStreamTransform(_keywords, _maxKeywordLength, _removeAllMatchedTokens);
}
/// <inheritdoc />
public async IAsyncEnumerable<string> TransformAsync(IAsyncEnumerable<string> tokens)
{