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:
@@ -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