昨日に続き、LLMを使用しているDiscordボットをLLM Broker対応にしていきます。
今日の一発目はAIチャットボット。

LLM Broker呼び出し部はこんなコードです。
LLM_PROFILEとLLM_BROKER_URLは、設定ファイルやdocker-composeファイルから読み込む想定です。
async def get_llm_response(messages_to_send):
"""Gets a response from LLM Broker."""
payload = {
"profile": LLM_PROFILE,
"messages": messages_to_send,
}
try:
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
response = await asyncio.wait_for(
client.post(f"{LLM_BROKER_URL}/v1/chat", json=payload),
timeout=TIMEOUT,
)
response.raise_for_status()
data = response.json()
meta = data.get("meta") or data.get("metadata") or {}
if isinstance(meta, dict):
logger.info(
"llm_call_done: profile=%s backend=%s model=%s",
data.get("profile") or meta.get("profile") or LLM_PROFILE,
data.get("backend") or meta.get("backend"),
data.get("model") or meta.get("model"),
)
else:
logger.info("llm_call_done: profile=%s", LLM_PROFILE)
return extract_broker_text(data).strip()
except asyncio.TimeoutError:
logger.warning("llm_call_timeout: profile=%s timeout=%.1f", LLM_PROFILE, TIMEOUT)
return "タイムアウトしました。もう一度お試しください。"
except httpx.HTTPStatusError as e:
body = e.response.text[:500] if e.response is not None else ""
logger.error(
"llm_call_http_error: status=%s profile=%s body=%r",
e.response.status_code if e.response is not None else None,
LLM_PROFILE,
body,
)
return f"LLM Brokerでエラーが発生しました: HTTP {e.response.status_code}"
except Exception as e:
logger.exception("llm_call_error: profile=%s error=%s", LLM_PROFILE, e)
return f"エラーが発生しました: {e}"
LLM_PROFILEは、LLM Broker側で管理している、このファイルに設定されているもので、AIチャットボットが使っているのが、text_conversationというプロフィールです。
# llm-broker/backend/profiles.yaml
profiles:
text_extract:
backend: llamacpp
base_url: http://xxxxx
model: gpt-oss-20b-Q4_K_M
temperature: 0.1
timeout_sec: 360
text_conversation:
backend: llamacpp
base_url: http://xxxxx
model: gpt-oss-20b-Q4_K_M
temperature: 0.7
timeout_sec: 360
text_extract_ollama_fallback:
backend: ollama
base_url: http://xxxxx
model: gpt-oss:20b
temperature: 0.1
timeout_sec: 360
text_conversation_ollama_fallback:
backend: ollama
base_url: http://xxxxx
model: gpt-oss:20b
temperature: 0.7
timeout_sec: 360
「AIチャットボットをLLM Broker対応にする」への1件のフィードバック