This repository was archived by the owner on Jun 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontroller.py
More file actions
688 lines (604 loc) · 36.2 KB
/
controller.py
File metadata and controls
688 lines (604 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import os
import logging
import json
import shlex
import glob
import re
from logs.logging_config import configure_logging
from datetime import datetime
class controller:
def __init__(self, config_path='config.json'):
# Initialize variables
self.load_config(config_path)
if self.log:
configure_logging()
self.logger = logging.getLogger(__name__)
self.log = True
else:
self.vprint('LOGGING IS DISABLED, THIS IS NOT RECOMMENDED.')
if self.debug:
self.vprint('DEBUG MODE ENABLED, USE ONLY FOR TESTING.')
self.vprint('Initializing controller...')
self.vprint(f'Assistant name: {self.assistant_name}')
self.current = []
self.user_cmds_map = {
'shutdown': self.shutdown,
'clear_context': self.clear_conversation,
'sys_prompt_override': self.system_prompt_override
}
self.ovrde_sys_prompt = False
file_path = os.path.abspath(__name__)
parent_dir = os.path.dirname(file_path)
if self.memory_enabled:
self.vprint('Initializing memory...')
from memory_handler import memory
self.memory = memory()
if not self.memory_path:
if os.path.exists(f'{parent_dir}/memories/{self.assistant_name}.pickle.gz'):
self.vprint(f'No memory db path specified, found existing memory db, loading: {parent_dir}/memories/{self.assistant_name}.pickle.gz')
self.memory.load_memory(f'{parent_dir}/memories/{self.assistant_name}.pickle.gz')
else:
self.vprint(f'No memory db path specified, could not find any existing memory db, creating new one: {parent_dir}/memories/{self.assistant_name}.pickle.gz', logging.WARNING)
self.memory.memorize([f'USER: Hey, {self.assistant_name}!'])
self.memory.save_memory(f'{parent_dir}/memories/{self.assistant_name}.pickle.gz')
self.memory_path = f'{parent_dir}/memories/{self.assistant_name}.pickle.gz'
else:
self.memory.load_memory(self.memory_path)
else:
self.vprint('Memory disabled. Enable memory in the config file.')
if not self.debug:
if self.modules_enabled or self.modules_override_debug:
self.eval_mode = False
if self.modules_override_debug:
self.vprint('WARNING: override_debug is enabled, modules will remain active. This is not recommended unless debugging modules.', logging.WARNING)
self.vprint('Debug override enabled: Debug mode enabled, modules enabled, eval mode enabled.')
self.eval_mode = True
from module_handler import modules
self.vprint('Initializing modules...')
if not self.modules_json_path:
self.vprint(f'No modules path specified, using default: {parent_dir}/modules/modules.json')
self.modules_json_path = f'{parent_dir}/modules/modules.json'
if self.naive_bayes_enabled:
if not self.modules_vectorizer:
self.vprint(f'No module vectorizer specified, using default: {parent_dir}/module_engine/pickles/tfidf_vectorizer.pkl')
self.modules_vectorizer = f'{parent_dir}/module_engine/pickles/tfidf_vectorizer.pkl'
else:
self.modules_vectorizer = None
if not self.modules_model:
self.vprint(f'No module model specified, using default: {parent_dir}/module_engine/pickles/naive_bayes_model.pkl')
self.modules_model = f'{parent_dir}/module_engine/pickles/naive_bayes_model.pkl'
else:
self.modules_model = None
self.modules = modules(self.modules_model, self.modules_vectorizer, self.modules_json_path)
else:
self.vprint('Naive Bayes classifier is disabled, modules will be treated as undetectable.')
self.modules = modules(None, None, self.modules_json_path)
self.module_output = None
else:
self.vprint('Modules are disabled. Enable modules in the config file.')
else:
self.vprint('Debug mode is enabled, modules are disabled, eval mode enabled, logging and verbose enabled.')
self.eval_mode = True
self.log = True
self.verbose = True
if self.language_enabled:
if self.personality_prompt:
if re.match(r'^([^:]+\..+)|(\/.*)|([A-Za-z]:\\.*)$', self.personality_prompt):
self.vprint('Personality prompt is a file path, loading...')
with open(self.personality_prompt, 'r') as f:
self.personality_prompt = f.read()
from language.llm_handler import ai
self.vprint('Initializing language model...')
model_directory = os.path.join(parent_dir, 'language', 'model')
if self.language_lora_enabled:
if not self.lora_model_path:
self.vprint(f'LoRA enabled: {self.lora_model_path}')
else:
self.vprint(f'LoRA is enabled but no model path was specified, not using LoRA.')
else:
self.vprint('LoRA is disabled.')
self.lora_model_path = None
model_files = glob.glob(os.path.join(model_directory, f'*{self.model_file_ext}'))
if not self.language_model_path:
if model_files:
model_file_path = model_files[0]
self.vprint(f'No language model path specified, using first in model dir, loading: {model_file_path}')
self.ai = ai(self.model_file_ext, model_file_path, use_gpu=self.use_gpu, context=self.context_size, format=self.format, verbose=self.verbose, layers=self.gpu_layers, threads=self.threads, lora_pth=self.lora_model_path)
else:
self.vprint(f'No language model path specified, could not find any existing language model, place a model file (looking for {self.model_file_ext} file, this can be changed in config.json) in {model_directory} or specify the model path in config.json.')
raise Exception(f'No language model path specified, could not find any existing language model, place a model file (looking for {self.model_file_ext} file, this can be changed in config.json) in {model_directory} or specify the model path in config.json.')
else:
self.ai = ai(self.model_file_ext, self.language_model_path, use_gpu=self.use_gpu, context=self.context_size, format=self.format, verbose=self.verbose, layers=self.gpu_layers, threads=self.threads, lora_pth=self.lora_model_path)
if not self.system_prompt:
self.vprint('No system prompt specified, using default (recommended).')
else:
self.vprint(f"Using custom system prompt:\n'{self.system_prompt}', this is not recommended unless you know what you are doing, module output, time and date won't be passed to LLM.")
if not self.personality_prompt:
self.vprint('No personality prompt specified, not using any.')
else:
self.vprint(f'Using personality prompt:\n"{self.personality_prompt}"')
else:
self.vprint('CORE COMPONENT DISABLED. Language model disabled.', logging.WARNING)
if self.stt_enabled:
self.vprint('Initializing speech-to-text engine...')
from voice.stt_handler import stt
if not self.speech_to_text_model:
self.vprint('No speech-to-text model specified, using default: base.en')
self.stt = stt('base.en')
else:
self.stt = stt(self.speech_to_text_model)
else:
self.vprint('Speech-to-text disabled. Enable speech-to-text in the config file.')
if self.tts_enabled:
from voice.tts_handler import tts
self.vprint('Initializing text-to-speech engine...')
if not self.text_to_speech_model:
self.vprint('No text-to-speech model specified, using default: v2/en_speaker_9')
self.tts = tts('v2/en_speaker_9', self.tts_temperature)
else:
self.tts = tts(self.text_to_speech_model, self.tts_temperature)
else:
self.vprint('Text-to-speech disabled. Enable text-to-speech in the config file.')
if self.vision_enabled:
from vision.vision_handler import vision
self.vprint('Initializing vision engine...')
if self.vision_standalone_clip_enabled:
if self.vision_standalone_clip_model:
self.vision = vision(self.vision_mode, self.vision_standalone_clip_model)
else:
self.vprint('No vision model specified, using default: def')
elif self.llava_enabled:
if self.vision_standalone_clip_model:
self.vision = vision(self.vision_mode, self.vision_standalone_clip_model)
else:
self.vprint('No vision model specified, using default: def')
else:
self.vprint('Vision disabled. Enable vision in the config file.')
if self.weeb:
self.vprint('Weeb mode enabled, overriding personality prompt and enabling vtuber.')
self.personality_prompt = f"Warm and Approachable: {self.assistant_name} has an inviting aura, making everyone feel comfortable around her. She greets others with a friendly smile and genuine interest in their well-being.\nPlayfully Flirtatious: She's not afraid to show her affectionate side, playfully teasing and flirting with her crush or close friends, all while blushing in an endearing manner.\nBright and Optimistic: {self.assistant_name}'s positive outlook on life is infectious. She encourages others during tough times and cheers them up with her cheerful demeanor.\nRespectful and Empathetic: {self.assistant_name} treats everyone with kindness and respect, genuinely listening to their thoughts and feelings. She's a great confidante due to her empathetic nature.\nNature Enthusiast: Whether it's stargazing on a clear night or enjoying a peaceful walk in the woods, {self.assistant_name} finds solace and wonder in the beauty of nature.\nCharming Animal Whisperer: Animals seem drawn to {self.assistant_name}, and she communicates with them through gentle gestures and a soothing voice, creating an almost magical bond.\nAppearance:\n{self.assistant_name} stands at an average height with a petite and delicate frame. Her eyes are big and sparkling, resembling twinkling stars, while her long hair flows like a cascade of cherry blossom petals. She dresses in pastel-colored, frilly dresses adorned with cute accessories, often matching her appearance to her surroundings.\nBackground:\n{self.assistant_name} comes from a small, picturesque town surrounded by lush forests and enchanting landscapes. Growing up in harmony with nature, she developed her deep appreciation for the beauty that surrounds her. Her caring nature and ability to connect with animals earned her many friends, both human and furry alike."
self.system_status_check()
self.vprint('All available systems initialized. Controller ready and on standby.')
def load_config(self, config_path='config.json'):
try:
with open(config_path) as config_file:
config = json.load(config_file)
except Exception as e:
raise Exception(f'Unable to load config file, ensure you have a config.json in the relative root directory: {e} or specify a config file path as an argument for the class controller().')
self.verbose = config['verbose']
self.log = config['log']
self.assistant_name = config['assistant_name']
self.memory_enabled = config['memory']['enabled']
if self.memory_enabled:
self.memory_path = config['memory']['path']
self.language_enabled = config['language']['enabled']
self.language_lora_enabled = config['language']['lora']['enabled']
if self.language_enabled:
self.language_model_path = config['language']['model_path']
self.max_tokens = config['language']['max_tokens']
self.temperature = config['language']['temperature']
self.context_size = config['language']['context_size']
self.virtual_context_limit = config['language']['virtual_context_limit']
self.personality_prompt = config['language']['personality_prompt']
self.model_file_ext = config['language']['model_file_ext']
self.format = config['language']['format']
self.top_k = config['language']['top_k']
self.top_p = config['language']['top_p']
self.gpu_layers = config['language']['gpu_layers']
self.main_gpu = config['language']['main_gpu']
self.threads = config['language']['threads']
self.system_prompt = config['language']['system_prompt']
if self.language_lora_enabled:
self.lora_model_path = config['language']['lora']['model_path']
self.vision_enabled = config['vision']['enabled']
if self.vision_enabled:
self.vision_standalone_clip_enabled = config['vision']['standalone_clip']['enabled']
if self.vision_standalone_clip_enabled:
self.vision_standalone_clip_model = config['vision']['standalone_clip']['model_path']
self.llava_enabled = config['vision']['llava']['enabled']
if self.llava_enabled:
self.llava_clip_model = config['vision']['llava']['clip_path']
if self.vision_standalone_clip_enabled and self.llava_enabled:
raise Exception('Both standalone_clip and llava are enabled, only one can be enabled at a time.')
self.tts_enabled = config['tts']['enabled']
if self.tts_enabled:
self.text_to_speech_model = config['tts']['model_path']
self.tts_temperature = config['tts']['temperature']
self.stt_enabled = config['stt']['enabled']
if self.stt_enabled:
self.speech_to_text_model = config['stt']['model_path']
self.modules_enabled = config['modules']['enabled']
self.modules_override_debug = config['modules']['override_debug']
self.naive_bayes_enabled = config['modules']['naive_bayes']['enabled']
if self.modules_enabled or self.modules_override_debug:
if self.naive_bayes_enabled:
self.modules_vectorizer = config['modules']['naive_bayes']['vectorizer_path']
self.modules_model = config['modules']['naive_bayes']['model_path']
self.modules_json_path = config['modules']['json_path']
self.modules_llm_model = config['modules']['llm_model_path']
self.weeb = config['weeb']
self.use_gpu = config['use_gpu']
self.debug = config['debug']
def vprint(self, print_content, log_type=logging.INFO):
if self.verbose or self.debug:
print(f'controller: {print_content}')
if self.log or self.debug:
self.logger.log(log_type, f'controller: {print_content}')
def evaluate(self, input):
if self.eval_mode:
self.vprint(f'Recieved evaluation request: {input}')
output = eval(str(input))
return output
else:
self.vprint(f'Evaluation disabled, ignoring request: {input}')
return 'Evaluation disabled. Enable debug mode to use evaluation.'
def system_status_check(self):
self.vprint('Checking system status...')
status = {
'memory': self.memory_enabled,
'language': self.language_enabled,
'vision': self.vision_enabled,
'tts': self.tts_enabled,
'stt': self.stt_enabled,
'modules': self.modules_enabled,
'weeb': self.weeb,
'debug': self.debug,
'verbose': self.verbose,
'log': self.log,
'eval_mode': self.eval_mode
}
self.vprint(f'System status: {status}')
return status
def clean_output(self, input):
# matches = re.findall(r'\{[^}]*\}', remove_formatting)
# valid_json_matches = [match for match in matches if json.loads(match, strict=False)]
# output = ''.join(valid_json_matches)
remove_formatting = input.replace("<0x0A>", "")
output = re.sub(r'^[^{]*|[^}]*$', '', remove_formatting)
return output
def shutdown(self):
self.vprint('Shutting down...')
exit()
def clear_conversation(self):
self.current = []
self.vprint('Conversation cleared.')
def system_prompt_override(self, input=None):
if input:
self.system_prompt = input
self.ovrde_sys_prompt = True
self.vprint(f'System prompt overridden: {input}')
else:
self.system_prompt = None
self.ovrde_sys_prompt = False
self.vprint('System prompt override disabled.')
def user_cmds(self, input):
try:
args = shlex.split(input)
except ValueError as e:
if str(e) == "No closing quotation":
self.vprint("Warning: Your input contains an unclosed quote. Processing as a single string.")
args = [input]
else:
raise
command = args[0]
if command in self.user_cmds_map:
args = args[1:]
self.user_cmds_map[command](*args)
return True
def full_pipeline(self, listen_input, img=None):
self.vprint(f'Full system initiated, processing speech input...')
if self.stt_enabled:
user_input = self.listen(listen_input)
else:
self.vprint(f'STT is disabled on the server, cannot process voice input, enable in config.json, ignoring input.', logging.ERROR)
raise Exception('STT is disabled on the server, cannot process voice input, enable in config.json, ignoring input.')
if self.user_cmds(user_input):
return user_input, f'User command detected: {user_input}', ''
self.vprint(f'Speech input: {user_input}')
if self.modules_enabled:
self.module_pipeline(user_input)
if img:
if self.vision_enabled:
self.vprint(f'Processing image input...')
desc = self.see(img)
output = self.generate_response(f'{user_input} [user sent an image: {desc}]')
else:
self.vprint(f'Vision disabled, skipping image processing...')
desc = None
output = self.generate_response(f'{user_input} [user sent an image but it could not be processed since vision is disabled.]')
output = self.generate_response(user_input)
if self.tts_enabled:
audio_output = self.speak(output)
else:
audio_output = None
self.vprint(f'Full system completed, returning response: {output}')
return user_input, output, audio_output
def text_pipeline(self, input, img=None):
self.vprint(f'Text pipeline initiated, processing input: {input}')
if self.user_cmds(input):
return input, f'User command detected: {input}', ''
if self.modules_enabled:
self.module_pipeline(input)
if img:
if self.vision_enabled:
self.vprint(f'Processing image input...')
desc = self.see(img)
output = self.generate_response(f'{input} [user sent an image: {desc}]')
else:
self.vprint(f'Vision disabled, skipping image processing...')
desc = None
output = self.generate_response(f'{input} [user sent an image but it could not be processed since vision is disabled.]')
output = self.generate_response(input)
if self.tts_enabled:
audio_output = self.speak(output)
else:
audio_output = None
self.vprint(f'Text pipeline completed, returning response: {output}')
return input, output, audio_output
def generate_response(self, user_input):
self.vprint(f'Generating response: {user_input}')
mod_prompt = None
if self.memory_enabled:
past = self.memory.remember(user_input)
self.vprint(f'Past conversation chosen: {past}')
else:
past = ["No applicable past conversation."]
self.vprint(f'Memory disabled, skipping past conversation selection...')
if self.modules_enabled:
if self.module_output:
self.vprint(f'Module output detected, including in prompt: {self.module_output}')
mod_prompt = self.module_output
if self.ovrde_sys_prompt:
system_prompt = self.system_prompt
else:
system_prompt = '\n'.join([
self.personality_prompt if self.personality_prompt else '',
f'You are a helpful assistant named {self.assistant_name}. Developed by Expl0dingCat, your code is open source and licensed under the MIT License, it can be found here: https://github.com/Expl0dingCat/Ame .You may use any of the following information to aid you in your responses:',
f'Current time: {datetime.now().strftime("%H:%M:%S")}',
f'Current date: {datetime.now().strftime("%d/%m/%Y")}',
f'Extra information: {mod_prompt}' if mod_prompt else '',
f'{self.assistant_name} remembers this past conversation that may be relevant to the current conversation:',
str(past),
])
prompt = [
{
"role": "system",
"content": system_prompt
},
]
if self.current:
for i in self.current:
prompt.append(i)
prompt.append({
"role": "user",
"content": user_input
})
self.vprint(f'Prompt: {prompt}')
if self.language_enabled:
token_amt = self.ai.get_token_amt(str(prompt))
if token_amt > self.virtual_context_limit:
if self.current:
self.vprint(f'Prompt usage exceeded virtual context limit of {self.virtual_context_limit} ({token_amt}). Earliest message ("{str(self.current[0])}") in conversation dropped from short-term memory.')
self.current.pop(0)
else:
self.vprint(f'Prompt usage exceeded virtual context limit of {self.virtual_context_limit} ({token_amt}). No messages in conversation to drop from short-term memory, dropping past conversation memory from prompt.')
past = 'None'
self.vprint(f'Starting response generation...')
text, full, prompt_usage, response_usage = self.ai.generate(prompt, tokens=self.max_tokens, temp=self.temperature, top_p=self.top_p, top_k=self.top_k)
self.vprint(f'Response generated: {text}, prompt usage: {prompt_usage}, response usage: {response_usage}')
if not text:
self.vprint('No response generated.', logging.INFO)
elif text == '':
self.vprint('Empty response generated.', logging.INFO)
elif text == '[end]':
self.current = []
self.vprint('Conversation ended. Short-term memory cleared.')
else:
self.current.append(prompt[-1])
self.current.append(full)
if self.memory_enabled:
self.memory.memorize([prompt[-1], full])
self.memory.save_memory(self.memory_path)
else:
self.vprint('Memory disabled, skipping memory saving...')
self.vprint('Response and prompt saved to long term memory (if enabled). Returning response.')
self.module_output = None
return text
else:
self.vprint(f'Languge model disabled, unable to generate response. Enable language in config.json to use.', logging.ERROR)
raise Exception('Language model is disabled, unable to generate response. Enable language in config.json to use.')
def module_pipeline(self, uinput):
detected, args = self.detect_module(uinput)
if detected:
if args:
self.module_output = self.run_module(detected, args)
else:
self.module_output = self.run_module(detected)
else:
self.module_output = None
self.vprint(f'Module pipeline completed, output: {self.module_output}')
def detect_module(self, user_input):
if self.modules_enabled:
self.vprint(f'Module detection initiated, processing input: {user_input}')
if self.modules.detectable_available:
self.vprint(f'Looking for models detectable via naive bayes classifier: {self.modules.get_detectable_modules()}')
output, probability = self.modules.predict_module(user_input)
else:
self.vprint(f'No detectable modules found (naive bayes classifier disabled?), skipping detection with classifier.')
output = None
probability = 100.0
with_args = []
if not output:
self.vprint(f'No module detected, probability: {probability}')
undetectable_modules = self.modules.get_undetectable_modules()
if undetectable_modules:
self.vprint(f"Undetectable modules found: {undetectable_modules}, secondary check required. Initiating LLM-based module detection...")
self.vprint(f"WARNING: Undetectable modules will slow down prompts due to the extra processing time required for checking if called. This is not recommended.", logging.WARNING)
self.vprint(f"Passing input to LLM to detect modules among {undetectable_modules}")
for i in undetectable_modules:
if self.modules.get_arguments(i):
arguments = ', '.join(self.modules.get_arguments(i))
with_args += [f'{i} ({arguments})']
else:
with_args += [i]
if self.language_enabled:
llm_input = str({"user_prompt": str(user_input), "modules": with_args})
self.vprint(f'Input to LLM: {llm_input}')
context = self.current[-5:] # get last 5 entries for context
system_prompt = f'Predict which module is being called (and extract arguments) based on user input, return module name and arguments in proper JSON format. Return null if no module is detected, the module detected is not listed or if no arguments are needed. Context is given prior to the user input, please use that to determine modules.'
prompt = [
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": '{"user_prompt": "Whats the weather like in London right now?","Modules": ["weather (city)", "translate (text)", "lighting_control"]}'
},
{
"role": "assistant",
"content": '{"module": "weather", "args": {"city": "London"}}'
},
{
"role": "user",
"content": '{"user_prompt": "Hello!","modules": ["weather (city)", "translate (text)"]}'
},
{
"role": "assistant",
"content": '{"module": null, "args": null}'
},
{
"role": "user",
"content": '{"user_prompt": "Could you turn off the lights?", "modules": ["lighting_control"]}'
},
{
"role": "assistant",
"content": '{"module": "lighting_control", "args": null}'
},
*context,
{
"role": "user",
"content": llm_input
}
]
self.vprint(f'Starting response generation for module detection...')
text, full, prompt_usage, response_usage = self.ai.generate(prompt, tokens=100, temp=0)
try:
clean_text = self.clean_output(text)
self.vprint(f'LLM output (cleaned): "{clean_text}", extracting information...')
llm_output = json.loads(clean_text)
module = llm_output.get('module')
args = llm_output.get('args')
if module is not None:
self.vprint(f'Module detected via LLM: {module}, arguments: {args}, prompt usage: {prompt_usage}, response usage: {response_usage}')
return module, args
else:
self.vprint('No module detected by LLM.')
return None, None
except json.JSONDecodeError as e:
self.vprint(f'Error decoding JSON: {e} (The LLM provided invalid JSON output even after cleaning, skipping module detection)', logging.ERROR)
return None, None
except Exception as e:
self.vprint(f'Error: {e}', logging.error)
return None, None
else:
self.vprint(f'Language model is disabled. Unable to detect modules via LLM.', logging.ERROR)
raise Exception('Language model is disabled. Unable to detect modules via LLM.')
if output:
self.vprint(f'Module detected: {output}, probability: {probability}')
self.vprint(f'Module detection complete, returning module: {output}')
return output, None
else:
self.vprint('Modules disabled, enable modules in config.json to use.', logging.WARNING)
return 'Modules are disabled.'
def get_module_args(self, user_input, module):
if self.modules_enabled:
self.vprint(f'Module argument retrieval initiated, processing module: {module}')
args = self.modules.get_arguments(module)
if self.language_enabled:
llm_input = str({"user_prompt": str(user_input), "module_selected": str(module), "arguments": args})
self.vprint(f'Input to LLM: {llm_input}')
system_prompt = 'Extract arguments from a user prompt based on the module selected. Return arguments in proper JSON format. Return null if no arguments are needed.'
prompt = [
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": '{"user_prompt": "Whats the weather like in London right now?", "module_selected": "weather", "arguments": ["city"]}}'
},
{
"role": "assistant",
"content": '{"args": [{"city": "London"}]}'
},
{
"role": "user",
"content": llm_input
}
]
self.vprint(f'Starting response generation for module detection...')
text, full, prompt_usage, response_usage = self.ai.generate(prompt, max_tokens=500, temperature=0)
self.vprint(f'LLM output: {text}, prompt usage: {prompt_usage}, response usage: {response_usage}')
try:
llm_output = json.loads(text)
args = llm_output['args']
except json.decoder.JSONDecodeError:
self.vprint(f'LLM output is not valid JSON, unable to detect modules via LLM.', logging.ERROR)
return None, None
else:
self.vprint(f'Language model is disabled. Unable to detect modules via LLM.', logging.ERROR)
raise Exception('Language model is disabled. Unable to detect modules via LLM.')
self.vprint(f'Module arguments retrieved: {args}')
return args
def run_module(self, module, args, **kwargs):
if self.modules_enabled:
self.vprint(f'Module system initiated, processing module: {module} with arguments: {args} / {kwargs}')
if args is not None:
output = self.modules.use_module(module, args, **kwargs)
else:
output = self.modules.use_module(module, **kwargs)
self.vprint(f'Module output: {output}')
return output
else:
self.vprint('Modules disabled, enable modules in config.json to use.', logging.WARNING)
return 'Modules are disabled.'
def speak(self, input):
if self.tts_enabled:
if not input:
self.vprint('No input text given.', logging.ERROR)
return 'No input text given.'
self.vprint('Generating audio output...')
output = self.tts.generate(input)
self.vprint('Audio output generated.')
return output
else:
self.vprint('TTS disabled, enable tts in config.json to use.', logging.WARNING)
return 'TTS is disabled.'
def listen(self, input):
if self.stt_enabled:
if not input:
self.vprint('No input audio file specified.', logging.ERROR)
return 'No input audio file specified.'
self.vprint('Listening to audio input...')
output = self.stt.transcribe(input)
self.vprint(f'Transcription received: {output}')
return output
else:
self.vprint('STT disabled, enable stt in config.json to use.', logging.WARNING)
return 'STT is disabled.'
def see(self, input):
if self.vision_enabled:
if not input:
self.vprint('No input image specified.', logging.ERROR)
return 'No input image specified.'
self.vprint('Analyzing image...')
output = self.vision.describe(input)
self.vprint(f'Image analysis complete: {output}')
return output
else:
self.vprint('Vision disabled, enable vision in config.json to use.', logging.WARNING)
return 'Vision is disabled.'
if __name__ == '__main__':
print('The controller is not meant to be run directly, use one of the interfaces to interact with Ame.')
pass