93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
# LLM chat for T-Deck CicruitPython version
|
|
# (also can be run on MicroPython and usual CPython)
|
|
# On the T-Deck, requires to be run from tdeck_repl for the
|
|
# keyboard to work correctly
|
|
# Created by Luxferre in 2025, released into public domain
|
|
|
|
from deck.chat import DeckChat
|
|
from deck.llm import LLMChat
|
|
|
|
CONFIG_FILE = 'llmcfg.json'
|
|
|
|
# chat handlers
|
|
|
|
def send_msg(msg, state):
|
|
print("Please wait for the answer...")
|
|
status, ans = state.get_completion(msg)
|
|
return ans, state
|
|
|
|
def set_sys_prompt(prompt, state):
|
|
if prompt:
|
|
state.config['system_prompt'] = prompt
|
|
state.save_config()
|
|
state.reset_context()
|
|
return 'System prompt updated', state
|
|
else:
|
|
return f"System: {state.config['system_prompt']}", state
|
|
|
|
def set_temp(temp, state):
|
|
if temp:
|
|
state.config['temperature'] = float(temp)
|
|
state.save_config()
|
|
return 'Temperature updated', state
|
|
else:
|
|
return f"Temperature: {state.config['temperature']}", state
|
|
|
|
def set_prov(prov, state):
|
|
if prov and prov in state.config.get('providers', {}):
|
|
state.config['active_provider'] = prov
|
|
state.setup_provider()
|
|
state.save_config()
|
|
state.reset_context()
|
|
return 'Provider updated', state
|
|
else:
|
|
return f"Active provider: {state.config['active_provider']}", state
|
|
|
|
def set_model(model, state):
|
|
if model:
|
|
state.provider['model'] = model
|
|
state.save_config()
|
|
return 'Model updated', state
|
|
else:
|
|
return f"Active model: {state.provider.get('model')}", state
|
|
|
|
def list_models(stub, state):
|
|
status, mlist = state.list_models()
|
|
if status:
|
|
resp = f"Model list for {state.config['active_provider']}:"
|
|
for m in mlist:
|
|
resp += f'\n- {m}'
|
|
else:
|
|
resp = mlist
|
|
return resp, state
|
|
|
|
def list_providers(stub, state):
|
|
resp = 'Provider list:'
|
|
for p in state.config.get('providers', {}):
|
|
resp += f'\n - {p}'
|
|
return resp, state
|
|
|
|
def display_help(stub, state):
|
|
resp = "/clear, /system, /temp, /prov, /model, /modellist, /provlist, /exit"
|
|
return resp, state
|
|
|
|
def clear_ctx(stub, state):
|
|
state.reset_context()
|
|
return 'Context reset', state
|
|
|
|
# main initialization
|
|
llm = LLMChat(config_file=CONFIG_FILE, message_limit=20)
|
|
chat = DeckChat(start_state=llm, chat_prefix='> ')
|
|
chat.command('default', send_msg)
|
|
chat.command('clear', clear_ctx)
|
|
chat.command('system', set_sys_prompt)
|
|
chat.command('temp', set_temp)
|
|
chat.command('prov', set_prov)
|
|
chat.command('model', set_model)
|
|
chat.command('modellist', list_models)
|
|
chat.command('provlist', list_providers)
|
|
chat.command('help', display_help)
|
|
chat.command('exit', lambda m,s: ('Exiting...', None))
|
|
chat.command('quit', lambda m,s: ('Exiting...', None))
|
|
chat.start()
|