42 lines
1.2 KiB
Markdown
42 lines
1.2 KiB
Markdown
# 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":"你好,请记住我的名字是小明。"}'
|
|
```
|
|
|
|
API 返回及存储的每条消息都包含 UTC `created_at`。只有模型完整响应成功后,本轮消息才会写入会话历史。
|
|
|
|
获取指定会话的完整历史:
|
|
|
|
```bash
|
|
curl http://127.0.0.1:8000/sessions/SESSION_ID/messages
|
|
```
|