Skip to content

Commit 84efdf7

Browse files
committed
fix setting up of functions dependent on caller object
1 parent ed276ca commit 84efdf7

2 files changed

Lines changed: 47 additions & 25 deletions

File tree

Runtime/LLMAgent.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ public override void Awake()
139139
base.Awake();
140140
}
141141

142-
protected override async Task SetupLLMClient()
142+
protected override async Task SetupCallerObject()
143143
{
144-
await base.SetupLLMClient();
144+
await base.SetupCallerObject();
145145

146146
string exceptionMessage = "";
147147
try
@@ -158,7 +158,14 @@ protected override async Task SetupLLMClient()
158158
if (exceptionMessage != "") error += ", error: " + exceptionMessage;
159159
LLMUnitySetup.LogError(error, true);
160160
}
161+
}
161162

163+
/// <summary>
164+
/// Initialisation after setting up the LLM client (local or remote).
165+
/// </summary>
166+
protected override async Task PostSetupCallerObject()
167+
{
168+
await base.PostSetupCallerObject();
162169
if (slot != -1) llmAgent.SlotId = slot;
163170
await InitHistory();
164171
}
@@ -217,7 +224,7 @@ public virtual string GetSavePath()
217224
/// </summary>
218225
public virtual async Task ClearHistory()
219226
{
220-
await CheckLLMClient(checkConnection: false);
227+
await CheckCaller(checkConnection: false);
221228
llmAgent.ClearHistory();
222229
}
223230

@@ -228,7 +235,7 @@ public virtual async Task ClearHistory()
228235
/// <param name="content">Message content</param>
229236
public virtual async Task AddMessage(string role, string content)
230237
{
231-
await CheckLLMClient();
238+
await CheckCaller();
232239
llmAgent.AddMessage(role, content);
233240
}
234241

@@ -238,7 +245,7 @@ public virtual async Task AddMessage(string role, string content)
238245
/// <param name="content">User message content</param>
239246
public virtual async Task AddUserMessage(string content)
240247
{
241-
await CheckLLMClient();
248+
await CheckCaller();
242249
llmAgent.AddUserMessage(content);
243250
}
244251

@@ -248,7 +255,7 @@ public virtual async Task AddUserMessage(string content)
248255
/// <param name="content">Assistant message content</param>
249256
public virtual async Task AddAssistantMessage(string content)
250257
{
251-
await CheckLLMClient();
258+
await CheckCaller();
252259
llmAgent.AddAssistantMessage(content);
253260
}
254261

@@ -275,7 +282,7 @@ public class CompletionResponseJson
275282
public virtual async Task<string> Chat(string query, LlamaLib.CharArrayCallback callback = null,
276283
EmptyCallback completionCallback = null, bool addToHistory = true)
277284
{
278-
await CheckLLMClient();
285+
await CheckCaller();
279286

280287
// Wrap callback to ensure it runs on the main thread
281288
LlamaLib.CharArrayCallback wrappedCallback = Utils.WrapCallbackForAsync(callback, this);
@@ -341,7 +348,7 @@ public virtual async Task SaveHistory()
341348
LLMUnitySetup.LogError("No save path specified");
342349
return;
343350
}
344-
await CheckLLMClient();
351+
await CheckCaller();
345352

346353
// Save chat history
347354
string jsonPath = GetSavePath();
@@ -373,7 +380,7 @@ public virtual async Task LoadHistory()
373380
LLMUnitySetup.LogError("No save path specified");
374381
return;
375382
}
376-
await CheckLLMClient();
383+
await CheckCaller();
377384

378385
// Load chat history
379386
string jsonPath = GetSavePath();

Runtime/LLMClient.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public bool remote
132132
if (_remote != value)
133133
{
134134
_remote = value;
135-
if (started) _ = SetupLLMClient();
135+
if (started) _ = SetupCaller();
136136
}
137137
}
138138
}
@@ -153,7 +153,7 @@ public string APIKey
153153
if (_APIKey != value)
154154
{
155155
_APIKey = value;
156-
if (started) _ = SetupLLMClient();
156+
if (started) _ = SetupCaller();
157157
}
158158
}
159159
}
@@ -167,7 +167,7 @@ public string host
167167
if (_host != value)
168168
{
169169
_host = value;
170-
if (started) _ = SetupLLMClient();
170+
if (started) _ = SetupCaller();
171171
}
172172
}
173173
}
@@ -181,7 +181,7 @@ public int port
181181
if (_port != value)
182182
{
183183
_port = value;
184-
if (started) _ = SetupLLMClient();
184+
if (started) _ = SetupCaller();
185185
}
186186
}
187187
}
@@ -223,7 +223,7 @@ public virtual void Awake()
223223
public virtual async void Start()
224224
{
225225
if (!enabled) return;
226-
await SetupLLMClient();
226+
await SetupCaller();
227227
started = true;
228228
}
229229

@@ -240,11 +240,11 @@ protected virtual void Reset()
240240
#endregion
241241

242242
#region Initialization
243-
protected virtual async Task CheckLLMClient(bool checkConnection = true)
243+
protected virtual async Task CheckCaller(bool checkConnection = true)
244244
{
245245
await startSemaphore.WaitAsync();
246246
startSemaphore.Release();
247-
if (GetCaller() == null) LLMUnitySetup.LogError("LLMClient not initialized", true);
247+
if (GetCaller() == null) LLMUnitySetup.LogError("LLM caller not initialized", true);
248248
if (remote && checkConnection)
249249
{
250250
for (int attempt = 0; attempt <= numRetries; attempt++)
@@ -258,7 +258,16 @@ protected virtual async Task CheckLLMClient(bool checkConnection = true)
258258
/// <summary>
259259
/// Sets up the underlying LLM client connection (local or remote).
260260
/// </summary>
261-
protected virtual async Task SetupLLMClient()
261+
protected virtual async Task SetupCaller()
262+
{
263+
await SetupCallerObject();
264+
await PostSetupCallerObject();
265+
}
266+
267+
/// <summary>
268+
/// Sets up the underlying LLM client connection (local or remote).
269+
/// </summary>
270+
protected virtual async Task SetupCallerObject()
262271
{
263272
await startSemaphore.WaitAsync();
264273

@@ -275,9 +284,6 @@ protected virtual async Task SetupLLMClient()
275284
{
276285
llmClient = new UndreamAI.LlamaLib.LLMClient(host, port, APIKey, numRetries);
277286
}
278-
279-
SetGrammar(grammar);
280-
completionParametersCache = "";
281287
}
282288
catch (Exception ex)
283289
{
@@ -297,6 +303,15 @@ protected virtual async Task SetupLLMClient()
297303
}
298304
}
299305

306+
/// <summary>
307+
/// Initialisation after setting up the LLM client (local or remote).
308+
/// </summary>
309+
protected virtual async Task PostSetupCallerObject()
310+
{
311+
SetGrammar(grammar);
312+
completionParametersCache = "";
313+
}
314+
300315
/// <summary>
301316
/// Gets the underlying LLMLocal instance for operations requiring local access.
302317
/// </summary>
@@ -320,7 +335,7 @@ protected virtual async Task SetLLM(LLM llmInstance)
320335
}
321336

322337
_llm = llmInstance;
323-
if (started) await SetupLLMClient();
338+
if (started) await SetupCaller();
324339
}
325340

326341
#endregion
@@ -497,7 +512,7 @@ public virtual async Task<List<int>> Tokenize(string query, Callback<List<int>>
497512
{
498513
throw new ArgumentNullException(nameof(query));
499514
}
500-
await CheckLLMClient();
515+
await CheckCaller();
501516

502517
List<int> tokens = llmClient.Tokenize(query);
503518
callback?.Invoke(tokens);
@@ -516,7 +531,7 @@ public virtual async Task<string> Detokenize(List<int> tokens, Callback<string>
516531
{
517532
throw new ArgumentNullException(nameof(tokens));
518533
}
519-
await CheckLLMClient();
534+
await CheckCaller();
520535

521536
string text = llmClient.Detokenize(tokens);
522537
callback?.Invoke(text);
@@ -535,7 +550,7 @@ public virtual async Task<List<float>> Embeddings(string query, Callback<List<fl
535550
{
536551
throw new ArgumentNullException(nameof(query));
537552
}
538-
await CheckLLMClient();
553+
await CheckCaller();
539554

540555
List<float> embeddings = llmClient.Embeddings(query);
541556
callback?.Invoke(embeddings);
@@ -553,7 +568,7 @@ public virtual async Task<List<float>> Embeddings(string query, Callback<List<fl
553568
public virtual async Task<string> Completion(string prompt, LlamaLib.CharArrayCallback callback = null,
554569
EmptyCallback completionCallback = null, int id_slot = -1)
555570
{
556-
await CheckLLMClient();
571+
await CheckCaller();
557572
SetCompletionParameters();
558573
string result = await llmClient.CompletionAsync(prompt, callback, id_slot);
559574
completionCallback?.Invoke();

0 commit comments

Comments
 (0)