AgentChat是一个基于大语言模型(LLM)的智能代理通信平台,采用前后端分离架构。本指南将详细说明如何搭建完整的本地开发环境,包括Python后端、Vue.js前端、数据库连接以及向量数据库配置。
在开始之前,请确保您的系统满足以下要求:
AgentChat采用模块化的项目结构:
graph TB
subgraph "项目根目录"
Root[AgentChat/]
subgraph "脚本目录"
Scripts[scripts/]
StartScript[start.py]
ReadmeScript[README.md]
end
subgraph "源码目录"
Src[src/]
subgraph "后端"
Backend[src/backend/]
AgentChat[agentchat/]
BackendMain[main.py]
BackendSettings[settings.py]
BackendConfig[config.yaml]
end
subgraph "前端"
Frontend[src/frontend/]
FrontendPackage[package.json]
FrontendSrc[src/]
FrontendPages[pages/]
end
end
subgraph "配置文件"
PyProject[pyproject.toml]
end
end
Root --> Scripts
Root --> Src
Root --> PyProject
Scripts --> StartScript
Scripts --> ReadmeScript
Src --> Backend
Src --> Frontend
Backend --> AgentChat
Backend --> BackendMain
Backend --> BackendSettings
Backend --> BackendConfig
Frontend --> FrontendPackage
Frontend --> FrontendSrc
Frontend --> FrontendPages
图表来源
章节来源
推荐使用以下方法之一创建隔离的Python环境:
# 创建虚拟环境
python -m venv agentchat_env
# 激活虚拟环境
# Windows:
agentchat_env\Scripts\activate
# macOS/Linux:
source agentchat_env/bin/activate
# 创建Conda环境
conda create -n agentchat python=3.12
# 激活环境
conda activate agentchat
项目支持两种依赖安装方式:
# 安装Poetry (如果尚未安装)
curl -sSL https://install.python-poetry.org | python -
# 安装项目依赖
cd AgentChat
poetry install
# 安装pip (如果尚未安装)
python -m ensurepip --upgrade
# 安装项目依赖
cd AgentChat
pip install -e .
根据pyproject.toml文件,项目需要以下核心依赖:
| 组件类别 | 主要依赖 | 版本要求 |
|---|---|---|
| Web框架 | FastAPI | ^0.121.0 |
| 数据库 | SQLModel | ^0.0.27 |
| AI模型 | LangChain | ^1.0.3 |
| 向量数据库 | ChromaDB | ^1.3.4 |
| 向量数据库 | Milvus | ^2.6.3 |
| 缓存 | Redis | ^7.0.1 |
| 文件处理 | PyMuPDF | ^0.1.8 |
章节来源
确保安装了Node.js 18+版本:
# 检查Node.js版本
node --version
# 检查npm版本
npm --version
导航到前端目录并安装依赖:
cd src/frontend
npm install
前端项目使用以下技术栈:
| 技术 | 版本 | 用途 |
|---|---|---|
| Vue.js | ^3.4.21 | 前端框架 |
| TypeScript | ^5.4.3 | 类型安全 |
| Vite | ^5.2.8 | 构建工具 |
| Element Plus | ^2.7.0 | UI组件库 |
| Pinia | ^2.1.7 | 状态管理 |
| Axios | ^1.6.8 | HTTP客户端 |
前端项目提供了以下npm脚本:
| 命令 | 功能 | 说明 |
|---|---|---|
npm run dev |
开发服务器 | 启动热重载开发服务器 |
npm run build |
生产构建 | 构建生产环境代码 |
npm run preview |
预览构建 | 预览生产构建效果 |
npm run lint |
代码检查 | TypeScript类型检查 |
章节来源
AgentChat支持多种数据库配置,主要使用MySQL和Redis。
mysql:
endpoint: "mysql+pymysql://root:mingguang@localhost:3306/agentchat"
async_endpoint: "mysql+aiomysql://root:mingguang@localhost:3306/agentchat"
redis:
endpoint: "redis://localhost:6379"
项目支持两种向量数据库:
rag:
vector_db:
host: "47.95.23.87"
port: "19530"
mode: "chroma"
# 需要在代码中实现
rag:
vector_db:
mode: "milvus"
multi_models:
conversation_model:
api_key: "sk-6d47d94f4c7343579030b6a75b65f31f"
base_url: "https://api.deepseek.com/v1"
model_name: "deepseek-chat"
tools:
weather:
api_key: "fac0ad465078da96a3ec6ba1ccc6746f"
tavily:
api_key: "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"
配置文件位于src/backend/agentchat/config.yaml,包含以下主要部分:
graph TD
Config[config.yaml] --> Server[服务配置]
Config --> MySQL[数据库配置]
Config --> Redis[缓存配置]
Config --> Models[多模型配置]
Config --> Tools[工具配置]
Config --> RAG[RAG配置]
Config --> OSS[阿里云OSS配置]
Config --> Default[默认配置]
Config --> WeChat[微信配置]
Config --> LangFuse[追踪工具配置]
Models --> Conversation[对话模型]
Models --> ToolCall[工具调用模型]
Models --> Reasoning[推理模型]
Models --> Text2Image[文生图模型]
Models --> QwenVL[多模态模型]
Models --> Embedding[嵌入模型]
Models --> Rerank[重排序模型]
图表来源
章节来源
项目提供了一键启动脚本,简化开发环境启动过程。
# Windows
agentchat_env\Scripts\activate
# macOS/Linux
source agentchat_env/bin/activate
cd AgentChat
python scripts/start.py
sequenceDiagram
participant User as 用户
participant Script as start.py
participant Backend as 后端服务
participant Frontend as 前端服务
User->>Script : 执行 python scripts/start.py
Script->>Script : 1. 定位项目根目录
Script->>Script : 2. 查找依赖文件 (request*.txt 或 requirements.txt)
Script->>Script : 3. 安装Python依赖
Script->>Backend : 4. 启动FastAPI服务 (端口7860)
Script->>Frontend : 5. 启动Vite开发服务器
Script->>User : 显示服务状态和日志
Note over User,Frontend : Ctrl+C 停止服务
图表来源
http://127.0.0.1:7860http://127.0.0.1:5173 (由Vite自动分配)启动成功后,您应该能看到以下输出:
🚀 [Step 1] 正在查找依赖文件...
📦 发现依赖文件: ./requirements.txt
⏳ 正在安装依赖 (pip install)...
✅ 依赖安装完成!
🚀 [Step 2] 启动后端 (cwd: src/backend)...
🚀 [Step 3] 启动前端 (cwd: src/frontend)...
✨ 服务已启动!日志将混合显示在下方。
🛑 按 Ctrl+C 可停止服务。
章节来源
❌ 依赖安装失败,脚本终止。
# 确认项目根目录存在依赖文件
ls requirements.txt # Linux/macOS
dir requirements.txt # Windows
pip install -r requirements.txt
poetry install
3. **网络问题处理**
```bash
# 设置国内镜像源
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
ERROR: Port 7860 is already in use
src/backend/agentchat/config.yaml:
server:
host: "127.0.0.1"
port: 7861 # 修改为其他可用端口
# 后端单独启动时指定端口
uvicorn agentchat.main:app --port 7861
# 解决方案:检查Node.js安装
node --version
npm --version
# 如果未安装,从官网下载:
# https://nodejs.org/
# 清理缓存重新安装
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
# 检查MySQL服务状态
# Windows:
net start mysql
# Linux:
sudo systemctl start mysql
# macOS:
brew services start mysql
# 检查Redis服务
redis-cli ping
# 如果失败,启动Redis服务
redis-server
# 检查ChromaDB服务
pip install chromadb
python -c "import chromadb; print('ChromaDB installed')"
# 检查Milvus服务
pip install pymilvus
python -c "import pymilvus; print('Milvus installed')"
# 以管理员身份运行PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 确保脚本可执行
chmod +x scripts/start.py
如果自动化脚本无法正常工作,可以手动启动各个服务。
cd src/backend
uvicorn agentchat.main:app --reload --port 7860
cd src/backend
python -m agentchat.main
/health端点可用cd src/frontend
npm run dev
cd src/backend
python -c "
from agentchat.database.init_data import init_database, init_default_agent
import asyncio
asyncio.run(init_database())
asyncio.run(init_default_agent())
print('数据库初始化完成')
"
# 启动Redis (如果未启动)
redis-server
# ChromaDB (本地模式)
python -c "
import chromadb
client = chromadb.Client()
print('ChromaDB连接成功')
"
# Milvus (需要单独安装和配置)
章节来源
AgentChat使用YAML配置文件,支持以下配置方式:
flowchart TD
Start[应用启动] --> LoadConfig[加载配置文件]
LoadConfig --> CheckEnv{检查环境变量}
CheckEnv --> |存在| Override[覆盖配置]
CheckEnv --> |不存在| UseFile[使用文件配置]
Override --> FinalConfig[最终配置]
UseFile --> FinalConfig
FinalConfig --> InitServices[初始化服务]
图表来源
mysql:
endpoint: "${MYSQL_ENDPOINT:-mysql+pymysql://root:password@localhost:3306/agentchat}"
async_endpoint: "${ASYNC_MYSQL_ENDPOINT:-mysql+aiomysql://root:password@localhost:3306/agentchat}"
redis:
endpoint: "${REDIS_ENDPOINT:-redis://localhost:6379}"
multi_models:
conversation_model:
api_key: "${OPENAI_API_KEY:-your-api-key-here}"
base_url: "${OPENAI_BASE_URL:-https://api.openai.com/v1}"
model_name: "${CONVERSATION_MODEL:-gpt-4}"
rag:
vector_db:
mode: "${VECTOR_DB_MODE:-chroma}" # chroma, milvus
host: "${VECTOR_DB_HOST:-localhost}"
port: "${VECTOR_DB_PORT:-19530}"
$env:MYSQL_ENDPOINT="mysql+pymysql://root:password@localhost:3306/agentchat"
$env:REDIS_ENDPOINT="redis://localhost:6379"
$env:OPENAI_API_KEY="your-api-key-here"
set MYSQL_ENDPOINT=mysql+pymysql://root:password@localhost:3306/agentchat
set REDIS_ENDPOINT=redis://localhost:6379
set OPENAI_API_KEY=your-api-key-here
export MYSQL_ENDPOINT="mysql+pymysql://root:password@localhost:3306/agentchat"
export REDIS_ENDPOINT="redis://localhost:6379"
export OPENAI_API_KEY="your-api-key-here"
创建.env文件(可选):
# 数据库配置
MYSQL_ENDPOINT=mysql+pymysql://root:password@localhost:3306/agentchat
ASYNC_MYSQL_ENDPOINT=mysql+aiomysql://root:password@localhost:3306/agentchat
# 缓存配置
REDIS_ENDPOINT=redis://localhost:6379
# LLM配置
OPENAI_API_KEY=your-api-key-here
OPENAI_BASE_URL=https://api.openai.com/v1
CONVERSATION_MODEL=gpt-4
# 向量数据库
VECTOR_DB_MODE=chroma
VECTOR_DB_HOST=localhost
VECTOR_DB_PORT=19530
章节来源