LLM for Unity  v3.0.0
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
LLMUnity.LLMAgent Class Reference

Unity MonoBehaviour that implements a conversational AI agent with persistent chat history. Extends LLMClient to provide chat-specific functionality including role management, conversation history persistence, and specialized chat completion methods. More...

Inheritance diagram for LLMUnity.LLMAgent:
[legend]

Public Member Functions

override void Awake ()
 Unity Awake method that validates configuration and assigns local LLM if needed.
 
virtual string GetSavePath ()
 Gets the full path for a file in the persistent data directory.
 
virtual async Task ClearHistory ()
 Clears the entire conversation history.
 
virtual async Task AddUserMessage (string content)
 Adds a user message to the conversation history.
 
virtual async Task AddAssistantMessage (string content)
 Adds an AI assistant message to the conversation history.
 
virtual async Task< string > Chat (string query, Action< string > callback=null, Action completionCallback=null, bool addToHistory=true)
 Processes a user query asynchronously and generates an AI response using conversation context. The query and response are automatically added to chat history if specified.
 
virtual async Task Warmup (Action completionCallback=null)
 Warms up the model by processing the system prompt without generating output. This caches the system prompt processing for faster subsequent responses.
 
virtual async Task Warmup (string query, Action completionCallback=null)
 Warms up the model with a specific prompt without adding it to history. This pre-processes prompts for faster response times in subsequent interactions.
 
virtual async Task SaveHistory ()
 Saves the conversation history and optionally the LLM cache to disk.
 
virtual async Task LoadHistory ()
 Loads conversation history and optionally the LLM cache from disk.
 
void CancelRequests ()
 Cancels any active requests for this agent.
 
- Public Member Functions inherited from LLMUnity.LLMClient
virtual async void Start ()
 Unity Start method that initializes the LLM client connection.
 
virtual bool IsAutoAssignableLLM (LLM llmInstance)
 Determines if an LLM instance can be auto-assigned to this client. Override in derived classes to implement specific assignment logic.
 
virtual void SetGrammar (string grammarString)
 Sets grammar constraints for structured output generation.
 
virtual void LoadGrammar (string path)
 Loads grammar constraints from a file.
 
virtual async Task< List< int > > Tokenize (string query, Action< List< int > > callback=null)
 Converts text into a list of token IDs.
 
virtual async Task< string > Detokenize (List< int > tokens, Action< string > callback=null)
 Converts token IDs back to text.
 
virtual async Task< List< float > > Embeddings (string query, Action< List< float > > callback=null)
 Generates embedding vectors for the input text.
 
virtual async Task< string > Completion (string prompt, Action< string > callback=null, Action completionCallback=null, int id_slot=-1)
 Generates text completion.
 
void CancelRequest (int id_slot)
 Cancels an active request in the specified slot.
 

Public Attributes

string save = ""
 Filename for saving chat history (saved in persistentDataPath)
 
bool debugPrompt = false
 Debug LLM prompts.
 
- Public Attributes inherited from LLMUnity.LLMClient
bool advancedOptions = false
 Show/hide advanced options in the inspector.
 
int numPredict = -1
 Maximum tokens to generate (-1 = unlimited)
 
bool cachePrompt = true
 Cache processed prompts to speed up subsequent requests.
 
int seed = 0
 Random seed for reproducible generation (0 = random)
 
float temperature = 0.2f
 Sampling temperature (0.0 = deterministic, higher = more creative)
 
int topK = 40
 Top-k sampling: limit to k most likely tokens (0 = disabled)
 
float topP = 0.9f
 Top-p (nucleus) sampling: cumulative probability threshold (1.0 = disabled)
 
float minP = 0.05f
 Minimum probability threshold for token selection.
 
float repeatPenalty = 1.1f
 Penalty for repeated tokens (1.0 = no penalty)
 
float presencePenalty = 0f
 Presence penalty: reduce likelihood of any repeated token (0.0 = disabled)
 
float frequencyPenalty = 0f
 Frequency penalty: reduce likelihood based on token frequency (0.0 = disabled)
 
float typicalP = 1f
 Locally typical sampling strength (1.0 = disabled)
 
int repeatLastN = 64
 Number of recent tokens to consider for repetition penalty (0 = disabled, -1 = context size)
 
int mirostat = 0
 Mirostat sampling mode (0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)
 
float mirostatTau = 5f
 Mirostat target entropy (tau) - balance between coherence and diversity.
 
float mirostatEta = 0.1f
 Mirostat learning rate (eta) - adaptation speed.
 
int nProbs = 0
 Include top N token probabilities in response (0 = disabled)
 
bool ignoreEos = false
 Ignore end-of-stream token and continue generating.
 

Properties

int slot [get, set]
 Server slot ID for this agent's requests.
 
string systemPrompt [get, set]
 System prompt defining the agent's behavior and personality.
 
UndreamAI.LlamaLib.LLMAgent llmAgent [get]
 The underlying LLMAgent instance from LlamaLib.
 
List< ChatMessagechat [get, set]
 Current conversation history as a list of chat messages.
 
- Properties inherited from LLMUnity.LLMClient
bool remote [get, set]
 Whether this client uses a remote server connection.
 
LLM llm [get, set]
 The local LLM instance (null if using remote)
 
string APIKey [get, set]
 API key for remote server authentication.
 
string host [get, set]
 Remote server hostname or IP address.
 
int port [get, set]
 Remote server port number.
 
string grammar [get, set]
 Current grammar constraints for output formatting.
 

Detailed Description

Unity MonoBehaviour that implements a conversational AI agent with persistent chat history. Extends LLMClient to provide chat-specific functionality including role management, conversation history persistence, and specialized chat completion methods.

Definition at line 20 of file LLMAgent.cs.

Member Function Documentation

◆ AddAssistantMessage()

virtual async Task LLMUnity.LLMAgent.AddAssistantMessage ( string content)
inlinevirtual

Adds an AI assistant message to the conversation history.

Parameters
contentAssistant message content

Definition at line 209 of file LLMAgent.cs.

◆ AddUserMessage()

virtual async Task LLMUnity.LLMAgent.AddUserMessage ( string content)
inlinevirtual

Adds a user message to the conversation history.

Parameters
contentUser message content

Definition at line 199 of file LLMAgent.cs.

◆ Awake()

override void LLMUnity.LLMAgent.Awake ( )
inlinevirtual

Unity Awake method that validates configuration and assigns local LLM if needed.

Reimplemented from LLMUnity.LLMClient.

Definition at line 100 of file LLMAgent.cs.

◆ CancelRequests()

void LLMUnity.LLMAgent.CancelRequests ( )
inline

Cancels any active requests for this agent.

Definition at line 374 of file LLMAgent.cs.

◆ Chat()

virtual async Task< string > LLMUnity.LLMAgent.Chat ( string query,
Action< string > callback = null,
Action completionCallback = null,
bool addToHistory = true )
inlinevirtual

Processes a user query asynchronously and generates an AI response using conversation context. The query and response are automatically added to chat history if specified.

Parameters
queryUser's message or question
callbackOptional streaming callback for partial responses
completionCallbackOptional callback when response is complete
addToHistoryWhether to add the exchange to conversation history
Returns
Task that returns the AI assistant's response

Definition at line 235 of file LLMAgent.cs.

Here is the caller graph for this function:

◆ ClearHistory()

virtual async Task LLMUnity.LLMAgent.ClearHistory ( )
inlinevirtual

Clears the entire conversation history.

Definition at line 189 of file LLMAgent.cs.

◆ GetSavePath()

virtual string LLMUnity.LLMAgent.GetSavePath ( )
inlinevirtual

Gets the full path for a file in the persistent data directory.

Returns
Full file path in persistent data directory

Definition at line 172 of file LLMAgent.cs.

Here is the caller graph for this function:

◆ LoadHistory()

virtual async Task LLMUnity.LLMAgent.LoadHistory ( )
inlinevirtual

Loads conversation history and optionally the LLM cache from disk.

Definition at line 341 of file LLMAgent.cs.

◆ SaveHistory()

virtual async Task LLMUnity.LLMAgent.SaveHistory ( )
inlinevirtual

Saves the conversation history and optionally the LLM cache to disk.

Definition at line 309 of file LLMAgent.cs.

Here is the caller graph for this function:

◆ Warmup() [1/2]

virtual async Task LLMUnity.LLMAgent.Warmup ( Action completionCallback = null)
inlinevirtual

Warms up the model by processing the system prompt without generating output. This caches the system prompt processing for faster subsequent responses.

Parameters
completionCallbackOptional callback when warmup completes
Returns
Task that completes when warmup finishes

Definition at line 274 of file LLMAgent.cs.

Here is the caller graph for this function:

◆ Warmup() [2/2]

virtual async Task LLMUnity.LLMAgent.Warmup ( string query,
Action completionCallback = null )
inlinevirtual

Warms up the model with a specific prompt without adding it to history. This pre-processes prompts for faster response times in subsequent interactions.

Parameters
queryWarmup prompt (not added to history)
completionCallbackOptional callback when warmup completes
Returns
Task that completes when warmup finishes

Definition at line 286 of file LLMAgent.cs.

Member Data Documentation

◆ debugPrompt

bool LLMUnity.LLMAgent.debugPrompt = false

Debug LLM prompts.

Definition at line 29 of file LLMAgent.cs.

◆ save

string LLMUnity.LLMAgent.save = ""

Filename for saving chat history (saved in persistentDataPath)

Definition at line 25 of file LLMAgent.cs.

Property Documentation

◆ chat

List<ChatMessage> LLMUnity.LLMAgent.chat
getset

Current conversation history as a list of chat messages.

Definition at line 74 of file LLMAgent.cs.

◆ llmAgent

UndreamAI.LlamaLib.LLMAgent LLMUnity.LLMAgent.llmAgent
get

The underlying LLMAgent instance from LlamaLib.

Definition at line 71 of file LLMAgent.cs.

◆ slot

int LLMUnity.LLMAgent.slot
getset

Server slot ID for this agent's requests.

Definition at line 43 of file LLMAgent.cs.

◆ systemPrompt

string LLMUnity.LLMAgent.systemPrompt
getset

System prompt defining the agent's behavior and personality.

Definition at line 57 of file LLMAgent.cs.


The documentation for this class was generated from the following file: