-
Notifications
You must be signed in to change notification settings - Fork 475
Chat session state management #560
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
Merged
SanftMonster
merged 13 commits into
SciSharp:master
from
eublefar:feature/chat-session-state-management
Mar 26, 2024
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
35153a7
Chat session Get/Load in-memory state operations, reset state ops for…
eublefar b2f7dbb
AddPromptAsync method for stateful executors, Chat session initialize…
eublefar 0763f30
Example chat session with preprocessing of chat history and reset ope…
eublefar e05d5d4
Remove resetting state ops and make SessionState.ExecutorState and Se…
eublefar af796fc
Change List types in executor state to arrays to enforce copy on get/…
eublefar 87fe982
Change method signature as suggested
eublefar 5f3803d
Make state editable by the user, add deepcopy to fields that require it
eublefar 6f76d77
Make text transform interfaces have explicit copy operation
eublefar a31391e
Polymorphic serialization for executor state and transforms
eublefar 00c873a
Avoid saving empty context state in binary format, it smh messes with…
eublefar d88f9e1
Return null executor state if it's serialized in an old way
eublefar 9440f15
Make process message method more flexible
eublefar b8cd5b7
loadTransforms flag for LoadSession methods
eublefar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| using LLama.Common; | ||
|
|
||
| namespace LLama.Examples.Examples; | ||
|
|
||
| public class ChatSessionWithRestart | ||
| { | ||
| public static async Task Run() | ||
| { | ||
| string modelPath = UserSettings.GetModelPath(); | ||
|
|
||
| var parameters = new ModelParams(modelPath) | ||
| { | ||
| ContextSize = 1024, | ||
| Seed = 1337, | ||
| GpuLayerCount = 5 | ||
| }; | ||
| using var model = LLamaWeights.LoadFromFile(parameters); | ||
| using var context = model.CreateContext(parameters); | ||
| var executor = new InteractiveExecutor(context); | ||
|
|
||
| var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json"); | ||
| ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory(); | ||
| ChatSession prototypeSession = | ||
| await ChatSession.InitializeSessionFromHistoryAsync(executor, chatHistory); | ||
| prototypeSession.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform( | ||
| new string[] { "User:", "Assistant:" }, | ||
| redundancyLength: 8)); | ||
| var resetState = prototypeSession.GetSessionState(); | ||
|
|
||
| ChatSession session = new ChatSession(executor); | ||
| session.LoadSession(resetState); | ||
|
|
||
| InferenceParams inferenceParams = new InferenceParams() | ||
| { | ||
| Temperature = 0.9f, | ||
| AntiPrompts = new List<string> { "User:" } | ||
| }; | ||
|
|
||
| Console.ForegroundColor = ConsoleColor.Yellow; | ||
| Console.WriteLine("The chat session has started. Write `save` to save session in memory." | ||
| + " Write `reset` to start from the last saved checkpoint"); | ||
|
|
||
| // show the prompt | ||
| Console.ForegroundColor = ConsoleColor.Green; | ||
| string userInput = Console.ReadLine() ?? ""; | ||
|
|
||
| while (userInput != "exit") | ||
| { | ||
| if(userInput == "reset") | ||
eublefar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| session.LoadSession(resetState); | ||
| Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.History)}"); | ||
| Console.ForegroundColor = ConsoleColor.Yellow; | ||
| Console.WriteLine("Session reset."); | ||
| } | ||
| else if (userInput == "save") | ||
| { | ||
| resetState = session.GetSessionState(); | ||
| Console.ForegroundColor = ConsoleColor.Yellow; | ||
| Console.WriteLine("Session saved."); | ||
| } | ||
| else if (userInput == "regenerate") | ||
| { | ||
| Console.ForegroundColor = ConsoleColor.Yellow; | ||
| Console.WriteLine("Regenerating last response ..."); | ||
|
|
||
| await foreach ( | ||
| var text | ||
| in session.RegenerateAssistantMessageAsync( | ||
| inferenceParams)) | ||
| { | ||
| Console.ForegroundColor = ConsoleColor.White; | ||
| Console.Write(text); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| await foreach ( | ||
| var text | ||
| in session.ChatAsync( | ||
| new ChatHistory.Message(AuthorRole.User, userInput), | ||
| inferenceParams)) | ||
| { | ||
| Console.ForegroundColor = ConsoleColor.White; | ||
| Console.Write(text); | ||
| } | ||
| } | ||
|
|
||
| Console.ForegroundColor = ConsoleColor.Green; | ||
| userInput = Console.ReadLine() ?? ""; | ||
|
|
||
| Console.ForegroundColor = ConsoleColor.White; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.