Files
t-deckard/app/llmchat.py
T

93 lines
2.6 KiB
Python
Raw Normal View History

2025-12-20 18:28:43 +02:00
# LLM chat for T-Deck CicruitPython version
2025-12-21 08:42:07 +02:00
# (also can be run on MicroPython and usual CPython)
2025-12-20 18:28:43 +02:00
# 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
2025-12-20 22:37:45 +02:00
from deck.chat import DeckChat
from deck.llm import LLMChat
2025-12-20 18:28:43 +02:00
CONFIG_FILE = 'llmcfg.json'
2025-12-20 22:37:45 +02:00
# chat handlers
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
def send_msg(msg, state):
print("Please wait for the answer...")
status, ans = state.get_completion(msg)
return ans, state
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
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
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
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
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
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
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
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
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
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
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
def list_providers(stub, state):
resp = 'Provider list:'
for p in state.config.get('providers', {}):
resp += f'\n - {p}'
return resp, state
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
def display_help(stub, state):
resp = "/clear, /system, /temp, /prov, /model, /modellist, /provlist, /exit"
return resp, state
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
def clear_ctx(stub, state):
state.reset_context()
return 'Context reset', state
2025-12-20 18:28:43 +02:00
2025-12-20 22:37:45 +02:00
# main initialization
llm = LLMChat(config_file=CONFIG_FILE, message_limit=20)
2025-12-21 14:05:03 +02:00
chat = DeckChat(start_state=llm, chat_prefix='> ')
2025-12-20 22:37:45 +02:00
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)
2025-12-20 22:50:08 +02:00
chat.command('exit', lambda m,s: ('Exiting...', None))
chat.command('quit', lambda m,s: ('Exiting...', None))
2025-12-20 22:37:45 +02:00
chat.start()