ai-chat-ui/server/README.md

112 lines
2.7 KiB
Markdown
Raw 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.

# Python AI Chat Server
这是原有Node.js服务器的Python替代版本使用FastAPI和DashScope Python SDK连接阿里云百炼平台API。
## 特性
- 基于FastAPI的高性能异步服务器
- 支持流式和非流式对话
- 完全兼容前端API端点
- 支持多模态输入(文本+图像)
- 集成阿里云百炼API
- 文件上传功能
- 对话历史管理
## 安装要求
- Python 3.8+
- pip包管理器
## 安装步骤
1. **克隆或复制代码**
2. **安装Python依赖**
```bash
cd server
pip install -r requirements.txt
```
3. **配置环境变量**
```bash
cp .env.example .env
```
编辑`.env`文件填入您的阿里云百炼API密钥
```
ALIYUN_API_KEY=your_actual_api_key_here
```
## 启动服务器
### 方法一:直接运行
```bash
python run_server.py
```
### 方法二使用uvicorn
```bash
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
```
## API端点
服务器提供了与原Node.js版本完全相同的API端点
- `POST /api/chat-ui/chat` - 聊天接口(支持流式和非流式)
- `GET /api/chat-ui/models` - 获取模型列表
- `GET /api/chat-ui/conversations` - 获取所有对话
- `GET /api/chat-ui/conversations/{id}` - 获取特定对话
- `POST /api/chat-ui/conversations` - 保存/更新对话
- `DELETE /api/chat-ui/conversations/{id}` - 删除对话
- `POST /api/chat-ui/upload` - 文件上传
- `POST /api/chat-ui/stop` - 停止生成
- `POST /api/chat-ui/stop/{id}` - 按ID停止生成
- `GET /health` - 健康检查
## 前端配置
修改Vite配置`vite.config.ts`)中的代理目标:
```typescript
server: {
proxy: {
"/api/chat-ui": {
target: "http://localhost:8000", // 修改为Python服务器端口
changeOrigin: true,
},
},
},
```
## 环境变量
- `ALIYUN_API_KEY`: 阿里云百炼API密钥必填
- `PORT`: 服务器端口默认8000
## 依赖说明
- `fastapi`: 现代高性能web框架
- `uvicorn`: ASGI服务器
- `dashscope`: 阿里云百炼SDK
- `python-multipart`: 处理文件上传
- `python-dotenv`: 环境变量管理
## 错误排查
1. **API密钥错误**: 确保在`.env`文件中正确设置了`ALIYUN_API_KEY`
2. **端口冲突**: 检查8000端口是否被占用可以修改`.env`中的`PORT`变量
3. **依赖问题**: 确保已正确安装所有依赖
## 注意事项
- Python服务器提供了与Node.js服务器相同的功能和API接口
- 保留了原有的日志记录机制
- 对话数据仍存储在内存中,生产环境建议使用数据库
- 支持与原前端应用无缝集成