diff --git a/LLama/LLamaModel.cs b/LLama/LLamaModel.cs
index 4ef7aaff..0edc2b63 100644
--- a/LLama/LLamaModel.cs
+++ b/LLama/LLamaModel.cs
@@ -1,16 +1,15 @@
-using LLama.Native;
+using LLama.Exceptions;
+using LLama.Extensions;
+using LLama.Native;
using System;
using System.Collections.Generic;
using System.IO;
-using System.Text;
-using LLama.Exceptions;
using System.Linq;
-using LLama.Extensions;
namespace LLama
{
using llama_token = Int32;
- public class LLamaModel: IChatModel, IDisposable
+ public class LLamaModel : IChatModel, IDisposable
{
LLamaParams _params;
SafeLLamaContextHandle _ctx;
@@ -39,8 +38,57 @@ namespace LLama
public string Name { get; set; }
public SafeLLamaContextHandle NativeHandle => _ctx;
+ ///
+ /// Please refer `LLamaParams` to find the meanings of each arg.
+ ///
+ /// The model file path.
+ /// The model name.
+ /// Whether to print the input messages.
+ /// Whether to print details when running the model.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
public LLamaModel(string model_path, string model_name, bool echo_input = false, bool verbose = false, int seed = 0, int n_threads = -1, int n_predict = -1,
- int n_parts = -1, int n_ctx = 512, int n_batch = 512, int n_keep = 0, int n_gpu_layers = -1,
+ int n_ctx = 512, int n_batch = 512, int n_keep = 0, int n_gpu_layers = -1,
Dictionary logit_bias = null, int top_k = 40, float top_p = 0.95f,
float tfs_z = 1.00f, float typical_p = 1.00f, float temp = 0.80f, float repeat_penalty = 1.10f,
int repeat_last_n = 64, float frequency_penalty = 0.00f, float presence_penalty = 0.00f,
@@ -91,10 +139,10 @@ namespace LLama
use_mmap: use_mmap,
use_mlock: use_mlock,
mem_test: mem_test,
- verbose_prompt: verbose_prompt),
+ verbose_prompt: verbose_prompt),
model_name, echo_input, verbose, encoding)
{
-
+
}
public unsafe LLamaModel(LLamaParams @params, string name = "", bool echo_input = false, bool verbose = false, string encoding = "UTF-8")
@@ -213,6 +261,13 @@ namespace LLama
_embed = new List();
}
+ ///
+ /// Apply a prompt to the model.
+ ///
+ ///
+ ///
+ ///
+ ///
public LLamaModel WithPrompt(string prompt, string encoding = "UTF-8")
{
_params.prompt = prompt.Insert(0, " ");
@@ -262,6 +317,11 @@ namespace LLama
return this;
}
+ ///
+ /// Apply the prompt file to the model.
+ ///
+ ///
+ ///
public LLamaModel WithPromptFile(string promptFileName)
{
return WithPrompt(File.ReadAllText(promptFileName));
@@ -317,9 +377,20 @@ namespace LLama
_params.antiprompt = antiprompt.ToList();
}
+ ///
+ /// Chat with the LLaMa model under interactive mode.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
public IEnumerable Chat(string text, string? prompt = null, string encoding = "UTF-8")
{
- _params.interactive = true;
+ if (!_params.interactive)
+ {
+ throw new ArgumentException("The chat API could be only used under interactive model.");
+ }
_input_echo = false;
if (!string.IsNullOrEmpty(prompt))
{
@@ -328,6 +399,10 @@ namespace LLama
return Call(text, encoding);
}
+ ///
+ /// Save the state to specified path.
+ ///
+ ///
public void SaveState(string filename)
{
var stateSize = NativeApi.llama_get_state_size(_ctx);
@@ -336,10 +411,16 @@ namespace LLama
File.WriteAllBytes(filename, stateMemory.Take(nbytes).ToArray());
}
+ ///
+ /// Load the state from specified path.
+ ///
+ ///
+ /// Whether to clear previous footprints of this model.
+ ///
public void LoadState(string filename, bool clearPreviousEmbed = true)
{
var stateMemory = File.ReadAllBytes(filename);
- if(stateMemory.Length != (int)NativeApi.llama_get_state_size(_ctx))
+ if (stateMemory.Length != (int)NativeApi.llama_get_state_size(_ctx))
{
throw new RuntimeError("Failed to validate state size.");
}
@@ -351,15 +432,22 @@ namespace LLama
}
}
+ ///
+ /// Call the model to run inference.
+ ///
+ ///
+ ///
+ ///
+ ///
public IEnumerable Call(string text, string encoding = "UTF-8")
{
_is_antiprompt = false;
- if(_n_past > 0)
+ if (_n_past > 0)
{
_is_interacting = false;
}
ProcessTextBeforeInfer(text, encoding);
-
+
while ((_n_remain != 0 || _params.interactive) && !_is_interacting)
{
if (_embed.Count > 0)
@@ -609,7 +697,7 @@ namespace LLama
}
}
- if(_n_past > 0 && _is_interacting)
+ if (_n_past > 0 && _is_interacting)
{
if (_params.instruct)
{
@@ -621,22 +709,25 @@ namespace LLama
if (_embed.Count > 0 && _embed.Last() == NativeApi.llama_token_eos())
{
- if (_params.instruct) {
+ if (_params.instruct)
+ {
_is_interacting = true;
- } else
+ }
+ else
{
Logger.Default.Info(" [end of text]");
}
}
- if (_params.interactive && _n_remain <= 0 && _params.n_predict != -1) {
+ if (_params.interactive && _n_remain <= 0 && _params.n_predict != -1)
+ {
_n_remain = _params.n_predict;
_is_interacting = true;
}
}
}
- if(!string.IsNullOrEmpty(_path_session) && _params.prompt_cache_all)
+ if (!string.IsNullOrEmpty(_path_session) && _params.prompt_cache_all)
{
Logger.Default.Info($"saving final output to session file {_path_session}");
var session_token_array = _session_tokens.ToArray();