Skip to content

Commit e17c8df

Browse files
authored
Merge pull request #604 from AsakusaRinne/fix_readme_example
docs: update the example in readme.
2 parents 3b2836e + a0dac62 commit e17c8df

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

README.md

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,56 +94,49 @@ LLamaSharp provides two ways to run inference: `LLamaExecutor` and `ChatSession`
9494
using LLama.Common;
9595
using LLama;
9696

97-
string modelPath = "<Your model path>"; // change it to your own model path
98-
var prompt = "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.\r\n\r\nUser: Hello, Bob.\r\nBob: Hello. How may I help you today?\r\nUser: Please tell me the largest city in Europe.\r\nBob: Sure. The largest city in Europe is Moscow, the capital of Russia.\r\nUser:"; // use the "chat-with-bob" prompt here.
97+
string modelPath = @"<Your Model Path>"; // change it to your own model path.
9998
100-
// Load a model
10199
var parameters = new ModelParams(modelPath)
102100
{
103-
ContextSize = 1024,
104-
Seed = 1337,
105-
GpuLayerCount = 5
101+
ContextSize = 1024, // The longest length of chat as memory.
102+
GpuLayerCount = 5 // How many layers to offload to GPU. Please adjust it according to your GPU memory.
106103
};
107104
using var model = LLamaWeights.LoadFromFile(parameters);
108-
109-
// Initialize a chat session
110105
using var context = model.CreateContext(parameters);
111-
var ex = new InteractiveExecutor(context);
112-
ChatSession session = new ChatSession(ex);
113-
114-
// show the prompt
115-
Console.WriteLine();
116-
Console.Write(prompt);
106+
var executor = new InteractiveExecutor(context);
117107

118-
// run the inference in a loop to chat with LLM
119-
while (prompt != "stop")
120-
{
121-
await foreach (var text in session.ChatAsync(new ChatHistory.Message(AuthorRole.User, prompt), new InferenceParams { Temperature = 0.6f, AntiPrompts = [ "User:" ] }))
122-
{
123-
Console.Write(text);
124-
}
125-
prompt = Console.ReadLine() ?? "";
126-
}
108+
// Add chat histories as prompt to tell AI how to act.
109+
var chatHistory = new ChatHistory();
110+
chatHistory.AddMessage(AuthorRole.System, "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.");
111+
chatHistory.AddMessage(AuthorRole.User, "Hello, Bob.");
112+
chatHistory.AddMessage(AuthorRole.Assistant, "Hello. How may I help you today?");
127113

128-
// save the session
129-
session.SaveSession("SavedSessionPath");
130-
```
114+
ChatSession session = new(executor, chatHistory);
131115

132-
#### Quantization
116+
InferenceParams inferenceParams = new InferenceParams()
117+
{
118+
MaxTokens = 256, // No more than 256 tokens should appear in answer. Remove it if antiprompt is enough for control.
119+
AntiPrompts = new List<string> { "User:" } // Stop generation once antiprompts appear.
120+
};
133121

134-
The following example shows how to quantize the model:
122+
Console.ForegroundColor = ConsoleColor.Yellow;
123+
Console.Write("The chat session has started.\nUser: ");
124+
Console.ForegroundColor = ConsoleColor.Green;
125+
string userInput = Console.ReadLine() ?? "";
135126

136-
```cs
137-
string srcFilename = "<Your source path>";
138-
string dstFilename = "<Your destination path>";
139-
string ftype = "q4_0";
140-
if(Quantizer.Quantize(srcFileName, dstFilename, ftype))
141-
{
142-
Console.WriteLine("Quantization succeed!");
143-
}
144-
else
127+
while (userInput != "exit")
145128
{
146-
Console.WriteLine("Quantization failed!");
129+
await foreach ( // Generate the response streamingly.
130+
var text
131+
in session.ChatAsync(
132+
new ChatHistory.Message(AuthorRole.User, userInput),
133+
inferenceParams))
134+
{
135+
Console.ForegroundColor = ConsoleColor.White;
136+
Console.Write(text);
137+
}
138+
Console.ForegroundColor = ConsoleColor.Green;
139+
userInput = Console.ReadLine() ?? "";
147140
}
148141
```
149142

0 commit comments

Comments
 (0)