4using System.Collections.Generic;
7using System.Threading.Tasks;
13 [DefaultExecutionOrder(-2)]
34 [ModelAdvanced]
public int slot = -1;
36 [ModelAdvanced]
public string grammar =
null;
40 [ModelAdvanced]
public int seed = 0;
45 [ModelAdvanced, Float(0f, 2f)]
public float temperature = 0.2f;
48 [ModelAdvanced, Int(-1, 100)]
public int topK = 40;
53 [ModelAdvanced, Float(0f, 1f)]
public float topP = 0.9f;
56 [ModelAdvanced, Float(0f, 1f)]
public float minP = 0.05f;
68 [ModelAdvanced, Float(0f, 1f)]
public float tfsZ = 1f;
70 [ModelAdvanced, Float(0f, 1f)]
public float typicalP = 1f;
79 [ModelAdvanced, Int(0, 2)]
public int mirostat = 0;
81 [ModelAdvanced, Float(0f, 10f)]
public float mirostatTau = 5f;
83 [ModelAdvanced, Float(0f, 1f)]
public float mirostatEta = 0.1f;
85 [ModelAdvanced, Int(0, 10)]
public int nProbs = 0;
92 public List<string>
stop =
new List<string>();
105 [TextArea(5, 10),
Chat]
public string prompt =
"A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.";
114 protected SemaphoreSlim chatLock =
new SemaphoreSlim(1, 1);
115 protected string chatTemplate;
130 if (!enabled)
return;
134 int slotFromServer = llm.
Register(
this);
135 if (
slot == -1)
slot = slotFromServer;
141 protected override void OnValidate()
147 protected override string NotValidLLMError()
149 return base.NotValidLLMError() +
$", it is an embedding only model";
159 return !
llmSet.embeddingsOnly;
162 protected virtual void InitHistory()
168 protected virtual async Task LoadHistory()
182 protected virtual string GetSavePath(
string filename)
184 return Path.Combine(
Application.persistentDataPath, filename).Replace(
'\\',
'/');
194 return GetSavePath(filename +
".json");
204 return GetSavePath(filename +
".cache");
207 protected virtual void InitPrompt(
bool clearChat =
true)
240 protected virtual bool CheckTemplate()
242 if (
template ==
null)
254 if (!CheckTemplate())
return false;
257 if (
tokens ==
null)
return false;
263 protected virtual void InitGrammar()
271 protected virtual void SetNKeep(List<int>
tokens)
316 if (!CheckTemplate())
return null;
389 return result.content.Trim();
446 if (!CheckTemplate())
return null;
447 if (!
await InitNKeep())
return null;
519 if (!CheckTemplate())
return;
520 if (!
await InitNKeep())
return;
539 protected override void CancelRequestsLocal()
606 if (llm.embeddingsOnly)
LLMUnitySetup.LogError(
"The LLM can't be used for completion, only for embeddings");
621 LLMUnitySetup.LogError(
$"wrong callback type, should be string");
636 public class ChatListWrapper
638 public List<ChatMessage> chat;
Class implementing the skeleton of a chat template.
static ChatTemplate GetTemplate(string template)
Creates the chat template based on the provided chat template name.
Class implementing calling of LLM functions (local and remote).
virtual async Task< List< int > > Tokenize(string query, Callback< List< int > > callback=null)
Tokenises the provided query.
bool remote
toggle to use remote LLM server or local LLM
Class implementing the LLM characters.
bool cachePrompt
option to cache the prompt as it is being created by the chat to avoid reprocessing the entire prompt...
int slot
specify which slot of the server to use for computation (affects caching)
virtual async Task< string > Chat(string query, Callback< string > callback=null, EmptyCallback completionCallback=null, bool addToHistory=true)
Chat functionality of the LLM. It calls the LLM completion based on the provided query including the ...
List< string > stop
stopwords to stop the LLM in addition to the default stopwords from the chat template.
float topP
top-p sampling (1.0 = disabled). The top p value controls the cumulative probability of generated tok...
virtual async Task LoadTemplate()
Loads the chat template of the LLMCharacter.
string AIName
the name of the AI
int nProbs
if greater than 0, the response also contains the probabilities of top N tokens for each generated to...
bool ignoreEos
ignore end of stream token and continue generating.
float mirostatTau
set the Mirostat target entropy, parameter tau.
int numPredict
number of tokens to predict (-1 = infinity, -2 = until context filled). This is the amount of tokens ...
string prompt
a description of the AI role. This defines the LLMCharacter system prompt
float temperature
LLM temperature, lower values give more deterministic answers. The temperature setting adjusts how ra...
override void Awake()
The Unity Awake function that initializes the state before the application starts....
float presencePenalty
repeated token presence penalty (0.0 = disabled). Positive values penalize new tokens based on whethe...
virtual async Task< string > AskTemplate()
Asks the LLM for the chat template to use.
string playerName
the name of the player
virtual string GetCacheSavePath(string filename)
Allows to get the save path of the LLM cache based on the provided filename or relative path.
float mirostatEta
set the Mirostat learning rate, parameter eta.
float minP
minimum probability for a token to be used. The probability is defined relative to the probability of...
float typicalP
enable locally typical sampling with parameter p (1.0 = disabled).
virtual async Task< string > Load(string filename)
Load the chat history and cache from the provided filename / relative path.
int nKeep
number of tokens to retain from the prompt when the model runs out of context (-1 = LLMCharacter prom...
virtual async void SetGrammar(string path)
Sets the grammar file of the LLMCharacter.
bool penalizeNl
penalize newline tokens when applying the repeat penalty.
bool debugPrompt
select to log the constructed prompt the Unity Editor.
bool saveCache
toggle to save the LLM cache. This speeds up the prompt calculation but also requires ~100MB of space...
int topK
top-k sampling (0 = disabled). The top k value controls the top k most probable tokens at each step o...
string grammar
grammar file used for the LLM in .cbnf format (relative to the Assets/StreamingAssets folder)
virtual async Task< string > Save(string filename)
Saves the chat history and cache to the provided filename / relative path.
virtual void AddAIMessage(string content)
Allows to add a AI message in the chat history.
string penaltyPrompt
prompt for the purpose of the penalty evaluation. Can be either null, a string or an array of numbers...
override bool IsValidLLM(LLM llmSet)
Checks if a LLM is valid for the LLMCaller.
virtual void AddPlayerMessage(string content)
Allows to add a player message in the chat history.
float repeatPenalty
control the repetition of token sequences in the generated text. The penalty is applied to repeated t...
virtual string GetJsonSavePath(string filename)
Allows to get the save path of the chat history based on the provided filename or relative path.
bool stream
option to receive the reply from the model as it is produced (recommended!). If it is not selected,...
float frequencyPenalty
repeated token frequency penalty (0.0 = disabled). Positive values penalize new tokens based on their...
virtual async Task Warmup(EmptyCallback completionCallback=null)
Allow to warm-up a model by processing the prompt. The prompt processing will be cached (if cacheProm...
int mirostat
enable Mirostat sampling, controlling perplexity during text generation (0 = disabled,...
bool setNKeepToPrompt
option to set the number of tokens to retain from the prompt (nKeep) based on the LLMCharacter system...
int repeatLastN
last n tokens to consider for penalizing repetition (0 = disabled, -1 = ctx-size).
int seed
seed for reproducibility. For random results every time set to -1.
float tfsZ
enable tail free sampling with parameter z (1.0 = disabled).
string grammarString
the grammar to use
Dictionary< int, string > logitBias
the logit bias option allows to manually adjust the likelihood of specific tokens appearing in the ge...
virtual async Task< string > Complete(string prompt, Callback< string > callback=null, EmptyCallback completionCallback=null)
Pure completion functionality of the LLM. It calls the LLM completion based solely on the provided pr...
virtual void AddMessage(string role, string content)
Allows to add a message in the chat history.
List< ChatMessage > chat
the chat history as list of chat messages
string save
file to save the chat history. The file is saved only for Chat calls with addToHistory set to true....
virtual void SetPrompt(string newPrompt, bool clearChat=true)
Set the system prompt for the LLMCharacter.
Class implementing helper functions for setup and process management.
Class implementing the LLM server.
string GetTemplate()
Returns the chat template of the LLM.
void CancelRequest(int id_slot)
Allows to cancel the requests in a specific slot of the LLM.
int parallelPrompts
number of prompts that can happen in parallel (-1 = number of LLMCaller objects)
bool started
Boolean set to true if the server has started and is ready to receive requests, false otherwise.
async Task< string > Completion(string json, Callback< string > streamCallback=null)
Allows to use the chat and completion functionality of the LLM.
int Register(LLMCaller llmCaller)
Registers a local LLMCaller object. This allows to bind the LLMCaller "client" to a specific slot of ...
bool failed
Boolean set to true if the server has failed to start.