ai: align web chat modes with atlasbot and remove stock path
This commit is contained in:
parent
94806c4b42
commit
a2deb0fcd5
@ -18,7 +18,6 @@ def register(app) -> None:
|
|||||||
def ai_chat() -> Any:
|
def ai_chat() -> Any:
|
||||||
payload = request.get_json(silent=True) or {}
|
payload = request.get_json(silent=True) or {}
|
||||||
user_message = (payload.get("message") or "").strip()
|
user_message = (payload.get("message") or "").strip()
|
||||||
history = payload.get("history") or []
|
|
||||||
profile = (payload.get("profile") or payload.get("mode") or "atlas-quick").strip().lower()
|
profile = (payload.get("profile") or payload.get("mode") or "atlas-quick").strip().lower()
|
||||||
conversation_id = payload.get("conversation_id") if isinstance(payload.get("conversation_id"), str) else ""
|
conversation_id = payload.get("conversation_id") if isinstance(payload.get("conversation_id"), str) else ""
|
||||||
|
|
||||||
@ -26,13 +25,13 @@ def register(app) -> None:
|
|||||||
return jsonify({"error": "message required"}), 400
|
return jsonify({"error": "message required"}), 400
|
||||||
|
|
||||||
started = time.time()
|
started = time.time()
|
||||||
if profile in {"stock", "stock-ai", "stock_ai"}:
|
mode = "quick"
|
||||||
reply = _stock_answer(user_message, history)
|
if profile in {"atlas-smart", "smart"}:
|
||||||
source = "stock"
|
mode = "smart"
|
||||||
else:
|
elif profile in {"atlas-genius", "genius"}:
|
||||||
mode = "smart" if profile in {"atlas-smart", "smart"} else "quick"
|
mode = "genius"
|
||||||
reply = _atlasbot_answer(user_message, mode, conversation_id)
|
reply = _atlasbot_answer(user_message, mode, conversation_id)
|
||||||
source = f"atlas-{mode}"
|
source = f"atlas-{mode}"
|
||||||
if reply:
|
if reply:
|
||||||
elapsed_ms = int((time.time() - started) * 1000)
|
elapsed_ms = int((time.time() - started) * 1000)
|
||||||
return jsonify({"reply": reply, "latency_ms": elapsed_ms, "source": source})
|
return jsonify({"reply": reply, "latency_ms": elapsed_ms, "source": source})
|
||||||
@ -76,37 +75,6 @@ def _atlasbot_answer(message: str, mode: str, conversation_id: str) -> str:
|
|||||||
except (httpx.RequestError, ValueError):
|
except (httpx.RequestError, ValueError):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def _stock_answer(message: str, history: list[dict[str, Any]]) -> str:
|
|
||||||
body = {
|
|
||||||
"model": settings.AI_CHAT_MODEL,
|
|
||||||
"messages": _build_messages(message, history),
|
|
||||||
"stream": False,
|
|
||||||
}
|
|
||||||
try:
|
|
||||||
with httpx.Client(timeout=settings.AI_CHAT_TIMEOUT_SEC) as client:
|
|
||||||
resp = client.post(f"{settings.AI_CHAT_API}/api/chat", json=body)
|
|
||||||
resp.raise_for_status()
|
|
||||||
data = resp.json()
|
|
||||||
except (httpx.RequestError, ValueError):
|
|
||||||
return ""
|
|
||||||
message_data = data.get("message") if isinstance(data, dict) else None
|
|
||||||
if isinstance(message_data, dict) and message_data.get("content"):
|
|
||||||
return str(message_data["content"]).strip()
|
|
||||||
if isinstance(data, dict) and data.get("response"):
|
|
||||||
return str(data["response"]).strip()
|
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
def _build_messages(message: str, history: list[dict[str, Any]]) -> list[dict[str, str]]:
|
|
||||||
messages = [{"role": "system", "content": settings.AI_CHAT_SYSTEM_PROMPT}]
|
|
||||||
for entry in history:
|
|
||||||
role = entry.get("role")
|
|
||||||
content = entry.get("content")
|
|
||||||
if role in {"user", "assistant"} and isinstance(content, str) and content.strip():
|
|
||||||
messages.append({"role": role, "content": content})
|
|
||||||
messages.append({"role": "user", "content": message})
|
|
||||||
return messages
|
|
||||||
|
|
||||||
|
|
||||||
def _discover_ai_meta(profile: str) -> dict[str, str]:
|
def _discover_ai_meta(profile: str) -> dict[str, str]:
|
||||||
meta = {
|
meta = {
|
||||||
@ -119,12 +87,12 @@ def _discover_ai_meta(profile: str) -> dict[str, str]:
|
|||||||
if profile in {"atlas-smart", "smart"}:
|
if profile in {"atlas-smart", "smart"}:
|
||||||
meta["model"] = settings.AI_ATLASBOT_MODEL_SMART or settings.AI_CHAT_MODEL
|
meta["model"] = settings.AI_ATLASBOT_MODEL_SMART or settings.AI_CHAT_MODEL
|
||||||
meta["endpoint"] = "/api/ai/chat"
|
meta["endpoint"] = "/api/ai/chat"
|
||||||
|
elif profile in {"atlas-genius", "genius"}:
|
||||||
|
meta["model"] = settings.AI_ATLASBOT_MODEL_GENIUS or settings.AI_CHAT_MODEL
|
||||||
|
meta["endpoint"] = "/api/ai/chat"
|
||||||
elif profile in {"atlas-quick", "quick"}:
|
elif profile in {"atlas-quick", "quick"}:
|
||||||
meta["model"] = settings.AI_ATLASBOT_MODEL_FAST or settings.AI_CHAT_MODEL
|
meta["model"] = settings.AI_ATLASBOT_MODEL_FAST or settings.AI_CHAT_MODEL
|
||||||
meta["endpoint"] = "/api/ai/chat"
|
meta["endpoint"] = "/api/ai/chat"
|
||||||
elif profile in {"stock", "stock-ai", "stock_ai"}:
|
|
||||||
meta["model"] = settings.AI_CHAT_MODEL
|
|
||||||
meta["endpoint"] = "/api/ai/chat"
|
|
||||||
|
|
||||||
sa_path = Path("/var/run/secrets/kubernetes.io/serviceaccount")
|
sa_path = Path("/var/run/secrets/kubernetes.io/serviceaccount")
|
||||||
token_path = sa_path / "token"
|
token_path = sa_path / "token"
|
||||||
|
|||||||
@ -31,6 +31,7 @@ AI_ATLASBOT_TOKEN = os.getenv("AI_ATLASBOT_TOKEN", "").strip()
|
|||||||
AI_ATLASBOT_TIMEOUT_SEC = float(os.getenv("AI_ATLASBOT_TIMEOUT_SEC", "5"))
|
AI_ATLASBOT_TIMEOUT_SEC = float(os.getenv("AI_ATLASBOT_TIMEOUT_SEC", "5"))
|
||||||
AI_ATLASBOT_MODEL_FAST = os.getenv("AI_ATLASBOT_MODEL_FAST", "").strip()
|
AI_ATLASBOT_MODEL_FAST = os.getenv("AI_ATLASBOT_MODEL_FAST", "").strip()
|
||||||
AI_ATLASBOT_MODEL_SMART = os.getenv("AI_ATLASBOT_MODEL_SMART", "").strip()
|
AI_ATLASBOT_MODEL_SMART = os.getenv("AI_ATLASBOT_MODEL_SMART", "").strip()
|
||||||
|
AI_ATLASBOT_MODEL_GENIUS = os.getenv("AI_ATLASBOT_MODEL_GENIUS", "").strip()
|
||||||
AI_NODE_NAME = os.getenv("AI_CHAT_NODE_NAME") or os.getenv("AI_NODE_NAME") or "ai-cluster"
|
AI_NODE_NAME = os.getenv("AI_CHAT_NODE_NAME") or os.getenv("AI_NODE_NAME") or "ai-cluster"
|
||||||
AI_GPU_DESC = os.getenv("AI_CHAT_GPU_DESC") or "local GPU (dynamic)"
|
AI_GPU_DESC = os.getenv("AI_CHAT_GPU_DESC") or "local GPU (dynamic)"
|
||||||
AI_PUBLIC_ENDPOINT = os.getenv("AI_PUBLIC_CHAT_ENDPOINT", "https://chat.ai.bstein.dev/api/chat")
|
AI_PUBLIC_ENDPOINT = os.getenv("AI_PUBLIC_CHAT_ENDPOINT", "https://chat.ai.bstein.dev/api/chat")
|
||||||
|
|||||||
@ -87,7 +87,7 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|||||||
const profiles = [
|
const profiles = [
|
||||||
{ id: "atlas-quick", label: "Atlas Quick" },
|
{ id: "atlas-quick", label: "Atlas Quick" },
|
||||||
{ id: "atlas-smart", label: "Atlas Smart" },
|
{ id: "atlas-smart", label: "Atlas Smart" },
|
||||||
{ id: "stock-ai", label: "Stock AI" },
|
{ id: "atlas-genius", label: "Atlas Genius" },
|
||||||
];
|
];
|
||||||
const activeProfile = ref("atlas-quick");
|
const activeProfile = ref("atlas-quick");
|
||||||
const profileState = reactive(
|
const profileState = reactive(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user