diff --git a/API.md b/API.md index 1bdc314..f3fed0a 100644 --- a/API.md +++ b/API.md @@ -49,6 +49,8 @@ Content-Type: application/json | `stream` | boolean | ❌ | 是否流式输出,默认 `false` | | `temperature` | number | ❌ | 温度参数,0-2 | | `max_tokens` | number | ❌ | 最大输出 token 数 | +| `tools` | array | ❌ | 工具定义列表(OpenAI 格式) | +| `tool_choice` | string | ❌ | 工具选择策略 | **支持的模型**: @@ -111,6 +113,54 @@ data: [DONE] } ``` +**工具调用请求示例**: + +```json +{ + "model": "deepseek-chat", + "messages": [{"role": "user", "content": "北京今天天气怎么样?"}], + "tools": [{ + "type": "function", + "function": { + "name": "get_weather", + "description": "获取指定城市的天气", + "parameters": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "城市名称"} + }, + "required": ["location"] + } + } + }] +} +``` + +**工具调用响应格式**: + +```json +{ + "id": "chatcmpl-xxx", + "object": "chat.completion", + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [{ + "id": "call_xxx", + "type": "function", + "function": { + "name": "get_weather", + "arguments": "{\"location\": \"北京\"}" + } + }] + }, + "finish_reason": "tool_calls" + }] +} +``` + --- ## 管理接口 diff --git a/core/config.py b/core/config.py index de74f2c..83e02ca 100644 --- a/core/config.py +++ b/core/config.py @@ -101,3 +101,6 @@ WASM_PATH = resolve_path("DS2API_WASM_PATH", "sha3_wasm_bg.7b9ca65ddd.wasm") # 模板目录 TEMPLATES_DIR = resolve_path("DS2API_TEMPLATES_DIR", "templates") + +# WebUI 静态文件目录 +STATIC_ADMIN_DIR = resolve_path("DS2API_STATIC_ADMIN_DIR", "static/admin") diff --git a/routes/home.py b/routes/home.py index cb08c73..69dce7f 100644 --- a/routes/home.py +++ b/routes/home.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- -"""首页路由""" +"""首页和 WebUI 路由""" +import os from fastapi import APIRouter, Request +from fastapi.responses import HTMLResponse, FileResponse from fastapi.templating import Jinja2Templates -from core.config import TEMPLATES_DIR +from core.config import TEMPLATES_DIR, STATIC_ADMIN_DIR router = APIRouter() templates = Jinja2Templates(directory=TEMPLATES_DIR) @@ -12,3 +14,29 @@ templates = Jinja2Templates(directory=TEMPLATES_DIR) @router.get("/") def index(request: Request): return templates.TemplateResponse("welcome.html", {"request": request}) + + +@router.get("/webui") +@router.get("/webui/{path:path}") +async def webui(request: Request, path: str = ""): + """提供 WebUI 静态文件""" + # 检查 static/admin 目录是否存在 + if not os.path.isdir(STATIC_ADMIN_DIR): + return HTMLResponse( + content="
Run cd webui && npm run build first.