增加鉴权系统
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
## 代码结构
|
||||
|
||||
- `routes.py`:集中定义所有 HTTP 路由。
|
||||
- `auth.py`:校验 API key 白名单并识别当前用户。
|
||||
- `schemas.py`:集中定义 API 请求和响应结构。
|
||||
- `domain.py`:定义会话、消息和工具调用的持久化模型。
|
||||
- `tools.py`:集中定义工具注册表、参数结构和工具函数。
|
||||
@@ -24,12 +25,19 @@ uv run uvicorn main:app --reload
|
||||
|
||||
服务会自动加载项目根目录的 `.env`,已有系统环境变量优先级更高。可配置 `DEEPSEEK_BASE_URL`、`DEEPSEEK_MODEL`、`DEFAULT_SYSTEM_PROMPT`、`CHAT_DATA_DIR` 和工具调用限制,完整示例见 `.env.example`。第一版应只使用一个 Uvicorn worker。
|
||||
|
||||
所有业务接口都必须通过 `X-API-Key` 请求头提供访问密钥。白名单使用用户到密钥的 JSON 映射:
|
||||
|
||||
```text
|
||||
API_KEY_WHITELIST={"11111111-1111-4111-8111-111111111111":"replace-with-a-secret-key"}
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
创建会话:
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions \
|
||||
-H 'X-API-Key: replace-with-a-secret-key' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"system_prompt":"你是一个简洁的中文助手。"}'
|
||||
```
|
||||
@@ -38,6 +46,7 @@ curl -X POST http://127.0.0.1:8000/sessions \
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'X-API-Key: replace-with-a-secret-key' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"content":"你好,请记住我的名字是小明。"}'
|
||||
```
|
||||
@@ -53,5 +62,15 @@ API 返回及存储的每条消息都包含 UTC `created_at`。只有模型完
|
||||
获取指定会话的完整历史:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8000/sessions/SESSION_ID/messages
|
||||
curl http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'X-API-Key: replace-with-a-secret-key'
|
||||
```
|
||||
|
||||
查询当前用户的累计 API 调用次数和 token 用量:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8000/usage \
|
||||
-H 'X-API-Key: replace-with-a-secret-key'
|
||||
```
|
||||
|
||||
每个会话 JSON 顶层保存所属 `user_id`、成功聊天次数及累计 token。`/usage` 会扫描并汇总当前用户的全部会话,不使用独立统计文件。创建会话、查询历史和查询用量不计入 `api_calls`。
|
||||
|
||||
Reference in New Issue
Block a user