From 495083f9b4a37cebd84631b86e7660a65dc0bace Mon Sep 17 00:00:00 2001 From: zhiqui Date: Sun, 15 Nov 2026 01:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD:=20=E5=85=B1=E4=BA=ABhttpx?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0/=E8=AE=BE=E7=BD=AE=E7=BC=93?= =?UTF-8?q?=E5=AD=98/=E5=A4=8D=E5=90=88=E7=B4=A2=E5=BC=95=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=A1=A5=E5=BB=BA/GZip/=E5=88=86=E9=A1=B5=20;=20?= =?UTF-8?q?=E5=95=86=E4=B8=9A=E5=8C=96v1:=20APIKey(sha256+scope+=E9=99=90?= =?UTF-8?q?=E6=B5=81)/=E7=94=A8=E9=87=8F=E8=B4=A6=E6=9C=AC/=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8C=96=E7=AB=AF=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++ app/api/v1.py | 165 +++++++++++++++++++++++++++++++++++++++++ app/db/repos.py | 15 +++- app/db/session.py | 6 ++ app/main.py | 8 ++ app/models/entities.py | 34 ++++++++- app/services/llm.py | 72 ++++++++++-------- 7 files changed, 283 insertions(+), 32 deletions(-) create mode 100644 app/api/v1.py diff --git a/README.md b/README.md index cbc7b3ed..da6dda6c 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,18 @@ OMO_FIRST_ADMIN_PASS=maopaoya-admin - 数据库切换只需改 DATABASE_URL(可选依赖: `uv sync --extra mysql` 或 `--extra postgres`) - 数据访问统一走 `app/db/repos.py` 仓储层 - Alembic 迁移: `uv run alembic init migrations` 后接入 `app.db.session.Base.metadata` + +## 商业化 API(v1 稳定契约) +- 前缀 `/v1`,独立于 `/api` 演进;契约只加字段不改语义 +- 认证:`X-API-Key` 头(sha256 落库,明文仅创建时返回一次) +- scope:`chat`(仅对话)/ `full`(含角色读取);每 key 独立限流(req/min) +- 端点:`GET /v1/ping`、`POST /v1/keys`、`GET /v1/keys`、`DELETE /v1/keys/{id}`、 + `GET /v1/usage`(用量账本,计费底座)、`GET /v1/characters`、`POST /v1/chat` +- 多实例部署时限流换 Redis(现为进程内滑动窗口) + +## 性能 +- 全局 httpx 连接池复用(不再每请求握手) +- 站点设置进程内缓存(写时失效) +- 复合索引覆盖热查询路径(消息/记忆/朋友圈/会话),旧表启动时自动补建 +- GZip 响应压缩(>1KB) +- 部署建议:`uvicorn app.main:app --workers 2` 或 nginx 反代 + 多 worker;量大切 PG diff --git a/app/api/v1.py b/app/api/v1.py new file mode 100644 index 00000000..84d69087 --- /dev/null +++ b/app/api/v1.py @@ -0,0 +1,165 @@ +"""商业化预留 v1 API:API Key 认证 + 限流 + 用量账本。 + +设计原则: +- /v1/* 稳定契约:只加字段不改语义,向前兼容 +- key 只存 sha256;明文只返回一次 +- scope 分级:chat / full;rate_limit 按 key 限流 +- 用量落 usage_logs,将来接计费系统零改造 +""" +import hashlib +import secrets +import time +import datetime as dt +from collections import defaultdict, deque + +from fastapi import APIRouter, Depends, HTTPException, Request +from pydantic import BaseModel +from sqlalchemy import select + +from ..db.session import get_db +from ..db import repos +from ..models.entities import ApiKey, UsageLog, User, Message +from ..api.routers.auth import current_user +from ..api.routers.chat import (_dec, _enc, _own_session, _key, build_context, + _channel_for) + +router = APIRouter(prefix="/v1", tags=["v1-commercial"]) + + +# ---------- API Key 认证 ---------- + +def _hash_key(raw: str) -> str: + return hashlib.sha256(raw.encode()).hexdigest() + + +async def api_key_auth(request: Request, db=Depends(get_db)) -> tuple[User, ApiKey]: + raw = request.headers.get("X-API-Key", "") + if not raw: + raise HTTPException(401, "缺少 X-API-Key") + row = (await db.execute(select(ApiKey).where( + ApiKey.key_hash == _hash_key(raw), ApiKey.revoked == False))).scalar_one_or_none() + if not row: + raise HTTPException(401, "API Key 无效或已吊销") + user = await db.get(User, row.user_id) + if not user or not user.is_active: + raise HTTPException(401, "账户无效") + return user, row + + +# ---------- 限流(滑动窗口,进程内;多实例部署换 Redis) ---------- + +_buckets: dict[int, deque] = defaultdict(deque) + + +def check_rate_limit(key_row: ApiKey): + b = _buckets[key_row.id] + now = time.time() + while b and b[0] < now - 60: + b.popleft() + if len(b) >= key_row.rate_limit: + raise HTTPException(429, f"超过限流 {key_row.rate_limit}/min") + b.append(now) + + +async def log_usage(db, user: User, key_row: ApiKey, endpoint: str): + db.add(UsageLog(user_id=user.id, api_key_id=key_row.id, + endpoint=endpoint, cost_units=1)) + key_row.last_used_at = dt.datetime.utcnow() + await db.commit() + + +# ---------- Key 管理(JWT 鉴权) ---------- + + +class KeyIn(BaseModel): + name: str = "default" + scope: str = "chat" # chat / full + rate_limit: int = 60 + + +@router.post("/keys") +async def create_key(body: KeyIn, user: User = Depends(current_user), + db=Depends(get_db)): + raw = "omk_" + secrets.token_urlsafe(24) + row = ApiKey(user_id=user.id, key_hash=_hash_key(raw), + name=body.name, scope=body.scope, rate_limit=body.rate_limit) + db.add(row) + await db.commit() + return {"key": raw, "id": row.id, "notice": "明文仅此一次,请立即保存"} + + +@router.get("/keys") +async def list_keys(user: User = Depends(current_user), db=Depends(get_db)): + rows = (await db.execute(select(ApiKey).where( + ApiKey.user_id == user.id, ApiKey.revoked == False))).scalars().all() + return {"keys": [{"id": k.id, "name": k.name, "scope": k.scope, + "rate_limit": k.rate_limit, + "last_used": str(k.last_used_at or "")} for k in rows]} + + +@router.delete("/keys/{kid}") +async def revoke_key(kid: int, user: User = Depends(current_user), db=Depends(get_db)): + row = await db.get(ApiKey, kid) + if not row or row.user_id != user.id: + raise HTTPException(404, "不存在") + row.revoked = True + await db.commit() + return {"ok": True} + + +@router.get("/usage") +async def my_usage(user: User = Depends(current_user), db=Depends(get_db)): + rows = (await db.execute(select(UsageLog).where( + UsageLog.user_id == user.id).order_by(UsageLog.id.desc()).limit(500))).scalars().all() + by_ep = {} + for r in rows: + by_ep[r.endpoint] = by_ep.get(r.endpoint, 0) + 1 + return {"recent_calls": len(rows), "by_endpoint": by_ep} + + +# ---------- v1 稳定接口 ---------- + + +@router.get("/ping") +async def ping(): + """健康检查,无鉴权""" + return {"ok": True, "service": "open-maopaoya", "version": "v1"} + + +@router.get("/characters") +async def v1_characters(pair: tuple = Depends(api_key_auth), db=Depends(get_db)): + user, key_row = pair + if key_row.scope != "full": + raise HTTPException(403, "scope 不足,需要 full") + from ..db.repos import dec + rows = await repos.CharacterRepo.list_for(db, user.id) + await log_usage(db, user, key_row, "/v1/characters") + k = _key(user) + return {"data": [{"id": c.id, "name": c.name, + "persona": dec(k, c.persona), "voice": c.voice} for c in rows]} + + +class V1ChatIn(BaseModel): + session_id: int + content: str + + +@router.post("/chat") +async def v1_chat(body: V1ChatIn, pair: tuple = Depends(api_key_auth), + db=Depends(get_db)): + """以 API Key 驱动一次对话(走用户自己的渠道配置)""" + user, key_row = pair + check_rate_limit(key_row) + s = await _own_session(db, user, body.session_id) + db.add(Message(session_id=s.id, user_id=user.id, role="user", + content_enc=_enc(user, body.content))) + await db.commit() + history = await repos.MessageRepo.history(db, user.id, s.id, limit=80) + msgs = await build_context(db, user, s, history) + client, models = await _channel_for(db, user, s) + reply = await client.chat(models.get("narrative") or "step-3.5-flash", msgs) + db.add(Message(session_id=s.id, user_id=user.id, role="assistant", + content_enc=_enc(user, reply))) + await log_usage(db, user, key_row, "/v1/chat") + await db.commit() + return {"reply": reply, "session_id": s.id} diff --git a/app/db/repos.py b/app/db/repos.py index 2589e5b5..d91eea90 100644 --- a/app/db/repos.py +++ b/app/db/repos.py @@ -36,11 +36,18 @@ class SettingRepo: "allow_byok": "true", "admin_data_visible": "true", # 默认明文;关闭后加密落库 } + _cache: dict | None = None # 进程内缓存(读多写少),写入即失效 @staticmethod async def get(db: AsyncSession, key: str) -> str: - v = (await db.execute(select(E.SiteSetting).where(E.SiteSetting.key == key))).scalar_one_or_none() - return v.value if v else SettingRepo.DEFAULTS.get(key, "") + cache = SettingRepo._cache + if cache is not None: + return cache.get(key, SettingRepo.DEFAULTS.get(key, "")) + rows = (await db.execute(select(E.SiteSetting))).scalars().all() + cache = dict(SettingRepo.DEFAULTS) + cache.update({r.key: r.value for r in rows}) + SettingRepo._cache = cache + return cache.get(key, "") @staticmethod async def set(db: AsyncSession, key: str, value: str): @@ -50,12 +57,16 @@ class SettingRepo: else: db.add(E.SiteSetting(key=key, value=value)) await db.commit() + SettingRepo._cache = None @staticmethod async def all(db: AsyncSession) -> dict: + if SettingRepo._cache is not None: + return SettingRepo._cache rows = (await db.execute(select(E.SiteSetting))).scalars().all() out = dict(SettingRepo.DEFAULTS) out.update({r.key: r.value for r in rows}) + SettingRepo._cache = out return out diff --git a/app/db/session.py b/app/db/session.py index 3fae1389..070464fd 100644 --- a/app/db/session.py +++ b/app/db/session.py @@ -49,3 +49,9 @@ async def init_db(): await conn.execute(text( f"UPDATE {table.name} SET {col.name}=0 " f"WHERE {col.name}=''")) + # 旧表补建索引(create_all 只对新建表生效) + for table in Base.metadata.tables.values(): + for idx in table.indexes: + await conn.execute(text( + f'CREATE INDEX IF NOT EXISTS "{idx.name}" ON "{table.name}" ' + f'({", ".join(f"\'{c.name}\'" for c in idx.columns)})')) diff --git a/app/main.py b/app/main.py index 5e8a681a..9cf4bd7f 100644 --- a/app/main.py +++ b/app/main.py @@ -4,9 +4,17 @@ from fastapi.staticfiles import StaticFiles from .db.session import init_db from .api.routers import auth, admin, channels, characters, chat, extra +from .api import v1 app = FastAPI(title="Open Maopaoya", docs_url="/api/docs", openapi_url="/api/openapi.json") +# 性能:响应压缩 +from starlette.middleware.gzip import GZipMiddleware +app.add_middleware(GZipMiddleware, minimum_size=1024) + +# API 路由(/v1 为商业化稳定契约,独立前缀不随 /api 演进) +app.include_router(v1.router) + for r in (auth.router, admin.router, channels.router, characters.router, chat.router, extra.router): app.include_router(r, prefix="/api") diff --git a/app/models/entities.py b/app/models/entities.py index f2ee57ea..409773b7 100644 --- a/app/models/entities.py +++ b/app/models/entities.py @@ -2,7 +2,7 @@ import datetime as dt from sqlalchemy import (String, Boolean, Integer, Text, DateTime, ForeignKey, - JSON, UniqueConstraint) + JSON, UniqueConstraint, Index) from sqlalchemy.orm import Mapped, mapped_column, relationship from ..db.session import Base @@ -78,6 +78,7 @@ class Character(Base): class Memory(Base): """角色记忆,双轨: fact(设定类) / event(事件类)。全部可查可改。""" __tablename__ = "memories" + __table_args__ = (Index("ix_mem_user_char", "user_id", "character_id"),) id: Mapped[int] = mapped_column(primary_key=True) character_id: Mapped[int] = mapped_column(ForeignKey("characters.id"), index=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) @@ -101,6 +102,7 @@ class WorldbookEntry(Base): class ChatSession(Base): """mode: roleplay(单角色) / novel(动态小说) / group(群聊) / call(语音通话)""" __tablename__ = "chat_sessions" + __table_args__ = (Index("ix_sess_user_pin", "user_id", "pinned", "id"),) id: Mapped[int] = mapped_column(primary_key=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) mode: Mapped[str] = mapped_column(String(16), default="roleplay") @@ -119,6 +121,10 @@ class ChatSession(Base): class Message(Base): __tablename__ = "messages" + __table_args__ = ( + Index("ix_msg_sess_user_time", "session_id", "user_id", "id"), + Index("ix_msg_user_time", "user_id", "id"), + ) id: Mapped[int] = mapped_column(primary_key=True) session_id: Mapped[int] = mapped_column(ForeignKey("chat_sessions.id"), index=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) @@ -136,6 +142,7 @@ class Message(Base): class Moment(Base): """角色朋友圈""" __tablename__ = "moments" + __table_args__ = (Index("ix_moment_user_time", "user_id", "id"),) id: Mapped[int] = mapped_column(primary_key=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) character_id: Mapped[int] = mapped_column(ForeignKey("characters.id"), index=True) @@ -160,3 +167,28 @@ class UserPrefs(Base): __tablename__ = "user_prefs" user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True) data: Mapped[str] = mapped_column(Text, default="{}") # JSON + + +class ApiKey(Base): + """商业化预留:第三方 API 密钥。scope: chat / characters / full""" + __tablename__ = "api_keys" + id: Mapped[int] = mapped_column(primary_key=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) + key_hash: Mapped[str] = mapped_column(String(64), unique=True) # sha256,不存明文 + name: Mapped[str] = mapped_column(String(128), default="default") + scope: Mapped[str] = mapped_column(String(32), default="chat") # chat / full + rate_limit: Mapped[int] = mapped_column(Integer, default=60) # req/min + revoked: Mapped[bool] = mapped_column(Boolean, default=False) + created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now) + last_used_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True) + + +class UsageLog(Base): + """商业化预留:API 用量账本(计费/配额的数据底座)""" + __tablename__ = "usage_logs" + __table_args__ = (Index("ix_usage_user_time", "user_id", "id"),) + id: Mapped[int] = mapped_column(primary_key=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) + api_key_id: Mapped[int | None] = mapped_column(nullable=True) + endpoint: Mapped[str] = mapped_column(String(128)) + cost_units: Mapped[int] = mapped_column(Integer, default=1) # 计费单位(暂为次数) diff --git a/app/services/llm.py b/app/services/llm.py index 9c7462c4..7c42921c 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -1,8 +1,23 @@ -"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。""" +"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。 + +性能:全局共享 AsyncClient(连接池复用,避免每请求握手)。 +""" import base64 import httpx +# 进程级共享连接池; comercial 场景下量大可调 max_connections +_shared_client: httpx.AsyncClient | None = None + + +def get_http() -> httpx.AsyncClient: + global _shared_client + if _shared_client is None or _shared_client.is_closed: + _shared_client = httpx.AsyncClient( + timeout=httpx.Timeout(180, connect=10), + limits=httpx.Limits(max_connections=100, max_keepalive_connections=20)) + return _shared_client + class LLMClient: def __init__(self, base_url: str, api_key: str): @@ -11,39 +26,38 @@ class LLMClient: async def chat(self, model: str, messages: list[dict], temperature: float = 0.9, max_tokens: int = 2048) -> str: - async with httpx.AsyncClient(timeout=120) as c: - r = await c.post(f"{self.base_url}/chat/completions", - headers={"Authorization": f"Bearer {self.api_key}"}, - json={"model": model, "messages": messages, - "temperature": temperature, "max_tokens": max_tokens}) - r.raise_for_status() - return r.json()["choices"][0]["message"]["content"] or "" + r = await get_http().post( + f"{self.base_url}/chat/completions", + headers={"Authorization": f"Bearer {self.api_key}"}, + json={"model": model, "messages": messages, + "temperature": temperature, "max_tokens": max_tokens}) + r.raise_for_status() + return r.json()["choices"][0]["message"]["content"] or "" async def image(self, model: str, prompt: str, size: str = "1024x1024") -> str: - async with httpx.AsyncClient(timeout=180) as c: - r = await c.post(f"{self.base_url}/images/generations", - headers={"Authorization": f"Bearer {self.api_key}"}, - json={"model": model, "prompt": prompt, - "size": size, "n": 1, "response_format": "url"}) - r.raise_for_status() - d = r.json()["data"][0] - return d.get("url") or ("data:image/png;base64," + d.get("b64_json", "")) + r = await get_http().post( + f"{self.base_url}/images/generations", + headers={"Authorization": f"Bearer {self.api_key}"}, + json={"model": model, "prompt": prompt, + "size": size, "n": 1, "response_format": "url"}) + r.raise_for_status() + d = r.json()["data"][0] + return d.get("url") or ("data:image/png;base64," + d.get("b64_json", "")) async def tts(self, model: str, text: str, voice: str) -> bytes: """优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。""" - async with httpx.AsyncClient(timeout=120) as c: - headers = {"Authorization": f"Bearer {self.api_key}"} - try: - r = await c.post(f"{self.base_url}/audio/speech", headers=headers, - json={"model": model, "input": text, "voice": voice}) - r.raise_for_status() - return r.content - except httpx.HTTPError: - r = await c.post(f"{self.base_url}/audio/speech", headers=headers, - json={"model": model, "text": text, "voice": voice, - "response_format": "wav"}) - r.raise_for_status() - return r.content + headers = {"Authorization": f"Bearer {self.api_key}"} + try: + r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers, + json={"model": model, "input": text, "voice": voice}) + r.raise_for_status() + return r.content + except httpx.HTTPError: + r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers, + json={"model": model, "text": text, "voice": voice, + "response_format": "wav"}) + r.raise_for_status() + return r.content async def ping(self, model: str) -> tuple[bool, str]: try: