Download chat/api/routes.py from Codexcoder/sovereign-integration: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/datasets/Codexcoder/sovereign-integration/resolve/main/chat/api/routes.py
- Command line
-
hf download hf://datasets/Codexcoder/sovereign-integration/chat/api/routes.py
-
curl -L -o routes.py https://huggingface.co/datasets/Codexcoder/sovereign-integration/resolve/main/chat/api/routes.py
3.34 kB
| # chat/api/routes.py | |
| """ | |
| DeepSeek V4 chat API — endpoints under /api/v1/chat/*. | |
| Sessions are held in memory (bounded history). Model calls go through | |
| DeepSeekV4Client — API mode with DEEPSEEK_API_KEY, local echo otherwise. | |
| """ | |
| from __future__ import annotations | |
| import time | |
| from typing import Any, Dict, List | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel, Field | |
| from chat.core import SOVEREIGN_SYSTEM_PROMPT, ChatSession, get_client | |
| router = APIRouter(prefix="/api/v1/chat", tags=["chat"]) | |
| sessions: Dict[str, ChatSession] = {} | |
| class SendIn(BaseModel): | |
| message: str = Field(min_length=1) | |
| session_id: str = "" | |
| temperature: float = 0.7 | |
| max_tokens: int = 1024 | |
| class SessionOut(BaseModel): | |
| session_id: str | |
| model: str | |
| messages: List[Dict[str, Any]] | |
| created_at: float | |
| def _get_or_create(session_id: str) -> ChatSession: | |
| if session_id and session_id in sessions: | |
| return sessions[session_id] | |
| session = ChatSession() | |
| session.add("system", SOVEREIGN_SYSTEM_PROMPT) | |
| sessions[session.session_id] = session | |
| return session | |
| async def create_session() -> Dict[str, Any]: | |
| """Create a new chat session.""" | |
| session = _get_or_create("") | |
| return { | |
| "session_id": session.session_id, | |
| "model": session.model, | |
| "mode": "api" if not get_client().local_mode else "local", | |
| "created_at": session.created_at, | |
| } | |
| async def get_session(session_id: str) -> Dict[str, Any]: | |
| """Session + message history.""" | |
| session = sessions.get(session_id) | |
| if session is None: | |
| raise HTTPException(status_code=404, detail="session not found") | |
| return { | |
| "session_id": session.session_id, | |
| "model": session.model, | |
| "messages": [{"role": m.role, "content": m.content, "ts": m.timestamp} | |
| for m in session.messages], | |
| "created_at": session.created_at, | |
| } | |
| async def send_message(body: SendIn) -> Dict[str, Any]: | |
| """Send a message and get an assistant reply.""" | |
| client = get_client() | |
| session = _get_or_create(body.session_id) | |
| session.add("user", body.message) | |
| try: | |
| result = client.complete(session, temperature=body.temperature, | |
| max_tokens=body.max_tokens) | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=502, detail=str(exc)) from exc | |
| session.add("assistant", result["content"]) | |
| return { | |
| "session_id": session.session_id, | |
| "reply": result["content"], | |
| "model": result["model"], | |
| "mode": result["mode"], | |
| "usage": result.get("usage", {}), | |
| "timestamp": time.time(), | |
| } | |
| async def list_models() -> Dict[str, Any]: | |
| """Available models.""" | |
| return {"models": get_client().list_models(), | |
| "active": get_client().model, | |
| "mode": "api" if not get_client().local_mode else "local"} | |
| async def chat_status() -> Dict[str, Any]: | |
| """Chat module status.""" | |
| client = get_client() | |
| return { | |
| "mode": "api" if not client.local_mode else "local", | |
| "model": client.model, | |
| "base_url": client.base_url, | |
| "sessions": len(sessions), | |
| } | |