# ChatAgent **Repository Path**: 3dseals/chat-agent ## Basic Information - **Project Name**: ChatAgent - **Description**: 通过 Playwright 自动化与 ChatGPT、DeepSeek、Grok 对话的 REST API 服务。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-21 - **Last Updated**: 2025-12-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ChatAgent 通过 Playwright 自动化与 ChatGPT、DeepSeek、Grok 对话的 REST API 服务。 ## 功能特性 - 🤖 支持与多个 AI 平台对话(ChatGPT、DeepSeek、Grok) - 🌐 提供 REST API 接口 - 🔄 异步处理,支持并发请求 - 🎭 无头浏览器模式,可配置 - 🔐 会话管理,支持多会话 - 📝 完整的测试覆盖 ## 项目结构 ``` ChatAgent/ ├── plan/ # 工作流和计划文档 │ ├── workflow.md # 工作流程文档 │ └── implementation_plan.md # 实施计划 ├── server/ # 服务器代码 │ ├── api/ # API 路由和模型 │ │ ├── routes.py # API 路由 │ │ └── models.py # 请求/响应模型 │ ├── core/ # 核心模块 │ │ ├── playwright_base.py # Playwright 基础类 │ │ └── session_manager.py # 会话管理器 │ ├── drivers/ # 平台驱动 │ │ ├── base_driver.py # 驱动基类 │ │ ├── chatgpt_driver.py # ChatGPT 驱动 │ │ ├── deepseek_driver.py # DeepSeek 驱动 │ │ ├── grok_driver.py # Grok 驱动 │ │ └── __init__.py # 驱动工厂 │ ├── config.py # 配置管理 │ └── main.py # FastAPI 主应用 ├── test/ # 测试代码 │ ├── conftest.py # pytest 配置 │ ├── test_drivers.py # 驱动测试 │ ├── test_api.py # API 测试 │ └── test_integration.py # 集成测试 ├── requirements.txt # Python 依赖 ├── .env.example # 环境变量示例 └── README.md # 项目说明 ``` ## 安装 ### 1. 克隆项目 ```bash git clone cd ChatAgent ``` ### 2. 安装 Python 依赖 ```bash pip install -r requirements.txt ``` ### 3. 安装 Playwright 浏览器 ```bash playwright install chromium ``` ### 4. 配置环境变量 复制 `.env.example` 为 `.env` 并填写配置: ```bash cp .env.example .env ``` 编辑 `.env` 文件,填入各平台的登录凭据: ```env # API 配置 API_HOST=0.0.0.0 API_PORT=8000 API_DEBUG=False # 浏览器配置 HEADLESS=True BROWSER_TYPE=chromium BROWSER_TIMEOUT=30000 # ChatGPT 凭据 CHATGPT_EMAIL=your_email@example.com CHATGPT_PASSWORD=your_password # DeepSeek 凭据 DEEPSEEK_EMAIL=your_email@example.com DEEPSEEK_PASSWORD=your_password # Grok 凭据(使用 Twitter/X 账号) GROK_USERNAME=your_username GROK_PASSWORD=your_password # 日志配置 LOG_LEVEL=INFO LOG_FILE= ``` ## 使用 ### 启动服务 ```bash python -m server.main ``` 或者使用 uvicorn: ```bash uvicorn server.main:app --host 0.0.0.0 --port 8000 ``` 服务启动后,访问: - API 文档: http://localhost:8000/docs - 健康检查: http://localhost:8000/health ### API 使用示例 #### 1. 获取支持的平台列表 ```bash curl http://localhost:8000/api/v1/platforms ``` #### 2. 获取服务状态 ```bash curl http://localhost:8000/api/v1/status ``` #### 3. 发送消息到 ChatGPT ```bash curl -X POST http://localhost:8000/api/v1/chat \ -H "Content-Type: application/json" \ -d '{ "platform": "chatgpt", "message": "你好,请介绍一下自己", "config": { "timeout": 30000, "headless": true } }' ``` #### 4. 发送消息到 DeepSeek ```bash curl -X POST http://localhost:8000/api/v1/chat \ -H "Content-Type: application/json" \ -d '{ "platform": "deepseek", "message": "解释一下量子计算", "config": { "timeout": 30000 } }' ``` #### 5. 发送消息到 Grok ```bash curl -X POST http://localhost:8000/api/v1/chat \ -H "Content-Type: application/json" \ -d '{ "platform": "grok", "message": "最新的 AI 新闻是什么?", "config": { "timeout": 30000 } }' ``` #### 6. 使用自定义凭据 ```bash curl -X POST http://localhost:8000/api/v1/chat \ -H "Content-Type: application/json" \ -d '{ "platform": "chatgpt", "message": "测试消息", "credentials": { "email": "custom@example.com", "password": "custom_password" } }' ``` ## 测试 运行所有测试: ```bash pytest ``` 运行特定测试文件: ```bash pytest test/test_drivers.py pytest test/test_api.py pytest test/test_integration.py ``` 运行测试并显示覆盖率: ```bash pytest --cov=server --cov-report=html ``` ## API 文档 启动服务后,访问以下地址查看完整的 API 文档: - Swagger UI: http://localhost:8000/docs - ReDoc: http://localhost:8000/redoc ## 注意事项 1. **登录凭据安全**: 请妥善保管登录凭据,不要将 `.env` 文件提交到版本控制系统。 2. **反爬虫机制**: 各平台可能有反爬虫机制(验证码、人机验证等),可能需要手动处理。 3. **页面结构变化**: 各平台的页面结构可能会变化,如果遇到问题,可能需要更新驱动中的选择器。 4. **并发限制**: 请注意各平台可能对并发请求有限制,建议合理控制请求频率。 5. **浏览器资源**: 每个会话会启动一个浏览器实例,注意系统资源消耗。 ## 开发 ### 添加新平台支持 1. 在 `server/drivers/` 目录下创建新的驱动文件 2. 继承 `BaseDriver` 类 3. 实现 `login()`, `send_message()`, `close()` 等方法 4. 在 `server/drivers/__init__.py` 中注册新驱动 ### 调试 设置 `API_DEBUG=True` 和 `HEADLESS=False` 可以在有头模式下运行,方便调试。 ## 许可证 MIT License ## 贡献 欢迎提交 Issue 和 Pull Request!