Skip to content

Commit 380a6ed

Browse files
author
User
committed
添加windows运行指南
1 parent b01e9af commit 380a6ed

3 files changed

Lines changed: 300 additions & 4 deletions

File tree

backend/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import sys
2+
# 使用内置sqlite3替代pysqlite3-binary (Windows不支持pysqlite3-binary)
3+
sys.modules['pysqlite3'] = __import__('sqlite3')
4+
15
from fastapi import FastAPI, HTTPException, Depends, UploadFile, File, Form, Request
26
from fastapi.middleware.cors import CORSMiddleware
37
from fastapi.responses import JSONResponse, Response
48
from fastapi.staticfiles import StaticFiles
59
from starlette.middleware.base import BaseHTTPMiddleware
610
from starlette.types import Message
7-
import sys
811
import os
912
import json
1013
import uuid

docs/Windows运行指南.md

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
# Windows 运行指南
2+
3+
本文档介绍如何在 Windows 系统上运行心语情感陪伴机器人项目。
4+
5+
## 📋 环境要求
6+
7+
- Windows 10/11
8+
- Python 3.10+
9+
- Node.js 16+
10+
- MySQL 8.0+
11+
- Git
12+
13+
## 🚀 快速开始
14+
15+
### 1. 安装基础软件
16+
17+
#### 1.1 安装 Python
18+
19+
1. 访问 [Python 官网](https://www.python.org/downloads/) 下载 Python 3.10+
20+
2. 安装时 **务必勾选** "Add Python to PATH"
21+
3. 验证安装:
22+
```powershell
23+
python --version
24+
pip --version
25+
```
26+
27+
#### 1.2 安装 Node.js
28+
29+
1. 访问 [Node.js 官网](https://nodejs.org/) 下载 LTS 版本
30+
2. 按默认选项安装
31+
3. 验证安装:
32+
```powershell
33+
node --version
34+
npm --version
35+
```
36+
37+
#### 1.3 安装 MySQL
38+
39+
1. 访问 [MySQL 下载页面](https://dev.mysql.com/downloads/installer/) 下载 MySQL Installer
40+
2. 选择 "MySQL Server" 进行安装
41+
3. 记住设置的 root 密码
42+
4. 验证安装:
43+
```powershell
44+
mysql --version
45+
```
46+
47+
#### 1.4 安装 Git(可选)
48+
49+
1. 访问 [Git 官网](https://git-scm.com/download/win) 下载安装
50+
2. 克隆项目:
51+
```powershell
52+
git clone https://github.com/congde/emotional_chat.git
53+
cd emotional_chat
54+
```
55+
56+
### 2. 安装项目依赖
57+
58+
打开 PowerShell,进入项目目录:
59+
60+
```powershell
61+
cd emotional_chat
62+
63+
# 安装后端依赖
64+
pip install -r requirements.txt
65+
66+
# 安装前端依赖
67+
cd frontend
68+
npm install
69+
cd ..
70+
```
71+
72+
> ⚠️ **注意**:如果安装 `dlib``face-recognition` 失败,可以在 `requirements.txt` 中注释掉这两行(人脸识别功能将不可用)。
73+
74+
### 3. 配置环境变量
75+
76+
```powershell
77+
# 复制配置文件模板
78+
copy config.env.example config.env
79+
80+
# 使用记事本编辑
81+
notepad config.env
82+
```
83+
84+
编辑 `config.env`,填入以下必要配置:
85+
86+
```bash
87+
# 阿里云通义千问 API Key(必填)
88+
LLM_API_KEY=your_qwen_api_key_here
89+
LLM_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
90+
91+
# MySQL 数据库配置(必填)
92+
MYSQL_HOST=localhost
93+
MYSQL_PORT=3306
94+
MYSQL_USER=root
95+
MYSQL_PASSWORD=your_mysql_password
96+
MYSQL_DATABASE=emotional_chat
97+
98+
# 模型配置
99+
DEFAULT_MODEL=qwen-plus
100+
TEMPERATURE=0.7
101+
MAX_TOKENS=1000
102+
103+
# 服务器配置
104+
HOST=0.0.0.0
105+
PORT=8000
106+
DEBUG=true
107+
```
108+
109+
**获取通义千问 API Key**
110+
1. 访问 [阿里云 DashScope](https://dashscope.console.aliyun.com/)
111+
2. 注册/登录阿里云账号
112+
3. 创建 API Key
113+
114+
### 4. 初始化数据库
115+
116+
#### 4.1 创建数据库
117+
118+
打开 MySQL 命令行或使用 MySQL Workbench:
119+
120+
```sql
121+
CREATE DATABASE emotional_chat CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
122+
```
123+
124+
#### 4.2 运行数据库迁移
125+
126+
```powershell
127+
# 在项目根目录执行
128+
alembic upgrade head
129+
```
130+
131+
#### 4.3 初始化 RAG 知识库(可选但推荐)
132+
133+
```powershell
134+
python init_rag_knowledge.py
135+
```
136+
137+
### 5. 启动服务
138+
139+
需要打开 **两个** PowerShell 窗口。
140+
141+
#### 终端 1:启动后端服务
142+
143+
```powershell
144+
cd emotional_chat
145+
python run_backend.py
146+
```
147+
148+
后端启动成功后会显示:
149+
```
150+
INFO: Uvicorn running on http://0.0.0.0:8000
151+
```
152+
153+
#### 终端 2:启动前端服务
154+
155+
```powershell
156+
cd emotional_chat\frontend
157+
npm start
158+
```
159+
160+
前端启动成功后会自动打开浏览器。
161+
162+
### 6. 访问应用
163+
164+
- 🌐 **前端界面**http://localhost:3000
165+
- 🔌 **后端 API**http://localhost:8000
166+
- 📖 **API 文档**http://localhost:8000/docs
167+
168+
### 7. 验证服务
169+
170+
```powershell
171+
# 健康检查
172+
curl http://localhost:8000/health
173+
174+
# 测试聊天接口
175+
curl -X POST http://localhost:8000/chat -H "Content-Type: application/json" -d "{\"message\": \"你好\", \"user_id\": \"test_user\"}"
176+
```
177+
178+
## 🐳 Docker 部署(可选)
179+
180+
如果你安装了 Docker Desktop for Windows,可以使用 Docker 一键部署:
181+
182+
```powershell
183+
# 配置环境变量
184+
copy config.env.example config.env
185+
notepad config.env
186+
187+
# 启动所有服务
188+
docker-compose up -d
189+
190+
# 查看服务状态
191+
docker-compose ps
192+
```
193+
194+
## ❓ 常见问题
195+
196+
### 1. pip 安装依赖失败
197+
198+
```powershell
199+
# 使用国内镜像源
200+
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
201+
```
202+
203+
### 2. npm 安装依赖慢
204+
205+
```powershell
206+
# 设置淘宝镜像
207+
npm config set registry https://registry.npmmirror.com
208+
209+
# 重新安装
210+
cd frontend
211+
npm install
212+
```
213+
214+
### 3. MySQL 连接失败
215+
216+
- 确认 MySQL 服务已启动(服务管理器中查看 MySQL80)
217+
- 检查 `config.env` 中的数据库配置是否正确
218+
- 确认数据库 `emotional_chat` 已创建
219+
220+
### 4. 端口被占用
221+
222+
```powershell
223+
# 查看端口占用
224+
netstat -ano | findstr :8000
225+
netstat -ano | findstr :3000
226+
227+
# 结束占用进程(替换 PID)
228+
taskkill /PID <PID> /F
229+
```
230+
231+
### 5. dlib/face-recognition 安装失败
232+
233+
这两个包需要 C++ 编译环境,如果不需要人脸识别功能,可以在 `requirements.txt` 中注释掉:
234+
235+
```
236+
# face-recognition>=1.3.0
237+
# dlib>=19.24.0
238+
```
239+
240+
### 6. pysqlite3-binary 不支持 Windows
241+
242+
Windows 不支持 `pysqlite3-binary`,项目已在 `backend/main.py` 中做了兼容处理:
243+
244+
```python
245+
import sys
246+
# 使用内置sqlite3替代pysqlite3-binary (Windows不支持pysqlite3-binary)
247+
sys.modules['pysqlite3'] = __import__('sqlite3')
248+
```
249+
250+
如果遇到 sqlite3 相关错误,确保这段代码在 `backend/main.py` 文件开头。
251+
252+
### 7. PowerShell 执行策略限制
253+
254+
如果遇到脚本执行限制,以管理员身份运行:
255+
256+
```powershell
257+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
258+
```
259+
260+
## 📝 开发建议
261+
262+
### 使用 VS Code
263+
264+
推荐使用 VS Code 进行开发,安装以下扩展:
265+
- Python
266+
- Pylance
267+
- ES7+ React/Redux/React-Native snippets
268+
- MySQL (可选)
269+
270+
### 虚拟环境(推荐)
271+
272+
```powershell
273+
# 创建虚拟环境
274+
python -m venv venv
275+
276+
# 激活虚拟环境
277+
.\venv\Scripts\Activate.ps1
278+
279+
# 安装依赖
280+
pip install -r requirements.txt
281+
```
282+
283+
## 🔗 相关链接
284+
285+
- [项目主页](https://github.com/congde/emotional_chat)
286+
- [阿里云 DashScope](https://dashscope.console.aliyun.com/)
287+
- [Python 下载](https://www.python.org/downloads/)
288+
- [Node.js 下载](https://nodejs.org/)
289+
- [MySQL 下载](https://dev.mysql.com/downloads/installer/)
290+
291+
---
292+
293+
如有问题,欢迎提交 Issue 或联系维护者。
294+

requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pandas>=2.0.0
3030
# ChromaDB 0.5.x 可能有 API 变化,锁定 0.4.x 以保持兼容性
3131
chromadb>=0.4.22,<0.5.0
3232
sentence-transformers>=2.7.0
33-
# 注意: pysqlite3-binary 用于替代系统sqlite3以支持ChromaDB
34-
# 如果镜像源没有,可以尝试: pip install pysqlite3-binary --index-url https://pypi.org/simple
35-
pysqlite3-binary>=0.5.0
33+
# 注意: pysqlite3-binary 在 Windows 上不可用,已在代码中使用内置 sqlite3 替代
34+
# 参见 backend/main.py 开头的 sys.modules['pysqlite3'] = __import__('sqlite3')
3635

3736
# 数据库
3837
PyMySQL>=1.1.0

0 commit comments

Comments
 (0)