76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
from pathlib import Path
|
|
p = Path('/root/homelab-brain/homelab-ai-bot/llm.py')
|
|
text = p.read_text()
|
|
|
|
# 1. Replace model config block
|
|
old_models = '''MODEL = openai/gpt-4o-mini
|
|
VISION_MODEL = openai/gpt-4o
|
|
MAX_TOOL_ROUNDS = 3'''
|
|
|
|
new_models = '''OLLAMA_BASE = http://100.84.255.83:11434
|
|
OPENROUTER_BASE = https://openrouter.ai/api/v1
|
|
|
|
MODEL = qwen2.5:14b
|
|
VISION_MODEL = openai/gpt-4o
|
|
MAX_TOOL_ROUNDS = 3'''
|
|
|
|
text = text.replace(old_models, new_models)
|
|
|
|
# 2. Add date to system prompt (right after first line)
|
|
old_prompt_start = 'SYSTEM_PROMPT = """Du bist der Hausmeister-Bot fuer ein Homelab. Deutsch, kurz, direkt, operativ.'
|
|
new_prompt_start = '''SYSTEM_PROMPT = f"""Du bist der Hausmeister-Bot fuer ein Homelab. Deutsch, kurz, direkt, operativ.
|
|
Heutiges Datum: {__import__('datetime').date.today().strftime('%d. %B %Y')}.'''
|
|
text = text.replace(old_prompt_start, new_prompt_start)
|
|
|
|
# 3. Replace _call_openrouter to support both backends
|
|
old_call = '''def _call_openrouter(messages: list, api_key: str, use_tools: bool = True,
|
|
model: str = None, max_tokens: int = 600) -> dict:
|
|
payload = {
|
|
model: model or MODEL,
|
|
messages: messages,
|
|
max_tokens: max_tokens,
|
|
}
|
|
if use_tools:
|
|
payload[tools] = TOOLS
|
|
payload[tool_choice] = auto
|
|
|
|
r = requests.post(
|
|
https://openrouter.ai/api/v1/chat/completions,
|
|
headers={Authorization: fBearer {api_key}},
|
|
json=payload,
|
|
timeout=90,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()'''
|
|
|
|
new_call = '''def _call_openrouter(messages: list, api_key: str, use_tools: bool = True,
|
|
model: str = None, max_tokens: int = 600) -> dict:
|
|
chosen_model = model or MODEL
|
|
use_ollama = chosen_model == MODEL
|
|
|
|
payload = {
|
|
model: chosen_model,
|
|
messages: messages,
|
|
max_tokens: max_tokens,
|
|
}
|
|
if use_tools:
|
|
payload[tools] = TOOLS
|
|
payload[tool_choice] = auto
|
|
|
|
if use_ollama:
|
|
url = f{OLLAMA_BASE}/v1/chat/completions
|
|
headers = {}
|
|
timeout = 180
|
|
else:
|
|
url = f{OPENROUTER_BASE}/chat/completions
|
|
headers = {Authorization: fBearer {api_key}}
|
|
timeout = 90
|
|
|
|
r = requests.post(url, headers=headers, json=payload, timeout=timeout)
|
|
r.raise_for_status()
|
|
return r.json()'''
|
|
|
|
text = text.replace(old_call, new_call)
|
|
|
|
p.write_text(text)
|
|
print('OK')
|