第一次提交
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# Simple Chat API
|
||||
|
||||
一个使用 FastAPI 和 DeepSeek API 的最小多轮对话服务。每个会话的系统提示词与消息历史保存在本地 JSON 文件中。
|
||||
|
||||
## 启动
|
||||
|
||||
```bash
|
||||
uv sync
|
||||
export DEEPSEEK_API_KEY="your-api-key"
|
||||
uv run uvicorn main:app --reload
|
||||
```
|
||||
|
||||
服务默认运行在 `http://127.0.0.1:8000`,交互式 API 文档位于 `/docs`。
|
||||
|
||||
可通过环境变量配置 `DEEPSEEK_BASE_URL`、`DEEPSEEK_MODEL`、`DEFAULT_SYSTEM_PROMPT` 和 `CHAT_DATA_DIR`,完整示例见 `.env.example`。第一版应只使用一个 Uvicorn worker。
|
||||
|
||||
## 使用
|
||||
|
||||
创建会话:
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"system_prompt":"你是一个简洁的中文助手。"}'
|
||||
```
|
||||
|
||||
使用返回的 `session_id` 发送消息:
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"content":"你好,请记住我的名字是小明。"}'
|
||||
```
|
||||
|
||||
设置 `stream` 为 `true` 可通过 SSE 接收增量回复:
|
||||
|
||||
```bash
|
||||
curl -N -X POST http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"content":"介绍一下你自己。","stream":true}'
|
||||
```
|
||||
|
||||
流式响应依次返回 `delta` 事件、包含完整消息与 token 用量的 `done` 事件,最后以 `data: [DONE]` 结束。返回及存储的每条消息都包含 UTC `created_at`。只有完整响应成功结束后,本轮消息才会写入会话历史。
|
||||
Reference in New Issue
Block a user