Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional IHistoryTransform added to ChatSession.InitializeSessionFromHistoryAsync #711

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions LLama/ChatSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@
/// </summary>
/// <param name="executor">The executor for this session</param>
/// <param name="history">History for this session</param>
/// <returns></returns>
/// <param name="transform">History Transform for this session</param>
/// <returns>A new chat session.</returns>
public static async Task<ChatSession> InitializeSessionFromHistoryAsync(
ILLamaExecutor executor, ChatHistory history)
ILLamaExecutor executor, ChatHistory history, IHistoryTransform? transform = null)
{
if (executor is not StatefulExecutorBase statefulExecutor)
{
throw new ArgumentException("Executor must have a StatefulExecutorBase", nameof(executor));
}
var session = new ChatSession(executor, history);
if (transform != null)
{
session = session.WithHistoryTransform(transform);
}

await statefulExecutor.PrefillPromptAsync(session.HistoryTransform.HistoryToText(history));
return session;
}
Expand Down Expand Up @@ -676,7 +682,7 @@
Directory.CreateDirectory(path);

string modelStateFilePath = Path.Combine(path, ChatSession.MODEL_STATE_FILENAME);
var bytes = ContextState?.ToByteArray();

Check warning on line 685 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (linux-release)

'LLamaContext.State.ToByteArray()' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'

Check warning on line 685 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

'LLamaContext.State.ToByteArray()' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'

Check warning on line 685 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

'LLamaContext.State.ToByteArray()' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'
if (bytes is not null)
{
File.WriteAllBytes(modelStateFilePath, bytes);
Expand Down Expand Up @@ -718,7 +724,7 @@

string modelStateFilePath = Path.Combine(path, ChatSession.MODEL_STATE_FILENAME);
var contextState = File.Exists(modelStateFilePath) ?
State.FromByteArray(File.ReadAllBytes(modelStateFilePath))

Check warning on line 727 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (linux-release)

'LLamaContext.State.FromByteArray(byte[])' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'

Check warning on line 727 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

'LLamaContext.State.FromByteArray(byte[])' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'

Check warning on line 727 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

'LLamaContext.State.FromByteArray(byte[])' is obsolete: 'It is not generally safe to convert a state into a byte array - it will fail if the state is very large'
: null;

string executorStateFilepath = Path.Combine(path, ChatSession.EXECUTOR_STATE_FILENAME);
Expand Down Expand Up @@ -774,10 +780,10 @@

return new SessionState(
contextState,
executorState,

Check warning on line 783 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (linux-release)

Possible null reference argument for parameter 'executorState' in 'SessionState.SessionState(State? contextState, ExecutorBaseState executorState, ChatHistory history, List<ITextTransform> inputTransformPipeline, ITextStreamTransform outputTransform, IHistoryTransform historyTransform)'.

Check warning on line 783 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

Possible null reference argument for parameter 'executorState' in 'SessionState.SessionState(State? contextState, ExecutorBaseState executorState, ChatHistory history, List<ITextTransform> inputTransformPipeline, ITextStreamTransform outputTransform, IHistoryTransform historyTransform)'.

Check warning on line 783 in LLama/ChatSession.cs

View workflow job for this annotation

GitHub Actions / Test (windows-release)

Possible null reference argument for parameter 'executorState' in 'SessionState.SessionState(State? contextState, ExecutorBaseState executorState, ChatHistory history, List<ITextTransform> inputTransformPipeline, ITextStreamTransform outputTransform, IHistoryTransform historyTransform)'.
history,
inputTransforms.ToList(),
outputTransform,
historyTransform);
}
}
}
Loading