finyx_data_ai/SILICONFLOW_SETUP.md
2026-01-11 07:48:19 +08:00

173 lines
3.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 硅基流动 API Key 配置指南
## 🔑 配置 API Key
### 步骤 1: 检查 .env 文件
请确保 `.env` 文件中有以下配置(**不是 `.env.example`**
```bash
# 硅基流动 (SiliconFlow) - 可选
SILICONFLOW_API_KEY=sk-xxxxxxxxxxxxx # 请替换为您的实际 API Key
SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1/chat/completions
SILICONFLOW_MODEL=deepseek-chat
```
### 步骤 2: 获取 API Key
1. 访问 [硅基流动官网](https://siliconflow.cn)
2. 注册/登录账号
3. 进入控制台,找到 API Key 管理页面
4. 创建或复制您的 API Key格式通常为 `sk-xxxxxxxxxxxxx`
### 步骤 3: 编辑 .env 文件
```bash
# 编辑 .env 文件
nano .env
# 或
vim .env
# 或使用您喜欢的编辑器
# 找到这一行:
SILICONFLOW_API_KEY=
# 替换为:
SILICONFLOW_API_KEY=sk-xxxxxxxxxxxxx # 您的实际 API Key
```
### 步骤 4: 验证配置
运行以下命令验证配置是否正确:
```bash
source venv/bin/activate
python3 -c "from app.core.config import settings; key = settings.SILICONFLOW_API_KEY; print(f'API Key 已配置: {key is not None and key != \"\"}'); print(f'API Key 前10个字符: {key[:10] if key else \"未配置\"}')"
```
如果输出显示 "API Key 已配置: True",说明配置成功。
### 步骤 5: 重启服务
配置完成后,需要重启服务:
```bash
# 停止当前服务
pkill -f "uvicorn app.main:app"
# 重新启动
source venv/bin/activate
nohup uvicorn app.main:app --host 0.0.0.0 --port 8000 > server.log 2>&1 &
```
## ✅ 验证配置是否生效
### 方法 1: 检查配置加载
```bash
source venv/bin/activate
python3 -c "from app.core.config import settings; print('API Key:', '已配置' if settings.SILICONFLOW_API_KEY else '未配置')"
```
### 方法 2: 测试接口
运行测试脚本:
```bash
./test_siliconflow.sh
```
或手动调用:
```bash
curl -X POST "http://localhost:8000/api/v1/inventory/ai-analyze" \
-H "Content-Type: application/json" \
-d '{
"tables": [
{
"raw_name": "t_user_base_01",
"fields": [
{
"raw_name": "user_id",
"type": "varchar(64)",
"comment": "用户ID"
}
]
}
],
"project_id": "project_001",
"options": {
"model": "deepseek-chat"
}
}'
```
## ⚠️ 常见问题
### 问题 1: API Key 配置后仍然显示未配置
**解决方案**:
- 确保编辑的是 `.env` 文件(不是 `.env.example`
- 确保 API Key 没有多余的引号或空格
- 重启服务(配置只在启动时加载)
- 检查 `.env` 文件路径是否正确
### 问题 2: 401 Unauthorized 错误
**可能原因**:
- API Key 错误或过期
- API Key 没有权限
- API Key 格式不正确
**解决方案**:
- 重新生成 API Key
- 检查 API Key 是否正确复制(没有多余空格)
- 确认账号余额是否充足
### 问题 3: 服务启动失败
**解决方案**:
- 检查日志文件:`tail -f server.log`
- 确认 Python 环境和依赖已正确安装
- 检查端口 8000 是否被占用
## 📝 配置示例
### 正确的 .env 配置示例
```bash
# 硅基流动 (SiliconFlow)
SILICONFLOW_API_KEY=sk-1234567890abcdefghijklmnopqrstuvwxyz
SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1/chat/completions
SILICONFLOW_MODEL=deepseek-chat
```
**注意**:
- API Key 前后不要有引号
- API Key 不要有空格
- 等号两边可以有空格,但不建议
## 🚀 测试命令
配置完成后,可以使用以下命令快速测试:
```bash
# 1. 验证配置
source venv/bin/activate
python3 -c "from app.core.config import settings; print('✅ API Key 已配置' if settings.SILICONFLOW_API_KEY else '❌ API Key 未配置')"
# 2. 重启服务
pkill -f "uvicorn app.main:app"
sleep 2
source venv/bin/activate
nohup uvicorn app.main:app --host 0.0.0.0 --port 8000 > server.log 2>&1 &
sleep 3
# 3. 测试接口
./test_siliconflow.sh
```
---
**最后更新**: 2026-01-10