Add shared ai-service container as AI provider intermediary
All feature containers now POST messages to ai-service (port 8010) instead of calling AI providers directly. ai-service routes to LM Studio, Ollama, or Anthropic based on /config/ai_service_config.json. doc-service AI providers removed; replaced by httpx ai_client.py. Backend settings restructured to /api/settings/ai. Frontend gets dedicated AIAdminSettingsPage and AI Service card in AppsPage. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,21 +2,21 @@
|
||||
Per-service runtime config helpers.
|
||||
|
||||
Config files live on the shared `app_config` Docker volume at /config/.
|
||||
Each service has its own JSON file, e.g. /config/doc_service_config.json.
|
||||
Each service has its own JSON file.
|
||||
|
||||
Atomic write pattern: write to .tmp in same dir, then os.replace() so
|
||||
doc-service never reads a partial file.
|
||||
services never read a partial file.
|
||||
"""
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
_CONFIG_DIR = Path(os.environ.get("APP_CONFIG_DIR", "/config"))
|
||||
|
||||
# ── Config schemas ─────────────────────────────────────────────────────────────
|
||||
# ── AI service config schemas ──────────────────────────────────────────────────
|
||||
|
||||
|
||||
class AnthropicConfig(BaseModel):
|
||||
@@ -25,32 +25,34 @@ class AnthropicConfig(BaseModel):
|
||||
|
||||
|
||||
class OllamaConfig(BaseModel):
|
||||
base_url: str = "http://192.168.1.x:11434/v1"
|
||||
base_url: str = "http://host.docker.internal:11434/v1"
|
||||
model: str = "llama3.2"
|
||||
api_key: str = "ollama"
|
||||
|
||||
|
||||
class LMStudioConfig(BaseModel):
|
||||
# host.docker.internal resolves to the host from inside Docker (macOS/Windows).
|
||||
# For local dev outside Docker, use http://localhost:1234/v1 instead.
|
||||
base_url: str = "http://host.docker.internal:1234/v1"
|
||||
model: str = "local-model"
|
||||
api_key: str = "lm-studio"
|
||||
|
||||
|
||||
class AIConfig(BaseModel):
|
||||
class AIServiceConfig(BaseModel):
|
||||
provider: str = "lmstudio"
|
||||
timeout_seconds: int = 60
|
||||
max_retries: int = 2
|
||||
anthropic: AnthropicConfig = AnthropicConfig()
|
||||
ollama: OllamaConfig = OllamaConfig()
|
||||
lmstudio: LMStudioConfig = LMStudioConfig()
|
||||
|
||||
|
||||
# ── Doc service config schemas ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
class DocumentsConfig(BaseModel):
|
||||
max_pdf_bytes: int = 20 * 1024 * 1024
|
||||
|
||||
|
||||
class DocServiceConfig(BaseModel):
|
||||
ai: AIConfig = AIConfig()
|
||||
documents: DocumentsConfig = DocumentsConfig()
|
||||
|
||||
|
||||
@@ -62,14 +64,11 @@ def _mask_key(key: str) -> str:
|
||||
return key[:7] + "••••"
|
||||
|
||||
|
||||
def _mask_config(data: dict) -> dict:
|
||||
"""Return a copy of data with api_key fields masked."""
|
||||
import copy
|
||||
def _mask_ai_config(data: dict) -> dict:
|
||||
masked = copy.deepcopy(data)
|
||||
ai = masked.get("ai", {})
|
||||
for provider in ("anthropic", "ollama", "lmstudio"):
|
||||
if provider in ai and "api_key" in ai[provider]:
|
||||
ai[provider]["api_key"] = _mask_key(ai[provider]["api_key"])
|
||||
if provider in masked and "api_key" in masked[provider]:
|
||||
masked[provider]["api_key"] = _mask_key(masked[provider]["api_key"])
|
||||
return masked
|
||||
|
||||
|
||||
@@ -82,7 +81,8 @@ def _config_path(service: str) -> Path:
|
||||
def load_service_config(service: str) -> dict:
|
||||
path = _config_path(service)
|
||||
if not path.exists():
|
||||
# Return default config if file doesn't exist yet
|
||||
if service == "ai_service":
|
||||
return AIServiceConfig().model_dump()
|
||||
if service == "doc_service":
|
||||
return DocServiceConfig().model_dump()
|
||||
return {}
|
||||
@@ -98,6 +98,24 @@ def save_service_config(service: str, data: dict) -> None:
|
||||
os.replace(tmp, path)
|
||||
|
||||
|
||||
# AI service helpers
|
||||
|
||||
def load_ai_service_config() -> AIServiceConfig:
|
||||
raw = load_service_config("ai_service")
|
||||
return AIServiceConfig.model_validate(raw)
|
||||
|
||||
|
||||
def save_ai_service_config(config: AIServiceConfig) -> None:
|
||||
save_service_config("ai_service", config.model_dump())
|
||||
|
||||
|
||||
def load_ai_service_config_masked() -> dict:
|
||||
raw = load_service_config("ai_service")
|
||||
return _mask_ai_config(raw)
|
||||
|
||||
|
||||
# Doc service helpers
|
||||
|
||||
def load_doc_service_config() -> DocServiceConfig:
|
||||
raw = load_service_config("doc_service")
|
||||
return DocServiceConfig.model_validate(raw)
|
||||
@@ -108,8 +126,7 @@ def save_doc_service_config(config: DocServiceConfig) -> None:
|
||||
|
||||
|
||||
def load_doc_service_config_masked() -> dict:
|
||||
raw = load_service_config("doc_service")
|
||||
return _mask_config(raw)
|
||||
return load_service_config("doc_service")
|
||||
|
||||
|
||||
def _merge_api_key(new_key: str, existing_key: str) -> str:
|
||||
|
||||
@@ -15,6 +15,8 @@ class Settings(BaseSettings):
|
||||
|
||||
CORS_ORIGINS: list[str] = ["http://localhost:5173"]
|
||||
|
||||
AI_SERVICE_URL: str = "http://ai-service:8010"
|
||||
|
||||
@field_validator("JWT_PRIVATE_KEY", "JWT_PUBLIC_KEY", mode="before")
|
||||
@classmethod
|
||||
def expand_newlines(cls, v: str) -> str:
|
||||
|
||||
@@ -6,16 +6,20 @@ Config files live on the shared app_config volume (/config/).
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.core.app_config import (
|
||||
DocServiceConfig,
|
||||
_merge_api_key,
|
||||
load_ai_service_config,
|
||||
load_ai_service_config_masked,
|
||||
load_doc_service_config,
|
||||
load_doc_service_config_masked,
|
||||
save_ai_service_config,
|
||||
save_doc_service_config,
|
||||
)
|
||||
from app.core.config import settings
|
||||
from app.deps import get_current_admin
|
||||
from app.models.user import User
|
||||
|
||||
@@ -41,18 +45,18 @@ class LimitsUpdate(BaseModel):
|
||||
max_pdf_mb: int
|
||||
|
||||
|
||||
# ── Documents settings ─────────────────────────────────────────────────────────
|
||||
# ── AI settings ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.get("/documents")
|
||||
async def get_documents_settings(
|
||||
@router.get("/ai")
|
||||
async def get_ai_settings(
|
||||
_: User = Depends(get_current_admin),
|
||||
) -> dict:
|
||||
return load_doc_service_config_masked()
|
||||
return load_ai_service_config_masked()
|
||||
|
||||
|
||||
@router.patch("/documents/ai")
|
||||
async def update_documents_ai(
|
||||
@router.patch("/ai")
|
||||
async def update_ai_settings(
|
||||
body: AIProviderUpdate,
|
||||
_: User = Depends(get_current_admin),
|
||||
) -> dict:
|
||||
@@ -60,85 +64,70 @@ async def update_documents_ai(
|
||||
if body.provider not in valid_providers:
|
||||
raise HTTPException(status_code=422, detail=f"provider must be one of {valid_providers}")
|
||||
|
||||
config = load_doc_service_config()
|
||||
|
||||
config.ai.provider = body.provider
|
||||
config = load_ai_service_config()
|
||||
config.provider = body.provider
|
||||
|
||||
# Anthropic
|
||||
if body.anthropic_api_key:
|
||||
config.ai.anthropic.api_key = _merge_api_key(
|
||||
body.anthropic_api_key, config.ai.anthropic.api_key
|
||||
config.anthropic.api_key = _merge_api_key(
|
||||
body.anthropic_api_key, config.anthropic.api_key
|
||||
)
|
||||
if body.anthropic_model:
|
||||
config.ai.anthropic.model = body.anthropic_model
|
||||
config.anthropic.model = body.anthropic_model
|
||||
|
||||
# Ollama
|
||||
if body.ollama_base_url:
|
||||
config.ai.ollama.base_url = body.ollama_base_url
|
||||
config.ollama.base_url = body.ollama_base_url
|
||||
if body.ollama_model:
|
||||
config.ai.ollama.model = body.ollama_model
|
||||
config.ollama.model = body.ollama_model
|
||||
if body.ollama_api_key:
|
||||
config.ai.ollama.api_key = _merge_api_key(body.ollama_api_key, config.ai.ollama.api_key)
|
||||
config.ollama.api_key = _merge_api_key(body.ollama_api_key, config.ollama.api_key)
|
||||
|
||||
# LM Studio
|
||||
if body.lmstudio_base_url:
|
||||
config.ai.lmstudio.base_url = body.lmstudio_base_url
|
||||
config.lmstudio.base_url = body.lmstudio_base_url
|
||||
if body.lmstudio_model:
|
||||
config.ai.lmstudio.model = body.lmstudio_model
|
||||
config.lmstudio.model = body.lmstudio_model
|
||||
if body.lmstudio_api_key:
|
||||
config.ai.lmstudio.api_key = _merge_api_key(
|
||||
body.lmstudio_api_key, config.ai.lmstudio.api_key
|
||||
config.lmstudio.api_key = _merge_api_key(
|
||||
body.lmstudio_api_key, config.lmstudio.api_key
|
||||
)
|
||||
|
||||
await asyncio.to_thread(save_doc_service_config, config)
|
||||
return load_doc_service_config_masked()
|
||||
await asyncio.to_thread(save_ai_service_config, config)
|
||||
return load_ai_service_config_masked()
|
||||
|
||||
|
||||
@router.post("/documents/ai/test")
|
||||
async def test_documents_ai(
|
||||
@router.post("/ai/test")
|
||||
async def test_ai_connection(
|
||||
_: User = Depends(get_current_admin),
|
||||
) -> dict:
|
||||
"""Test the configured AI connection with a minimal prompt."""
|
||||
from app.core.app_config import load_service_config
|
||||
|
||||
raw = await asyncio.to_thread(load_service_config, "doc_service")
|
||||
ai_cfg = raw.get("ai", {})
|
||||
provider_name = ai_cfg.get("provider", "anthropic")
|
||||
|
||||
"""Proxy a minimal chat request to ai-service to verify the connection."""
|
||||
try:
|
||||
if provider_name == "anthropic":
|
||||
import anthropic
|
||||
client = anthropic.AsyncAnthropic(api_key=ai_cfg["anthropic"]["api_key"])
|
||||
msg = await client.messages.create(
|
||||
model=ai_cfg["anthropic"].get("model", "claude-haiku-4-5-20251001"),
|
||||
max_tokens=16,
|
||||
messages=[{"role": "user", "content": "Reply with: ok"}],
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
resp = await client.post(
|
||||
f"{settings.AI_SERVICE_URL}/chat",
|
||||
json={
|
||||
"messages": [{"role": "user", "content": "Reply with: ok"}],
|
||||
"max_tokens": 16,
|
||||
"temperature": 0,
|
||||
},
|
||||
)
|
||||
return {"ok": True, "provider": provider_name, "response": msg.content[0].text}
|
||||
|
||||
elif provider_name in ("ollama", "lmstudio"):
|
||||
import openai
|
||||
pcfg = ai_cfg[provider_name]
|
||||
client = openai.AsyncOpenAI(
|
||||
base_url=pcfg["base_url"],
|
||||
api_key=pcfg.get("api_key") or "none",
|
||||
)
|
||||
resp = await client.chat.completions.create(
|
||||
model=pcfg["model"],
|
||||
messages=[{"role": "user", "content": "Reply with: ok"}],
|
||||
max_tokens=16,
|
||||
temperature=0,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"provider": provider_name,
|
||||
"response": resp.choices[0].message.content,
|
||||
}
|
||||
else:
|
||||
raise HTTPException(status_code=422, detail=f"Unknown provider: {provider_name}")
|
||||
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
return {"ok": True, "provider": data.get("provider"), "response": data.get("content")}
|
||||
return {"ok": False, "error": f"ai-service returned {resp.status_code}: {resp.text[:200]}"}
|
||||
except Exception as exc:
|
||||
return {"ok": False, "provider": provider_name, "error": str(exc)}
|
||||
return {"ok": False, "error": str(exc)}
|
||||
|
||||
|
||||
# ── Document limits ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.get("/documents/limits")
|
||||
async def get_documents_limits(
|
||||
_: User = Depends(get_current_admin),
|
||||
) -> dict:
|
||||
return load_doc_service_config_masked()
|
||||
|
||||
|
||||
@router.patch("/documents/limits")
|
||||
|
||||
Reference in New Issue
Block a user