ChatSession - load & save
⚠️Warning: this example has been outdated for the latest version of LLamaSharp, please refer to this example to see how to save and load state for ChatSession
. If you are using some old versions of LLamaSharp, this example may help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 | using LLama.Common;
namespace LLama.Examples.Examples
{
public class SaveAndLoadSession
{
public static async Task Run()
{
string modelPath = UserSettings.GetModelPath();
var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var ex = new InteractiveExecutor(context);
var session = new ChatSession(ex);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The chat session has started. In this example, the prompt is printed for better visual result. Input \"save\" to save and reload the session.");
Console.ForegroundColor = ConsoleColor.White;
// show the prompt
Console.Write(prompt);
while (true)
{
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, prompt),
new InferenceParams()
{
Temperature = 0.6f,
AntiPrompts = new List<string> { "User:" }
}))
{
Console.Write(text);
}
Console.ForegroundColor = ConsoleColor.Green;
prompt = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
if (prompt == "save")
{
Console.Write("Preparing to save the state, please input the path you want to save it: ");
Console.ForegroundColor = ConsoleColor.Green;
var statePath = Console.ReadLine();
session.SaveSession(statePath);
Console.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Saved session!");
Console.ForegroundColor = ConsoleColor.White;
ex.Context.Dispose();
ex = new(new LLamaContext(model, parameters));
session = new ChatSession(ex);
session.LoadSession(statePath);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Loaded session!");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Now you can continue your session: ");
Console.ForegroundColor = ConsoleColor.Green;
prompt = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
}
}
}
}
|