提交 99090008 authored 作者: caifuming's avatar caifuming

ljy_统一 LLM 调用,支持多模型切换、异常降级

上级 cc32bf75
"""Internal authentication helpers for AI endpoints."""
from __future__ import annotations
import os
import secrets
from fastapi import HTTPException, Request, status
def _get_expected_internal_token() -> str:
return os.getenv("AI_INTERNAL_TOKEN", "dev-internal-token")
def require_internal_token(
request: Request,
) -> None:
expected_token = _get_expected_internal_token()
x_internal_token = request.headers.get("X-Internal-Token")
if not x_internal_token or not secrets.compare_digest(x_internal_token, expected_token):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid internal token.",
)
...@@ -20,3 +20,7 @@ class AIServiceHealthTracker: ...@@ -20,3 +20,7 @@ class AIServiceHealthTracker:
@property @property
def consecutive_failures(self) -> int: def consecutive_failures(self) -> int:
return self._consecutive_failures return self._consecutive_failures
@property
def failure_threshold(self) -> int:
return self._failure_threshold
...@@ -43,3 +43,14 @@ class AIChatResponse(BaseModel): ...@@ -43,3 +43,14 @@ class AIChatResponse(BaseModel):
prompt_name: str | None = None prompt_name: str | None = None
prompt_version: str | None = None prompt_version: str | None = None
error: AIResponseError | None = None error: AIResponseError | None = None
class AIHealthResponse(BaseModel):
status: Literal["ok", "unhealthy"]
service: str
unhealthy: bool
consecutive_failures: int
failure_threshold: int
default_provider: str
default_model: str
rate_limit_rps: int
...@@ -6,21 +6,28 @@ import os ...@@ -6,21 +6,28 @@ import os
import time import time
from dataclasses import dataclass from dataclasses import dataclass
from functools import lru_cache from functools import lru_cache
import json
from uuid import uuid4 from uuid import uuid4
from app.ai_calling.call_log import CallLogger from app.ai_calling.call_log import CallLogger
from app.ai_calling.errors import ( from app.ai_calling.errors import (
ALL_PROVIDERS_FAILED, ALL_PROVIDERS_FAILED,
MODEL_NOT_CONFIGURED, MODEL_NOT_CONFIGURED,
MODEL_TIMEOUT,
AIServiceError, AIServiceError,
) )
from app.ai_calling.fallback import build_error_response, build_fallback_response from app.ai_calling.fallback import build_error_response, build_fallback_response
from app.ai_calling.health import AIServiceHealthTracker from app.ai_calling.health import AIServiceHealthTracker
from app.ai_calling.limiter import InMemoryRateLimiter from app.ai_calling.limiter import InMemoryRateLimiter
from app.ai_calling.schemas import AIChatRequest, AIChatResponse, AIResponseData from app.ai_calling.schemas import (
AIChatRequest,
AIChatResponse,
AIHealthResponse,
AIResponseData,
)
from app.llm.base import ProviderRequest, ProviderResult from app.llm.base import ProviderRequest, ProviderResult
from app.llm.factory import ProviderFactory, ProviderFactoryConfig from app.llm.factory import ProviderFactory, ProviderFactoryConfig
from app.prompt.loader import PromptLoader, PromptTemplate from app.prompt.loader import PromptLoader
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
...@@ -36,6 +43,10 @@ class AISettings: ...@@ -36,6 +43,10 @@ class AISettings:
openai_api_key: str | None openai_api_key: str | None
openai_base_url: str openai_base_url: str
openai_organization: str | None openai_organization: str | None
tongyi_api_key: str | None
tongyi_base_url: str | None
wenxin_api_key: str | None
wenxin_base_url: str | None
@classmethod @classmethod
def from_env(cls) -> "AISettings": def from_env(cls) -> "AISettings":
...@@ -52,6 +63,10 @@ class AISettings: ...@@ -52,6 +63,10 @@ class AISettings:
openai_api_key=os.getenv("OPENAI_API_KEY"), openai_api_key=os.getenv("OPENAI_API_KEY"),
openai_base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1"), openai_base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1"),
openai_organization=os.getenv("OPENAI_ORGANIZATION"), openai_organization=os.getenv("OPENAI_ORGANIZATION"),
tongyi_api_key=os.getenv("TONGYI_API_KEY"),
tongyi_base_url=os.getenv("TONGYI_BASE_URL"),
wenxin_api_key=os.getenv("WENXIN_API_KEY"),
wenxin_base_url=os.getenv("WENXIN_BASE_URL"),
) )
...@@ -120,6 +135,11 @@ class AICallingService: ...@@ -120,6 +135,11 @@ class AICallingService:
request=request, request=request,
trace_id=trace_id, trace_id=trace_id,
) )
result = self._normalize_provider_result(
prompt=prompt,
request=request,
result=result,
)
except AIServiceError as exc: except AIServiceError as exc:
last_error = exc last_error = exc
self._call_logger.log( self._call_logger.log(
...@@ -214,7 +234,7 @@ class AICallingService: ...@@ -214,7 +234,7 @@ class AICallingService:
) )
except asyncio.TimeoutError as exc: except asyncio.TimeoutError as exc:
last_error = AIServiceError( last_error = AIServiceError(
"MODEL_TIMEOUT", MODEL_TIMEOUT,
"Model request timed out.", "Model request timed out.",
retryable=current_attempt < attempts - 1, retryable=current_attempt < attempts - 1,
) )
...@@ -231,6 +251,41 @@ class AICallingService: ...@@ -231,6 +251,41 @@ class AICallingService:
"All configured providers failed.", "All configured providers failed.",
) )
def get_health_status(self) -> AIHealthResponse:
unhealthy = self._health_tracker.unhealthy
return AIHealthResponse(
status="unhealthy" if unhealthy else "ok",
service="ai-service",
unhealthy=unhealthy,
consecutive_failures=self._health_tracker.consecutive_failures,
failure_threshold=self._health_tracker.failure_threshold,
default_provider=self._settings.default_provider,
default_model=self._settings.default_model,
rate_limit_rps=self._settings.rate_limit_rps,
)
def _normalize_provider_result(
self,
*,
prompt,
request: AIChatRequest,
result: ProviderResult,
) -> ProviderResult:
if request.response_format != "json":
return result
structured = self._prompt_loader.normalize_structured_output(
prompt,
result.content,
result.structured,
)
content = result.content or json.dumps(structured, ensure_ascii=True)
return ProviderResult(
content=content,
structured=structured,
raw=result.raw,
)
def _build_provider_targets(self, request: AIChatRequest) -> list[tuple[str, str]]: def _build_provider_targets(self, request: AIChatRequest) -> list[tuple[str, str]]:
primary_provider = (request.provider or self._settings.default_provider or "").strip() primary_provider = (request.provider or self._settings.default_provider or "").strip()
primary_model = (request.model or self._settings.default_model or "").strip() primary_model = (request.model or self._settings.default_model or "").strip()
...@@ -266,6 +321,10 @@ def get_ai_service() -> AICallingService: ...@@ -266,6 +321,10 @@ def get_ai_service() -> AICallingService:
openai_api_key=settings.openai_api_key, openai_api_key=settings.openai_api_key,
openai_base_url=settings.openai_base_url, openai_base_url=settings.openai_base_url,
openai_organization=settings.openai_organization, openai_organization=settings.openai_organization,
tongyi_api_key=settings.tongyi_api_key,
tongyi_base_url=settings.tongyi_base_url,
wenxin_api_key=settings.wenxin_api_key,
wenxin_base_url=settings.wenxin_base_url,
) )
) )
return AICallingService( return AICallingService(
......
"""AI chat API routes.""" """AI chat API routes."""
from fastapi import APIRouter from fastapi import APIRouter, Depends
from app.ai_calling.schemas import AIChatRequest, AIChatResponse from app.ai_calling.auth import require_internal_token
from app.ai_calling.schemas import AIChatRequest, AIChatResponse, AIHealthResponse
from app.ai_calling.service import get_ai_service from app.ai_calling.service import get_ai_service
router = APIRouter() router = APIRouter()
@router.post("/chat", response_model=AIChatResponse) @router.post("/chat", response_model=AIChatResponse, dependencies=[Depends(require_internal_token)])
async def chat(req: AIChatRequest) -> AIChatResponse: async def chat(req: AIChatRequest) -> AIChatResponse:
"""Unified LLM chat entrypoint.""" """Unified LLM chat entrypoint."""
service = get_ai_service() service = get_ai_service()
return await service.chat(req) return await service.chat(req)
@router.get("/health", response_model=AIHealthResponse, dependencies=[Depends(require_internal_token)])
async def ai_health() -> AIHealthResponse:
"""Return AI-calling specific health status without changing legacy health output."""
service = get_ai_service()
return service.get_health_status()
...@@ -7,6 +7,8 @@ from app.ai_calling.errors import MODEL_NOT_CONFIGURED, AIServiceError ...@@ -7,6 +7,8 @@ from app.ai_calling.errors import MODEL_NOT_CONFIGURED, AIServiceError
from app.llm.base import LLMProvider from app.llm.base import LLMProvider
from app.llm.mock import MockProvider from app.llm.mock import MockProvider
from app.llm.openai import OpenAIProvider from app.llm.openai import OpenAIProvider
from app.llm.tongyi import TongyiProvider
from app.llm.wenxin import WenxinProvider
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
...@@ -14,6 +16,10 @@ class ProviderFactoryConfig: ...@@ -14,6 +16,10 @@ class ProviderFactoryConfig:
openai_api_key: str | None openai_api_key: str | None
openai_base_url: str openai_base_url: str
openai_organization: str | None = None openai_organization: str | None = None
tongyi_api_key: str | None = None
tongyi_base_url: str | None = None
wenxin_api_key: str | None = None
wenxin_base_url: str | None = None
class ProviderFactory: class ProviderFactory:
...@@ -32,6 +38,16 @@ class ProviderFactory: ...@@ -32,6 +38,16 @@ class ProviderFactory:
base_url=self._config.openai_base_url, base_url=self._config.openai_base_url,
organization=self._config.openai_organization, organization=self._config.openai_organization,
) )
if normalized == "tongyi":
return TongyiProvider(
api_key=self._config.tongyi_api_key,
base_url=self._config.tongyi_base_url,
)
if normalized == "wenxin":
return WenxinProvider(
api_key=self._config.wenxin_api_key,
base_url=self._config.wenxin_base_url,
)
raise AIServiceError( raise AIServiceError(
MODEL_NOT_CONFIGURED, MODEL_NOT_CONFIGURED,
f"Provider '{provider_name}' is not supported.", f"Provider '{provider_name}' is not supported.",
......
...@@ -18,9 +18,19 @@ class MockProvider(LLMProvider): ...@@ -18,9 +18,19 @@ class MockProvider(LLMProvider):
structured = None structured = None
if request.response_format == "json": if request.response_format == "json":
structured = { structured = {
"provider": self.name, "summary": "Mock structured output for local verification.",
"model": request.model, "knowledge_points": [
"echo": user_message[:400], {
"name": "Tensor basics",
"description": "Understand tensors, shapes, and simple operations.",
"difficulty": "beginner",
"estimated_minutes": 30,
"prerequisites": ["Python basics"],
}
],
"prerequisites": ["Python basics"],
"study_order": ["Tensor basics", "Autograd", "Training loop"],
"common_mistakes": ["Shape mismatch", "Device mismatch"],
} }
content = json.dumps(structured, ensure_ascii=True) content = json.dumps(structured, ensure_ascii=True)
return ProviderResult( return ProviderResult(
......
...@@ -20,16 +20,26 @@ from app.llm.base import LLMProvider, ProviderRequest, ProviderResult ...@@ -20,16 +20,26 @@ from app.llm.base import LLMProvider, ProviderRequest, ProviderResult
class OpenAIProvider(LLMProvider): class OpenAIProvider(LLMProvider):
name = "openai" name = "openai"
def __init__(self, api_key: str | None, base_url: str, organization: str | None = None) -> None: def __init__(
self,
api_key: str | None,
base_url: str | None,
organization: str | None = None,
) -> None:
self._api_key = api_key self._api_key = api_key
self._base_url = base_url.rstrip("/") self._base_url = base_url.rstrip("/") if base_url else None
self._organization = organization self._organization = organization
async def chat(self, request: ProviderRequest) -> ProviderResult: async def chat(self, request: ProviderRequest) -> ProviderResult:
if not self._api_key: if not self._api_key:
raise AIServiceError( raise AIServiceError(
MODEL_NOT_CONFIGURED, MODEL_NOT_CONFIGURED,
"OPENAI_API_KEY is not configured.", f"{self.name.upper()} API key is not configured.",
)
if not self._base_url:
raise AIServiceError(
MODEL_NOT_CONFIGURED,
f"{self.name.upper()} base URL is not configured.",
) )
payload: dict[str, Any] = { payload: dict[str, Any] = {
......
"""Tongyi provider adapter."""
from __future__ import annotations
from app.llm.openai import OpenAIProvider
class TongyiProvider(OpenAIProvider):
name = "tongyi"
"""Wenxin provider adapter."""
from __future__ import annotations
from app.llm.openai import OpenAIProvider
class WenxinProvider(OpenAIProvider):
name = "wenxin"
...@@ -2,17 +2,20 @@ ...@@ -2,17 +2,20 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
import json
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import yaml import yaml
from app.ai_calling.errors import PROMPT_NOT_FOUND, AIServiceError from app.ai_calling.errors import MODEL_INVALID_RESPONSE, PROMPT_NOT_FOUND, AIServiceError
from app.llm.base import ChatMessage from app.llm.base import ChatMessage
from app.prompt.schema_loader import PromptSchemaLoader
from app.prompt.validator import ( from app.prompt.validator import (
extract_template_variables, extract_template_variables,
validate_prompt_definition, validate_prompt_definition,
validate_prompt_variables, validate_prompt_variables,
validate_structured_output,
) )
...@@ -23,6 +26,7 @@ class PromptTemplate: ...@@ -23,6 +26,7 @@ class PromptTemplate:
system: str system: str
user: str user: str
required_variables: set[str] required_variables: set[str]
response_schema_name: str | None = None
class PromptLoader: class PromptLoader:
...@@ -30,6 +34,7 @@ class PromptLoader: ...@@ -30,6 +34,7 @@ class PromptLoader:
def __init__(self, templates_dir: Path | None = None) -> None: def __init__(self, templates_dir: Path | None = None) -> None:
self._templates_dir = templates_dir or Path(__file__).resolve().parent / "templates" self._templates_dir = templates_dir or Path(__file__).resolve().parent / "templates"
self._schema_loader = PromptSchemaLoader()
self._cache: dict[str, PromptTemplate] = {} self._cache: dict[str, PromptTemplate] = {}
def load(self, prompt_name: str) -> PromptTemplate: def load(self, prompt_name: str) -> PromptTemplate:
...@@ -54,6 +59,7 @@ class PromptLoader: ...@@ -54,6 +59,7 @@ class PromptLoader:
system=str(data["system"]), system=str(data["system"]),
user=str(data["user"]), user=str(data["user"]),
required_variables=required_variables, required_variables=required_variables,
response_schema_name=self._normalize_schema_name(data.get("response_schema")),
) )
self._cache[prompt_name] = template self._cache[prompt_name] = template
return template return template
...@@ -77,3 +83,32 @@ class PromptLoader: ...@@ -77,3 +83,32 @@ class PromptLoader:
if value is None: if value is None:
return "" return ""
return str(value) return str(value)
def normalize_structured_output(
self,
template: PromptTemplate,
content: str,
structured: Any | None,
) -> Any | None:
candidate = structured
if candidate is None:
if not content:
return None
try:
candidate = json.loads(content)
except json.JSONDecodeError as exc:
raise AIServiceError(
MODEL_INVALID_RESPONSE,
"Structured response is not valid JSON.",
) from exc
if template.response_schema_name:
schema = self._schema_loader.load(template.response_schema_name)
validate_structured_output(candidate, schema)
return candidate
@staticmethod
def _normalize_schema_name(value: Any) -> str | None:
if not value:
return None
return str(value)
"""JSON schema loading helpers."""
from __future__ import annotations
import json
from pathlib import Path
class PromptSchemaLoader:
"""Load JSON schemas used by prompt metadata and structured outputs."""
def __init__(self, schemas_dir: Path | None = None) -> None:
self._schemas_dir = schemas_dir or Path(__file__).resolve().parent / "schemas"
self._cache: dict[str, dict] = {}
def load(self, schema_name: str) -> dict:
if schema_name in self._cache:
return self._cache[schema_name]
path = self._schemas_dir / schema_name
schema = json.loads(path.read_text(encoding="utf-8"))
self._cache[schema_name] = schema
return schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"required": [
"summary",
"knowledge_points",
"prerequisites",
"study_order",
"common_mistakes"
],
"properties": {
"summary": {
"type": "string",
"minLength": 1
},
"knowledge_points": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"additionalProperties": false,
"required": ["name", "description", "difficulty", "estimated_minutes", "prerequisites"],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"description": {
"type": "string",
"minLength": 1
},
"difficulty": {
"type": "string",
"enum": ["beginner", "intermediate", "advanced"]
},
"estimated_minutes": {
"type": "integer",
"minimum": 1
},
"prerequisites": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"prerequisites": {
"type": "array",
"items": {
"type": "string"
}
},
"study_order": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
},
"common_mistakes": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"required": ["name", "version", "system", "user"],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"version": {
"type": "string",
"minLength": 1
},
"required_variables": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
},
"uniqueItems": true
},
"system": {
"type": "string",
"minLength": 1
},
"user": {
"type": "string",
"minLength": 1
},
"response_schema": {
"type": "string",
"minLength": 1
}
}
}
"""Prompt validation helpers.""" """Prompt validation helpers."""
from __future__ import annotations from __future__ import annotations
import json
from pathlib import Path
from string import Formatter from string import Formatter
from typing import Any from typing import Any
from jsonschema import ValidationError, validate
from app.ai_calling.errors import ( from app.ai_calling.errors import (
INVALID_REQUEST, INVALID_REQUEST,
MODEL_INVALID_RESPONSE,
PROMPT_VARIABLE_MISSING, PROMPT_VARIABLE_MISSING,
AIServiceError, AIServiceError,
) )
...@@ -20,13 +25,15 @@ def extract_template_variables(text: str) -> set[str]: ...@@ -20,13 +25,15 @@ def extract_template_variables(text: str) -> set[str]:
def validate_prompt_definition(data: dict[str, Any]) -> None: def validate_prompt_definition(data: dict[str, Any]) -> None:
required_keys = {"name", "version", "system", "user"} schema_path = Path(__file__).resolve().parent / "schemas" / "prompt_template.schema.json"
missing = sorted(required_keys - data.keys()) schema = json.loads(schema_path.read_text(encoding="utf-8"))
if missing: try:
validate(instance=data, schema=schema)
except ValidationError as exc:
raise AIServiceError( raise AIServiceError(
INVALID_REQUEST, INVALID_REQUEST,
f"Prompt template is missing keys: {', '.join(missing)}.", f"Prompt template schema validation failed: {exc.message}",
) ) from exc
def validate_prompt_variables( def validate_prompt_variables(
...@@ -39,3 +46,16 @@ def validate_prompt_variables( ...@@ -39,3 +46,16 @@ def validate_prompt_variables(
PROMPT_VARIABLE_MISSING, PROMPT_VARIABLE_MISSING,
f"Prompt variables are missing: {', '.join(missing)}.", f"Prompt variables are missing: {', '.join(missing)}.",
) )
def validate_structured_output(
instance: Any,
schema: dict[str, Any],
) -> None:
try:
validate(instance=instance, schema=schema)
except ValidationError as exc:
raise AIServiceError(
MODEL_INVALID_RESPONSE,
f"Structured output schema validation failed: {exc.message}",
) from exc
差异被折叠。
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论