mirror of
https://github.com/camel-ai/owl.git
synced 2026-03-22 05:57:17 +08:00
Merge branch 'main' into branch_mk
This commit is contained in:
74
.container/.dockerignore
Normal file
74
.container/.dockerignore
Normal file
@@ -0,0 +1,74 @@
|
||||
# Git
|
||||
.git
|
||||
.gitignore
|
||||
.github
|
||||
|
||||
# Docker
|
||||
Dockerfile
|
||||
docker-compose.yml
|
||||
.dockerignore
|
||||
DOCKER_README.md
|
||||
run_in_docker.sh
|
||||
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
env/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
.pytest_cache/
|
||||
.coverage
|
||||
htmlcov/
|
||||
|
||||
# 虚拟环境
|
||||
venv/
|
||||
ENV/
|
||||
env/
|
||||
.env
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
.DS_Store
|
||||
|
||||
# 临时文件
|
||||
temp_*
|
||||
*.tmp
|
||||
*.log
|
||||
*.bak
|
||||
|
||||
# 缓存
|
||||
.cache/
|
||||
.npm/
|
||||
.yarn/
|
||||
|
||||
# 大型数据文件
|
||||
*.csv
|
||||
*.sqlite
|
||||
*.db
|
||||
*.hdf5
|
||||
*.h5
|
||||
*.parquet
|
||||
*.feather
|
||||
*.pkl
|
||||
*.pickle
|
||||
|
||||
# 数据目录
|
||||
data/
|
||||
298
.container/DOCKER_README.md
Normal file
298
.container/DOCKER_README.md
Normal file
@@ -0,0 +1,298 @@
|
||||
# OWL项目Docker使用指南
|
||||
|
||||
本文档提供了如何使用Docker运行OWL项目的详细说明。
|
||||
|
||||
## 前提条件
|
||||
|
||||
- 安装 [Docker](https://docs.docker.com/get-docker/)
|
||||
- 安装 [Docker Compose](https://docs.docker.com/compose/install/) (推荐v2.x版本)
|
||||
- 获取必要的API密钥(OpenAI API等)
|
||||
|
||||
## 技术说明
|
||||
|
||||
本Docker配置使用了以下技术来确保OWL项目在容器中正常运行:
|
||||
|
||||
- **Xvfb**:虚拟帧缓冲区,用于在无显示器的环境中模拟X服务器
|
||||
- **Playwright**:用于自动化浏览器操作,配置为无头模式
|
||||
- **共享内存**:增加了共享内存大小,以提高浏览器性能
|
||||
- **BuildKit**:使用Docker BuildKit加速构建过程
|
||||
- **缓存优化**:使用持久化卷缓存pip和Playwright依赖
|
||||
- **跨平台兼容**:提供了适用于Windows和macOS/Linux的脚本
|
||||
|
||||
## Docker Compose版本说明
|
||||
|
||||
本项目使用的docker-compose.yml文件兼容Docker Compose v2.x版本。如果您使用的是较旧的Docker Compose v1.x版本,可能需要手动添加版本号:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
# ...其余配置保持不变
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 0. 检查环境
|
||||
|
||||
首先,运行检查脚本确保您的环境已准备好:
|
||||
|
||||
#### 在macOS/Linux上检查
|
||||
|
||||
```bash
|
||||
# 先给脚本添加执行权限
|
||||
chmod +x check_docker.sh
|
||||
|
||||
# 运行检查脚本
|
||||
./check_docker.sh
|
||||
```
|
||||
|
||||
#### 在Windows上检查
|
||||
|
||||
```cmd
|
||||
check_docker.bat
|
||||
```
|
||||
|
||||
如果检查脚本发现任何问题,请按照提示进行修复。
|
||||
|
||||
### 1. 配置环境变量
|
||||
|
||||
复制环境变量模板文件并填写必要的API密钥:
|
||||
|
||||
```bash
|
||||
cp owl/.env_template owl/.env
|
||||
```
|
||||
|
||||
然后编辑 `owl/.env` 文件,填写必要的API密钥,例如:
|
||||
|
||||
```
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
GOOGLE_API_KEY=your_google_api_key
|
||||
SEARCH_ENGINE_ID=your_search_engine_id
|
||||
```
|
||||
|
||||
### 2. 快速构建Docker镜像
|
||||
|
||||
#### 在macOS/Linux上构建
|
||||
|
||||
使用提供的Shell脚本,可以加速Docker镜像的构建:
|
||||
|
||||
```bash
|
||||
# 先给脚本添加执行权限
|
||||
chmod +x build_docker.sh
|
||||
|
||||
# 运行构建脚本
|
||||
./build_docker.sh
|
||||
```
|
||||
|
||||
#### 在Windows上构建
|
||||
|
||||
使用提供的批处理文件:
|
||||
|
||||
```cmd
|
||||
build_docker.bat
|
||||
```
|
||||
|
||||
或者使用标准方式构建并启动:
|
||||
|
||||
```bash
|
||||
# 使用BuildKit加速构建
|
||||
set DOCKER_BUILDKIT=1
|
||||
set COMPOSE_DOCKER_CLI_BUILD=1
|
||||
docker-compose build --build-arg BUILDKIT_INLINE_CACHE=1
|
||||
|
||||
# 启动容器
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
### 3. 交互式使用容器
|
||||
|
||||
容器启动后,会自动进入交互式shell环境,并显示欢迎信息和可用脚本列表:
|
||||
|
||||
```bash
|
||||
# 进入容器(如果没有自动进入)
|
||||
docker-compose exec owl bash
|
||||
```
|
||||
|
||||
在容器内,您可以直接运行任何可用的脚本:
|
||||
|
||||
```bash
|
||||
# 运行默认脚本
|
||||
xvfb-python run.py
|
||||
|
||||
# 运行DeepSeek示例
|
||||
xvfb-python run_deepseek_example.py
|
||||
|
||||
# 运行脚本并传递查询参数
|
||||
xvfb-python run.py "什么是人工智能?"
|
||||
```
|
||||
|
||||
### 4. 使用外部脚本运行查询
|
||||
|
||||
#### 在macOS/Linux上运行
|
||||
|
||||
```bash
|
||||
# 先给脚本添加执行权限
|
||||
chmod +x run_in_docker.sh
|
||||
|
||||
# 默认使用 run.py 脚本
|
||||
./run_in_docker.sh "你的问题"
|
||||
|
||||
# 指定使用特定脚本
|
||||
./run_in_docker.sh run_deepseek_example.py "你的问题"
|
||||
```
|
||||
|
||||
#### 在Windows上运行
|
||||
|
||||
```cmd
|
||||
REM 默认使用 run.py 脚本
|
||||
run_in_docker.bat "你的问题"
|
||||
|
||||
REM 指定使用特定脚本
|
||||
run_in_docker.bat run_deepseek_example.py "你的问题"
|
||||
```
|
||||
|
||||
**可用脚本**:
|
||||
- `run.py` - 默认脚本,使用OpenAI GPT-4o模型
|
||||
- `run_deepseek_example.py` - 使用DeepSeek模型
|
||||
- `run_gaia_roleplaying.py` - GAIA基准测试脚本
|
||||
|
||||
## 目录挂载
|
||||
|
||||
Docker Compose配置中已经设置了以下挂载点:
|
||||
|
||||
- `./owl/.env:/app/owl/.env`:挂载环境变量文件,方便修改API密钥
|
||||
- `./data:/app/data`:挂载数据目录,用于存储和访问数据文件
|
||||
- `playwright-cache`:持久化卷,用于缓存Playwright浏览器
|
||||
- `pip-cache`:持久化卷,用于缓存pip包
|
||||
|
||||
## 环境变量
|
||||
|
||||
您可以通过以下两种方式设置环境变量:
|
||||
|
||||
1. 修改 `owl/.env` 文件
|
||||
2. 在 `docker-compose.yml` 文件的 `environment` 部分添加环境变量
|
||||
|
||||
## 构建优化
|
||||
|
||||
本Docker配置包含多项构建优化:
|
||||
|
||||
1. **使用国内镜像源**:使用清华大学镜像源加速pip包下载
|
||||
2. **层优化**:减少Dockerfile中的层数,提高构建效率
|
||||
3. **缓存利用**:
|
||||
- 启用pip缓存,避免重复下载依赖包
|
||||
- 使用Docker BuildKit内联缓存
|
||||
- 合理安排Dockerfile指令顺序,最大化利用缓存
|
||||
4. **BuildKit**:启用Docker BuildKit加速构建
|
||||
5. **持久化缓存**:
|
||||
- 使用Docker卷缓存pip包(`pip-cache`)
|
||||
- 使用Docker卷缓存Playwright浏览器(`playwright-cache`)
|
||||
- 本地缓存目录(`.docker-cache`)
|
||||
|
||||
### 缓存清理
|
||||
|
||||
如果需要清理缓存,可以使用以下命令:
|
||||
|
||||
```bash
|
||||
# 清理Docker构建缓存
|
||||
docker builder prune
|
||||
|
||||
# 清理Docker卷(会删除所有未使用的卷,包括缓存卷)
|
||||
docker volume prune
|
||||
|
||||
# 清理本地缓存目录
|
||||
rm -rf .docker-cache
|
||||
```
|
||||
|
||||
## 跨平台兼容性
|
||||
|
||||
本项目提供了适用于不同操作系统的脚本:
|
||||
|
||||
1. **检查脚本**:
|
||||
- `check_docker.sh`(macOS/Linux):检查Docker环境
|
||||
- `check_docker.bat`(Windows):检查Docker环境
|
||||
|
||||
2. **构建脚本**:
|
||||
- `build_docker.sh`(macOS/Linux):构建Docker镜像
|
||||
- `build_docker.bat`(Windows):构建Docker镜像
|
||||
|
||||
3. **运行脚本**:
|
||||
- `run_in_docker.sh`(macOS/Linux):运行Docker容器中的脚本
|
||||
- `run_in_docker.bat`(Windows):运行Docker容器中的脚本
|
||||
|
||||
这些脚本会自动检测操作系统类型,并使用适当的命令。
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 容器无法启动
|
||||
|
||||
检查日志以获取更多信息:
|
||||
|
||||
```bash
|
||||
docker-compose logs
|
||||
```
|
||||
|
||||
### API密钥问题
|
||||
|
||||
确保您已经在 `owl/.env` 文件中正确设置了所有必要的API密钥。
|
||||
|
||||
### Docker Compose警告
|
||||
|
||||
如果您看到关于`version`属性过时的警告:
|
||||
|
||||
```
|
||||
WARN[0000] docker-compose.yml: the attribute `version` is obsolete
|
||||
```
|
||||
|
||||
这是因为您使用的是Docker Compose v2.x,它不再需要显式指定版本号。我们已经从配置文件中移除了这个属性,所以您不会再看到这个警告。
|
||||
|
||||
### 浏览器相关问题
|
||||
|
||||
如果遇到浏览器相关的问题,可以尝试以下解决方案:
|
||||
|
||||
1. 确保在Docker容器中使用`xvfb-python`命令运行Python脚本
|
||||
2. 检查是否正确安装了Xvfb和相关依赖
|
||||
3. 增加共享内存大小(在docker-compose.yml中已设置为2GB)
|
||||
|
||||
### 构建速度慢
|
||||
|
||||
如果构建速度慢,可以尝试以下解决方案:
|
||||
|
||||
1. 确保启用了Docker BuildKit(`DOCKER_BUILDKIT=1`)
|
||||
2. 确保启用了pip缓存(已在docker-compose.yml中配置)
|
||||
3. 使用`--build-arg BUILDKIT_INLINE_CACHE=1`参数构建(已在构建脚本中配置)
|
||||
4. 如果是首次构建,下载依赖包可能需要较长时间,后续构建会更快
|
||||
|
||||
### Windows特有问题
|
||||
|
||||
如果在Windows上遇到问题:
|
||||
|
||||
1. 确保使用管理员权限运行命令提示符或PowerShell
|
||||
2. 如果遇到路径问题,尝试使用正斜杠(/)而不是反斜杠(\)
|
||||
3. 如果遇到Docker Compose命令问题,尝试使用`docker compose`(无连字符)
|
||||
|
||||
### 内存不足
|
||||
|
||||
如果遇到内存不足的问题,可以在 `docker-compose.yml` 文件中调整资源限制:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
owl:
|
||||
# 其他配置...
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '4' # 增加CPU核心数
|
||||
memory: 8G # 增加内存限制
|
||||
```
|
||||
|
||||
## 自定义Docker镜像
|
||||
|
||||
如果需要自定义Docker镜像,可以修改 `Dockerfile` 文件,然后重新构建:
|
||||
|
||||
```bash
|
||||
# macOS/Linux
|
||||
./build_docker.sh
|
||||
|
||||
# Windows
|
||||
build_docker.bat
|
||||
```
|
||||
298
.container/DOCKER_README_en.md
Normal file
298
.container/DOCKER_README_en.md
Normal file
@@ -0,0 +1,298 @@
|
||||
# OWL Project Docker Usage Guide
|
||||
|
||||
This document provides detailed instructions on how to run the OWL project using Docker.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
• Install [Docker](https://docs.docker.com/get-docker/)
|
||||
• Install [Docker Compose](https://docs.docker.com/compose/install/) (recommended v2.x version)
|
||||
• Obtain necessary API keys (OpenAI API, etc.)
|
||||
|
||||
## Technical Notes
|
||||
|
||||
This Docker configuration uses the following technologies to ensure the OWL project runs smoothly in containers:
|
||||
|
||||
• **Xvfb**: Virtual framebuffer, used to simulate an X server in a headless environment
|
||||
• **Playwright**: Used for browser automation, configured in headless mode
|
||||
• **Shared Memory**: Increased shared memory size to improve browser performance
|
||||
• **BuildKit**: Uses Docker BuildKit to accelerate the build process
|
||||
• **Cache Optimization**: Uses persistent volumes to cache pip and Playwright dependencies
|
||||
• **Cross-Platform Compatibility**: Provides scripts for both Windows and macOS/Linux
|
||||
|
||||
## Docker Compose Version Notes
|
||||
|
||||
The docker-compose.yml file used in this project is compatible with Docker Compose v2.x. If you are using an older Docker Compose v1.x version, you may need to manually add the version number:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
# ...rest of the configuration remains unchanged
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 0. Check Environment
|
||||
|
||||
First, run the check script to ensure your environment is ready:
|
||||
|
||||
#### Check on macOS/Linux
|
||||
|
||||
```bash
|
||||
# First, add execute permissions to the script
|
||||
chmod +x check_docker.sh
|
||||
|
||||
# Run the check script
|
||||
./check_docker.sh
|
||||
```
|
||||
|
||||
#### Check on Windows
|
||||
|
||||
```cmd
|
||||
check_docker.bat
|
||||
```
|
||||
|
||||
If the check script finds any issues, please follow the prompts to fix them.
|
||||
|
||||
### 1. Configure Environment Variables
|
||||
|
||||
Copy the environment variable template file and fill in the necessary API keys:
|
||||
|
||||
```bash
|
||||
cp owl/.env_template owl/.env
|
||||
```
|
||||
|
||||
Then edit the `owl/.env` file and fill in the necessary API keys, for example:
|
||||
|
||||
```
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
GOOGLE_API_KEY=your_google_api_key
|
||||
SEARCH_ENGINE_ID=your_search_engine_id
|
||||
```
|
||||
|
||||
### 2. Quick Build Docker Image
|
||||
|
||||
#### Build on macOS/Linux
|
||||
|
||||
Use the provided shell script to speed up the Docker image build:
|
||||
|
||||
```bash
|
||||
# First, add execute permissions to the script
|
||||
chmod +x build_docker.sh
|
||||
|
||||
# Run the build script
|
||||
./build_docker.sh
|
||||
```
|
||||
|
||||
#### Build on Windows
|
||||
|
||||
Use the provided batch file:
|
||||
|
||||
```cmd
|
||||
build_docker.bat
|
||||
```
|
||||
|
||||
Or build and start using the standard method:
|
||||
|
||||
```bash
|
||||
# Use BuildKit to accelerate the build
|
||||
set DOCKER_BUILDKIT=1
|
||||
set COMPOSE_DOCKER_CLI_BUILD=1
|
||||
docker-compose build --build-arg BUILDKIT_INLINE_CACHE=1
|
||||
|
||||
# Start the container
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
### 3. Interactive Use of the Container
|
||||
|
||||
After the container starts, it will automatically enter an interactive shell environment and display a welcome message and a list of available scripts:
|
||||
|
||||
```bash
|
||||
# Enter the container (if not automatically entered)
|
||||
docker-compose exec owl bash
|
||||
```
|
||||
|
||||
Inside the container, you can directly run any available script:
|
||||
|
||||
```bash
|
||||
# Run the default script
|
||||
xvfb-python run.py
|
||||
|
||||
# Run the DeepSeek example
|
||||
xvfb-python run_deepseek_example.py
|
||||
|
||||
# Run the script and pass query parameters
|
||||
xvfb-python run.py "What is artificial intelligence?"
|
||||
```
|
||||
|
||||
### 4. Run Queries Using External Scripts
|
||||
|
||||
#### Run on macOS/Linux
|
||||
|
||||
```bash
|
||||
# First, add execute permissions to the script
|
||||
chmod +x run_in_docker.sh
|
||||
|
||||
# Default to using the run.py script
|
||||
./run_in_docker.sh "your question"
|
||||
|
||||
# Specify a particular script
|
||||
./run_in_docker.sh run_deepseek_example.py "your question"
|
||||
```
|
||||
|
||||
#### Run on Windows
|
||||
|
||||
```cmd
|
||||
REM Default to using the run.py script
|
||||
run_in_docker.bat "your question"
|
||||
|
||||
REM Specify a particular script
|
||||
run_in_docker.bat run_deepseek_example.py "your question"
|
||||
```
|
||||
|
||||
**Available Scripts**:
|
||||
• `run.py` - Default script, uses OpenAI GPT-4o model
|
||||
• `run_deepseek_example.py` - Uses the DeepSeek model
|
||||
• `run_gaia_roleplaying.py` - GAIA benchmark script
|
||||
|
||||
## Directory Mounts
|
||||
|
||||
The Docker Compose configuration has set up the following mount points:
|
||||
|
||||
• `./owl/.env:/app/owl/.env`: Mounts the environment variable file for easy modification of API keys
|
||||
• `./data:/app/data`: Mounts the data directory for storing and accessing data files
|
||||
• `playwright-cache`: Persistent volume for caching Playwright browsers
|
||||
• `pip-cache`: Persistent volume for caching pip packages
|
||||
|
||||
## Environment Variables
|
||||
|
||||
You can set environment variables in two ways:
|
||||
|
||||
1. Modify the `owl/.env` file
|
||||
2. Add environment variables in the `environment` section of the `docker-compose.yml` file
|
||||
|
||||
## Build Optimization
|
||||
|
||||
This Docker configuration includes several build optimizations:
|
||||
|
||||
1. **Use of Domestic Mirror Sources**: Uses Tsinghua University mirror sources to accelerate pip package downloads
|
||||
2. **Layer Optimization**: Reduces the number of layers in the Dockerfile to improve build efficiency
|
||||
3. **Cache Utilization**:
|
||||
• Enables pip caching to avoid repeated dependency downloads
|
||||
• Uses Docker BuildKit inline caching
|
||||
• Arranges Dockerfile instructions to maximize cache utilization
|
||||
4. **BuildKit**: Enables Docker BuildKit to accelerate builds
|
||||
5. **Persistent Caching**:
|
||||
• Uses Docker volumes to cache pip packages (`pip-cache`)
|
||||
• Uses Docker volumes to cache Playwright browsers (`playwright-cache`)
|
||||
• Local cache directory (`.docker-cache`)
|
||||
|
||||
### Cache Cleanup
|
||||
|
||||
If you need to clean the cache, you can use the following commands:
|
||||
|
||||
```bash
|
||||
# Clean Docker build cache
|
||||
docker builder prune
|
||||
|
||||
# Clean Docker volumes (will delete all unused volumes, including cache volumes)
|
||||
docker volume prune
|
||||
|
||||
# Clean local cache directory
|
||||
rm -rf .docker-cache
|
||||
```
|
||||
|
||||
## Cross-Platform Compatibility
|
||||
|
||||
This project provides scripts for different operating systems:
|
||||
|
||||
1. **Check Scripts**:
|
||||
• `check_docker.sh` (macOS/Linux): Checks the Docker environment
|
||||
• `check_docker.bat` (Windows): Checks the Docker environment
|
||||
|
||||
2. **Build Scripts**:
|
||||
• `build_docker.sh` (macOS/Linux): Builds the Docker image
|
||||
• `build_docker.bat` (Windows): Builds the Docker image
|
||||
|
||||
3. **Run Scripts**:
|
||||
• `run_in_docker.sh` (macOS/Linux): Runs scripts in the Docker container
|
||||
• `run_in_docker.bat` (Windows): Runs scripts in the Docker container
|
||||
|
||||
These scripts automatically detect the operating system type and use appropriate commands.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Container Fails to Start
|
||||
|
||||
Check the logs for more information:
|
||||
|
||||
```bash
|
||||
docker-compose logs
|
||||
```
|
||||
|
||||
### API Key Issues
|
||||
|
||||
Ensure that you have correctly set all necessary API keys in the `owl/.env` file.
|
||||
|
||||
### Docker Compose Warnings
|
||||
|
||||
If you see a warning about the `version` attribute being obsolete:
|
||||
|
||||
```
|
||||
WARN[0000] docker-compose.yml: the attribute `version` is obsolete
|
||||
```
|
||||
|
||||
This is because you are using Docker Compose v2.x, which no longer requires an explicit version number. We have removed this attribute from the configuration file, so you should no longer see this warning.
|
||||
|
||||
### Browser-Related Issues
|
||||
|
||||
If you encounter browser-related issues, try the following solutions:
|
||||
|
||||
1. Ensure that you are using the `xvfb-python` command to run Python scripts in the Docker container
|
||||
2. Check that Xvfb and related dependencies are correctly installed
|
||||
3. Increase the shared memory size (set to 2GB in docker-compose.yml)
|
||||
|
||||
### Slow Build Speed
|
||||
|
||||
If the build speed is slow, try the following solutions:
|
||||
|
||||
1. Ensure that Docker BuildKit is enabled (`DOCKER_BUILDKIT=1`)
|
||||
2. Ensure that pip caching is enabled (configured in docker-compose.yml)
|
||||
3. Use the `--build-arg BUILDKIT_INLINE_CACHE=1` parameter when building (configured in the build script)
|
||||
4. If this is the first build, downloading dependencies may take some time, but subsequent builds will be faster
|
||||
|
||||
### Windows-Specific Issues
|
||||
|
||||
If you encounter issues on Windows:
|
||||
|
||||
1. Ensure that you are running the Command Prompt or PowerShell with administrator privileges
|
||||
2. If you encounter path issues, try using forward slashes (/) instead of backslashes (\)
|
||||
3. If you encounter Docker Compose command issues, try using `docker compose` (without the hyphen)
|
||||
|
||||
### Insufficient Memory
|
||||
|
||||
If you encounter insufficient memory issues, you can adjust resource limits in the `docker-compose.yml` file:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
owl:
|
||||
# Other configurations...
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '4' # Increase CPU cores
|
||||
memory: 8G # Increase memory limit
|
||||
```
|
||||
|
||||
## Custom Docker Image
|
||||
|
||||
If you need to customize the Docker image, modify the `Dockerfile` file and then rebuild:
|
||||
|
||||
```bash
|
||||
# macOS/Linux
|
||||
./build_docker.sh
|
||||
|
||||
# Windows
|
||||
build_docker.bat
|
||||
```
|
||||
58
.container/Dockerfile
Normal file
58
.container/Dockerfile
Normal file
@@ -0,0 +1,58 @@
|
||||
FROM python:3.10-slim
|
||||
|
||||
# 设置环境变量
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_NO_CACHE_DIR=0 \
|
||||
PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
|
||||
PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright \
|
||||
PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright \
|
||||
DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 安装系统依赖(合并为一个RUN命令减少层数)
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
curl git ffmpeg libsm6 libxext6 xvfb xauth x11-utils \
|
||||
gcc python3-dev \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
# 复制项目文件
|
||||
COPY owl/ ./owl/
|
||||
COPY licenses/ ./licenses/
|
||||
COPY assets/ ./assets/
|
||||
COPY README.md .
|
||||
COPY README_zh.md .
|
||||
COPY pyproject.toml .
|
||||
|
||||
# 创建README.md文件以避免构建错误
|
||||
RUN echo "# OWL Project\n\n这是OWL项目的Docker环境。" > README.md
|
||||
# 安装uv工具
|
||||
RUN pip install uv
|
||||
|
||||
# 创建虚拟环境并安装依赖
|
||||
RUN uv venv .venv --python=3.10 && \
|
||||
. .venv/bin/activate && \
|
||||
uv pip install -e .
|
||||
|
||||
|
||||
|
||||
|
||||
# 创建启动脚本
|
||||
RUN echo '#!/bin/bash\nxvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" python "$@"' > /usr/local/bin/xvfb-python && \
|
||||
chmod +x /usr/local/bin/xvfb-python
|
||||
|
||||
# 创建欢迎脚本
|
||||
RUN echo '#!/bin/bash\necho "欢迎使用OWL项目Docker环境!"\necho "Welcome to OWL Project Docker environment!"\necho ""\necho "可用的脚本 | Available scripts:"\nls -1 *.py | grep -v "__" | sed "s/^/- /"\necho ""\necho "运行示例 | Run examples:"\necho " xvfb-python run.py # 运行默认脚本 | Run default script"\necho " xvfb-python run_deepseek_example.py # 运行DeepSeek示例 | Run DeepSeek example"\necho ""\necho "或者使用自定义查询 | Or use custom query:"\necho " xvfb-python run.py \"你的问题 | Your question\""\necho ""' > /usr/local/bin/owl-welcome && \
|
||||
chmod +x /usr/local/bin/owl-welcome
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app/owl
|
||||
|
||||
# 添加健康检查
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||||
CMD python -c "import sys; sys.exit(0 if __import__('os').path.exists('/app/owl') else 1)"
|
||||
|
||||
# 容器启动命令
|
||||
CMD ["/bin/bash", "-c", "owl-welcome && /bin/bash"]
|
||||
186
.container/build_docker.bat
Normal file
186
.container/build_docker.bat
Normal file
@@ -0,0 +1,186 @@
|
||||
@echo off
|
||||
chcp 65001 >nul
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
echo 在Windows上构建Docker镜像...
|
||||
echo Building Docker image on Windows...
|
||||
|
||||
REM 设置配置变量
|
||||
REM Set configuration variables
|
||||
set CACHE_DIR=.docker-cache\pip
|
||||
set BUILD_ARGS=--build-arg BUILDKIT_INLINE_CACHE=1
|
||||
set COMPOSE_FILE=docker-compose.yml
|
||||
|
||||
REM 解析命令行参数
|
||||
REM Parse command line arguments
|
||||
set CLEAN_CACHE=0
|
||||
set REBUILD=0
|
||||
set SERVICE=
|
||||
|
||||
:parse_args
|
||||
if "%~1"=="" goto :end_parse_args
|
||||
if /i "%~1"=="--clean" (
|
||||
set CLEAN_CACHE=1
|
||||
shift
|
||||
goto :parse_args
|
||||
)
|
||||
if /i "%~1"=="--rebuild" (
|
||||
set REBUILD=1
|
||||
shift
|
||||
goto :parse_args
|
||||
)
|
||||
if /i "%~1"=="--service" (
|
||||
set SERVICE=%~2
|
||||
shift
|
||||
shift
|
||||
goto :parse_args
|
||||
)
|
||||
if /i "%~1"=="--help" (
|
||||
echo 用法: build_docker.bat [选项]
|
||||
echo Usage: build_docker.bat [options]
|
||||
echo 选项:
|
||||
echo Options:
|
||||
echo --clean 清理缓存目录
|
||||
echo --clean Clean cache directory
|
||||
echo --rebuild 强制重新构建镜像
|
||||
echo --rebuild Force rebuild image
|
||||
echo --service 指定要构建的服务名称
|
||||
echo --service Specify service name to build
|
||||
echo --help 显示此帮助信息
|
||||
echo --help Show this help message
|
||||
exit /b 0
|
||||
)
|
||||
shift
|
||||
goto :parse_args
|
||||
:end_parse_args
|
||||
|
||||
REM 检查Docker是否安装
|
||||
REM Check if Docker is installed
|
||||
where docker >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 错误: Docker未安装
|
||||
echo Error: Docker not installed
|
||||
echo 请先安装Docker Desktop
|
||||
echo Please install Docker Desktop first: https://docs.docker.com/desktop/install/windows-install/
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 检查Docker是否运行
|
||||
REM Check if Docker is running
|
||||
docker info >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 错误: Docker未运行
|
||||
echo Error: Docker not running
|
||||
echo 请启动Docker Desktop应用程序
|
||||
echo Please start Docker Desktop application
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 检查docker-compose.yml文件是否存在
|
||||
REM Check if docker-compose.yml file exists
|
||||
if not exist "%COMPOSE_FILE%" (
|
||||
echo 错误: 未找到%COMPOSE_FILE%文件
|
||||
echo Error: %COMPOSE_FILE% file not found
|
||||
echo 请确保在正确的目录中运行此脚本
|
||||
echo Please make sure you are running this script in the correct directory
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 检查Docker Compose命令
|
||||
REM Check Docker Compose command
|
||||
where docker-compose >nul 2>nul
|
||||
if %ERRORLEVEL% EQU 0 (
|
||||
set COMPOSE_CMD=docker-compose
|
||||
) else (
|
||||
echo 尝试使用新的docker compose命令...
|
||||
echo Trying to use new docker compose command...
|
||||
docker compose version >nul 2>nul
|
||||
if %ERRORLEVEL% EQU 0 (
|
||||
set COMPOSE_CMD=docker compose
|
||||
) else (
|
||||
echo 错误: 未找到Docker Compose命令
|
||||
echo Error: Docker Compose command not found
|
||||
echo 请确保Docker Desktop已正确安装
|
||||
echo Please make sure Docker Desktop is properly installed
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
)
|
||||
|
||||
REM 设置Docker BuildKit环境变量
|
||||
REM Set Docker BuildKit environment variables
|
||||
set DOCKER_BUILDKIT=1
|
||||
set COMPOSE_DOCKER_CLI_BUILD=1
|
||||
|
||||
echo 启用Docker BuildKit加速构建...
|
||||
echo Enabling Docker BuildKit to accelerate build...
|
||||
|
||||
REM 清理缓存(如果指定)
|
||||
REM Clean cache (if specified)
|
||||
if %CLEAN_CACHE% EQU 1 (
|
||||
echo 清理缓存目录...
|
||||
echo Cleaning cache directory...
|
||||
if exist "%CACHE_DIR%" rmdir /s /q "%CACHE_DIR%"
|
||||
)
|
||||
|
||||
REM 创建缓存目录
|
||||
REM Create cache directory
|
||||
if not exist "%CACHE_DIR%" (
|
||||
echo 创建缓存目录...
|
||||
echo Creating cache directory...
|
||||
mkdir "%CACHE_DIR%"
|
||||
)
|
||||
|
||||
REM 添加构建时间标记
|
||||
REM Add build time tag
|
||||
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
|
||||
set "YEAR=%dt:~0,4%"
|
||||
set "MONTH=%dt:~4,2%"
|
||||
set "DAY=%dt:~6,2%"
|
||||
set "HOUR=%dt:~8,2%"
|
||||
set "MINUTE=%dt:~10,2%"
|
||||
set "BUILD_TIME=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%"
|
||||
set "BUILD_ARGS=%BUILD_ARGS% --build-arg BUILD_TIME=%BUILD_TIME%"
|
||||
|
||||
REM 构建Docker镜像
|
||||
REM Build Docker image
|
||||
echo 开始构建Docker镜像...
|
||||
echo Starting to build Docker image...
|
||||
|
||||
if "%SERVICE%"=="" (
|
||||
if %REBUILD% EQU 1 (
|
||||
echo 强制重新构建所有服务...
|
||||
echo Force rebuilding all services...
|
||||
%COMPOSE_CMD% build --no-cache %BUILD_ARGS%
|
||||
) else (
|
||||
%COMPOSE_CMD% build %BUILD_ARGS%
|
||||
)
|
||||
) else (
|
||||
if %REBUILD% EQU 1 (
|
||||
echo 强制重新构建服务 %SERVICE%...
|
||||
echo Force rebuilding service %SERVICE%...
|
||||
%COMPOSE_CMD% build --no-cache %BUILD_ARGS% %SERVICE%
|
||||
) else (
|
||||
echo 构建服务 %SERVICE%...
|
||||
echo Building service %SERVICE%...
|
||||
%COMPOSE_CMD% build %BUILD_ARGS% %SERVICE%
|
||||
)
|
||||
)
|
||||
|
||||
if %ERRORLEVEL% EQU 0 (
|
||||
echo Docker镜像构建成功!
|
||||
echo Docker image build successful!
|
||||
echo 构建时间: %BUILD_TIME%
|
||||
echo Build time: %BUILD_TIME%
|
||||
echo 可以使用以下命令启动容器:
|
||||
echo You can use the following command to start the container:
|
||||
echo %COMPOSE_CMD% up -d
|
||||
) else (
|
||||
echo Docker镜像构建失败,请检查错误信息。
|
||||
echo Docker image build failed, please check error messages.
|
||||
)
|
||||
|
||||
pause
|
||||
150
.container/build_docker.sh
Executable file
150
.container/build_docker.sh
Executable file
@@ -0,0 +1,150 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 设置配置变量 | Set configuration variables
|
||||
CACHE_DIR=".docker-cache/pip"
|
||||
BUILD_ARGS="--build-arg BUILDKIT_INLINE_CACHE=1"
|
||||
COMPOSE_FILE="docker-compose.yml"
|
||||
CLEAN_CACHE=0
|
||||
REBUILD=0
|
||||
SERVICE=""
|
||||
|
||||
# 解析命令行参数 | Parse command line arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--clean)
|
||||
CLEAN_CACHE=1
|
||||
shift
|
||||
;;
|
||||
--rebuild)
|
||||
REBUILD=1
|
||||
shift
|
||||
;;
|
||||
--service)
|
||||
SERVICE="$2"
|
||||
shift 2
|
||||
;;
|
||||
--help)
|
||||
echo "用法 | Usage: ./build_docker.sh [选项 | options]"
|
||||
echo "选项 | Options:"
|
||||
echo " --clean 清理缓存目录 | Clean cache directory"
|
||||
echo " --rebuild 强制重新构建镜像 | Force rebuild image"
|
||||
echo " --service 指定要构建的服务名称 | Specify service name to build"
|
||||
echo " --help 显示此帮助信息 | Show this help message"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "未知选项 | Unknown option: $1"
|
||||
echo "使用 --help 查看帮助 | Use --help to see help"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 检测操作系统类型 | Detect operating system type
|
||||
OS_TYPE=$(uname -s)
|
||||
echo "检测到操作系统 | Detected OS: $OS_TYPE"
|
||||
|
||||
# 检查Docker是否安装 | Check if Docker is installed
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "错误 | Error: Docker未安装 | Docker not installed"
|
||||
echo "请先安装Docker | Please install Docker first: https://docs.docker.com/get-docker/"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查Docker是否运行 | Check if Docker is running
|
||||
if ! docker info &> /dev/null; then
|
||||
echo "错误 | Error: Docker未运行 | Docker not running"
|
||||
echo "请启动Docker服务 | Please start Docker service"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查docker-compose.yml文件是否存在 | Check if docker-compose.yml file exists
|
||||
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||
echo "错误 | Error: 未找到$COMPOSE_FILE文件 | $COMPOSE_FILE file not found"
|
||||
echo "请确保在正确的目录中运行此脚本 | Please make sure you are running this script in the correct directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 设置Docker BuildKit环境变量 | Set Docker BuildKit environment variables
|
||||
export DOCKER_BUILDKIT=1
|
||||
export COMPOSE_DOCKER_CLI_BUILD=1
|
||||
|
||||
echo "启用Docker BuildKit加速构建... | Enabling Docker BuildKit to accelerate build..."
|
||||
|
||||
# 清理缓存(如果指定) | Clean cache (if specified)
|
||||
if [ $CLEAN_CACHE -eq 1 ]; then
|
||||
echo "清理缓存目录... | Cleaning cache directory..."
|
||||
rm -rf "$CACHE_DIR"
|
||||
fi
|
||||
|
||||
# 创建缓存目录 | Create cache directory
|
||||
mkdir -p "$CACHE_DIR"
|
||||
|
||||
# 添加构建时间标记 | Add build time tag
|
||||
BUILD_TIME=$(date +"%Y%m%d_%H%M%S")
|
||||
BUILD_ARGS="$BUILD_ARGS --build-arg BUILD_TIME=$BUILD_TIME"
|
||||
|
||||
# 获取脚本所在目录 | Get script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# 获取项目根目录(脚本所在目录的父目录) | Get project root directory (parent directory of script directory)
|
||||
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
|
||||
echo "脚本目录 | Script directory: $SCRIPT_DIR"
|
||||
echo "项目根目录 | Project root directory: $PROJECT_ROOT"
|
||||
|
||||
# 切换到项目根目录 | Change to project root directory
|
||||
cd "$PROJECT_ROOT"
|
||||
|
||||
# 检查Docker Compose命令 | Check Docker Compose command
|
||||
if command -v docker-compose &> /dev/null; then
|
||||
COMPOSE_CMD="docker-compose"
|
||||
echo "使用 docker-compose 命令 | Using docker-compose command"
|
||||
elif docker compose version &> /dev/null; then
|
||||
COMPOSE_CMD="docker compose"
|
||||
echo "使用 docker compose 命令 | Using docker compose command"
|
||||
else
|
||||
echo "错误 | Error: 未找到Docker Compose命令 | Docker Compose command not found"
|
||||
echo "请安装Docker Compose | Please install Docker Compose: https://docs.docker.com/compose/install/"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检测CPU核心数,用于并行构建 | Detect CPU cores for parallel build
|
||||
CPU_CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)
|
||||
if [ $CPU_CORES -gt 2 ]; then
|
||||
PARALLEL_FLAG="--parallel"
|
||||
echo "检测到${CPU_CORES}个CPU核心,启用并行构建... | Detected ${CPU_CORES} CPU cores, enabling parallel build..."
|
||||
else
|
||||
PARALLEL_FLAG=""
|
||||
fi
|
||||
|
||||
# 构建命令基础部分 | Base part of build command
|
||||
BUILD_CMD="$COMPOSE_CMD -f \"$SCRIPT_DIR/docker-compose.yml\" build $PARALLEL_FLAG --build-arg BUILDKIT_INLINE_CACHE=1"
|
||||
|
||||
# 根据操作系统类型执行不同的命令 | Execute different commands based on OS type
|
||||
if [[ "$OS_TYPE" == "Darwin" ]]; then
|
||||
# macOS
|
||||
echo "在macOS上构建Docker镜像... | Building Docker image on macOS..."
|
||||
eval $BUILD_CMD
|
||||
elif [[ "$OS_TYPE" == "Linux" ]]; then
|
||||
# Linux
|
||||
echo "在Linux上构建Docker镜像... | Building Docker image on Linux..."
|
||||
eval $BUILD_CMD
|
||||
elif [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
# Windows
|
||||
echo "在Windows上构建Docker镜像... | Building Docker image on Windows..."
|
||||
eval $BUILD_CMD
|
||||
else
|
||||
echo "未知操作系统,尝试使用标准命令构建... | Unknown OS, trying to build with standard command..."
|
||||
eval $BUILD_CMD
|
||||
fi
|
||||
|
||||
# 检查构建结果 | Check build result
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Docker镜像构建成功! | Docker image build successful!"
|
||||
echo "构建时间 | Build time: $BUILD_TIME"
|
||||
echo "可以使用以下命令启动容器: | You can use the following command to start the container:"
|
||||
echo "$COMPOSE_CMD -f \"$SCRIPT_DIR/docker-compose.yml\" up -d"
|
||||
else
|
||||
echo "Docker镜像构建失败,请检查错误信息。 | Docker image build failed, please check error messages."
|
||||
exit 1
|
||||
fi
|
||||
88
.container/check_docker.bat
Normal file
88
.container/check_docker.bat
Normal file
@@ -0,0 +1,88 @@
|
||||
@echo off
|
||||
chcp 65001 >nul
|
||||
echo 检查Docker环境...
|
||||
echo Checking Docker environment...
|
||||
|
||||
REM 检查Docker是否安装
|
||||
REM Check if Docker is installed
|
||||
where docker >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 错误: Docker未安装
|
||||
echo Error: Docker not installed
|
||||
echo 在Windows上安装Docker的方法:
|
||||
echo How to install Docker on Windows:
|
||||
echo 1. 访问 https://docs.docker.com/desktop/install/windows-install/ 下载Docker Desktop
|
||||
echo 1. Visit https://docs.docker.com/desktop/install/windows-install/ to download Docker Desktop
|
||||
echo 2. 安装并启动Docker Desktop
|
||||
echo 2. Install and start Docker Desktop
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Docker已安装
|
||||
echo Docker is installed
|
||||
|
||||
REM 检查Docker Compose是否安装
|
||||
REM Check if Docker Compose is installed
|
||||
where docker-compose >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 警告: Docker-Compose未找到,尝试使用新的docker compose命令
|
||||
echo Warning: Docker-Compose not found, trying to use new docker compose command
|
||||
docker compose version >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 错误: Docker Compose未安装
|
||||
echo Error: Docker Compose not installed
|
||||
echo Docker Desktop for Windows应该已包含Docker Compose
|
||||
echo Docker Desktop for Windows should already include Docker Compose
|
||||
echo 请确保Docker Desktop已正确安装
|
||||
echo Please make sure Docker Desktop is properly installed
|
||||
pause
|
||||
exit /b 1
|
||||
) else (
|
||||
echo 使用新的docker compose命令
|
||||
echo Using new docker compose command
|
||||
set COMPOSE_CMD=docker compose
|
||||
)
|
||||
) else (
|
||||
echo Docker-Compose已安装
|
||||
echo Docker-Compose is installed
|
||||
set COMPOSE_CMD=docker-compose
|
||||
)
|
||||
|
||||
REM 检查Docker是否正在运行
|
||||
REM Check if Docker is running
|
||||
docker info >nul 2>nul
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
echo 错误: Docker未运行
|
||||
echo Error: Docker not running
|
||||
echo 请启动Docker Desktop应用程序
|
||||
echo Please start Docker Desktop application
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Docker正在运行
|
||||
echo Docker is running
|
||||
|
||||
REM 检查是否有.env文件
|
||||
REM Check if .env file exists
|
||||
if not exist "..\owl\.env" (
|
||||
echo 警告: 未找到owl\.env文件
|
||||
echo Warning: owl\.env file not found
|
||||
echo 请运行以下命令创建环境变量文件
|
||||
echo Please run the following command to create environment variable file:
|
||||
echo copy ..\owl\.env_template ..\owl\.env
|
||||
echo 然后编辑owl\.env文件,填写必要的API密钥
|
||||
echo Then edit owl\.env file and fill in necessary API keys
|
||||
) else (
|
||||
echo 环境变量文件已存在
|
||||
echo Environment variable file exists
|
||||
)
|
||||
|
||||
echo 所有检查完成,您的系统已准备好构建和运行OWL项目的Docker容器
|
||||
echo All checks completed, your system is ready to build and run OWL project Docker container
|
||||
echo 请运行以下命令构建Docker镜像:
|
||||
echo Please run the following command to build Docker image:
|
||||
echo %COMPOSE_CMD% build
|
||||
|
||||
pause
|
||||
92
.container/check_docker.sh
Executable file
92
.container/check_docker.sh
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 检测操作系统类型 | Detect operating system type
|
||||
OS_TYPE=$(uname -s)
|
||||
echo "检测到操作系统 | Detected OS: $OS_TYPE"
|
||||
|
||||
# 检查Docker是否安装 | Check if Docker is installed
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "错误 | Error: Docker未安装 | Docker not installed"
|
||||
|
||||
if [[ "$OS_TYPE" == "Darwin" ]]; then
|
||||
echo "在macOS上安装Docker的方法 | How to install Docker on macOS:"
|
||||
echo "1. 访问 | Visit https://docs.docker.com/desktop/install/mac-install/ 下载Docker Desktop | to download Docker Desktop"
|
||||
echo "2. 安装并启动Docker Desktop | Install and start Docker Desktop"
|
||||
elif [[ "$OS_TYPE" == "Linux" ]]; then
|
||||
echo "在Linux上安装Docker的方法 | How to install Docker on Linux:"
|
||||
echo "1. 运行以下命令 | Run the following commands:"
|
||||
echo " sudo apt-get update"
|
||||
echo " sudo apt-get install docker.io docker-compose"
|
||||
echo "2. 启动Docker服务 | Start Docker service:"
|
||||
echo " sudo systemctl start docker"
|
||||
echo " sudo systemctl enable docker"
|
||||
elif [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
echo "在Windows上安装Docker的方法 | How to install Docker on Windows:"
|
||||
echo "1. 访问 | Visit https://docs.docker.com/desktop/install/windows-install/ 下载Docker Desktop | to download Docker Desktop"
|
||||
echo "2. 安装并启动Docker Desktop | Install and start Docker Desktop"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Docker已安装 | Docker is installed"
|
||||
|
||||
# 检查Docker Compose是否安装 | Check if Docker Compose is installed
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
echo "错误 | Error: Docker Compose未安装 | Docker Compose not installed"
|
||||
|
||||
if [[ "$OS_TYPE" == "Darwin" ]]; then
|
||||
echo "Docker Desktop for Mac已包含Docker Compose | Docker Desktop for Mac already includes Docker Compose"
|
||||
elif [[ "$OS_TYPE" == "Linux" ]]; then
|
||||
echo "在Linux上安装Docker Compose的方法 | How to install Docker Compose on Linux:"
|
||||
echo "1. 运行以下命令 | Run the following command:"
|
||||
echo " sudo apt-get install docker-compose"
|
||||
elif [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
echo "Docker Desktop for Windows已包含Docker Compose | Docker Desktop for Windows already includes Docker Compose"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Docker Compose已安装 | Docker Compose is installed"
|
||||
|
||||
# 检查Docker是否正在运行 | Check if Docker is running
|
||||
if ! docker info &> /dev/null; then
|
||||
echo "错误 | Error: Docker未运行 | Docker not running"
|
||||
|
||||
if [[ "$OS_TYPE" == "Darwin" ]]; then
|
||||
echo "请启动Docker Desktop应用程序 | Please start Docker Desktop application"
|
||||
elif [[ "$OS_TYPE" == "Linux" ]]; then
|
||||
echo "请运行以下命令启动Docker服务 | Please run the following command to start Docker service:"
|
||||
echo "sudo systemctl start docker"
|
||||
elif [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
echo "请启动Docker Desktop应用程序 | Please start Docker Desktop application"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Docker正在运行 | Docker is running"
|
||||
|
||||
# 检查是否有足够的磁盘空间 | Check if there is enough disk space
|
||||
FREE_SPACE=$(df -h . | awk 'NR==2 {print $4}')
|
||||
echo "可用磁盘空间 | Available disk space: $FREE_SPACE"
|
||||
|
||||
# 检查是否有.env文件 | Check if .env file exists
|
||||
if [ ! -f "owl/.env" ]; then
|
||||
echo "警告 | Warning: 未找到owl/.env文件 | owl/.env file not found"
|
||||
echo "请运行以下命令创建环境变量文件 | Please run the following command to create environment variable file:"
|
||||
echo "cp owl/.env_template owl/.env"
|
||||
echo "然后编辑owl/.env文件,填写必要的API密钥 | Then edit owl/.env file and fill in necessary API keys"
|
||||
else
|
||||
echo "环境变量文件已存在 | Environment variable file exists"
|
||||
fi
|
||||
|
||||
echo "所有检查完成,您的系统已准备好构建和运行OWL项目的Docker容器 | All checks completed, your system is ready to build and run OWL project Docker container"
|
||||
echo "请运行以下命令构建Docker镜像 | Please run the following command to build Docker image:"
|
||||
|
||||
if [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
echo "build_docker.bat"
|
||||
else
|
||||
echo "./build_docker.sh"
|
||||
fi
|
||||
34
.container/docker-compose.yml
Normal file
34
.container/docker-compose.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
services:
|
||||
owl:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: .container/Dockerfile
|
||||
volumes:
|
||||
# 挂载.env文件,方便配置API密钥
|
||||
- ../owl/.env:/app/owl/.env
|
||||
# 挂载数据目录
|
||||
- ./data:/app/data
|
||||
# 挂载缓存目录,避免重复下载
|
||||
- ~/.cache/pip:/root/.pip/cache
|
||||
- ~/.cache/playwright:/root/.cache/ms-playwright
|
||||
environment:
|
||||
- OPENAI_API_KEY=${OPENAI_API_KEY}
|
||||
- DISPLAY=:99
|
||||
- PYTHONDONTWRITEBYTECODE=1
|
||||
- PYTHONUNBUFFERED=1
|
||||
- TERM=xterm-256color
|
||||
ports:
|
||||
- "8000:8000"
|
||||
stdin_open: true
|
||||
tty: true
|
||||
shm_size: 2gb
|
||||
# 简化资源限制
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
memory: 4G
|
||||
|
||||
# 定义持久化卷,用于缓存 | Define persistent volumes for caching
|
||||
volumes:
|
||||
playwright-cache:
|
||||
pip-cache:
|
||||
181
.container/run_in_docker.bat
Normal file
181
.container/run_in_docker.bat
Normal file
@@ -0,0 +1,181 @@
|
||||
@echo off
|
||||
chcp 65001 >nul
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
REM 定义配置变量
|
||||
REM Define configuration variables
|
||||
set SERVICE_NAME=owl
|
||||
set PYTHON_CMD=xvfb-python
|
||||
set MAX_WAIT_SECONDS=60
|
||||
set CHECK_INTERVAL_SECONDS=2
|
||||
|
||||
REM 检查参数
|
||||
REM Check parameters
|
||||
if "%~1"=="" (
|
||||
echo 用法: run_in_docker.bat [脚本名称] "你的问题"
|
||||
echo Usage: run_in_docker.bat [script name] "your question"
|
||||
echo 例如: run_in_docker.bat run.py "什么是人工智能?"
|
||||
echo Example: run_in_docker.bat run.py "What is artificial intelligence?"
|
||||
echo 或者: run_in_docker.bat run_deepseek_example.py "什么是人工智能?"
|
||||
echo Or: run_in_docker.bat run_deepseek_example.py "What is artificial intelligence?"
|
||||
echo 如果不指定脚本名称,默认使用 run.py
|
||||
echo If script name is not specified, run.py will be used by default
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
REM 判断第一个参数是否是脚本名称
|
||||
REM Determine if the first parameter is a script name
|
||||
set SCRIPT_NAME=%~1
|
||||
set QUERY=%~2
|
||||
|
||||
if "!SCRIPT_NAME:~-3!"==".py" (
|
||||
REM 如果提供了第二个参数,则为查询内容
|
||||
REM If a second parameter is provided, it's the query content
|
||||
if "!QUERY!"=="" (
|
||||
echo 请提供查询参数,例如: run_in_docker.bat !SCRIPT_NAME! "你的问题"
|
||||
echo Please provide query parameter, e.g.: run_in_docker.bat !SCRIPT_NAME! "your question"
|
||||
exit /b 1
|
||||
)
|
||||
) else (
|
||||
REM 如果第一个参数不是脚本名称,则默认使用 run.py
|
||||
REM If the first parameter is not a script name, use run.py by default
|
||||
set QUERY=!SCRIPT_NAME!
|
||||
set SCRIPT_NAME=run.py
|
||||
)
|
||||
|
||||
REM 检查脚本是否存在
|
||||
REM Check if the script exists
|
||||
if not exist "..\owl\!SCRIPT_NAME!" (
|
||||
echo 错误: 脚本 '..\owl\!SCRIPT_NAME!' 不存在
|
||||
echo Error: Script '..\owl\!SCRIPT_NAME!' does not exist
|
||||
echo 可用的脚本有:
|
||||
echo Available scripts:
|
||||
dir /b ..\owl\*.py | findstr /v "__"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo 使用脚本: !SCRIPT_NAME!
|
||||
echo Using script: !SCRIPT_NAME!
|
||||
echo 查询内容: !QUERY!
|
||||
echo Query content: !QUERY!
|
||||
|
||||
REM 优先检查新版 docker compose 命令
|
||||
REM Check new docker compose command first
|
||||
docker compose version >nul 2>nul
|
||||
if %ERRORLEVEL% EQU 0 (
|
||||
echo 使用新版 docker compose 命令
|
||||
echo Using new docker compose command
|
||||
set COMPOSE_CMD=docker compose
|
||||
) else (
|
||||
REM 如果新版不可用,检查旧版 docker-compose
|
||||
REM If new version is not available, check old docker-compose
|
||||
where docker-compose >nul 2>nul
|
||||
if %ERRORLEVEL% EQU 0 (
|
||||
echo 使用旧版 docker-compose 命令
|
||||
echo Using old docker-compose command
|
||||
set COMPOSE_CMD=docker-compose
|
||||
) else (
|
||||
echo 错误: Docker Compose 未安装
|
||||
echo Error: Docker Compose not installed
|
||||
echo 请确保 Docker Desktop 已正确安装
|
||||
echo Please make sure Docker Desktop is properly installed
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
)
|
||||
|
||||
REM 从docker-compose.yml获取服务名称(如果文件存在)
|
||||
REM Get service name from docker-compose.yml (if file exists)
|
||||
if exist "docker-compose.yml" (
|
||||
for /f "tokens=*" %%a in ('findstr /r "^ [a-zA-Z0-9_-]*:" docker-compose.yml') do (
|
||||
set line=%%a
|
||||
set service=!line:~2,-1!
|
||||
if not "!service!"=="" (
|
||||
REM 使用第一个找到的服务名称
|
||||
REM Use the first service name found
|
||||
set SERVICE_NAME=!service!
|
||||
echo 从docker-compose.yml检测到服务名称: !SERVICE_NAME!
|
||||
echo Detected service name from docker-compose.yml: !SERVICE_NAME!
|
||||
goto :found_service
|
||||
)
|
||||
)
|
||||
)
|
||||
:found_service
|
||||
|
||||
REM 确保Docker容器正在运行
|
||||
REM Ensure Docker container is running
|
||||
%COMPOSE_CMD% ps | findstr "!SERVICE_NAME!.*Up" > nul
|
||||
if errorlevel 1 (
|
||||
echo 启动Docker容器...
|
||||
echo Starting Docker container...
|
||||
%COMPOSE_CMD% up -d
|
||||
|
||||
REM 使用循环检查容器是否就绪
|
||||
REM Use loop to check if container is ready
|
||||
echo 等待容器启动...
|
||||
echo Waiting for container to start...
|
||||
set /a total_wait=0
|
||||
|
||||
:wait_loop
|
||||
timeout /t !CHECK_INTERVAL_SECONDS! /nobreak > nul
|
||||
set /a total_wait+=!CHECK_INTERVAL_SECONDS!
|
||||
|
||||
%COMPOSE_CMD% ps | findstr "!SERVICE_NAME!.*Up" > nul
|
||||
if errorlevel 1 (
|
||||
if !total_wait! LSS !MAX_WAIT_SECONDS! (
|
||||
echo 容器尚未就绪,已等待!total_wait!秒,继续等待...
|
||||
echo Container not ready yet, waited for !total_wait! seconds, continuing to wait...
|
||||
goto :wait_loop
|
||||
) else (
|
||||
echo 错误:容器启动超时,已等待!MAX_WAIT_SECONDS!秒
|
||||
echo Error: Container startup timeout, waited for !MAX_WAIT_SECONDS! seconds
|
||||
echo 请检查Docker容器状态:%COMPOSE_CMD% ps
|
||||
echo Please check Docker container status: %COMPOSE_CMD% ps
|
||||
exit /b 1
|
||||
)
|
||||
) else (
|
||||
echo 容器已就绪,共等待了!total_wait!秒
|
||||
echo Container is ready, waited for !total_wait! seconds in total
|
||||
)
|
||||
)
|
||||
|
||||
REM 检查容器中是否存在xvfb-python命令
|
||||
REM Check if xvfb-python command exists in container
|
||||
echo 检查容器中的命令...
|
||||
echo Checking commands in container...
|
||||
%COMPOSE_CMD% exec -T !SERVICE_NAME! which !PYTHON_CMD! > nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo 警告:容器中未找到!PYTHON_CMD!命令,尝试使用python替代
|
||||
echo Warning: !PYTHON_CMD! command not found in container, trying to use python instead
|
||||
set PYTHON_CMD=python
|
||||
|
||||
REM 检查python命令是否存在
|
||||
REM Check if python command exists
|
||||
%COMPOSE_CMD% exec -T !SERVICE_NAME! which python > nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo 错误:容器中未找到python命令
|
||||
echo Error: python command not found in container
|
||||
echo 请检查容器配置
|
||||
echo Please check container configuration
|
||||
exit /b 1
|
||||
)
|
||||
)
|
||||
|
||||
REM 在容器中运行指定的脚本,传递查询参数
|
||||
REM Run the specified script in container, passing query parameter
|
||||
echo 在Docker容器中使用!PYTHON_CMD!运行脚本...
|
||||
echo Running script in Docker container using !PYTHON_CMD!...
|
||||
|
||||
REM 修改执行命令,按照README中的方式执行
|
||||
REM Modify execution command according to README
|
||||
%COMPOSE_CMD% exec -T !SERVICE_NAME! bash -c "cd .. && source .venv/bin/activate && cd owl && !PYTHON_CMD! !SCRIPT_NAME! \"!QUERY!\""
|
||||
|
||||
if errorlevel 0 (
|
||||
echo 查询完成!
|
||||
echo Query completed!
|
||||
) else (
|
||||
echo 查询执行失败,请检查错误信息。
|
||||
echo Query execution failed, please check error messages.
|
||||
)
|
||||
|
||||
pause
|
||||
135
.container/run_in_docker.sh
Executable file
135
.container/run_in_docker.sh
Executable file
@@ -0,0 +1,135 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 定义配置变量 | Define configuration variables
|
||||
SERVICE_NAME="owl"
|
||||
PYTHON_CMD="xvfb-python"
|
||||
MAX_WAIT_SECONDS=60
|
||||
CHECK_INTERVAL_SECONDS=2
|
||||
|
||||
# 检测操作系统类型 | Detect operating system type
|
||||
OS_TYPE=$(uname -s)
|
||||
echo "检测到操作系统 | Detected operating system: $OS_TYPE"
|
||||
|
||||
# 检查是否提供了查询参数 | Check if query parameters are provided
|
||||
if [ $# -lt 1 ]; then
|
||||
echo "用法 | Usage: ./run_in_docker.sh [脚本名称 | script name] '你的问题 | your question'"
|
||||
echo "例如 | Example: ./run_in_docker.sh run.py '什么是人工智能? | What is artificial intelligence?'"
|
||||
echo "或者 | Or: ./run_in_docker.sh run_deepseek_example.py '什么是人工智能? | What is artificial intelligence?'"
|
||||
echo "如果不指定脚本名称,默认使用 run.py | If script name is not specified, run.py will be used by default"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 判断第一个参数是否是脚本名称 | Determine if the first parameter is a script name
|
||||
if [[ $1 == *.py ]]; then
|
||||
SCRIPT_NAME="$1"
|
||||
# 如果提供了第二个参数,则为查询内容 | If a second parameter is provided, it's the query content
|
||||
if [ $# -ge 2 ]; then
|
||||
QUERY="$2"
|
||||
else
|
||||
echo "请提供查询参数,例如 | Please provide query parameter, e.g.: ./run_in_docker.sh $SCRIPT_NAME '你的问题 | your question'"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
# 如果第一个参数不是脚本名称,则默认使用 run.py | If the first parameter is not a script name, use run.py by default
|
||||
SCRIPT_NAME="run.py"
|
||||
QUERY="$1"
|
||||
fi
|
||||
|
||||
# 检查脚本是否存在 | Check if the script exists
|
||||
if [ ! -f "../owl/$SCRIPT_NAME" ]; then
|
||||
echo "错误 | Error: 脚本 | Script '../owl/$SCRIPT_NAME' 不存在 | does not exist"
|
||||
echo "可用的脚本有 | Available scripts:"
|
||||
if [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
find ../owl -name "*.py" | grep -v "__" | sed 's/\\/\//g'
|
||||
else
|
||||
ls -1 ../owl/*.py | grep -v "__"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "使用脚本 | Using script: $SCRIPT_NAME"
|
||||
echo "查询内容 | Query content: $QUERY"
|
||||
|
||||
# 从docker-compose.yml获取服务名称(如果文件存在) | Get service name from docker-compose.yml (if file exists)
|
||||
if [ -f "docker-compose.yml" ]; then
|
||||
DETECTED_SERVICE=$(grep -E "^ [a-zA-Z0-9_-]*:" docker-compose.yml | head -1 | sed 's/^ \(.*\):.*/\1/')
|
||||
if [ ! -z "$DETECTED_SERVICE" ]; then
|
||||
SERVICE_NAME="$DETECTED_SERVICE"
|
||||
echo "从docker-compose.yml检测到服务名称 | Detected service name from docker-compose.yml: $SERVICE_NAME"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 检查Docker Compose命令 | Check Docker Compose command
|
||||
if command -v docker-compose &> /dev/null; then
|
||||
COMPOSE_CMD="docker-compose"
|
||||
elif docker compose version &> /dev/null; then
|
||||
COMPOSE_CMD="docker compose"
|
||||
else
|
||||
echo "错误 | Error: 未找到Docker Compose命令 | Docker Compose command not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 确保Docker容器正在运行 | Ensure Docker container is running
|
||||
CONTAINER_RUNNING=$($COMPOSE_CMD ps | grep -c "$SERVICE_NAME.*Up" || true)
|
||||
if [ "$CONTAINER_RUNNING" -eq 0 ]; then
|
||||
echo "启动Docker容器... | Starting Docker container..."
|
||||
$COMPOSE_CMD up -d
|
||||
|
||||
# 使用循环检查容器是否就绪 | Use loop to check if container is ready
|
||||
echo "等待容器启动... | Waiting for container to start..."
|
||||
TOTAL_WAIT=0
|
||||
|
||||
while [ $TOTAL_WAIT -lt $MAX_WAIT_SECONDS ]; do
|
||||
sleep $CHECK_INTERVAL_SECONDS
|
||||
TOTAL_WAIT=$((TOTAL_WAIT + CHECK_INTERVAL_SECONDS))
|
||||
|
||||
CONTAINER_RUNNING=$($COMPOSE_CMD ps | grep -c "$SERVICE_NAME.*Up" || true)
|
||||
if [ "$CONTAINER_RUNNING" -gt 0 ]; then
|
||||
echo "容器已就绪,共等待了 $TOTAL_WAIT 秒 | Container is ready, waited for $TOTAL_WAIT seconds in total"
|
||||
break
|
||||
else
|
||||
echo "容器尚未就绪,已等待 $TOTAL_WAIT 秒,继续等待... | Container not ready yet, waited for $TOTAL_WAIT seconds, continuing to wait..."
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$CONTAINER_RUNNING" -eq 0 ]; then
|
||||
echo "错误 | Error:容器启动超时,已等待 $MAX_WAIT_SECONDS 秒 | Container startup timeout, waited for $MAX_WAIT_SECONDS seconds"
|
||||
echo "请检查Docker容器状态 | Please check Docker container status:$COMPOSE_CMD ps"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 检查容器中是否存在指定的Python命令 | Check if specified Python command exists in container
|
||||
echo "检查容器中的命令... | Checking commands in container..."
|
||||
if ! $COMPOSE_CMD exec -T $SERVICE_NAME which $PYTHON_CMD &> /dev/null; then
|
||||
echo "警告 | Warning:容器中未找到 $PYTHON_CMD 命令,尝试使用python替代 | $PYTHON_CMD command not found in container, trying to use python instead"
|
||||
PYTHON_CMD="python"
|
||||
|
||||
# 检查python命令是否存在 | Check if python command exists
|
||||
if ! $COMPOSE_CMD exec -T $SERVICE_NAME which python &> /dev/null; then
|
||||
echo "错误 | Error:容器中未找到python命令 | python command not found in container"
|
||||
echo "请检查容器配置 | Please check container configuration"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 在容器中运行指定的脚本,传递查询参数 | Run the specified script in container, passing query parameter
|
||||
echo "在Docker容器中使用 $PYTHON_CMD 运行脚本... | Running script in Docker container using $PYTHON_CMD..."
|
||||
|
||||
# 根据操作系统类型执行不同的命令 | Execute different commands based on operating system type
|
||||
if [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then
|
||||
# Windows可能需要特殊处理引号 | Windows may need special handling for quotes
|
||||
winpty $COMPOSE_CMD exec -T $SERVICE_NAME bash -c "cd .. && source .venv/bin/activate && cd owl && $PYTHON_CMD $SCRIPT_NAME \"$QUERY\""
|
||||
RESULT=$?
|
||||
else
|
||||
# macOS 或 Linux | macOS or Linux
|
||||
$COMPOSE_CMD exec -T $SERVICE_NAME bash -c "cd .. && source .venv/bin/activate && cd owl && $PYTHON_CMD $SCRIPT_NAME \"$QUERY\""
|
||||
RESULT=$?
|
||||
fi
|
||||
|
||||
# 检查命令执行结果 | Check command execution result
|
||||
if [ $RESULT -eq 0 ]; then
|
||||
echo "查询完成! | Query completed!"
|
||||
else
|
||||
echo "查询执行失败,请检查错误信息。 | Query execution failed, please check error messages."
|
||||
fi
|
||||
29
.pre-commit-config.yaml
Normal file
29
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: 'v0.7.4'
|
||||
hooks:
|
||||
- id: ruff
|
||||
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
|
||||
exclude: ^docs/cookbooks/ # Ignore files under docs/cookbooks
|
||||
- id: ruff-format
|
||||
exclude: ^docs/cookbooks/ # Ignore files under docs/cookbooks
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: mypy
|
||||
name: Check mypy
|
||||
entry: mypy --namespace-packages -p owl
|
||||
language: python
|
||||
types: [python]
|
||||
pass_filenames: false
|
||||
require_serial: true
|
||||
exclude: ^docs/cookbooks/ # Ignore files under docs/cookbooks
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: check-license
|
||||
name: Check License
|
||||
entry: python licenses/update_license.py . licenses/license_template.txt
|
||||
language: system
|
||||
types: [python]
|
||||
exclude: ^docs/cookbooks/ # Ignore files under docs/cookbooks
|
||||
463
README.md
463
README.md
@@ -64,89 +64,282 @@ Our vision is to revolutionize how AI agents collaborate to solve real-world tas
|
||||
- [📋 Table of Contents](#-table-of-contents)
|
||||
- [🔥 News](#-news)
|
||||
- [🎬 Demo Video](#-demo-video)
|
||||
- [✨️ Core Features](#-core-features)
|
||||
- [🛠️ Installation](#️-installation)
|
||||
- [**Clone the Github repository**](#clone-the-github-repository)
|
||||
- [**Set up Environment**](#set-up-environment)
|
||||
- [**Install Dependencies**](#install-dependencies)
|
||||
- [**Setup Environment Variables**](#setup-environment-variables)
|
||||
- [**Running with Docker**](#running-with-docker)
|
||||
- [🚀 Quick Start](#-quick-start)
|
||||
- [🧰 Toolkits and Capabilities](#-toolkits-and-capabilities)
|
||||
- [🌐 Web Interface](#-web-interface)
|
||||
- [🧪 Experiments](#-experiments)
|
||||
- [⏱️ Future Plans](#️-future-plans)
|
||||
- [📄 License](#-license)
|
||||
- [🖊️ Cite](#️-cite)
|
||||
- [🤝 Contributing](#-contributing)
|
||||
- [🔥 Community](#-community)
|
||||
- [❓ FAQ](#-faq)
|
||||
- [📚 Exploring CAMEL Dependency](#-exploring-camel-dependency)
|
||||
- [⭐ Star History](#-star-history)
|
||||
|
||||
|
||||
# 🔥 News
|
||||
|
||||
- **[2025.03.10]**: We have cleaned up the code and move most of the toolkit implementation into CAMEL.
|
||||
- **[2025.03.07]**: We open-source the codebase of 🦉 OWL project.
|
||||
|
||||
<div align="center" style="background-color: #fffacd; padding: 15px; border-radius: 10px; border: 2px solid #ffd700; margin: 20px 0;">
|
||||
<h3 style="color: #d81b60; margin: 0; font-size: 1.3em;">
|
||||
🌟🌟🌟 <b>COMMUNITY CALL FOR USE CASES!</b> 🌟🌟🌟
|
||||
</h3>
|
||||
<p style="font-size: 1.1em; margin: 10px 0;">
|
||||
We're inviting the community to contribute innovative use cases for OWL! <br>
|
||||
The <b>top ten submissions</b> will receive special community gifts and recognition.
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://github.com/camel-ai/owl/tree/main/community_usecase/COMMUNITY_CALL_FOR_USE_CASES.md" style="background-color: #d81b60; color: white; padding: 8px 15px; text-decoration: none; border-radius: 5px; font-weight: bold;">Learn More & Submit</a>
|
||||
</p>
|
||||
<p style="margin: 5px 0;">
|
||||
Submission deadline: <b>March 31, 2025</b>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
- **[2025.03.12]**: Added Bocha search in SearchToolkit, integrated Volcano Engine model platform, and enhanced Azure and OpenAI Compatible models with structured output and tool calling.
|
||||
- **[2025.03.11]**: We added MCPToolkit, FileWriteToolkit, and TerminalToolkit to enhance OWL agents with MCP tool calling, file writing capabilities, and terminal command execution.
|
||||
- **[2025.03.09]**: We added a web-based user interface that makes it easier to interact with the system.
|
||||
- **[2025.03.07]**: We open-sourced the codebase of the 🦉 OWL project.
|
||||
- **[2025.03.03]**: OWL achieved the #1 position among open-source frameworks on the GAIA benchmark with a score of 58.18.
|
||||
|
||||
|
||||
# 🎬 Demo Video
|
||||
|
||||
https://private-user-images.githubusercontent.com/55657767/420211368-f29f477d-7eef-46da-8d7a-8f3bcf506da2.mp4
|
||||
https://github.com/user-attachments/assets/2a2a825d-39ea-45c5-9ba1-f9d58efbc372
|
||||
|
||||
https://private-user-images.githubusercontent.com/55657767/420212194-e813fc05-136a-485f-8df3-f10d9b4e63ec.mp4
|
||||
|
||||
# ✨️ Core Features
|
||||
|
||||
- **Real-time Information Retrieval**: Leverage Wikipedia, Google Search, and other online sources for up-to-date information.
|
||||
- **Multimodal Processing**: Support for handling internet or local videos, images, and audio data.
|
||||
- **Browser Automation**: Utilize the Playwright framework for simulating browser interactions, including scrolling, clicking, input handling, downloading, navigation, and more.
|
||||
- **Document Parsing**: Extract content from Word, Excel, PDF, and PowerPoint files, converting them into text or Markdown format.
|
||||
- **Code Execution**: Write and execute Python code using interpreter.
|
||||
- **Built-in Toolkits**: Access to a comprehensive set of built-in toolkits including:
|
||||
- **Model Context Protocol (MCP)**: A universal protocol layer that standardizes AI model interactions with various tools and data sources
|
||||
- **Core Toolkits**: ArxivToolkit, AudioAnalysisToolkit, CodeExecutionToolkit, DalleToolkit, DataCommonsToolkit, ExcelToolkit, GitHubToolkit, GoogleMapsToolkit, GoogleScholarToolkit, ImageAnalysisToolkit, MathToolkit, NetworkXToolkit, NotionToolkit, OpenAPIToolkit, RedditToolkit, SearchToolkit, SemanticScholarToolkit, SymPyToolkit, VideoAnalysisToolkit, WeatherToolkit, BrowserToolkit, and many more for specialized tasks
|
||||
|
||||
# 🛠️ Installation
|
||||
|
||||
## **Clone the Github repository**
|
||||
OWL supports multiple installation methods to fit your workflow preferences. Choose the option that works best for you.
|
||||
|
||||
## Option 1: Using uv (Recommended)
|
||||
|
||||
```bash
|
||||
# Clone github repo
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# Change directory into project directory
|
||||
cd owl
|
||||
|
||||
# Install uv if you don't have it already
|
||||
pip install uv
|
||||
|
||||
# Create a virtual environment and install dependencies
|
||||
# We support using Python 3.10, 3.11, 3.12
|
||||
uv venv .venv --python=3.10
|
||||
|
||||
# Activate the virtual environment
|
||||
# For macOS/Linux
|
||||
source .venv/bin/activate
|
||||
# For Windows
|
||||
.venv\Scripts\activate
|
||||
|
||||
# Install CAMEL with all dependencies
|
||||
uv pip install -e .
|
||||
|
||||
# Exit the virtual environment when done
|
||||
deactivate
|
||||
```
|
||||
|
||||
## Option 2: Using venv and pip
|
||||
|
||||
```bash
|
||||
# Clone github repo
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# Change directory into project directory
|
||||
cd owl
|
||||
|
||||
# Create a virtual environment
|
||||
# For Python 3.10 (also works with 3.11, 3.12)
|
||||
python3.10 -m venv .venv
|
||||
|
||||
# Activate the virtual environment
|
||||
# For macOS/Linux
|
||||
source .venv/bin/activate
|
||||
# For Windows
|
||||
.venv\Scripts\activate
|
||||
|
||||
# Install from requirements.txt
|
||||
pip install -r requirements.txt --use-pep517
|
||||
```
|
||||
|
||||
## Option 3: Using conda
|
||||
|
||||
```bash
|
||||
# Clone github repo
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# Change directory into project directory
|
||||
cd owl
|
||||
|
||||
# Create a conda environment
|
||||
conda create -n owl python=3.10
|
||||
|
||||
# Activate the conda environment
|
||||
conda activate owl
|
||||
|
||||
# Option 1: Install as a package (recommended)
|
||||
pip install -e .
|
||||
|
||||
# Option 2: Install from requirements.txt
|
||||
pip install -r requirements.txt --use-pep517
|
||||
|
||||
# Exit the conda environment when done
|
||||
conda deactivate
|
||||
```
|
||||
|
||||
## **Setup Environment Variables**
|
||||
|
||||
OWL requires various API keys to interact with different services. The `owl/.env_template` file contains placeholders for all necessary API keys along with links to the services where you can register for them.
|
||||
|
||||
### Option 1: Using a `.env` File (Recommended)
|
||||
|
||||
1. **Copy and Rename the Template**:
|
||||
```bash
|
||||
cd owl
|
||||
cp .env_template .env
|
||||
```
|
||||
|
||||
2. **Configure Your API Keys**:
|
||||
Open the `.env` file in your preferred text editor and insert your API keys in the corresponding fields.
|
||||
|
||||
> **Note**: For the minimal example (`run_mini.py`), you only need to configure the LLM API key (e.g., `OPENAI_API_KEY`).
|
||||
|
||||
### Option 2: Setting Environment Variables Directly
|
||||
|
||||
Alternatively, you can set environment variables directly in your terminal:
|
||||
|
||||
- **macOS/Linux (Bash/Zsh)**:
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-openai-api-key-here"
|
||||
```
|
||||
|
||||
- **Windows (Command Prompt)**:
|
||||
```batch
|
||||
set OPENAI_API_KEY="your-openai-api-key-here"
|
||||
```
|
||||
|
||||
- **Windows (PowerShell)**:
|
||||
```powershell
|
||||
$env:OPENAI_API_KEY = "your-openai-api-key-here"
|
||||
```
|
||||
|
||||
> **Note**: Environment variables set directly in the terminal will only persist for the current session.
|
||||
|
||||
|
||||
|
||||
## **Running with Docker**
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
cd owl
|
||||
|
||||
# Configure environment variables
|
||||
cp owl/.env_template owl/.env
|
||||
# Edit the .env file and fill in your API keys
|
||||
|
||||
|
||||
# Option 1: Using docker-compose directly
|
||||
cd .container
|
||||
|
||||
docker-compose up -d
|
||||
|
||||
# Run OWL inside the container
|
||||
docker-compose exec owl bash -c "cd .. && source .venv/bin/activate && cd owl"
|
||||
|
||||
#run example demo script
|
||||
xvfb-python run.py
|
||||
|
||||
# Option 2: Build and run using the provided scripts
|
||||
cd .container
|
||||
chmod +x build_docker.sh
|
||||
./build_docker.sh
|
||||
# Run OWL inside the container
|
||||
./run_in_docker.sh "your question"
|
||||
```
|
||||
|
||||
## **Set up Environment**
|
||||
|
||||
Using Conda (recommended):
|
||||
```bash
|
||||
conda create -n owl python=3.11
|
||||
conda activate owl
|
||||
```
|
||||
|
||||
Using venv (alternative):
|
||||
```bash
|
||||
python -m venv owl_env
|
||||
# On Windows
|
||||
owl_env\Scripts\activate
|
||||
# On Unix or MacOS
|
||||
source owl_env/bin/activate
|
||||
```
|
||||
|
||||
|
||||
## **Install Dependencies**
|
||||
|
||||
```bash
|
||||
python -m pip install -r requirements.txt
|
||||
playwright install
|
||||
```
|
||||
|
||||
## **Setup Environment Variables**
|
||||
|
||||
In the `owl/.env_example` file, you will find all the necessary API keys along with the websites where you can register for each service. To use these API services, follow these steps:
|
||||
|
||||
1. *Copy and Rename*: Duplicate the `.env_example` file and rename the copy to `.env`.
|
||||
```bash
|
||||
cp owl/.env_template .env
|
||||
```
|
||||
2. *Fill in Your Keys*: Open the `.env` file and insert your API keys in the corresponding fields. (For the minimal example (`run_mini.py`), you only need to configure the LLM API key (e.g., OPENAI_API_KEY).)
|
||||
3. *For using more other models*: please refer to our CAMEL models docs:https://docs.camel-ai.org/key_modules/models.html#supported-model-platforms-in-camel
|
||||
|
||||
|
||||
> **Note**: For optimal performance, we strongly recommend using OpenAI models. Our experiments show that other models may result in significantly lower performance on complex tasks and benchmarks.
|
||||
For more detailed Docker usage instructions, including cross-platform support, optimized configurations, and troubleshooting, please refer to [DOCKER_README.md](.container/DOCKER_README_en.md).
|
||||
|
||||
# 🚀 Quick Start
|
||||
|
||||
Run the following demo case:
|
||||
|
||||
## Try MCP (Model Context Protocol) Integration
|
||||
|
||||
Experience the power of MCP by running our example that demonstrates multi-agent information retrieval and processing:
|
||||
|
||||
```bash
|
||||
# Set up MCP servers (one-time setup)
|
||||
npx -y @smithery/cli install @wonderwhy-er/desktop-commander --client claude
|
||||
npx @wonderwhy-er/desktop-commander setup
|
||||
|
||||
# Run the MCP example
|
||||
python owl/run_mcp.py
|
||||
```
|
||||
|
||||
This example showcases how OWL agents can seamlessly interact with file systems, web automation, and information retrieval through the MCP protocol. Check out `owl/run_mcp.py` for the full implementation.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
After installation and setting up your environment variables, you can start using OWL right away:
|
||||
|
||||
```bash
|
||||
python owl/run.py
|
||||
```
|
||||
|
||||
## Running with Different Models
|
||||
|
||||
### Model Requirements
|
||||
|
||||
- **Tool Calling**: OWL requires models with robust tool calling capabilities to interact with various toolkits. Models must be able to understand tool descriptions, generate appropriate tool calls, and process tool outputs.
|
||||
|
||||
- **Multimodal Understanding**: For tasks involving web interaction, image analysis, or video processing, models with multimodal capabilities are required to interpret visual content and context.
|
||||
|
||||
#### Supported Models
|
||||
|
||||
For information on configuring AI models, please refer to our [CAMEL models documentation](https://docs.camel-ai.org/key_modules/models.html#supported-model-platforms-in-camel).
|
||||
|
||||
> **Note**: For optimal performance, we strongly recommend using OpenAI models (GPT-4 or later versions). Our experiments show that other models may result in significantly lower performance on complex tasks and benchmarks, especially those requiring advanced multi-modal understanding and tool use.
|
||||
|
||||
OWL supports various LLM backends, though capabilities may vary depending on the model's tool calling and multimodal abilities. You can use the following scripts to run with different models:
|
||||
|
||||
```bash
|
||||
# Run with Qwen model
|
||||
python owl/run_qwen_zh.py
|
||||
|
||||
# Run with Deepseek model
|
||||
python owl/run_deepseek_zh.py
|
||||
|
||||
# Run with other OpenAI-compatible models
|
||||
python owl/run_openai_compatiable_model.py
|
||||
|
||||
# Run with Azure OpenAI
|
||||
python owl/run_azure_openai.py
|
||||
|
||||
# Run with Ollama
|
||||
python owl/run_ollama.py
|
||||
```
|
||||
|
||||
For a simpler version that only requires an LLM API key, you can try our minimal example:
|
||||
|
||||
```bash
|
||||
@@ -162,35 +355,149 @@ question = "Task description here."
|
||||
society = construct_society(question)
|
||||
answer, chat_history, token_count = run_society(society)
|
||||
|
||||
logger.success(f"Answer: {answer}")
|
||||
print(f"\033[94mAnswer: {answer}\033[0m")
|
||||
```
|
||||
|
||||
Example tasks you can try:
|
||||
For uploading files, simply provide the file path along with your question:
|
||||
|
||||
```python
|
||||
# Task with a local file (e.g., file path: `tmp/example.docx`)
|
||||
question = "What is in the given DOCX file? Here is the file path: tmp/example.docx"
|
||||
|
||||
society = construct_society(question)
|
||||
answer, chat_history, token_count = run_society(society)
|
||||
print(f"\033[94mAnswer: {answer}\033[0m")
|
||||
```
|
||||
|
||||
OWL will then automatically invoke document-related tools to process the file and extract the answer.
|
||||
|
||||
|
||||
### Example Tasks
|
||||
|
||||
Here are some tasks you can try with OWL:
|
||||
|
||||
- "Find the latest stock price for Apple Inc."
|
||||
- "Analyze the sentiment of recent tweets about climate change"
|
||||
- "Help me debug this Python code: [your code here]"
|
||||
- "Summarize the main points from this research paper: [paper URL]"
|
||||
- "Create a data visualization for this dataset: [dataset path]"
|
||||
|
||||
# 🧰 Toolkits and Capabilities
|
||||
|
||||
## Model Context Protocol (MCP)
|
||||
|
||||
OWL's MCP integration provides a standardized way for AI models to interact with various tools and data sources:
|
||||
|
||||
Try our comprehensive MCP example in `owl/run_mcp.py` to see these capabilities in action!
|
||||
|
||||
## Available Toolkits
|
||||
|
||||
> **Important**: Effective use of toolkits requires models with strong tool calling capabilities. For multimodal toolkits (Web, Image, Video), models must also have multimodal understanding abilities.
|
||||
|
||||
OWL supports various toolkits that can be customized by modifying the `tools` list in your script:
|
||||
|
||||
```python
|
||||
# Configure toolkits
|
||||
tools = [
|
||||
*BrowserToolkit(headless=False).get_tools(), # Browser automation
|
||||
*VideoAnalysisToolkit(model=models["video"]).get_tools(),
|
||||
*AudioAnalysisToolkit().get_tools(), # Requires OpenAI Key
|
||||
*CodeExecutionToolkit(sandbox="subprocess").get_tools(),
|
||||
*ImageAnalysisToolkit(model=models["image"]).get_tools(),
|
||||
SearchToolkit().search_duckduckgo,
|
||||
SearchToolkit().search_google, # Comment out if unavailable
|
||||
SearchToolkit().search_wiki,
|
||||
*ExcelToolkit().get_tools(),
|
||||
*DocumentProcessingToolkit(model=models["document"]).get_tools(),
|
||||
*FileWriteToolkit(output_dir="./").get_tools(),
|
||||
]
|
||||
```
|
||||
|
||||
## Available Toolkits
|
||||
|
||||
Key toolkits include:
|
||||
|
||||
### Multimodal Toolkits (Require multimodal model capabilities)
|
||||
- **BrowserToolkit**: Browser automation for web interaction and navigation
|
||||
- **VideoAnalysisToolkit**: Video processing and content analysis
|
||||
- **ImageAnalysisToolkit**: Image analysis and interpretation
|
||||
|
||||
### Text-Based Toolkits
|
||||
- **AudioAnalysisToolkit**: Audio processing (requires OpenAI API)
|
||||
- **CodeExecutionToolkit**: Python code execution and evaluation
|
||||
- **SearchToolkit**: Web searches (Google, DuckDuckGo, Wikipedia)
|
||||
- **DocumentProcessingToolkit**: Document parsing (PDF, DOCX, etc.)
|
||||
|
||||
Additional specialized toolkits: ArxivToolkit, GitHubToolkit, GoogleMapsToolkit, MathToolkit, NetworkXToolkit, NotionToolkit, RedditToolkit, WeatherToolkit, and more. For a complete list, see the [CAMEL toolkits documentation](https://docs.camel-ai.org/key_modules/tools.html#built-in-toolkits).
|
||||
|
||||
## Customizing Your Configuration
|
||||
|
||||
To customize available tools:
|
||||
|
||||
```python
|
||||
# 1. Import toolkits
|
||||
from camel.toolkits import BrowserToolkit, SearchToolkit, CodeExecutionToolkit
|
||||
|
||||
# 2. Configure tools list
|
||||
tools = [
|
||||
*BrowserToolkit(headless=True).get_tools(),
|
||||
SearchToolkit().search_wiki,
|
||||
*CodeExecutionToolkit(sandbox="subprocess").get_tools(),
|
||||
]
|
||||
|
||||
# 3. Pass to assistant agent
|
||||
assistant_agent_kwargs = {"model": models["assistant"], "tools": tools}
|
||||
```
|
||||
|
||||
Selecting only necessary toolkits optimizes performance and reduces resource usage.
|
||||
|
||||
# 🌐 Web Interface
|
||||
|
||||
OWL includes an intuitive web-based user interface that makes it easier to interact with the system.
|
||||
|
||||
## Starting the Web UI
|
||||
|
||||
```bash
|
||||
# Start the Chinese version
|
||||
python run_app_zh.py
|
||||
|
||||
# Start the English version
|
||||
python run_app.py
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- **Easy Model Selection**: Choose between different models (OpenAI, Qwen, DeepSeek, etc.)
|
||||
- **Environment Variable Management**: Configure your API keys and other settings directly from the UI
|
||||
- **Interactive Chat Interface**: Communicate with OWL agents through a user-friendly interface
|
||||
- **Task History**: View the history and results of your interactions
|
||||
|
||||
The web interface is built using Gradio and runs locally on your machine. No data is sent to external servers beyond what's required for the model API calls you configure.
|
||||
|
||||
# 🧪 Experiments
|
||||
|
||||
To reproduce OWL's GAIA benchmark score of 58.18:
|
||||
|
||||
1. Switch to the `gaia58.18` branch:
|
||||
```bash
|
||||
git checkout gaia58.18
|
||||
```
|
||||
```bash
|
||||
git checkout gaia58.18
|
||||
```
|
||||
|
||||
1. Run the evaluation script:
|
||||
```bash
|
||||
python run_gaia_roleplaying.py
|
||||
```
|
||||
2. Run the evaluation script:
|
||||
```bash
|
||||
python run_gaia_roleplaying.py
|
||||
```
|
||||
|
||||
This will execute the same configuration that achieved our top-ranking performance on the GAIA benchmark.
|
||||
|
||||
# ⏱️ Future Plans
|
||||
|
||||
- [ ] Write a technical blog post detailing our exploration and insights in multi-agent collaboration in real-world tasks.
|
||||
- [ ] Enhance the toolkit ecosystem with more specialized tools for domain-specific tasks.
|
||||
- [ ] Develop more sophisticated agent interaction patterns and communication protocols
|
||||
We're continuously working to improve OWL. Here's what's on our roadmap:
|
||||
|
||||
- [ ] Write a technical blog post detailing our exploration and insights in multi-agent collaboration in real-world tasks
|
||||
- [ ] Enhance the toolkit ecosystem with more specialized tools for domain-specific tasks
|
||||
- [ ] Develop more sophisticated agent interaction patterns and communication protocols
|
||||
- [ ] Improve performance on complex multi-step reasoning tasks
|
||||
|
||||
# 📄 License
|
||||
|
||||
@@ -211,17 +518,55 @@ If you find this repo useful, please cite:
|
||||
}
|
||||
```
|
||||
|
||||
# 🤝 Contributing
|
||||
|
||||
We welcome contributions from the community! Here's how you can help:
|
||||
|
||||
1. Read our [Contribution Guidelines](https://github.com/camel-ai/camel/blob/master/CONTRIBUTING.md)
|
||||
2. Check [open issues](https://github.com/camel-ai/camel/issues) or create new ones
|
||||
3. Submit pull requests with your improvements
|
||||
|
||||
**Current Issues Open for Contribution:**
|
||||
- [#1857](https://github.com/camel-ai/camel/issues/1857)
|
||||
- [#1770](https://github.com/camel-ai/camel/issues/1770)
|
||||
- [#1712](https://github.com/camel-ai/camel/issues/1712)
|
||||
- [#1537](https://github.com/camel-ai/camel/issues/1537)
|
||||
|
||||
|
||||
To take on an issue, simply leave a comment stating your interest.
|
||||
|
||||
# 🔥 Community
|
||||
Join us ([*Discord*](https://discord.camel-ai.org/) or [*WeChat*](https://ghli.org/camel/wechat.png)) in pushing the boundaries of finding the scaling laws of agents.
|
||||
|
||||
Join us for further discussions!
|
||||
<!--  -->
|
||||

|
||||
<!--  -->
|
||||
|
||||
# ❓ FAQ
|
||||
|
||||
**Q: Why is my Chrome browser showing a blank screen even though there's output in the console?**
|
||||
**Q: Why don't I see Chrome running locally after starting the example script?**
|
||||
|
||||
A: This is expected behavior. When OWL determines that a task can be completed using non-browser tools (like search, code analysis, etc.), the browser window may remain blank. The browser is only activated when web interaction is necessary. We plan to implement lazy loading in future updates to improve this user experience.
|
||||
A: If OWL determines that a task can be completed using non-browser tools (such as search or code execution), the browser will not be launched. The browser window will only appear when OWL determines that browser-based interaction is necessary.
|
||||
|
||||
**Q: Which Python version should I use?**
|
||||
|
||||
A: OWL supports Python 3.10, 3.11, and 3.12.
|
||||
|
||||
**Q: How can I contribute to the project?**
|
||||
|
||||
A: See our [Contributing](#-contributing) section for details on how to get involved. We welcome contributions of all kinds, from code improvements to documentation updates.
|
||||
|
||||
# 📚 Exploring CAMEL Dependency
|
||||
|
||||
OWL is built on top of the [CAMEL](https://github.com/camel-ai/camel) Framework, here's how you can explore the CAMEL source code and understand how it works with OWL:
|
||||
|
||||
## Accessing CAMEL Source Code
|
||||
|
||||
```bash
|
||||
# Clone the CAMEL repository
|
||||
git clone https://github.com/camel-ai/camel.git
|
||||
cd camel
|
||||
```
|
||||
|
||||
# ⭐ Star History
|
||||
|
||||
|
||||
429
README_zh.md
429
README_zh.md
@@ -1,6 +1,6 @@
|
||||
<h1 align="center">
|
||||
🦉 OWL: Optimized Workforce Learning for General Multi-Agent Assistance in Real-World Task Automation
|
||||
🦉 OWL: 优化劳动力学习的通用智能体,用于处理现实世界的自动化任务
|
||||
🦉 OWL: 优化劳动力学习的通用智能体,用于处理现实世界的自动化任务
|
||||
</h1>
|
||||
|
||||
|
||||
@@ -65,23 +65,50 @@
|
||||
- [📋 目录](#-目录)
|
||||
- [🔥 新闻](#-新闻)
|
||||
- [🎬 演示视频](#-演示视频)
|
||||
- [✨️ 核心功能](#-核心功能)
|
||||
- [🛠️ 安装](#️-安装)
|
||||
- [**克隆 Github 仓库**](#克隆-github-仓库)
|
||||
- [**设置环境**](#设置环境)
|
||||
- [**安装依赖**](#安装依赖)
|
||||
- [**选项1:使用 uv(推荐)**](#选项1使用-uv推荐)
|
||||
- [**选项2:使用 venv 和 pip**](#选项2使用-venv-和-pip)
|
||||
- [**选项3:使用 conda**](#选项3使用-conda)
|
||||
- [**设置环境变量**](#设置环境变量)
|
||||
- [**使用Docker运行**](#使用docker运行)
|
||||
- [🚀 快速开始](#-快速开始)
|
||||
- [🧰 工具包与功能](#-工具包与功能)
|
||||
- [🌐 网页界面](#-网页界面)
|
||||
- [🧪 实验](#-实验)
|
||||
- [⏱️ 未来计划](#️-未来计划)
|
||||
- [📄 许可证](#-许可证)
|
||||
- [🖊️ 引用](#️-引用)
|
||||
- [🤝 贡献](#-贡献)
|
||||
- [🔥 社区](#-社区)
|
||||
- [❓ 常见问题](#-常见问题)
|
||||
- [📚 探索 CAMEL 依赖](#-探索-camel-依赖)
|
||||
- [⭐ Star History](#-star-history)
|
||||
|
||||
|
||||
# 🔥 新闻
|
||||
|
||||
<div align="center" style="background-color: #fffacd; padding: 15px; border-radius: 10px; border: 2px solid #ffd700; margin: 20px 0;">
|
||||
<h3 style="color: #d81b60; margin: 0; font-size: 1.3em;">
|
||||
🌟🌟🌟 <b>OWL社区用例征集令!</b> 🌟🌟🌟
|
||||
</h3>
|
||||
<p style="font-size: 1.1em; margin: 10px 0;">
|
||||
我们请社区成员贡献创新的OWL用例!<br>
|
||||
<b>前十名提交</b>将获得特别社区礼物和认可。
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://github.com/camel-ai/owl/tree/main/community_usecase/COMMUNITY_CALL_FOR_USE_CASES.md" style="background-color: #d81b60; color: white; padding: 8px 15px; text-decoration: none; border-radius: 5px; font-weight: bold;">了解更多并提交</a>
|
||||
</p>
|
||||
<p style="margin: 5px 0;">
|
||||
提交截止日期:<b>2025年3月31日</b>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
- **[2025.03.12]**: 在SearchToolkit中添加了Bocha搜索功能,集成了火山引擎模型平台,并更新了Azure和OpenAI Compatible模型的结构化输出和工具调用能力。
|
||||
- **[2025.03.11]**: 我们添加了 MCPToolkit、FileWriteToolkit 和 TerminalToolkit,增强了 OWL Agent 的 MCP(模型上下文协议)集成、文件写入能力和终端命令执行功能。MCP 作为一个通用协议层,标准化了 AI 模型与各种数据源和工具的交互方式。
|
||||
- **[2025.03.09]**: 我们添加了基于网页的用户界面,使系统交互变得更加简便。
|
||||
- **[2025.03.07]**: 我们开源了 🦉 OWL 项目的代码库。
|
||||
- **[2025.03.03]**: OWL 在 GAIA 基准测试中取得 58.18 平均分,在开源框架中排名第一!
|
||||
|
||||
# 🎬 演示视频
|
||||
|
||||
@@ -89,49 +116,184 @@ https://private-user-images.githubusercontent.com/55657767/420211368-f29f477d-7e
|
||||
|
||||
https://private-user-images.githubusercontent.com/55657767/420212194-e813fc05-136a-485f-8df3-f10d9b4e63ec.mp4
|
||||
|
||||
# ✨️ 核心功能
|
||||
|
||||
- **在线搜索**:使用维基百科、谷歌搜索等,进行实时信息检索
|
||||
- **多模态处理**:支持互联网或本地视频、图片、语音处理
|
||||
- **浏览器操作**:借助Playwright框架开发浏览器模拟交互,支持页面滚动、点击、输入、下载、历史回退等功能
|
||||
- **文件解析**:word、excel、PDF、PowerPoint信息提取,内容转文本/Markdown
|
||||
- **代码执行**:编写python代码,并使用解释器运行
|
||||
- **丰富工具包**:提供丰富的工具包,包括ArxivToolkit(学术论文检索)、AudioAnalysisToolkit(音频分析)、CodeExecutionToolkit(代码执行)、DalleToolkit(图像生成)、DataCommonsToolkit(数据共享)、ExcelToolkit(Excel处理)、GitHubToolkit(GitHub交互)、GoogleMapsToolkit(地图服务)、GoogleScholarToolkit(学术搜索)、ImageAnalysisToolkit(图像分析)、MathToolkit(数学计算)、NetworkXToolkit(图形分析)、NotionToolkit(Notion交互)、OpenAPIToolkit(API操作)、RedditToolkit(Reddit交互)、SearchToolkit(搜索服务)、SemanticScholarToolkit(语义学术搜索)、SymPyToolkit(符号计算)、VideoAnalysisToolkit(视频分析)、WeatherToolkit(天气查询)、BrowserToolkit(网页交互)等多种专业工具,满足各类特定任务需求。
|
||||
|
||||
# 🛠️ 安装
|
||||
|
||||
## **克隆 Github 仓库**
|
||||
## 选项1:使用 uv(推荐)
|
||||
|
||||
```bash
|
||||
# 克隆 GitHub 仓库
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# 进入项目目录
|
||||
cd owl
|
||||
|
||||
# 如果你还没有安装 uv,请先安装
|
||||
pip install uv
|
||||
|
||||
# 创建虚拟环境并安装依赖
|
||||
# 我们支持使用 Python 3.10、3.11、3.12
|
||||
uv venv .venv --python=3.10
|
||||
|
||||
# 激活虚拟环境
|
||||
# 对于 macOS/Linux
|
||||
source .venv/bin/activate
|
||||
# 对于 Windows
|
||||
.venv\Scripts\activate
|
||||
|
||||
# 安装 CAMEL 及其所有依赖
|
||||
uv pip install -e .
|
||||
|
||||
# 完成后退出虚拟环境
|
||||
deactivate
|
||||
```
|
||||
|
||||
## 选项2:使用 venv 和 pip
|
||||
|
||||
```bash
|
||||
# 克隆 GitHub 仓库
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# 进入项目目录
|
||||
cd owl
|
||||
|
||||
# 创建虚拟环境
|
||||
# 对于 Python 3.10(也适用于 3.11、3.12)
|
||||
python3.10 -m venv .venv
|
||||
|
||||
# 激活虚拟环境
|
||||
# 对于 macOS/Linux
|
||||
source .venv/bin/activate
|
||||
# 对于 Windows
|
||||
.venv\Scripts\activate
|
||||
|
||||
# 从 requirements.txt 安装
|
||||
pip install -r requirements.txt --use-pep517
|
||||
```
|
||||
|
||||
## 选项3:使用 conda
|
||||
|
||||
```bash
|
||||
# 克隆 GitHub 仓库
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
|
||||
# 进入项目目录
|
||||
cd owl
|
||||
|
||||
# 创建 conda 环境
|
||||
conda create -n owl python=3.10
|
||||
|
||||
# 激活 conda 环境
|
||||
conda activate owl
|
||||
|
||||
# 选项1:作为包安装(推荐)
|
||||
pip install -e .
|
||||
|
||||
# 选项2:从 requirements.txt 安装
|
||||
pip install -r requirements.txt --use-pep517
|
||||
|
||||
# 完成后退出 conda 环境
|
||||
conda deactivate
|
||||
```
|
||||
|
||||
## **设置环境变量**
|
||||
|
||||
OWL 需要各种 API 密钥来与不同的服务进行交互。`owl/.env_template` 文件包含了所有必要 API 密钥的占位符,以及可以注册这些服务的链接。
|
||||
|
||||
### 选项 1:使用 `.env` 文件(推荐)
|
||||
|
||||
1. **复制并重命名模板**:
|
||||
```bash
|
||||
cd owl
|
||||
cp .env_template .env
|
||||
```
|
||||
|
||||
2. **配置你的 API 密钥**:
|
||||
在你喜欢的文本编辑器中打开 `.env` 文件,并在相应字段中插入你的 API 密钥。
|
||||
|
||||
> **注意**:对于最小示例(`run_mini.py`),你只需要配置 LLM API 密钥(例如,`OPENAI_API_KEY`)。
|
||||
|
||||
### 选项 2:直接设置环境变量
|
||||
|
||||
或者,你可以直接在终端中设置环境变量:
|
||||
|
||||
- **macOS/Linux (Bash/Zsh)**:
|
||||
```bash
|
||||
export OPENAI_API_KEY="你的-openai-api-密钥"
|
||||
```
|
||||
|
||||
- **Windows (命令提示符)**:
|
||||
```batch
|
||||
set OPENAI_API_KEY="你的-openai-api-密钥"
|
||||
```
|
||||
|
||||
- **Windows (PowerShell)**:
|
||||
```powershell
|
||||
$env:OPENAI_API_KEY = "你的-openai-api-密钥"
|
||||
```
|
||||
|
||||
> **注意**:直接在终端中设置的环境变量仅在当前会话中有效。
|
||||
|
||||
## **使用Docker运行**
|
||||
|
||||
如果您希望使用Docker运行OWL项目,我们提供了完整的Docker支持:
|
||||
|
||||
```bash
|
||||
# 克隆仓库
|
||||
git clone https://github.com/camel-ai/owl.git
|
||||
cd owl
|
||||
|
||||
# 配置环境变量
|
||||
cp owl/.env_template owl/.env
|
||||
# 编辑.env文件,填入您的API密钥
|
||||
|
||||
# 选项1:直接使用docker-compose
|
||||
cd .container
|
||||
|
||||
docker-compose up -d
|
||||
|
||||
# 在容器中运行OWL
|
||||
docker-compose exec owl bash -c "cd .. && source .venv/bin/activate && cd owl"
|
||||
|
||||
#运行例子演示脚本
|
||||
xvfb-python run.py
|
||||
|
||||
# 选项2:使用提供的脚本构建和运行
|
||||
cd .container
|
||||
chmod +x build_docker.sh
|
||||
./build_docker.sh
|
||||
# 在容器中运行OWL
|
||||
./run_in_docker.sh "您的问题"
|
||||
```
|
||||
|
||||
## **设置环境**
|
||||
|
||||
使用 Conda(推荐):
|
||||
```bash
|
||||
conda create -n owl python=3.11
|
||||
conda activate owl
|
||||
```
|
||||
|
||||
使用 venv(备用):
|
||||
```bash
|
||||
python -m venv owl_env
|
||||
# Windows 系统
|
||||
owl_env\Scripts\activate
|
||||
# Unix 或 MacOS 系统
|
||||
source owl_env/bin/activate
|
||||
```
|
||||
|
||||
## **安装依赖**
|
||||
|
||||
```bash
|
||||
python -m pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## **设置环境变量**
|
||||
|
||||
在 `owl/.env_example` 文件中,你可以找到所有必要的 API 密钥以及各服务的注册网址。要使用这些 API 服务,请按照以下步骤操作:
|
||||
|
||||
1. *复制并重命名*: 复制 `.env_example` 文件,并将副本重命名为 `.env`。
|
||||
2. *填写你的密钥*: 打开 `.env` 文件,在相应字段中填入你的 API 密钥。
|
||||
3. *如需使用更多其他模型*:请参考我们CAMEL的models文档:https://docs.camel-ai.org/key_modules/models.html#supported-model-platforms-in-camel
|
||||
|
||||
> **注意**:为获得最佳性能,我们强烈建议使用 OpenAI 模型。我们通过测试发现,其他模型在处理复杂任务和基准测试时可能会导致性能显著降低。
|
||||
更多详细的Docker使用说明,包括跨平台支持、优化配置和故障排除,请参阅 [DOCKER_README.md](.container/DOCKER_README.md)
|
||||
|
||||
# 🚀 快速开始
|
||||
|
||||
## 尝试 MCP(模型上下文协议)集成
|
||||
|
||||
体验 MCP 的强大功能,运行我们的示例来展示多智能体信息检索和处理:
|
||||
|
||||
```bash
|
||||
# 设置 MCP 服务器(仅需一次性设置)
|
||||
npx -y @smithery/cli install @wonderwhy-er/desktop-commander --client claude
|
||||
npx @wonderwhy-er/desktop-commander setup
|
||||
|
||||
# 运行 MCP 示例
|
||||
python owl/run_mcp.py
|
||||
```
|
||||
|
||||
这个示例展示了 OWL 智能体如何通过 MCP 协议无缝地与文件系统、网页自动化和信息检索进行交互。查看 `owl/run_mcp.py` 了解完整实现。
|
||||
|
||||
## 基本用法
|
||||
|
||||
运行以下示例:
|
||||
|
||||
@@ -145,6 +307,39 @@ python owl/run.py
|
||||
python owl/run_mini.py
|
||||
```
|
||||
|
||||
## 使用不同的模型
|
||||
|
||||
### 模型要求
|
||||
|
||||
- **工具调用能力**:OWL 需要具有强大工具调用能力的模型来与各种工具包交互。模型必须能够理解工具描述、生成适当的工具调用,并处理工具输出。
|
||||
|
||||
- **多模态理解能力**:对于涉及网页交互、图像分析或视频处理的任务,需要具备多模态能力的模型来解释视觉内容和上下文。
|
||||
|
||||
#### 支持的模型
|
||||
|
||||
有关配置模型的信息,请参阅我们的 [CAMEL 模型文档](https://docs.camel-ai.org/key_modules/models.html#supported-model-platforms-in-camel)。
|
||||
|
||||
> **注意**:为获得最佳性能,我们强烈推荐使用 OpenAI 模型(GPT-4 或更高版本)。我们的实验表明,其他模型在复杂任务和基准测试上可能表现明显较差,尤其是那些需要多模态理解和工具使用的任务。
|
||||
|
||||
OWL 支持多种 LLM 后端,但功能可能因模型的工具调用和多模态能力而异。您可以使用以下脚本来运行不同的模型:
|
||||
|
||||
```bash
|
||||
# 使用 Qwen 模型运行
|
||||
python owl/run_qwen_zh.py
|
||||
|
||||
# 使用 Deepseek 模型运行
|
||||
python owl/run_deepseek_zh.py
|
||||
|
||||
# 使用其他 OpenAI 兼容模型运行
|
||||
python owl/run_openai_compatiable_model.py
|
||||
|
||||
# 使用 Azure OpenAI模型运行
|
||||
python owl/run_azure_openai.py
|
||||
|
||||
# 使用 Ollama 运行
|
||||
python owl/run_ollama.py
|
||||
```
|
||||
|
||||
你可以通过修改 `run.py` 脚本来运行自己的任务:
|
||||
|
||||
```python
|
||||
@@ -154,14 +349,119 @@ question = "Task description here."
|
||||
society = construct_society(question)
|
||||
answer, chat_history, token_count = run_society(society)
|
||||
|
||||
logger.success(f"Answer: {answer}")
|
||||
print(f"\033[94mAnswer: {answer}\033[0m")
|
||||
```
|
||||
|
||||
上传文件时,只需提供文件路径和问题:
|
||||
|
||||
```python
|
||||
# 处理本地文件(例如,文件路径为 `tmp/example.docx`)
|
||||
question = "给定的 DOCX 文件中有什么内容?文件路径如下:tmp/example.docx"
|
||||
|
||||
society = construct_society(question)
|
||||
answer, chat_history, token_count = run_society(society)
|
||||
|
||||
print(f"答案:{answer}")
|
||||
```
|
||||
|
||||
OWL 将自动调用与文档相关的工具来处理文件并提取答案。
|
||||
|
||||
你可以尝试以下示例任务:
|
||||
- "查询苹果公司的最新股票价格"
|
||||
- "分析关于气候变化的最新推文情绪"
|
||||
- "帮我调试这段 Python 代码:[在此粘贴你的代码]"
|
||||
- "总结这篇研究论文的主要观点:[论文URL]"
|
||||
|
||||
# 🧰 工具包与功能
|
||||
|
||||
## 模型上下文协议(MCP)
|
||||
|
||||
OWL 的 MCP 集成为 AI 模型与各种工具和数据源的交互提供了标准化的方式。
|
||||
|
||||
查看我们的综合示例 `owl/run_mcp.py` 来体验这些功能!
|
||||
|
||||
## 可用工具包
|
||||
|
||||
> **重要提示**:有效使用工具包需要具备强大工具调用能力的模型。对于多模态工具包(Web、图像、视频),模型还必须具备多模态理解能力。
|
||||
|
||||
OWL支持多种工具包,可通过修改脚本中的`tools`列表进行自定义:
|
||||
|
||||
```python
|
||||
# 配置工具包
|
||||
tools = [
|
||||
*BrowserToolkit(headless=False).get_tools(), # 浏览器自动化
|
||||
*VideoAnalysisToolkit(model=models["video"]).get_tools(),
|
||||
*AudioAnalysisToolkit().get_tools(), # 需要OpenAI API密钥
|
||||
*CodeExecutionToolkit(sandbox="subprocess").get_tools(),
|
||||
*ImageAnalysisToolkit(model=models["image"]).get_tools(),
|
||||
SearchToolkit().search_duckduckgo,
|
||||
SearchToolkit().search_google, # 如果不可用请注释
|
||||
SearchToolkit().search_wiki,
|
||||
*ExcelToolkit().get_tools(),
|
||||
*DocumentProcessingToolkit(model=models["document"]).get_tools(),
|
||||
*FileWriteToolkit(output_dir="./").get_tools(),
|
||||
]
|
||||
```
|
||||
|
||||
## 主要工具包
|
||||
|
||||
关键工具包包括:
|
||||
|
||||
### 多模态工具包(需要模型具备多模态能力)
|
||||
- **BrowserToolkit**:浏览器自动化,用于网页交互和导航
|
||||
- **VideoAnalysisToolkit**:视频处理和内容分析
|
||||
- **ImageAnalysisToolkit**:图像分析和解释
|
||||
|
||||
### 基于文本的工具包
|
||||
- **AudioAnalysisToolkit**:音频处理(需要 OpenAI API)
|
||||
- **CodeExecutionToolkit**:Python 代码执行和评估
|
||||
- **SearchToolkit**:网络搜索(Google、DuckDuckGo、维基百科)
|
||||
- **DocumentProcessingToolkit**:文档解析(PDF、DOCX等)
|
||||
|
||||
其他专用工具包:ArxivToolkit、GitHubToolkit、GoogleMapsToolkit、MathToolkit、NetworkXToolkit、NotionToolkit、RedditToolkit、WeatherToolkit等。完整工具包列表请参阅[CAMEL工具包文档](https://docs.camel-ai.org/key_modules/tools.html#built-in-toolkits)。
|
||||
|
||||
## 自定义配置
|
||||
|
||||
自定义可用工具的方法:
|
||||
|
||||
```python
|
||||
# 1. 导入工具包
|
||||
from camel.toolkits import BrowserToolkit, SearchToolkit, CodeExecutionToolkit
|
||||
|
||||
# 2. 配置工具列表
|
||||
tools = [
|
||||
*BrowserToolkit(headless=True).get_tools(),
|
||||
SearchToolkit().search_wiki,
|
||||
*CodeExecutionToolkit(sandbox="subprocess").get_tools(),
|
||||
]
|
||||
|
||||
# 3. 传递给助手代理
|
||||
assistant_agent_kwargs = {"model": models["assistant"], "tools": tools}
|
||||
```
|
||||
|
||||
选择必要的工具包可优化性能并减少资源使用。
|
||||
|
||||
# 🌐 网页界面
|
||||
|
||||
OWL 现在包含一个基于网页的用户界面,使与系统交互变得更加容易。要启动网页界面,请运行:
|
||||
|
||||
```bash
|
||||
# 中文版本
|
||||
python run_app_zh.py
|
||||
|
||||
# 英文版本
|
||||
python run_app.py
|
||||
```
|
||||
|
||||
网页界面提供以下功能:
|
||||
|
||||
- **便捷的模型选择**:选择不同的模型(OpenAI、Qwen、DeepSeek等)
|
||||
- **环境变量管理**:直接从界面配置API密钥和其他设置
|
||||
- **交互式聊天界面**:通过用户友好的界面与OWL智能体交流
|
||||
- **任务历史**:查看交互的历史记录和结果
|
||||
|
||||
网页界面使用Gradio构建,在您的本地机器上运行。除了您配置的模型API调用所需的数据外,不会向外部服务器发送任何数据。
|
||||
|
||||
# 🧪 实验
|
||||
|
||||
我们提供了一个脚本用于复现 GAIA 上的实验结果。
|
||||
@@ -179,10 +479,12 @@ python run_gaia_roleplaying.py
|
||||
|
||||
# ⏱️ 未来计划
|
||||
|
||||
- [ ] 撰写一篇技术博客,详细介绍我们在现实任务中多智能体协作方面的探索与见解。
|
||||
- [ ] 通过引入更多针对特定领域任务的专业工具,进一步完善工具生态系统。
|
||||
- [ ] 开发更复杂的智能体交互模式和通信协议
|
||||
我们正在不断努力改进 OWL。以下是我们的路线图:
|
||||
|
||||
- [ ] 撰写技术博客,详细介绍我们在现实任务中多智能体协作方面的探索与见解
|
||||
- [ ] 通过引入更多针对特定领域任务的专业工具,进一步完善工具生态系统
|
||||
- [ ] 开发更复杂的智能体交互模式和通信协议
|
||||
- [ ] 提高复杂多步推理任务的性能
|
||||
|
||||
# 📄 许可证
|
||||
|
||||
@@ -203,7 +505,25 @@ python run_gaia_roleplaying.py
|
||||
}
|
||||
```
|
||||
|
||||
# 🤝 贡献
|
||||
|
||||
我们欢迎社区的贡献!以下是您可以提供帮助的方式:
|
||||
|
||||
1. 阅读我们的[贡献指南](https://github.com/camel-ai/camel/blob/master/CONTRIBUTING.md)
|
||||
2. 查看[开放的问题](https://github.com/camel-ai/camel/issues)或创建新的问题
|
||||
3. 提交包含您改进的拉取请求
|
||||
|
||||
**当前开放贡献的问题:**
|
||||
- [#1857](https://github.com/camel-ai/camel/issues/1857)
|
||||
- [#1770](https://github.com/camel-ai/camel/issues/1770)
|
||||
- [#1712](https://github.com/camel-ai/camel/issues/1712)
|
||||
- [#1537](https://github.com/camel-ai/camel/issues/1537)
|
||||
|
||||
要认领一个问题,只需在该问题下留言表明您的兴趣即可。
|
||||
|
||||
# 🔥 社区
|
||||
加入我们的 ([*Discord*](https://discord.camel-ai.org/) 或 [*微信*](https://ghli.org/camel/wechat.png)) 社区,一起探索智能体扩展规律的边界。
|
||||
|
||||
加入我们,参与更多讨论!
|
||||
<!--  -->
|
||||

|
||||
@@ -211,10 +531,33 @@ python run_gaia_roleplaying.py
|
||||
|
||||
# ❓ 常见问题
|
||||
|
||||
**Q: 为什么我的Chrome浏览器显示空白页面,但控制台有输出结果?**
|
||||
**Q: 为什么启动示例脚本后,我没有看到本地运行Chrome浏览器?**
|
||||
|
||||
A: 这是预期的行为。当OWL判断某个任务可以使用非浏览器工具(如搜索、代码分析等)完成时,浏览器窗口可能保持空白。浏览器仅在需要网页交互时才会被激活。我们计划在未来的更新中实现延迟加载以改善这一用户体验。
|
||||
A: 当OWL判断某个任务可以使用非浏览器工具(如搜索、代码分析等)完成时,浏览器就不会启动。只有在判断需要使用浏览器工具的时候,本地才会弹出浏览器窗口,并进行浏览器模拟交互。
|
||||
|
||||
**Q: 我应该使用哪个Python版本?**
|
||||
|
||||
A: OWL支持Python 3.10、3.11和3.12。为了与所有依赖项获得最佳兼容性,我们推荐使用Python 3.10。
|
||||
|
||||
**Q: 我如何为项目做贡献?**
|
||||
|
||||
A: 请参阅我们的[贡献](#-贡献)部分,了解如何参与的详细信息。我们欢迎各种形式的贡献,从代码改进到文档更新。
|
||||
|
||||
# 📚 探索 CAMEL 依赖
|
||||
|
||||
OWL 是基于 [CAMEL](https://github.com/camel-ai/camel) 框架构建的,以下是如何探索 CAMEL 源代码并了解其与 OWL 的工作方式:
|
||||
|
||||
## 访问 CAMEL 源代码
|
||||
|
||||
```bash
|
||||
# 克隆 CAMEL 仓库
|
||||
git clone https://github.com/camel-ai/camel.git
|
||||
cd camel
|
||||
```
|
||||
|
||||
# ⭐ Star History
|
||||
|
||||
[](https://star-history.com/#camel-ai/owl&Date)
|
||||
|
||||
[docs-image]: https://img.shields.io/badge/Documentation-EB3ECC
|
||||
[docs-url]: https://camel-ai.github.io/camel/index.html
|
||||
|
||||
BIN
assets/community.jpeg
Normal file
BIN
assets/community.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 279 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 300 KiB |
175
community_usecase/COMMUNITY_CALL_FOR_USE_CASES.md
Normal file
175
community_usecase/COMMUNITY_CALL_FOR_USE_CASES.md
Normal file
@@ -0,0 +1,175 @@
|
||||
# 🦉 OWL Community Call for Use Cases
|
||||
# 🦉 OWL 社区用例征集令
|
||||
|
||||
<div align="center">
|
||||
|
||||
[![Documentation][docs-image]][docs-url]
|
||||
[![Discord][discord-image]][discord-url]
|
||||
[![X][x-image]][x-url]
|
||||
[![Reddit][reddit-image]][reddit-url]
|
||||
[![Wechat][wechat-image]][wechat-url]
|
||||
[![Star][star-image]][star-url]
|
||||
|
||||
</div>
|
||||
|
||||
<div align="center">
|
||||
<h4 align="center">
|
||||
|
||||
[English](#join-the-owl-community-contribute-your-use-cases) | [中文](#加入owl社区贡献您的用例)
|
||||
|
||||
</h4>
|
||||
</div>
|
||||
|
||||
## Join the OWL Community: Contribute Your Use Cases!
|
||||
|
||||
Dear OWL Community,
|
||||
|
||||
We are excited to announce a special initiative to expand the capabilities and applications of the OWL framework! As the #1 ranked open-source multi-agent collaboration framework on the [GAIA benchmark](https://huggingface.co/spaces/gaia-benchmark/leaderboard), OWL is revolutionizing how AI agents collaborate to solve real-world tasks.
|
||||
|
||||
### 🌟 What We're Looking For
|
||||
|
||||
We invite you to contribute use cases that demonstrate the power and versatility of OWL in two ways:
|
||||
|
||||
1. **Leverage Existing Tools and Models**: Create innovative use cases using OWL's supported tools and models, then submit a PR to our repository.
|
||||
2. **Extend OWL's Capabilities**: Develop new tools that expand OWL's functionality to implement your own unique use cases.
|
||||
|
||||
### 🏆 Community Rewards
|
||||
|
||||
The **top ten submissions** will receive:
|
||||
- Special community gifts
|
||||
- Featured promotion within the OWL community
|
||||
- Recognition of your contributions and authorship
|
||||
|
||||
### 💡 Submission Guidelines
|
||||
|
||||
Your submission should include:
|
||||
|
||||
1. **Well-documented code**: Clear comments and instructions for running your use case
|
||||
2. **Description file**: Explaining what your use case does and why it's valuable
|
||||
3. **Requirements**: Any additional dependencies needed
|
||||
4. **Example outputs**: Demonstrations of your use case in action
|
||||
|
||||
### 🔍 Evaluation Criteria
|
||||
|
||||
Submissions will be evaluated based on:
|
||||
- **Innovation**: How creative and novel is your use case?
|
||||
- **Utility**: How useful is it for real-world applications?
|
||||
- **Implementation**: How well is it coded and documented?
|
||||
- **Extensibility**: How easily can others build upon your work?
|
||||
- **Community Engagement**: Sharing your use case on social media platforms (Zhihu, Xiaohongshu, X/Twitter, YouTube, etc.) will earn you extra points
|
||||
|
||||
### 📝 How to Submit
|
||||
|
||||
1. Fork the OWL repository
|
||||
2. Create your use case in the `examples/community/` directory
|
||||
3. Submit a Pull Request with a detailed description of your contribution
|
||||
4. Tag your PR with `community-use-case`
|
||||
|
||||
### ⏰ Timeline
|
||||
|
||||
- Submission deadline: March 31, 2025
|
||||
- Winners announcement: April 7, 2025
|
||||
|
||||
### 🚀 Inspiration Areas
|
||||
|
||||
Consider exploring use cases in:
|
||||
- Data analysis and visualization
|
||||
- Content creation and summarization
|
||||
- Research assistance
|
||||
- Educational tools
|
||||
- Business process automation
|
||||
- Creative applications
|
||||
- Cross-modal interactions (text, image, audio, video)
|
||||
|
||||
### 🤝 Community Support
|
||||
|
||||
Need help or have questions? Join our community channels:
|
||||
- [Discord](https://discord.gg/CNcNpquyDc)
|
||||
- [GitHub Discussions](https://github.com/camel-ai/owl/discussions)
|
||||
|
||||
Let's build the future of multi-agent AI together!
|
||||
|
||||
---
|
||||
|
||||
## 加入OWL社区:贡献您的用例!
|
||||
|
||||
亲爱的OWL社区成员,
|
||||
|
||||
我们很高兴宣布一项特别计划,旨在扩展OWL框架的功能和应用!作为在[GAIA基准测试](https://huggingface.co/spaces/gaia-benchmark/leaderboard)中排名第一的开源多智能体协作框架,OWL正在彻底改变AI智能体协作解决现实任务的方式。
|
||||
|
||||
### 🌟 我们在寻找什么
|
||||
|
||||
我们邀请您通过以下两种方式贡献展示OWL强大功能和多样性的用例:
|
||||
|
||||
1. **利用现有工具和模型**:使用OWL支持的工具和模型创建创新用例,然后向我们的仓库提交PR。
|
||||
2. **扩展OWL的功能**:开发新工具,扩展OWL的功能,实现您自己独特的用例。
|
||||
|
||||
### 🏆 社区奖励
|
||||
|
||||
**前十名**将获得:
|
||||
- 特别社区礼物
|
||||
- 在OWL社区内的推广展示
|
||||
- 对您贡献和作者身份的认可
|
||||
|
||||
### 💡 提交指南
|
||||
|
||||
您的提交应包括:
|
||||
|
||||
1. **文档完善的代码**:清晰的注释和运行用例的说明
|
||||
2. **描述文件**:解释您的用例做什么以及为什么它有价值
|
||||
3. **依赖要求**:需要的任何额外依赖
|
||||
4. **示例输出**:展示您的用例实际运行效果
|
||||
|
||||
### 🔍 评估标准
|
||||
|
||||
提交将基于以下标准进行评估:
|
||||
- **创新性**:您的用例有多创新和新颖?
|
||||
- **实用性**:它对现实世界应用有多大用处?
|
||||
- **实现质量**:代码和文档的质量如何?
|
||||
- **可扩展性**:其他人能多容易地在您的工作基础上进行扩展?
|
||||
- **社区参与度**:在社交媒体平台(知乎、小红书、X/Twitter、YouTube等)分享您的用例将获得额外加分
|
||||
|
||||
### 📝 如何提交
|
||||
|
||||
1. Fork OWL仓库
|
||||
2. 在`community_usecase/`目录中创建您的用例
|
||||
3. 提交一个包含您贡献详细描述的Pull Request
|
||||
4. 使用`community-use-case`标签标记您的PR
|
||||
|
||||
### ⏰ 时间线
|
||||
|
||||
- 提交截止日期:2025年3月31日
|
||||
- 获奖者公布:2025年4月7日
|
||||
|
||||
### 🚀 灵感领域
|
||||
|
||||
考虑探索以下领域的用例:
|
||||
- 数据分析和可视化
|
||||
- 内容创建和摘要
|
||||
- 研究辅助
|
||||
- 教育工具
|
||||
- 业务流程自动化
|
||||
- 创意应用
|
||||
- 跨模态交互(文本、图像、音频、视频)
|
||||
|
||||
### 🤝 社区支持
|
||||
|
||||
需要帮助或有问题?加入我们的社区渠道:
|
||||
- [Discord](https://discord.gg/CNcNpquyDc)
|
||||
- [GitHub讨论](https://github.com/camel-ai/owl/discussions)
|
||||
|
||||
让我们一起构建多智能体AI的未来!
|
||||
|
||||
<!-- Links and badges -->
|
||||
[docs-image]: https://img.shields.io/badge/docs-OWL-blue
|
||||
[docs-url]: https://docs.camel-ai.org/
|
||||
[discord-image]: https://img.shields.io/discord/1135106975706013747?color=7289da&label=Discord&logo=discord&logoColor=white
|
||||
[discord-url]: https://discord.gg/CNcNpquyDc
|
||||
[x-image]: https://img.shields.io/badge/Twitter-black?logo=x
|
||||
[x-url]: https://twitter.com/CamelAIOrg
|
||||
[reddit-image]: https://img.shields.io/badge/Reddit-FF4500?logo=reddit&logoColor=white
|
||||
[reddit-url]: https://www.reddit.com/r/camelai/
|
||||
[wechat-image]: https://img.shields.io/badge/WeChat-07C160?logo=wechat&logoColor=white
|
||||
[wechat-url]: https://docs.camel-ai.org/blog/2023/11/29/camel-wechat/
|
||||
[star-image]: https://img.shields.io/github/stars/camel-ai/owl?style=social
|
||||
[star-url]: https://github.com/camel-ai/owl
|
||||
@@ -39,43 +39,37 @@ def update_license_in_file(
|
||||
start_line_start_with: str,
|
||||
end_line_start_with: str,
|
||||
) -> bool:
|
||||
with open(
|
||||
file_path, 'r', encoding='utf-8'
|
||||
) as f: # for windows compatibility
|
||||
with open(file_path, "r", encoding="utf-8") as f: # for windows compatibility
|
||||
content = f.read()
|
||||
|
||||
with open(license_template_path, 'r', encoding='utf-8') as f:
|
||||
with open(license_template_path, "r", encoding="utf-8") as f:
|
||||
new_license = f.read().strip()
|
||||
|
||||
maybe_existing_licenses = re.findall(
|
||||
r'^#.*?(?=\n)', content, re.MULTILINE | re.DOTALL
|
||||
r"^#.*?(?=\n)", content, re.MULTILINE | re.DOTALL
|
||||
)
|
||||
start_index = fine_license_start_line(
|
||||
maybe_existing_licenses, start_line_start_with
|
||||
)
|
||||
end_index = find_license_end_line(
|
||||
maybe_existing_licenses, end_line_start_with
|
||||
)
|
||||
end_index = find_license_end_line(maybe_existing_licenses, end_line_start_with)
|
||||
if start_index is not None and end_index is not None:
|
||||
maybe_existing_licenses = maybe_existing_licenses[
|
||||
start_index : end_index + 1
|
||||
]
|
||||
maybe_existing_licenses = maybe_existing_licenses[start_index : end_index + 1]
|
||||
else:
|
||||
maybe_existing_licenses = None
|
||||
if maybe_existing_licenses:
|
||||
maybe_old_licenses = '\n'.join(maybe_existing_licenses)
|
||||
maybe_old_licenses = "\n".join(maybe_existing_licenses)
|
||||
if maybe_old_licenses.strip() != new_license.strip():
|
||||
replaced_content = content.replace(maybe_old_licenses, new_license)
|
||||
with open(file_path, 'w') as f:
|
||||
with open(file_path, "w") as f:
|
||||
f.write(replaced_content)
|
||||
print(f'Replaced license in {file_path}')
|
||||
print(f"Replaced license in {file_path}")
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(new_license + '\n' + content)
|
||||
print(f'Added license to {file_path}')
|
||||
with open(file_path, "w") as f:
|
||||
f.write(new_license + "\n" + content)
|
||||
print(f"Added license to {file_path}")
|
||||
return True
|
||||
|
||||
|
||||
@@ -87,16 +81,16 @@ def update_license_in_directory(
|
||||
) -> None:
|
||||
# Check if directory exists
|
||||
if not os.path.isdir(directory_path):
|
||||
raise NotADirectoryError(f'{directory_path} is not a directory')
|
||||
raise NotADirectoryError(f"{directory_path} is not a directory")
|
||||
# Check if license template exists
|
||||
if not os.path.isfile(license_template_path):
|
||||
raise FileNotFoundError(f'{license_template_path} not found')
|
||||
raise FileNotFoundError(f"{license_template_path} not found")
|
||||
|
||||
file_count = 0
|
||||
for py_files in Path(directory_path).rglob("*.py"):
|
||||
if py_files.name.startswith('.'):
|
||||
if py_files.name.startswith("."):
|
||||
continue
|
||||
if any(part.startswith('.') for part in py_files.parts):
|
||||
if any(part.startswith(".") for part in py_files.parts):
|
||||
continue
|
||||
if update_license_in_file(
|
||||
py_files,
|
||||
@@ -106,10 +100,10 @@ def update_license_in_directory(
|
||||
):
|
||||
file_count += 1
|
||||
|
||||
print(f'License updated in {file_count} files')
|
||||
print(f"License updated in {file_count} files")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 3:
|
||||
print(
|
||||
"Usage from command line: "
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
# MODEL & API (See https://github.com/camel-ai/camel/blob/master/camel/types/enums.py)
|
||||
# MODEL & API (See https://docs.camel-ai.org/key_modules/models.html#)
|
||||
|
||||
# OPENAI API
|
||||
OPENAI_API_KEY = ""
|
||||
# OPENAI_API_BASE_URL = ""
|
||||
# OPENAI_API_KEY= ""
|
||||
# OPENAI_API_BASE_URL=""
|
||||
|
||||
# Azure OpenAI API
|
||||
# AZURE_OPENAI_BASE_URL=""
|
||||
# AZURE_API_VERSION=""
|
||||
# AZURE_OPENAI_API_KEY=""
|
||||
# AZURE_DEPLOYMENT_NAME=""
|
||||
|
||||
|
||||
# Qwen API (https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key)
|
||||
# QWEN_API_KEY=""
|
||||
@@ -26,3 +33,4 @@ CHUNKR_API_KEY=""
|
||||
|
||||
# Firecrawl API (https://www.firecrawl.dev/)
|
||||
FIRECRAWL_API_KEY=""
|
||||
#FIRECRAWL_API_URL="https://api.firecrawl.dev"
|
||||
921
owl/app.py
Normal file
921
owl/app.py
Normal file
@@ -0,0 +1,921 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import os
|
||||
import sys
|
||||
import gradio as gr
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
import queue
|
||||
from pathlib import Path
|
||||
import json
|
||||
import signal
|
||||
import dotenv
|
||||
|
||||
# 设置日志队列
|
||||
log_queue: queue.Queue[str] = queue.Queue()
|
||||
|
||||
# 当前运行的进程
|
||||
current_process = None
|
||||
process_lock = threading.Lock()
|
||||
|
||||
# 脚本选项
|
||||
SCRIPTS = {
|
||||
"Qwen Mini (中文)": "run_qwen_mini_zh.py",
|
||||
"Qwen (中文)": "run_qwen_zh.py",
|
||||
"Mini": "run_mini.py",
|
||||
"DeepSeek (中文)": "run_deepseek_zh.py",
|
||||
"Default": "run.py",
|
||||
"GAIA Roleplaying": "run_gaia_roleplaying.py",
|
||||
"OpenAI Compatible": "run_openai_compatiable_model.py",
|
||||
"Azure OpenAI": "run_azure_openai.py",
|
||||
"Ollama": "run_ollama.py",
|
||||
"Terminal": "run_terminal_zh.py",
|
||||
}
|
||||
|
||||
# 脚本描述
|
||||
SCRIPT_DESCRIPTIONS = {
|
||||
"Qwen Mini (中文)": "使用阿里云Qwen模型的中文版本,适合中文问答和任务",
|
||||
"Qwen (中文)": "使用阿里云Qwen模型,支持多种工具和功能",
|
||||
"Mini": "轻量级版本,使用OpenAI GPT-4o模型",
|
||||
"DeepSeek (中文)": "使用DeepSeek模型,适合非多模态任务",
|
||||
"Default": "默认OWL实现,使用OpenAI GPT-4o模型和全套工具",
|
||||
"GAIA Roleplaying": "GAIA基准测试实现,用于评估模型能力",
|
||||
"OpenAI Compatible": "使用兼容OpenAI API的第三方模型,支持自定义API端点",
|
||||
"Azure OpenAI": "使用Azure OpenAI API",
|
||||
"Ollama": "使用Ollama API",
|
||||
"Terminal": "使用本地终端执行python文件",
|
||||
}
|
||||
|
||||
# 环境变量分组
|
||||
ENV_GROUPS = {
|
||||
"模型API": [
|
||||
{
|
||||
"name": "OPENAI_API_KEY",
|
||||
"label": "OpenAI API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "OpenAI API密钥,用于访问GPT模型。获取方式:https://platform.openai.com/api-keys",
|
||||
},
|
||||
{
|
||||
"name": "OPENAI_API_BASE_URL",
|
||||
"label": "OpenAI API基础URL",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "OpenAI API的基础URL,可选。如果使用代理或自定义端点,请设置此项。",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_KEY",
|
||||
"label": "Azure OpenAI API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI API密钥,用于访问Azure部署的GPT模型",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_ENDPOINT",
|
||||
"label": "Azure OpenAI端点",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI服务的端点URL",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_DEPLOYMENT_NAME",
|
||||
"label": "Azure OpenAI部署名称",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI服务的部署名称",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_VERSION",
|
||||
"label": "Azure OpenAI API版本",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI API版本,例如:2023-12-01-preview",
|
||||
},
|
||||
{
|
||||
"name": "QWEN_API_KEY",
|
||||
"label": "阿里云Qwen API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "阿里云Qwen API密钥,用于访问Qwen模型。获取方式:https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key",
|
||||
},
|
||||
{
|
||||
"name": "DEEPSEEK_API_KEY",
|
||||
"label": "DeepSeek API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "DeepSeek API密钥,用于访问DeepSeek模型。获取方式:https://platform.deepseek.com/api_keys",
|
||||
},
|
||||
],
|
||||
"搜索工具": [
|
||||
{
|
||||
"name": "GOOGLE_API_KEY",
|
||||
"label": "Google API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Google搜索API密钥,用于网络搜索功能。获取方式:https://developers.google.com/custom-search/v1/overview",
|
||||
},
|
||||
{
|
||||
"name": "SEARCH_ENGINE_ID",
|
||||
"label": "搜索引擎ID",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Google自定义搜索引擎ID,与Google API密钥配合使用。获取方式:https://developers.google.com/custom-search/v1/overview",
|
||||
},
|
||||
],
|
||||
"其他工具": [
|
||||
{
|
||||
"name": "HF_TOKEN",
|
||||
"label": "Hugging Face令牌",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Hugging Face API令牌,用于访问Hugging Face模型和数据集。获取方式:https://huggingface.co/join",
|
||||
},
|
||||
{
|
||||
"name": "CHUNKR_API_KEY",
|
||||
"label": "Chunkr API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Chunkr API密钥,用于文档处理功能。获取方式:https://chunkr.ai/",
|
||||
},
|
||||
{
|
||||
"name": "FIRECRAWL_API_KEY",
|
||||
"label": "Firecrawl API密钥",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Firecrawl API密钥,用于网页爬取功能。获取方式:https://www.firecrawl.dev/",
|
||||
},
|
||||
],
|
||||
"自定义环境变量": [], # 用户自定义的环境变量将存储在这里
|
||||
}
|
||||
|
||||
|
||||
def get_script_info(script_name):
|
||||
"""获取脚本的详细信息"""
|
||||
return SCRIPT_DESCRIPTIONS.get(script_name, "无描述信息")
|
||||
|
||||
|
||||
def load_env_vars():
|
||||
"""加载环境变量"""
|
||||
env_vars = {}
|
||||
# 尝试从.env文件加载
|
||||
dotenv.load_dotenv()
|
||||
|
||||
# 获取所有环境变量
|
||||
for group in ENV_GROUPS.values():
|
||||
for var in group:
|
||||
env_vars[var["name"]] = os.environ.get(var["name"], "")
|
||||
|
||||
# 加载.env文件中可能存在的其他环境变量
|
||||
if Path(".env").exists():
|
||||
try:
|
||||
with open(".env", "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
try:
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
|
||||
# 处理引号包裹的值
|
||||
if (value.startswith('"') and value.endswith('"')) or (
|
||||
value.startswith("'") and value.endswith("'")
|
||||
):
|
||||
value = value[1:-1] # 移除首尾的引号
|
||||
|
||||
# 检查是否是已知的环境变量
|
||||
known_var = False
|
||||
for group in ENV_GROUPS.values():
|
||||
if any(var["name"] == key for var in group):
|
||||
known_var = True
|
||||
break
|
||||
|
||||
# 如果不是已知的环境变量,添加到自定义环境变量组
|
||||
if not known_var and key not in env_vars:
|
||||
ENV_GROUPS["自定义环境变量"].append(
|
||||
{
|
||||
"name": key,
|
||||
"label": key,
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "用户自定义环境变量",
|
||||
}
|
||||
)
|
||||
env_vars[key] = value
|
||||
except Exception as e:
|
||||
print(f"解析环境变量行时出错: {line}, 错误: {str(e)}")
|
||||
except Exception as e:
|
||||
print(f"加载.env文件时出错: {str(e)}")
|
||||
|
||||
return env_vars
|
||||
|
||||
|
||||
def save_env_vars(env_vars):
|
||||
"""保存环境变量到.env文件"""
|
||||
# 读取现有的.env文件内容
|
||||
env_path = Path(".env")
|
||||
existing_content = {}
|
||||
|
||||
if env_path.exists():
|
||||
try:
|
||||
with open(env_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
try:
|
||||
key, value = line.split("=", 1)
|
||||
existing_content[key.strip()] = value.strip()
|
||||
except Exception as e:
|
||||
print(f"解析环境变量行时出错: {line}, 错误: {str(e)}")
|
||||
except Exception as e:
|
||||
print(f"读取.env文件时出错: {str(e)}")
|
||||
|
||||
# 更新环境变量
|
||||
for key, value in env_vars.items():
|
||||
if value is not None: # 允许空字符串值,但不允许None
|
||||
# 确保值是字符串形式
|
||||
value = str(value) # 确保值是字符串
|
||||
|
||||
# 检查值是否已经被引号包裹
|
||||
if (value.startswith('"') and value.endswith('"')) or (
|
||||
value.startswith("'") and value.endswith("'")
|
||||
):
|
||||
# 已经被引号包裹,保持原样
|
||||
existing_content[key] = value
|
||||
# 更新环境变量时移除引号
|
||||
os.environ[key] = value[1:-1]
|
||||
else:
|
||||
# 没有被引号包裹,添加双引号
|
||||
# 用双引号包裹值,确保特殊字符被正确处理
|
||||
quoted_value = f'"{value}"'
|
||||
existing_content[key] = quoted_value
|
||||
# 同时更新当前进程的环境变量(使用未引用的值)
|
||||
os.environ[key] = value
|
||||
|
||||
# 写入.env文件
|
||||
try:
|
||||
with open(env_path, "w", encoding="utf-8") as f:
|
||||
for key, value in existing_content.items():
|
||||
f.write(f"{key}={value}\n")
|
||||
except Exception as e:
|
||||
print(f"写入.env文件时出错: {str(e)}")
|
||||
return f"❌ 保存环境变量失败: {str(e)}"
|
||||
|
||||
return "✅ 环境变量已保存"
|
||||
|
||||
|
||||
def add_custom_env_var(name, value, var_type):
|
||||
"""添加自定义环境变量"""
|
||||
if not name:
|
||||
return "❌ 环境变量名不能为空", None
|
||||
|
||||
# 检查是否已存在同名环境变量
|
||||
for group in ENV_GROUPS.values():
|
||||
if any(var["name"] == name for var in group):
|
||||
return f"❌ 环境变量 {name} 已存在", None
|
||||
|
||||
# 添加到自定义环境变量组
|
||||
ENV_GROUPS["自定义环境变量"].append(
|
||||
{
|
||||
"name": name,
|
||||
"label": name,
|
||||
"type": var_type,
|
||||
"required": False,
|
||||
"help": "用户自定义环境变量",
|
||||
}
|
||||
)
|
||||
|
||||
# 保存环境变量
|
||||
env_vars = {name: value}
|
||||
save_env_vars(env_vars)
|
||||
|
||||
# 返回成功消息和更新后的环境变量组
|
||||
return f"✅ 已添加环境变量 {name}", ENV_GROUPS["自定义环境变量"]
|
||||
|
||||
|
||||
def update_custom_env_var(name, value, var_type):
|
||||
"""更改自定义环境变量"""
|
||||
if not name:
|
||||
return "❌ 环境变量名不能为空", None
|
||||
|
||||
# 检查环境变量是否存在于自定义环境变量组中
|
||||
found = False
|
||||
for i, var in enumerate(ENV_GROUPS["自定义环境变量"]):
|
||||
if var["name"] == name:
|
||||
# 更新类型
|
||||
ENV_GROUPS["自定义环境变量"][i]["type"] = var_type
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
return f"❌ 自定义环境变量 {name} 不存在", None
|
||||
|
||||
# 保存环境变量值
|
||||
env_vars = {name: value}
|
||||
save_env_vars(env_vars)
|
||||
|
||||
# 返回成功消息和更新后的环境变量组
|
||||
return f"✅ 已更新环境变量 {name}", ENV_GROUPS["自定义环境变量"]
|
||||
|
||||
|
||||
def delete_custom_env_var(name):
|
||||
"""删除自定义环境变量"""
|
||||
if not name:
|
||||
return "❌ 环境变量名不能为空", None
|
||||
|
||||
# 检查环境变量是否存在于自定义环境变量组中
|
||||
found = False
|
||||
for i, var in enumerate(ENV_GROUPS["自定义环境变量"]):
|
||||
if var["name"] == name:
|
||||
# 从自定义环境变量组中删除
|
||||
del ENV_GROUPS["自定义环境变量"][i]
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
return f"❌ 自定义环境变量 {name} 不存在", None
|
||||
|
||||
# 从.env文件中删除该环境变量
|
||||
env_path = Path(".env")
|
||||
if env_path.exists():
|
||||
try:
|
||||
with open(env_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
with open(env_path, "w", encoding="utf-8") as f:
|
||||
for line in lines:
|
||||
try:
|
||||
# 更精确地匹配环境变量行
|
||||
line_stripped = line.strip()
|
||||
# 检查是否为注释行或空行
|
||||
if not line_stripped or line_stripped.startswith("#"):
|
||||
f.write(line) # 保留注释行和空行
|
||||
continue
|
||||
|
||||
# 检查是否包含等号
|
||||
if "=" not in line_stripped:
|
||||
f.write(line) # 保留不包含等号的行
|
||||
continue
|
||||
|
||||
# 提取变量名并检查是否与要删除的变量匹配
|
||||
var_name = line_stripped.split("=", 1)[0].strip()
|
||||
if var_name != name:
|
||||
f.write(line) # 保留不匹配的变量
|
||||
except Exception as e:
|
||||
print(f"处理.env文件行时出错: {line}, 错误: {str(e)}")
|
||||
# 出错时保留原行
|
||||
f.write(line)
|
||||
except Exception as e:
|
||||
print(f"删除环境变量时出错: {str(e)}")
|
||||
return f"❌ 删除环境变量失败: {str(e)}", None
|
||||
|
||||
# 从当前进程的环境变量中删除
|
||||
if name in os.environ:
|
||||
del os.environ[name]
|
||||
|
||||
# 返回成功消息和更新后的环境变量组
|
||||
return f"✅ 已删除环境变量 {name}", ENV_GROUPS["自定义环境变量"]
|
||||
|
||||
|
||||
def terminate_process():
|
||||
"""终止当前运行的进程"""
|
||||
global current_process
|
||||
|
||||
with process_lock:
|
||||
if current_process is not None and current_process.poll() is None:
|
||||
try:
|
||||
# 在Windows上使用taskkill强制终止进程树
|
||||
if os.name == "nt":
|
||||
# 获取进程ID
|
||||
pid = current_process.pid
|
||||
# 使用taskkill命令终止进程及其子进程 - 避免使用shell=True以提高安全性
|
||||
try:
|
||||
subprocess.run(
|
||||
["taskkill", "/F", "/T", "/PID", str(pid)], check=False
|
||||
)
|
||||
except subprocess.SubprocessError as e:
|
||||
log_queue.put(f"终止进程时出错: {str(e)}\n")
|
||||
return f"❌ 终止进程时出错: {str(e)}"
|
||||
else:
|
||||
# 在Unix上使用SIGTERM和SIGKILL
|
||||
current_process.terminate()
|
||||
try:
|
||||
current_process.wait(timeout=3)
|
||||
except subprocess.TimeoutExpired:
|
||||
current_process.kill()
|
||||
|
||||
# 等待进程终止
|
||||
try:
|
||||
current_process.wait(timeout=2)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass # 已经尝试强制终止,忽略超时
|
||||
|
||||
log_queue.put("进程已终止\n")
|
||||
return "✅ 进程已终止"
|
||||
except Exception as e:
|
||||
log_queue.put(f"终止进程时出错: {str(e)}\n")
|
||||
return f"❌ 终止进程时出错: {str(e)}"
|
||||
else:
|
||||
return "❌ 没有正在运行的进程"
|
||||
|
||||
|
||||
def run_script(script_dropdown, question, progress=gr.Progress()):
|
||||
"""运行选定的脚本并返回输出"""
|
||||
global current_process
|
||||
|
||||
script_name = SCRIPTS.get(script_dropdown)
|
||||
if not script_name:
|
||||
return "❌ 无效的脚本选择", "", "", "", None
|
||||
|
||||
if not question.strip():
|
||||
return "请输入问题!", "", "", "", None
|
||||
|
||||
# 清空日志队列
|
||||
while not log_queue.empty():
|
||||
log_queue.get()
|
||||
|
||||
# 创建日志目录
|
||||
log_dir = Path("logs")
|
||||
log_dir.mkdir(exist_ok=True)
|
||||
|
||||
# 创建带时间戳的日志文件
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
log_file = log_dir / f"{script_name.replace('.py', '')}_{timestamp}.log"
|
||||
|
||||
# 构建命令
|
||||
# 获取当前脚本所在的基础路径
|
||||
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
cmd = [
|
||||
sys.executable,
|
||||
os.path.join(base_path, "owl", "script_adapter.py"),
|
||||
os.path.join(base_path, "owl", script_name),
|
||||
]
|
||||
|
||||
# 创建环境变量副本并添加问题
|
||||
env = os.environ.copy()
|
||||
# 确保问题是字符串类型
|
||||
if not isinstance(question, str):
|
||||
question = str(question)
|
||||
# 保留换行符,但确保是有效的字符串
|
||||
env["OWL_QUESTION"] = question
|
||||
|
||||
# 启动进程
|
||||
with process_lock:
|
||||
current_process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
env=env,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
# 创建线程来读取输出
|
||||
def read_output():
|
||||
try:
|
||||
# 使用唯一的时间戳确保日志文件名不重复
|
||||
timestamp_unique = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
||||
unique_log_file = (
|
||||
log_dir / f"{script_name.replace('.py', '')}_{timestamp_unique}.log"
|
||||
)
|
||||
|
||||
# 使用这个唯一的文件名写入日志
|
||||
with open(unique_log_file, "w", encoding="utf-8") as f:
|
||||
# 更新全局日志文件路径
|
||||
nonlocal log_file
|
||||
log_file = unique_log_file
|
||||
|
||||
for line in iter(current_process.stdout.readline, ""):
|
||||
if line:
|
||||
# 写入日志文件
|
||||
f.write(line)
|
||||
f.flush()
|
||||
# 添加到队列
|
||||
log_queue.put(line)
|
||||
except Exception as e:
|
||||
log_queue.put(f"读取输出时出错: {str(e)}\n")
|
||||
|
||||
# 启动读取线程
|
||||
threading.Thread(target=read_output, daemon=True).start()
|
||||
|
||||
# 收集日志
|
||||
logs = []
|
||||
progress(0, desc="正在运行...")
|
||||
|
||||
# 等待进程完成或超时
|
||||
start_time = time.time()
|
||||
timeout = 1800 # 30分钟超时
|
||||
|
||||
while current_process.poll() is None:
|
||||
# 检查是否超时
|
||||
if time.time() - start_time > timeout:
|
||||
with process_lock:
|
||||
if current_process.poll() is None:
|
||||
if os.name == "nt":
|
||||
current_process.send_signal(signal.CTRL_BREAK_EVENT)
|
||||
else:
|
||||
current_process.terminate()
|
||||
log_queue.put("执行超时,已终止进程\n")
|
||||
break
|
||||
|
||||
# 从队列获取日志
|
||||
while not log_queue.empty():
|
||||
log = log_queue.get()
|
||||
logs.append(log)
|
||||
|
||||
# 更新进度
|
||||
elapsed = time.time() - start_time
|
||||
progress(min(elapsed / 300, 0.99), desc="正在运行...")
|
||||
|
||||
# 短暂休眠以减少CPU使用
|
||||
time.sleep(0.1)
|
||||
|
||||
# 每秒更新一次日志显示
|
||||
yield (
|
||||
status_message(current_process),
|
||||
extract_answer(logs),
|
||||
"".join(logs),
|
||||
str(log_file),
|
||||
None,
|
||||
)
|
||||
|
||||
# 获取剩余日志
|
||||
while not log_queue.empty():
|
||||
logs.append(log_queue.get())
|
||||
|
||||
# 提取聊天历史(如果有)
|
||||
chat_history = extract_chat_history(logs)
|
||||
|
||||
# 返回最终状态和日志
|
||||
return (
|
||||
status_message(current_process),
|
||||
extract_answer(logs),
|
||||
"".join(logs),
|
||||
str(log_file),
|
||||
chat_history,
|
||||
)
|
||||
|
||||
|
||||
def status_message(process):
|
||||
"""根据进程状态返回状态消息"""
|
||||
if process.poll() is None:
|
||||
return "⏳ 正在运行..."
|
||||
elif process.returncode == 0:
|
||||
return "✅ 执行成功"
|
||||
else:
|
||||
return f"❌ 执行失败 (返回码: {process.returncode})"
|
||||
|
||||
|
||||
def extract_answer(logs):
|
||||
"""从日志中提取答案"""
|
||||
answer = ""
|
||||
for log in logs:
|
||||
if "Answer:" in log:
|
||||
answer = log.split("Answer:", 1)[1].strip()
|
||||
break
|
||||
return answer
|
||||
|
||||
|
||||
def extract_chat_history(logs):
|
||||
"""尝试从日志中提取聊天历史"""
|
||||
try:
|
||||
chat_json_str = ""
|
||||
capture_json = False
|
||||
|
||||
for log in logs:
|
||||
if "chat_history" in log:
|
||||
# 开始捕获JSON
|
||||
start_idx = log.find("[")
|
||||
if start_idx != -1:
|
||||
capture_json = True
|
||||
chat_json_str = log[start_idx:]
|
||||
elif capture_json:
|
||||
# 继续捕获JSON直到找到匹配的结束括号
|
||||
chat_json_str += log
|
||||
if "]" in log:
|
||||
# 找到结束括号,尝试解析JSON
|
||||
end_idx = chat_json_str.rfind("]") + 1
|
||||
if end_idx > 0:
|
||||
try:
|
||||
# 清理可能的额外文本
|
||||
json_str = chat_json_str[:end_idx].strip()
|
||||
chat_data = json.loads(json_str)
|
||||
|
||||
# 格式化为Gradio聊天组件可用的格式
|
||||
formatted_chat = []
|
||||
for msg in chat_data:
|
||||
if "role" in msg and "content" in msg:
|
||||
role = "用户" if msg["role"] == "user" else "助手"
|
||||
formatted_chat.append([role, msg["content"]])
|
||||
return formatted_chat
|
||||
except json.JSONDecodeError:
|
||||
# 如果解析失败,继续捕获
|
||||
pass
|
||||
except Exception:
|
||||
# 其他错误,停止捕获
|
||||
capture_json = False
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def create_ui():
|
||||
"""创建Gradio界面"""
|
||||
# 加载环境变量
|
||||
env_vars = load_env_vars()
|
||||
|
||||
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
|
||||
gr.Markdown(
|
||||
"""
|
||||
# 🦉 OWL 智能助手运行平台
|
||||
|
||||
选择一个模型并输入您的问题,系统将运行相应的脚本并显示结果。
|
||||
"""
|
||||
)
|
||||
|
||||
with gr.Tabs():
|
||||
with gr.TabItem("运行模式"):
|
||||
with gr.Row():
|
||||
with gr.Column(scale=1):
|
||||
# 确保默认值是SCRIPTS中存在的键
|
||||
default_script = list(SCRIPTS.keys())[0] if SCRIPTS else None
|
||||
script_dropdown = gr.Dropdown(
|
||||
choices=list(SCRIPTS.keys()),
|
||||
value=default_script,
|
||||
label="选择模式",
|
||||
)
|
||||
|
||||
script_info = gr.Textbox(
|
||||
value=get_script_info(default_script)
|
||||
if default_script
|
||||
else "",
|
||||
label="模型描述",
|
||||
interactive=False,
|
||||
)
|
||||
|
||||
script_dropdown.change(
|
||||
fn=lambda x: get_script_info(x),
|
||||
inputs=script_dropdown,
|
||||
outputs=script_info,
|
||||
)
|
||||
|
||||
question_input = gr.Textbox(
|
||||
lines=8,
|
||||
placeholder="请输入您的问题...",
|
||||
label="问题",
|
||||
elem_id="question_input",
|
||||
show_copy_button=True,
|
||||
)
|
||||
|
||||
gr.Markdown(
|
||||
"""
|
||||
> **注意**: 您输入的问题将替换脚本中的默认问题。系统会自动处理问题的替换,确保您的问题被正确使用。
|
||||
> 支持多行输入,换行将被保留。
|
||||
"""
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
run_button = gr.Button("运行", variant="primary")
|
||||
stop_button = gr.Button("终止", variant="stop")
|
||||
|
||||
with gr.Column(scale=2):
|
||||
with gr.Tabs():
|
||||
with gr.TabItem("结果"):
|
||||
status_output = gr.Textbox(label="状态")
|
||||
answer_output = gr.Textbox(label="回答", lines=10)
|
||||
log_file_output = gr.Textbox(label="日志文件路径")
|
||||
|
||||
with gr.TabItem("运行日志"):
|
||||
log_output = gr.Textbox(label="完整日志", lines=25)
|
||||
|
||||
with gr.TabItem("聊天历史"):
|
||||
chat_output = gr.Chatbot(label="对话历史")
|
||||
|
||||
# 示例问题
|
||||
examples = [
|
||||
[
|
||||
"Qwen Mini (中文)",
|
||||
"浏览亚马逊并找出一款对程序员有吸引力的产品。请提供产品名称和价格",
|
||||
],
|
||||
[
|
||||
"DeepSeek (中文)",
|
||||
"请分析GitHub上CAMEL-AI项目的最新统计数据。找出该项目的星标数量、贡献者数量和最近的活跃度。然后,创建一个简单的Excel表格来展示这些数据,并生成一个柱状图来可视化这些指标。最后,总结CAMEL项目的受欢迎程度和发展趋势。",
|
||||
],
|
||||
[
|
||||
"Default",
|
||||
"Navigate to Amazon.com and identify one product that is attractive to coders. Please provide me with the product name and price. No need to verify your answer.",
|
||||
],
|
||||
]
|
||||
|
||||
gr.Examples(examples=examples, inputs=[script_dropdown, question_input])
|
||||
|
||||
with gr.TabItem("环境变量配置"):
|
||||
env_inputs = {}
|
||||
save_status = gr.Textbox(label="保存状态", interactive=False)
|
||||
|
||||
# 添加自定义环境变量部分
|
||||
with gr.Accordion("添加自定义环境变量", open=True):
|
||||
with gr.Row():
|
||||
new_var_name = gr.Textbox(
|
||||
label="环境变量名", placeholder="例如:MY_CUSTOM_API_KEY"
|
||||
)
|
||||
new_var_value = gr.Textbox(
|
||||
label="环境变量值", placeholder="输入值"
|
||||
)
|
||||
new_var_type = gr.Dropdown(
|
||||
choices=["text", "password"], value="text", label="类型"
|
||||
)
|
||||
|
||||
add_var_button = gr.Button("添加环境变量", variant="primary")
|
||||
add_var_status = gr.Textbox(label="添加状态", interactive=False)
|
||||
|
||||
# 自定义环境变量列表
|
||||
custom_vars_list = gr.JSON(
|
||||
value=ENV_GROUPS["自定义环境变量"],
|
||||
label="已添加的自定义环境变量",
|
||||
visible=len(ENV_GROUPS["自定义环境变量"]) > 0,
|
||||
)
|
||||
|
||||
# 更改和删除自定义环境变量部分
|
||||
with gr.Accordion(
|
||||
"更改或删除自定义环境变量",
|
||||
open=True,
|
||||
visible=len(ENV_GROUPS["自定义环境变量"]) > 0,
|
||||
) as update_delete_accordion:
|
||||
with gr.Row():
|
||||
# 创建下拉菜单,显示所有自定义环境变量
|
||||
custom_var_dropdown = gr.Dropdown(
|
||||
choices=[
|
||||
var["name"] for var in ENV_GROUPS["自定义环境变量"]
|
||||
],
|
||||
label="选择环境变量",
|
||||
interactive=True,
|
||||
)
|
||||
update_var_value = gr.Textbox(
|
||||
label="新的环境变量值", placeholder="输入新值"
|
||||
)
|
||||
update_var_type = gr.Dropdown(
|
||||
choices=["text", "password"], value="text", label="类型"
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
update_var_button = gr.Button("更新环境变量", variant="primary")
|
||||
delete_var_button = gr.Button("删除环境变量", variant="stop")
|
||||
|
||||
update_var_status = gr.Textbox(label="操作状态", interactive=False)
|
||||
|
||||
# 添加环境变量按钮点击事件
|
||||
add_var_button.click(
|
||||
fn=add_custom_env_var,
|
||||
inputs=[new_var_name, new_var_value, new_var_type],
|
||||
outputs=[add_var_status, custom_vars_list],
|
||||
).then(
|
||||
fn=lambda vars: {"visible": len(vars) > 0},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[update_delete_accordion],
|
||||
)
|
||||
|
||||
# 更新环境变量按钮点击事件
|
||||
update_var_button.click(
|
||||
fn=update_custom_env_var,
|
||||
inputs=[custom_var_dropdown, update_var_value, update_var_type],
|
||||
outputs=[update_var_status, custom_vars_list],
|
||||
)
|
||||
|
||||
# 删除环境变量按钮点击事件
|
||||
delete_var_button.click(
|
||||
fn=delete_custom_env_var,
|
||||
inputs=[custom_var_dropdown],
|
||||
outputs=[update_var_status, custom_vars_list],
|
||||
).then(
|
||||
fn=lambda vars: {"visible": len(vars) > 0},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[update_delete_accordion],
|
||||
)
|
||||
|
||||
# 当自定义环境变量列表更新时,更新下拉菜单选项
|
||||
custom_vars_list.change(
|
||||
fn=lambda vars: {
|
||||
"choices": [var["name"] for var in vars],
|
||||
"value": None,
|
||||
},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[custom_var_dropdown],
|
||||
)
|
||||
|
||||
# 现有环境变量配置
|
||||
for group_name, vars in ENV_GROUPS.items():
|
||||
if (
|
||||
group_name != "自定义环境变量" or len(vars) > 0
|
||||
): # 只显示非空的自定义环境变量组
|
||||
with gr.Accordion(
|
||||
group_name, open=(group_name != "自定义环境变量")
|
||||
):
|
||||
for var in vars:
|
||||
# 添加帮助信息
|
||||
gr.Markdown(f"**{var['help']}**")
|
||||
|
||||
if var["type"] == "password":
|
||||
env_inputs[var["name"]] = gr.Textbox(
|
||||
value=env_vars.get(var["name"], ""),
|
||||
label=var["label"],
|
||||
placeholder=f"请输入{var['label']}",
|
||||
type="password",
|
||||
)
|
||||
else:
|
||||
env_inputs[var["name"]] = gr.Textbox(
|
||||
value=env_vars.get(var["name"], ""),
|
||||
label=var["label"],
|
||||
placeholder=f"请输入{var['label']}",
|
||||
)
|
||||
|
||||
save_button = gr.Button("保存环境变量", variant="primary")
|
||||
|
||||
# 保存环境变量
|
||||
save_inputs = [
|
||||
env_inputs[var_name]
|
||||
for group in ENV_GROUPS.values()
|
||||
for var in group
|
||||
for var_name in [var["name"]]
|
||||
if var_name in env_inputs
|
||||
]
|
||||
save_button.click(
|
||||
fn=lambda *values: save_env_vars(
|
||||
dict(
|
||||
zip(
|
||||
[
|
||||
var["name"]
|
||||
for group in ENV_GROUPS.values()
|
||||
for var in group
|
||||
if var["name"] in env_inputs
|
||||
],
|
||||
values,
|
||||
)
|
||||
)
|
||||
),
|
||||
inputs=save_inputs,
|
||||
outputs=save_status,
|
||||
)
|
||||
|
||||
# 运行脚本
|
||||
run_button.click(
|
||||
fn=run_script,
|
||||
inputs=[script_dropdown, question_input],
|
||||
outputs=[
|
||||
status_output,
|
||||
answer_output,
|
||||
log_output,
|
||||
log_file_output,
|
||||
chat_output,
|
||||
],
|
||||
show_progress=True,
|
||||
)
|
||||
|
||||
# 终止运行
|
||||
stop_button.click(fn=terminate_process, inputs=[], outputs=[status_output])
|
||||
|
||||
# 添加页脚
|
||||
gr.Markdown(
|
||||
"""
|
||||
### 📝 使用说明
|
||||
|
||||
- 选择一个模型并输入您的问题
|
||||
- 点击"运行"按钮开始执行
|
||||
- 如需终止运行,点击"终止"按钮
|
||||
- 在"结果"标签页查看执行状态和回答
|
||||
- 在"运行日志"标签页查看完整日志
|
||||
- 在"聊天历史"标签页查看对话历史(如果有)
|
||||
- 在"环境变量配置"标签页配置API密钥和其他环境变量
|
||||
- 您可以添加自定义环境变量,满足特殊需求
|
||||
|
||||
### ⚠️ 注意事项
|
||||
|
||||
- 运行某些模型可能需要API密钥,请确保在"环境变量配置"标签页中设置了相应的环境变量
|
||||
- 某些脚本可能需要较长时间运行,请耐心等待
|
||||
- 如果运行超过30分钟,进程将自动终止
|
||||
- 您输入的问题将替换脚本中的默认问题,确保问题与所选模型兼容
|
||||
"""
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 创建并启动应用
|
||||
app = create_ui()
|
||||
app.queue().launch(share=True)
|
||||
948
owl/app_en.py
Normal file
948
owl/app_en.py
Normal file
@@ -0,0 +1,948 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import os
|
||||
import sys
|
||||
import gradio as gr
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
import queue
|
||||
from pathlib import Path
|
||||
import json
|
||||
import signal
|
||||
import dotenv
|
||||
|
||||
# Set up log queue
|
||||
log_queue: queue.Queue[str] = queue.Queue()
|
||||
|
||||
# Currently running process
|
||||
current_process = None
|
||||
process_lock = threading.Lock()
|
||||
|
||||
# Script options
|
||||
SCRIPTS = {
|
||||
"Qwen Mini (Chinese)": "run_qwen_mini_zh.py",
|
||||
"Qwen (Chinese)": "run_qwen_zh.py",
|
||||
"Mini": "run_mini.py",
|
||||
"DeepSeek (Chinese)": "run_deepseek_zh.py",
|
||||
"Default": "run.py",
|
||||
"GAIA Roleplaying": "run_gaia_roleplaying.py",
|
||||
"OpenAI Compatible": "run_openai_compatiable_model.py",
|
||||
"Azure OpenAI": "run_azure_openai.py",
|
||||
"Ollama": "run_ollama.py",
|
||||
"Terminal": "run_terminal.py",
|
||||
}
|
||||
|
||||
# Script descriptions
|
||||
SCRIPT_DESCRIPTIONS = {
|
||||
"Qwen Mini (Chinese)": "Uses the Chinese version of Alibaba Cloud's Qwen model, suitable for Chinese Q&A and tasks",
|
||||
"Qwen (Chinese)": "Uses Alibaba Cloud's Qwen model, supports various tools and functions",
|
||||
"Mini": "Lightweight version, uses OpenAI GPT-4o model",
|
||||
"DeepSeek (Chinese)": "Uses DeepSeek model, suitable for non-multimodal tasks",
|
||||
"Default": "Default OWL implementation, uses OpenAI GPT-4o model and full set of tools",
|
||||
"GAIA Roleplaying": "GAIA benchmark implementation, used to evaluate model capabilities",
|
||||
"OpenAI Compatible": "Uses third-party models compatible with OpenAI API, supports custom API endpoints",
|
||||
"Azure OpenAI": "Uses Azure OpenAI API",
|
||||
"Ollama": "Uses Ollama API",
|
||||
"Terminal": "Uses local terminal to execute python files",
|
||||
}
|
||||
|
||||
# Environment variable groups
|
||||
ENV_GROUPS = {
|
||||
"Model API": [
|
||||
{
|
||||
"name": "OPENAI_API_KEY",
|
||||
"label": "OpenAI API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "OpenAI API key for accessing GPT models. Get it from: https://platform.openai.com/api-keys",
|
||||
},
|
||||
{
|
||||
"name": "OPENAI_API_BASE_URL",
|
||||
"label": "OpenAI API Base URL",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Base URL for OpenAI API, optional. Set this if using a proxy or custom endpoint.",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_KEY",
|
||||
"label": "Azure OpenAI API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI API key for accessing Azure deployed GPT models. Get it from: https://portal.azure.com/",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_ENDPOINT",
|
||||
"label": "Azure OpenAI Endpoint",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI service endpoint URL",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_DEPLOYMENT_NAME",
|
||||
"label": "Azure OpenAI Deployment Name",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI service deployment name",
|
||||
},
|
||||
{
|
||||
"name": "AZURE_OPENAI_VERSION",
|
||||
"label": "Azure OpenAI API Version",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Azure OpenAI API version, e.g. 2023-12-01-preview",
|
||||
},
|
||||
{
|
||||
"name": "QWEN_API_KEY",
|
||||
"label": "Alibaba Cloud Qwen API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Alibaba Cloud Qwen API key for accessing Qwen models. Get it from: https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key",
|
||||
},
|
||||
{
|
||||
"name": "DEEPSEEK_API_KEY",
|
||||
"label": "DeepSeek API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "DeepSeek API key for accessing DeepSeek models. Get it from: https://platform.deepseek.com/api_keys",
|
||||
},
|
||||
],
|
||||
"Search Tools": [
|
||||
{
|
||||
"name": "GOOGLE_API_KEY",
|
||||
"label": "Google API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Google Search API key for web search functionality. Get it from: https://developers.google.com/custom-search/v1/overview",
|
||||
},
|
||||
{
|
||||
"name": "SEARCH_ENGINE_ID",
|
||||
"label": "Search Engine ID",
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "Google Custom Search Engine ID, used with Google API key. Get it from: https://developers.google.com/custom-search/v1/overview",
|
||||
},
|
||||
],
|
||||
"Other Tools": [
|
||||
{
|
||||
"name": "HF_TOKEN",
|
||||
"label": "Hugging Face Token",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Hugging Face API token for accessing Hugging Face models and datasets. Get it from: https://huggingface.co/join",
|
||||
},
|
||||
{
|
||||
"name": "CHUNKR_API_KEY",
|
||||
"label": "Chunkr API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Chunkr API key for document processing functionality. Get it from: https://chunkr.ai/",
|
||||
},
|
||||
{
|
||||
"name": "FIRECRAWL_API_KEY",
|
||||
"label": "Firecrawl API Key",
|
||||
"type": "password",
|
||||
"required": False,
|
||||
"help": "Firecrawl API key for web crawling functionality. Get it from: https://www.firecrawl.dev/",
|
||||
},
|
||||
],
|
||||
"Custom Environment Variables": [], # User-defined environment variables will be stored here
|
||||
}
|
||||
|
||||
|
||||
def get_script_info(script_name):
|
||||
"""Get detailed information about the script"""
|
||||
return SCRIPT_DESCRIPTIONS.get(script_name, "No description available")
|
||||
|
||||
|
||||
def load_env_vars():
|
||||
"""Load environment variables"""
|
||||
env_vars = {}
|
||||
# Try to load from .env file
|
||||
dotenv.load_dotenv()
|
||||
|
||||
# Get all environment variables
|
||||
for group in ENV_GROUPS.values():
|
||||
for var in group:
|
||||
env_vars[var["name"]] = os.environ.get(var["name"], "")
|
||||
|
||||
# Load other environment variables that may exist in the .env file
|
||||
if Path(".env").exists():
|
||||
try:
|
||||
with open(".env", "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
try:
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
|
||||
# Handle quoted values
|
||||
if (value.startswith('"') and value.endswith('"')) or (
|
||||
value.startswith("'") and value.endswith("'")
|
||||
):
|
||||
value = value[
|
||||
1:-1
|
||||
] # Remove quotes at the beginning and end
|
||||
|
||||
# Check if it's a known environment variable
|
||||
known_var = False
|
||||
for group in ENV_GROUPS.values():
|
||||
if any(var["name"] == key for var in group):
|
||||
known_var = True
|
||||
break
|
||||
|
||||
# If it's not a known environment variable, add it to the custom environment variables group
|
||||
if not known_var and key not in env_vars:
|
||||
ENV_GROUPS["Custom Environment Variables"].append(
|
||||
{
|
||||
"name": key,
|
||||
"label": key,
|
||||
"type": "text",
|
||||
"required": False,
|
||||
"help": "User-defined environment variable",
|
||||
}
|
||||
)
|
||||
env_vars[key] = value
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error parsing environment variable line: {line}, error: {str(e)}"
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error loading .env file: {str(e)}")
|
||||
|
||||
return env_vars
|
||||
|
||||
|
||||
def save_env_vars(env_vars):
|
||||
"""Save environment variables to .env file"""
|
||||
# Read existing .env file content
|
||||
env_path = Path(".env")
|
||||
existing_content = {}
|
||||
|
||||
if env_path.exists():
|
||||
try:
|
||||
with open(env_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
try:
|
||||
key, value = line.split("=", 1)
|
||||
existing_content[key.strip()] = value.strip()
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error parsing environment variable line: {line}, error: {str(e)}"
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error reading .env file: {str(e)}")
|
||||
|
||||
# Update environment variables
|
||||
for key, value in env_vars.items():
|
||||
if value is not None: # Allow empty string values, but not None
|
||||
# Ensure the value is a string
|
||||
value = str(value) # Ensure the value is a string
|
||||
|
||||
# Check if the value is already wrapped in quotes
|
||||
if (value.startswith('"') and value.endswith('"')) or (
|
||||
value.startswith("'") and value.endswith("'")
|
||||
):
|
||||
# Already wrapped in quotes, keep as is
|
||||
existing_content[key] = value
|
||||
# Update environment variable by removing quotes
|
||||
os.environ[key] = value[1:-1]
|
||||
else:
|
||||
# Not wrapped in quotes, add double quotes
|
||||
# Wrap the value in double quotes to ensure special characters are handled correctly
|
||||
quoted_value = f'"{value}"'
|
||||
existing_content[key] = quoted_value
|
||||
# Also update the environment variable for the current process (using the unquoted value)
|
||||
os.environ[key] = value
|
||||
|
||||
# Write to .env file
|
||||
try:
|
||||
with open(env_path, "w", encoding="utf-8") as f:
|
||||
for key, value in existing_content.items():
|
||||
f.write(f"{key}={value}\n")
|
||||
except Exception as e:
|
||||
print(f"Error writing to .env file: {str(e)}")
|
||||
return f"❌ Failed to save environment variables: {str(e)}"
|
||||
|
||||
return "✅ Environment variables saved"
|
||||
|
||||
|
||||
def add_custom_env_var(name, value, var_type):
|
||||
"""Add custom environment variable"""
|
||||
if not name:
|
||||
return "❌ Environment variable name cannot be empty", None
|
||||
|
||||
# Check if an environment variable with the same name already exists
|
||||
for group in ENV_GROUPS.values():
|
||||
if any(var["name"] == name for var in group):
|
||||
return f"❌ Environment variable {name} already exists", None
|
||||
|
||||
# Add to custom environment variables group
|
||||
ENV_GROUPS["Custom Environment Variables"].append(
|
||||
{
|
||||
"name": name,
|
||||
"label": name,
|
||||
"type": var_type,
|
||||
"required": False,
|
||||
"help": "User-defined environment variable",
|
||||
}
|
||||
)
|
||||
|
||||
# Save environment variables
|
||||
env_vars = {name: value}
|
||||
save_env_vars(env_vars)
|
||||
|
||||
# Return success message and updated environment variable group
|
||||
return f"✅ Added environment variable {name}", ENV_GROUPS[
|
||||
"Custom Environment Variables"
|
||||
]
|
||||
|
||||
|
||||
def update_custom_env_var(name, value, var_type):
|
||||
"""Update custom environment variable"""
|
||||
if not name:
|
||||
return "❌ Environment variable name cannot be empty", None
|
||||
|
||||
# Check if the environment variable exists in the custom environment variables group
|
||||
found = False
|
||||
for i, var in enumerate(ENV_GROUPS["Custom Environment Variables"]):
|
||||
if var["name"] == name:
|
||||
# Update type
|
||||
ENV_GROUPS["Custom Environment Variables"][i]["type"] = var_type
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
return f"❌ Custom environment variable {name} does not exist", None
|
||||
|
||||
# Save environment variable value
|
||||
env_vars = {name: value}
|
||||
save_env_vars(env_vars)
|
||||
|
||||
# Return success message and updated environment variable group
|
||||
return f"✅ Updated environment variable {name}", ENV_GROUPS[
|
||||
"Custom Environment Variables"
|
||||
]
|
||||
|
||||
|
||||
def delete_custom_env_var(name):
|
||||
"""Delete custom environment variable"""
|
||||
if not name:
|
||||
return "❌ Environment variable name cannot be empty", None
|
||||
|
||||
# Check if the environment variable exists in the custom environment variables group
|
||||
found = False
|
||||
for i, var in enumerate(ENV_GROUPS["Custom Environment Variables"]):
|
||||
if var["name"] == name:
|
||||
# Delete from custom environment variables group
|
||||
del ENV_GROUPS["Custom Environment Variables"][i]
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
return f"❌ Custom environment variable {name} does not exist", None
|
||||
|
||||
# Delete the environment variable from .env file
|
||||
env_path = Path(".env")
|
||||
if env_path.exists():
|
||||
try:
|
||||
with open(env_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
with open(env_path, "w", encoding="utf-8") as f:
|
||||
for line in lines:
|
||||
try:
|
||||
# More precisely match environment variable lines
|
||||
line_stripped = line.strip()
|
||||
# Check if it's a comment line or empty line
|
||||
if not line_stripped or line_stripped.startswith("#"):
|
||||
f.write(line) # Keep comment lines and empty lines
|
||||
continue
|
||||
|
||||
# Check if it contains an equals sign
|
||||
if "=" not in line_stripped:
|
||||
f.write(line) # Keep lines without equals sign
|
||||
continue
|
||||
|
||||
# Extract variable name and check if it matches the variable to be deleted
|
||||
var_name = line_stripped.split("=", 1)[0].strip()
|
||||
if var_name != name:
|
||||
f.write(line) # Keep variables that don't match
|
||||
except Exception as e:
|
||||
print(
|
||||
f"Error processing .env file line: {line}, error: {str(e)}"
|
||||
)
|
||||
# Keep the original line when an error occurs
|
||||
f.write(line)
|
||||
except Exception as e:
|
||||
print(f"Error deleting environment variable: {str(e)}")
|
||||
return f"❌ Failed to delete environment variable: {str(e)}", None
|
||||
|
||||
# Delete from current process environment variables
|
||||
if name in os.environ:
|
||||
del os.environ[name]
|
||||
|
||||
# Return success message and updated environment variable group
|
||||
return f"✅ Deleted environment variable {name}", ENV_GROUPS[
|
||||
"Custom Environment Variables"
|
||||
]
|
||||
|
||||
|
||||
def terminate_process():
|
||||
"""Terminate the currently running process"""
|
||||
global current_process
|
||||
|
||||
with process_lock:
|
||||
if current_process is not None and current_process.poll() is None:
|
||||
try:
|
||||
# On Windows, use taskkill to forcibly terminate the process tree
|
||||
if os.name == "nt":
|
||||
# Get process ID
|
||||
pid = current_process.pid
|
||||
# Use taskkill command to terminate the process and its children - avoid using shell=True for better security
|
||||
try:
|
||||
subprocess.run(
|
||||
["taskkill", "/F", "/T", "/PID", str(pid)], check=False
|
||||
)
|
||||
except subprocess.SubprocessError as e:
|
||||
log_queue.put(f"Error terminating process: {str(e)}\n")
|
||||
return f"❌ Error terminating process: {str(e)}"
|
||||
else:
|
||||
# On Unix, use SIGTERM and SIGKILL
|
||||
current_process.terminate()
|
||||
try:
|
||||
current_process.wait(timeout=3)
|
||||
except subprocess.TimeoutExpired:
|
||||
current_process.kill()
|
||||
|
||||
# Wait for process to terminate
|
||||
try:
|
||||
current_process.wait(timeout=2)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass # Already tried to force terminate, ignore timeout
|
||||
|
||||
log_queue.put("Process terminated\n")
|
||||
return "✅ Process terminated"
|
||||
except Exception as e:
|
||||
log_queue.put(f"Error terminating process: {str(e)}\n")
|
||||
return f"❌ Error terminating process: {str(e)}"
|
||||
else:
|
||||
return "❌ No process is currently running"
|
||||
|
||||
|
||||
def run_script(script_dropdown, question, progress=gr.Progress()):
|
||||
"""Run the selected script and return the output"""
|
||||
global current_process
|
||||
|
||||
script_name = SCRIPTS.get(script_dropdown)
|
||||
if not script_name:
|
||||
return "❌ Invalid script selection", "", "", "", None
|
||||
|
||||
if not question.strip():
|
||||
return "Please enter a question!", "", "", "", None
|
||||
|
||||
# Clear the log queue
|
||||
while not log_queue.empty():
|
||||
log_queue.get()
|
||||
|
||||
# Create log directory
|
||||
log_dir = Path("logs")
|
||||
log_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Create log file with timestamp
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
log_file = log_dir / f"{script_name.replace('.py', '')}_{timestamp}.log"
|
||||
|
||||
# Build command
|
||||
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
cmd = [
|
||||
sys.executable,
|
||||
os.path.join(base_path, "owl", "script_adapter.py"),
|
||||
os.path.join(base_path, "owl", script_name),
|
||||
]
|
||||
|
||||
# Create a copy of environment variables and add the question
|
||||
env = os.environ.copy()
|
||||
# Ensure question is a string type
|
||||
if not isinstance(question, str):
|
||||
question = str(question)
|
||||
# Preserve newlines, but ensure it's a valid string
|
||||
env["OWL_QUESTION"] = question
|
||||
|
||||
# Start the process
|
||||
with process_lock:
|
||||
current_process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
env=env,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
# Create thread to read output
|
||||
def read_output():
|
||||
try:
|
||||
# Use a unique timestamp to ensure log filename is not duplicated
|
||||
timestamp_unique = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
||||
unique_log_file = (
|
||||
log_dir / f"{script_name.replace('.py', '')}_{timestamp_unique}.log"
|
||||
)
|
||||
|
||||
# Use this unique filename to write logs
|
||||
with open(unique_log_file, "w", encoding="utf-8") as f:
|
||||
# Update global log file path
|
||||
nonlocal log_file
|
||||
log_file = unique_log_file
|
||||
|
||||
for line in iter(current_process.stdout.readline, ""):
|
||||
if line:
|
||||
# Write to log file
|
||||
f.write(line)
|
||||
f.flush()
|
||||
# Add to queue
|
||||
log_queue.put(line)
|
||||
except Exception as e:
|
||||
log_queue.put(f"Error reading output: {str(e)}\n")
|
||||
|
||||
# Start the reading thread
|
||||
threading.Thread(target=read_output, daemon=True).start()
|
||||
|
||||
# Collect logs
|
||||
logs = []
|
||||
progress(0, desc="Running...")
|
||||
|
||||
# Wait for process to complete or timeout
|
||||
start_time = time.time()
|
||||
timeout = 1800 # 30 minutes timeout
|
||||
|
||||
while current_process.poll() is None:
|
||||
# Check if timeout
|
||||
if time.time() - start_time > timeout:
|
||||
with process_lock:
|
||||
if current_process.poll() is None:
|
||||
if os.name == "nt":
|
||||
current_process.send_signal(signal.CTRL_BREAK_EVENT)
|
||||
else:
|
||||
current_process.terminate()
|
||||
log_queue.put("Execution timeout, process terminated\n")
|
||||
break
|
||||
|
||||
# Get logs from queue
|
||||
while not log_queue.empty():
|
||||
log = log_queue.get()
|
||||
logs.append(log)
|
||||
|
||||
# Update progress
|
||||
elapsed = time.time() - start_time
|
||||
progress(min(elapsed / 300, 0.99), desc="Running...")
|
||||
|
||||
# Short sleep to reduce CPU usage
|
||||
time.sleep(0.1)
|
||||
|
||||
# Update log display once per second
|
||||
yield (
|
||||
status_message(current_process),
|
||||
extract_answer(logs),
|
||||
"".join(logs),
|
||||
str(log_file),
|
||||
None,
|
||||
)
|
||||
|
||||
# Get remaining logs
|
||||
while not log_queue.empty():
|
||||
logs.append(log_queue.get())
|
||||
|
||||
# Extract chat history (if any)
|
||||
chat_history = extract_chat_history(logs)
|
||||
|
||||
# Return final status and logs
|
||||
return (
|
||||
status_message(current_process),
|
||||
extract_answer(logs),
|
||||
"".join(logs),
|
||||
str(log_file),
|
||||
chat_history,
|
||||
)
|
||||
|
||||
|
||||
def status_message(process):
|
||||
"""Return status message based on process status"""
|
||||
if process.poll() is None:
|
||||
return "⏳ Running..."
|
||||
elif process.returncode == 0:
|
||||
return "✅ Execution successful"
|
||||
else:
|
||||
return f"❌ Execution failed (return code: {process.returncode})"
|
||||
|
||||
|
||||
def extract_answer(logs):
|
||||
"""Extract answer from logs"""
|
||||
answer = ""
|
||||
for log in logs:
|
||||
if "Answer:" in log:
|
||||
answer = log.split("Answer:", 1)[1].strip()
|
||||
break
|
||||
return answer
|
||||
|
||||
|
||||
def extract_chat_history(logs):
|
||||
"""Try to extract chat history from logs"""
|
||||
try:
|
||||
chat_json_str = ""
|
||||
capture_json = False
|
||||
|
||||
for log in logs:
|
||||
if "chat_history" in log:
|
||||
# Start capturing JSON
|
||||
start_idx = log.find("[")
|
||||
if start_idx != -1:
|
||||
capture_json = True
|
||||
chat_json_str = log[start_idx:]
|
||||
elif capture_json:
|
||||
# Continue capturing JSON until finding the matching closing bracket
|
||||
chat_json_str += log
|
||||
if "]" in log:
|
||||
# Found closing bracket, try to parse JSON
|
||||
end_idx = chat_json_str.rfind("]") + 1
|
||||
if end_idx > 0:
|
||||
try:
|
||||
# Clean up possible extra text
|
||||
json_str = chat_json_str[:end_idx].strip()
|
||||
chat_data = json.loads(json_str)
|
||||
|
||||
# Format for use with Gradio chat component
|
||||
formatted_chat = []
|
||||
for msg in chat_data:
|
||||
if "role" in msg and "content" in msg:
|
||||
role = (
|
||||
"User" if msg["role"] == "user" else "Assistant"
|
||||
)
|
||||
formatted_chat.append([role, msg["content"]])
|
||||
return formatted_chat
|
||||
except json.JSONDecodeError:
|
||||
# If parsing fails, continue capturing
|
||||
pass
|
||||
except Exception:
|
||||
# Other errors, stop capturing
|
||||
capture_json = False
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def create_ui():
|
||||
"""Create Gradio interface"""
|
||||
# Load environment variables
|
||||
env_vars = load_env_vars()
|
||||
|
||||
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
|
||||
gr.Markdown(
|
||||
"""
|
||||
# 🦉 OWL Intelligent Assistant Platform
|
||||
|
||||
Select a model and enter your question, the system will run the corresponding script and display the results.
|
||||
"""
|
||||
)
|
||||
|
||||
with gr.Tabs():
|
||||
with gr.TabItem("Run Mode"):
|
||||
with gr.Row():
|
||||
with gr.Column(scale=1):
|
||||
# Ensure default value is a key that exists in SCRIPTS
|
||||
default_script = list(SCRIPTS.keys())[0] if SCRIPTS else None
|
||||
script_dropdown = gr.Dropdown(
|
||||
choices=list(SCRIPTS.keys()),
|
||||
value=default_script,
|
||||
label="Select Mode",
|
||||
)
|
||||
|
||||
script_info = gr.Textbox(
|
||||
value=get_script_info(default_script)
|
||||
if default_script
|
||||
else "",
|
||||
label="Model Description",
|
||||
interactive=False,
|
||||
)
|
||||
|
||||
script_dropdown.change(
|
||||
fn=lambda x: get_script_info(x),
|
||||
inputs=script_dropdown,
|
||||
outputs=script_info,
|
||||
)
|
||||
|
||||
question_input = gr.Textbox(
|
||||
lines=8,
|
||||
placeholder="Please enter your question...",
|
||||
label="Question",
|
||||
elem_id="question_input",
|
||||
show_copy_button=True,
|
||||
)
|
||||
|
||||
gr.Markdown(
|
||||
"""
|
||||
> **Note**: Your question will replace the default question in the script. The system will automatically handle the replacement, ensuring your question is used correctly.
|
||||
> Multi-line input is supported, line breaks will be preserved.
|
||||
"""
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
run_button = gr.Button("Run", variant="primary")
|
||||
stop_button = gr.Button("Stop", variant="stop")
|
||||
|
||||
with gr.Column(scale=2):
|
||||
with gr.Tabs():
|
||||
with gr.TabItem("Results"):
|
||||
status_output = gr.Textbox(label="Status")
|
||||
answer_output = gr.Textbox(label="Answer", lines=10)
|
||||
log_file_output = gr.Textbox(label="Log File Path")
|
||||
|
||||
with gr.TabItem("Run Logs"):
|
||||
log_output = gr.Textbox(label="Complete Logs", lines=25)
|
||||
|
||||
with gr.TabItem("Chat History"):
|
||||
chat_output = gr.Chatbot(label="Conversation History")
|
||||
|
||||
# Example questions
|
||||
examples = [
|
||||
[
|
||||
"Qwen Mini (Chinese)",
|
||||
"Browse Amazon and find a product that is attractive to programmers. Please provide the product name and price.",
|
||||
],
|
||||
[
|
||||
"DeepSeek (Chinese)",
|
||||
"Please analyze the latest statistics of the CAMEL-AI project on GitHub. Find out the number of stars, number of contributors, and recent activity of the project. Then, create a simple Excel spreadsheet to display this data and generate a bar chart to visualize these metrics. Finally, summarize the popularity and development trends of the CAMEL project.",
|
||||
],
|
||||
[
|
||||
"Default",
|
||||
"Navigate to Amazon.com and identify one product that is attractive to coders. Please provide me with the product name and price. No need to verify your answer.",
|
||||
],
|
||||
]
|
||||
|
||||
gr.Examples(examples=examples, inputs=[script_dropdown, question_input])
|
||||
|
||||
with gr.TabItem("Environment Variable Configuration"):
|
||||
env_inputs = {}
|
||||
save_status = gr.Textbox(label="Save Status", interactive=False)
|
||||
|
||||
# Add custom environment variables section
|
||||
with gr.Accordion("Add Custom Environment Variables", open=True):
|
||||
with gr.Row():
|
||||
new_var_name = gr.Textbox(
|
||||
label="Environment Variable Name",
|
||||
placeholder="Example: MY_CUSTOM_API_KEY",
|
||||
)
|
||||
new_var_value = gr.Textbox(
|
||||
label="Environment Variable Value",
|
||||
placeholder="Enter value",
|
||||
)
|
||||
new_var_type = gr.Dropdown(
|
||||
choices=["text", "password"], value="text", label="Type"
|
||||
)
|
||||
|
||||
add_var_button = gr.Button(
|
||||
"Add Environment Variable", variant="primary"
|
||||
)
|
||||
add_var_status = gr.Textbox(label="Add Status", interactive=False)
|
||||
|
||||
# Custom environment variables list
|
||||
custom_vars_list = gr.JSON(
|
||||
value=ENV_GROUPS["Custom Environment Variables"],
|
||||
label="Added Custom Environment Variables",
|
||||
visible=len(ENV_GROUPS["Custom Environment Variables"]) > 0,
|
||||
)
|
||||
|
||||
# Update and delete custom environment variables section
|
||||
with gr.Accordion(
|
||||
"Update or Delete Custom Environment Variables",
|
||||
open=True,
|
||||
visible=len(ENV_GROUPS["Custom Environment Variables"]) > 0,
|
||||
) as update_delete_accordion:
|
||||
with gr.Row():
|
||||
# Create dropdown menu to display all custom environment variables
|
||||
custom_var_dropdown = gr.Dropdown(
|
||||
choices=[
|
||||
var["name"]
|
||||
for var in ENV_GROUPS["Custom Environment Variables"]
|
||||
],
|
||||
label="Select Environment Variable",
|
||||
interactive=True,
|
||||
)
|
||||
update_var_value = gr.Textbox(
|
||||
label="New Environment Variable Value",
|
||||
placeholder="Enter new value",
|
||||
)
|
||||
update_var_type = gr.Dropdown(
|
||||
choices=["text", "password"], value="text", label="Type"
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
update_var_button = gr.Button(
|
||||
"Update Environment Variable", variant="primary"
|
||||
)
|
||||
delete_var_button = gr.Button(
|
||||
"Delete Environment Variable", variant="stop"
|
||||
)
|
||||
|
||||
update_var_status = gr.Textbox(
|
||||
label="Operation Status", interactive=False
|
||||
)
|
||||
|
||||
# Add environment variable button click event
|
||||
add_var_button.click(
|
||||
fn=add_custom_env_var,
|
||||
inputs=[new_var_name, new_var_value, new_var_type],
|
||||
outputs=[add_var_status, custom_vars_list],
|
||||
).then(
|
||||
fn=lambda vars: {"visible": len(vars) > 0},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[update_delete_accordion],
|
||||
)
|
||||
|
||||
# Update environment variable button click event
|
||||
update_var_button.click(
|
||||
fn=update_custom_env_var,
|
||||
inputs=[custom_var_dropdown, update_var_value, update_var_type],
|
||||
outputs=[update_var_status, custom_vars_list],
|
||||
)
|
||||
|
||||
# Delete environment variable button click event
|
||||
delete_var_button.click(
|
||||
fn=delete_custom_env_var,
|
||||
inputs=[custom_var_dropdown],
|
||||
outputs=[update_var_status, custom_vars_list],
|
||||
).then(
|
||||
fn=lambda vars: {"visible": len(vars) > 0},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[update_delete_accordion],
|
||||
)
|
||||
|
||||
# When custom environment variables list is updated, update dropdown menu options
|
||||
custom_vars_list.change(
|
||||
fn=lambda vars: {
|
||||
"choices": [var["name"] for var in vars],
|
||||
"value": None,
|
||||
},
|
||||
inputs=[custom_vars_list],
|
||||
outputs=[custom_var_dropdown],
|
||||
)
|
||||
|
||||
# Existing environment variable configuration
|
||||
for group_name, vars in ENV_GROUPS.items():
|
||||
if (
|
||||
group_name != "Custom Environment Variables" or len(vars) > 0
|
||||
): # Only show non-empty custom environment variable groups
|
||||
with gr.Accordion(
|
||||
group_name,
|
||||
open=(group_name != "Custom Environment Variables"),
|
||||
):
|
||||
for var in vars:
|
||||
# Add help information
|
||||
gr.Markdown(f"**{var['help']}**")
|
||||
|
||||
if var["type"] == "password":
|
||||
env_inputs[var["name"]] = gr.Textbox(
|
||||
value=env_vars.get(var["name"], ""),
|
||||
label=var["label"],
|
||||
placeholder=f"Please enter {var['label']}",
|
||||
type="password",
|
||||
)
|
||||
else:
|
||||
env_inputs[var["name"]] = gr.Textbox(
|
||||
value=env_vars.get(var["name"], ""),
|
||||
label=var["label"],
|
||||
placeholder=f"Please enter {var['label']}",
|
||||
)
|
||||
|
||||
save_button = gr.Button("Save Environment Variables", variant="primary")
|
||||
|
||||
# Save environment variables
|
||||
save_inputs = [
|
||||
env_inputs[var_name]
|
||||
for group in ENV_GROUPS.values()
|
||||
for var in group
|
||||
for var_name in [var["name"]]
|
||||
if var_name in env_inputs
|
||||
]
|
||||
save_button.click(
|
||||
fn=lambda *values: save_env_vars(
|
||||
dict(
|
||||
zip(
|
||||
[
|
||||
var["name"]
|
||||
for group in ENV_GROUPS.values()
|
||||
for var in group
|
||||
if var["name"] in env_inputs
|
||||
],
|
||||
values,
|
||||
)
|
||||
)
|
||||
),
|
||||
inputs=save_inputs,
|
||||
outputs=save_status,
|
||||
)
|
||||
|
||||
# Run script
|
||||
run_button.click(
|
||||
fn=run_script,
|
||||
inputs=[script_dropdown, question_input],
|
||||
outputs=[
|
||||
status_output,
|
||||
answer_output,
|
||||
log_output,
|
||||
log_file_output,
|
||||
chat_output,
|
||||
],
|
||||
show_progress=True,
|
||||
)
|
||||
|
||||
# Terminate execution
|
||||
stop_button.click(fn=terminate_process, inputs=[], outputs=[status_output])
|
||||
|
||||
# Add footer
|
||||
gr.Markdown(
|
||||
"""
|
||||
### 📝 Instructions
|
||||
|
||||
- Select a model and enter your question
|
||||
- Click the "Run" button to start execution
|
||||
- To stop execution, click the "Stop" button
|
||||
- View execution status and answers in the "Results" tab
|
||||
- View complete logs in the "Run Logs" tab
|
||||
- View conversation history in the "Chat History" tab (if available)
|
||||
- Configure API keys and other environment variables in the "Environment Variable Configuration" tab
|
||||
- You can add custom environment variables to meet special requirements
|
||||
|
||||
### ⚠️ Notes
|
||||
|
||||
- Running some models may require API keys, please make sure you have set the corresponding environment variables in the "Environment Variable Configuration" tab
|
||||
- Some scripts may take a long time to run, please be patient
|
||||
- If execution exceeds 30 minutes, the process will automatically terminate
|
||||
- Your question will replace the default question in the script, ensure the question is compatible with the selected model
|
||||
"""
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Create and launch the application
|
||||
app = create_ui()
|
||||
app.queue().launch(share=True)
|
||||
@@ -1,25 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
from camel.logger import disable_logging, enable_logging, set_log_level
|
||||
|
||||
__version__ = '0.2.11'
|
||||
|
||||
__all__ = [
|
||||
'__version__',
|
||||
'camel',
|
||||
'disable_logging',
|
||||
'enable_logging',
|
||||
'set_log_level',
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,44 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from .base import BaseAgent
|
||||
from .chat_agent import ChatAgent
|
||||
from .critic_agent import CriticAgent
|
||||
from .embodied_agent import EmbodiedAgent
|
||||
from .knowledge_graph_agent import KnowledgeGraphAgent
|
||||
from .role_assignment_agent import RoleAssignmentAgent
|
||||
from .search_agent import SearchAgent
|
||||
from .task_agent import (
|
||||
TaskCreationAgent,
|
||||
TaskPlannerAgent,
|
||||
TaskPrioritizationAgent,
|
||||
TaskSpecifyAgent,
|
||||
)
|
||||
from .tool_agents.base import BaseToolAgent
|
||||
from .tool_agents.hugging_face_tool_agent import HuggingFaceToolAgent
|
||||
|
||||
__all__ = [
|
||||
'BaseAgent',
|
||||
'ChatAgent',
|
||||
'TaskSpecifyAgent',
|
||||
'TaskPlannerAgent',
|
||||
'TaskCreationAgent',
|
||||
'TaskPrioritizationAgent',
|
||||
'CriticAgent',
|
||||
'BaseToolAgent',
|
||||
'HuggingFaceToolAgent',
|
||||
'EmbodiedAgent',
|
||||
'RoleAssignmentAgent',
|
||||
'SearchAgent',
|
||||
'KnowledgeGraphAgent',
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,29 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class BaseAgent(ABC):
|
||||
r"""An abstract base class for all CAMEL agents."""
|
||||
|
||||
@abstractmethod
|
||||
def reset(self, *args: Any, **kwargs: Any) -> Any:
|
||||
r"""Resets the agent to its initial state."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def step(self, *args: Any, **kwargs: Any) -> Any:
|
||||
r"""Performs a single step of the agent."""
|
||||
pass
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,202 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import random
|
||||
import warnings
|
||||
from typing import Any, Dict, Optional, Sequence
|
||||
|
||||
from colorama import Fore
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.memories import AgentMemory
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.responses import ChatAgentResponse
|
||||
from camel.utils import get_first_int, print_text_animated
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="CriticAgent")
|
||||
class CriticAgent(ChatAgent):
|
||||
r"""A class for the critic agent that assists in selecting an option.
|
||||
|
||||
Args:
|
||||
system_message (BaseMessage): The system message for the critic
|
||||
agent.
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
message_window_size (int, optional): The maximum number of previous
|
||||
messages to include in the context window. If `None`, no windowing
|
||||
is performed. (default: :obj:`6`)
|
||||
retry_attempts (int, optional): The number of retry attempts if the
|
||||
critic fails to return a valid option. (default: :obj:`2`)
|
||||
verbose (bool, optional): Whether to print the critic's messages.
|
||||
logger_color (Any): The color of the menu options displayed to the
|
||||
user. (default: :obj:`Fore.MAGENTA`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
system_message: BaseMessage,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
memory: Optional[AgentMemory] = None,
|
||||
message_window_size: int = 6,
|
||||
retry_attempts: int = 2,
|
||||
verbose: bool = False,
|
||||
logger_color: Any = Fore.MAGENTA,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
system_message,
|
||||
model=model,
|
||||
memory=memory,
|
||||
message_window_size=message_window_size,
|
||||
)
|
||||
self.options_dict: Dict[str, str] = dict()
|
||||
self.retry_attempts = retry_attempts
|
||||
self.verbose = verbose
|
||||
self.logger_color = logger_color
|
||||
|
||||
def flatten_options(self, messages: Sequence[BaseMessage]) -> str:
|
||||
r"""Flattens the options to the critic.
|
||||
|
||||
Args:
|
||||
messages (Sequence[BaseMessage]): A list of `BaseMessage` objects.
|
||||
|
||||
Returns:
|
||||
str: A string containing the flattened options to the critic.
|
||||
"""
|
||||
options = [message.content for message in messages]
|
||||
flatten_options = (
|
||||
f"> Proposals from "
|
||||
f"{messages[0].role_name} ({messages[0].role_type}). "
|
||||
"Please choose an option:\n"
|
||||
)
|
||||
for index, option in enumerate(options):
|
||||
flatten_options += f"Option {index + 1}:\n{option}\n\n"
|
||||
self.options_dict[str(index + 1)] = option
|
||||
format = (
|
||||
f"Please first enter your choice ([1-{len(self.options_dict)}]) "
|
||||
"and then your explanation and comparison: "
|
||||
)
|
||||
return flatten_options + format
|
||||
|
||||
def get_option(self, input_message: BaseMessage) -> str:
|
||||
r"""Gets the option selected by the critic.
|
||||
|
||||
Args:
|
||||
input_message (BaseMessage): A `BaseMessage` object representing
|
||||
the input message.
|
||||
|
||||
Returns:
|
||||
str: The option selected by the critic.
|
||||
"""
|
||||
# TODO: Add support for editing options by the critic.
|
||||
msg_content = input_message.content
|
||||
i = 0
|
||||
while i < self.retry_attempts:
|
||||
critic_response = self.step(input_message)
|
||||
|
||||
if critic_response.msgs is None or len(critic_response.msgs) == 0:
|
||||
raise RuntimeError("Got None critic messages.")
|
||||
if critic_response.terminated:
|
||||
raise RuntimeError("Critic step failed.")
|
||||
|
||||
critic_msg = critic_response.msg
|
||||
if self.verbose:
|
||||
print_text_animated(
|
||||
self.logger_color + "\n> Critic response: "
|
||||
f"\x1b[3m{critic_msg.content}\x1b[0m\n"
|
||||
)
|
||||
choice = self.parse_critic(critic_msg)
|
||||
|
||||
if choice in self.options_dict:
|
||||
return self.options_dict[choice]
|
||||
else:
|
||||
input_message = BaseMessage(
|
||||
role_name=input_message.role_name,
|
||||
role_type=input_message.role_type,
|
||||
meta_dict=input_message.meta_dict,
|
||||
content="> Invalid choice. Please choose again.\n"
|
||||
+ msg_content,
|
||||
)
|
||||
i += 1
|
||||
warnings.warn(
|
||||
"Critic failed to get a valid option. "
|
||||
f"After {self.retry_attempts} attempts. "
|
||||
"Returning a random option."
|
||||
)
|
||||
return random.choice(list(self.options_dict.values()))
|
||||
|
||||
def parse_critic(self, critic_msg: BaseMessage) -> Optional[str]:
|
||||
r"""Parses the critic's message and extracts the choice.
|
||||
|
||||
Args:
|
||||
critic_msg (BaseMessage): A `BaseMessage` object representing the
|
||||
critic's response.
|
||||
|
||||
Returns:
|
||||
Optional[str]: The critic's choice as a string, or None if the
|
||||
message could not be parsed.
|
||||
"""
|
||||
choice = str(get_first_int(critic_msg.content))
|
||||
return choice
|
||||
|
||||
def reduce_step(
|
||||
self,
|
||||
input_messages: Sequence[BaseMessage],
|
||||
) -> ChatAgentResponse:
|
||||
r"""Performs one step of the conversation by flattening options to the
|
||||
critic, getting the option, and parsing the choice.
|
||||
|
||||
Args:
|
||||
input_messages (Sequence[BaseMessage]): A list of BaseMessage
|
||||
objects.
|
||||
|
||||
Returns:
|
||||
ChatAgentResponse: A `ChatAgentResponse` object includes the
|
||||
critic's choice.
|
||||
"""
|
||||
meta_chat_message = BaseMessage(
|
||||
role_name=input_messages[0].role_name,
|
||||
role_type=input_messages[0].role_type,
|
||||
meta_dict=input_messages[0].meta_dict,
|
||||
content="",
|
||||
)
|
||||
|
||||
flatten_options = self.flatten_options(input_messages)
|
||||
if self.verbose:
|
||||
print_text_animated(
|
||||
self.logger_color + f"\x1b[3m{flatten_options}\x1b[0m\n"
|
||||
)
|
||||
input_msg = meta_chat_message.create_new_instance(flatten_options)
|
||||
|
||||
option = self.get_option(input_msg)
|
||||
output_msg = meta_chat_message.create_new_instance(option)
|
||||
|
||||
# TODO: The return `info` can be improved.
|
||||
return ChatAgentResponse(
|
||||
msgs=[output_msg],
|
||||
terminated=False,
|
||||
info={},
|
||||
)
|
||||
@@ -1,303 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import re
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.logger import get_logger
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.prompts import TextPrompt
|
||||
from camel.types import RoleType
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="DeductiveReasonerAgent")
|
||||
class DeductiveReasonerAgent(ChatAgent):
|
||||
r"""An agent responsible for deductive reasoning. Model of deductive
|
||||
reasoning:
|
||||
- L: A ⊕ C -> q * B
|
||||
- A represents the known starting state.
|
||||
- B represents the known target state.
|
||||
- C represents the conditions required to transition from A to B.
|
||||
- Q represents the quality or effectiveness of the transition from
|
||||
A to B.
|
||||
- L represents the path or process from A to B.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
) -> None:
|
||||
system_message = BaseMessage(
|
||||
role_name="Insight Agent",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You assign roles based on tasks.",
|
||||
)
|
||||
super().__init__(system_message, model=model)
|
||||
|
||||
def deduce_conditions_and_quality(
|
||||
self,
|
||||
starting_state: str,
|
||||
target_state: str,
|
||||
role_descriptions_dict: Optional[Dict[str, str]] = None,
|
||||
) -> Dict[str, Union[List[str], Dict[str, str]]]:
|
||||
r"""Derives the conditions and quality from the starting state and the
|
||||
target state based on the model of the deductive reasoning and the
|
||||
knowledge base. It can optionally consider the roles involved in the
|
||||
scenario, which allows tailoring the output more closely to the AI
|
||||
agent's environment.
|
||||
|
||||
Args:
|
||||
starting_state (str): The initial or starting state from which
|
||||
conditions are deduced.
|
||||
target_state (str): The target state of the task.
|
||||
role_descriptions_dict (Optional[Dict[str, str]], optional): The
|
||||
descriptions of the roles. (default: :obj:`None`)
|
||||
role_descriptions_dict (Optional[Dict[str, str]], optional): A
|
||||
dictionary describing the roles involved in the scenario. This
|
||||
is optional and can be used to provide a context for the
|
||||
CAMEL's role-playing, enabling the generation of more relevant
|
||||
and tailored conditions and quality assessments. This could be
|
||||
generated using a `RoleAssignmentAgent()` or defined manually
|
||||
by the user.
|
||||
|
||||
Returns:
|
||||
Dict[str, Union[List[str], Dict[str, str]]]: A dictionary with the
|
||||
extracted data from the message. The dictionary contains three
|
||||
keys:
|
||||
- 'conditions': A list where each key is a condition ID and
|
||||
each value is the corresponding condition text.
|
||||
- 'labels': A list of label strings extracted from the message.
|
||||
- 'quality': A string of quality assessment strings extracted
|
||||
from the message.
|
||||
"""
|
||||
self.reset()
|
||||
|
||||
deduce_prompt = """You are a deductive reasoner. You are tasked to
|
||||
complete the TASK based on the THOUGHT OF DEDUCTIVE REASONING, the
|
||||
STARTING STATE A and the TARGET STATE B. You are given the CONTEXT
|
||||
CONTENT to help you complete the TASK.
|
||||
Your answer MUST strictly adhere to the structure of ANSWER TEMPLATE, ONLY
|
||||
fill in the BLANKs, and DO NOT alter or modify any other part of the template
|
||||
|
||||
===== MODELING OF DEDUCTIVE REASONING =====
|
||||
You are tasked with understanding a mathematical model based on the components
|
||||
${A, B, C, Q, L}$. In this model: ``L: A ⊕ C -> q * B``.
|
||||
- $A$ represents the known starting state.
|
||||
- $B$ represents the known target state.
|
||||
- $C$ represents the conditions required to transition from $A$ to $B$.
|
||||
- $Q$ represents the quality or effectiveness of the transition from $A$ to
|
||||
$B$.
|
||||
- $L$ represents the path or process from $A$ to $B$.
|
||||
|
||||
===== THOUGHT OF DEDUCTIVE REASONING =====
|
||||
1. Define the Parameters of A and B:
|
||||
- Characterization: Before delving into transitions, thoroughly understand
|
||||
the nature and boundaries of both $A$ and $B$. This includes the type,
|
||||
properties, constraints, and possible interactions between the two.
|
||||
- Contrast and Compare: Highlight the similarities and differences between
|
||||
$A$ and $B$. This comparative analysis will give an insight into what
|
||||
needs changing and what remains constant.
|
||||
2. Historical & Empirical Analysis:
|
||||
- Previous Transitions according to the Knowledge Base of GPT: (if
|
||||
applicable) Extract conditions and patterns from the historical instances
|
||||
where a similar transition from a state comparable to $A$ moved towards
|
||||
$B$.
|
||||
- Scientific Principles: (if applicable) Consider the underlying
|
||||
scientific principles governing or related to the states and their
|
||||
transition. For example, if $A$ and $B$ are physical states, laws of
|
||||
physics might apply.
|
||||
3. Logical Deduction of Conditions ($C$):
|
||||
- Direct Path Analysis: What are the immediate and direct conditions
|
||||
required to move from $A$ to $B$?
|
||||
- Intermediate States: Are there states between $A$ and $B$ that must be
|
||||
traversed or can be used to make the transition smoother or more
|
||||
efficient? If yes, what is the content?
|
||||
- Constraints & Limitations: Identify potential barriers or restrictions
|
||||
in moving from $A$ to $B$. These can be external (e.g., environmental
|
||||
factors) or internal (properties of $A$ or $B$).
|
||||
- Resource and Information Analysis: What resources and information are
|
||||
required for the transition? This could be time, entity, factor, code
|
||||
language, software platform, unknowns, etc.
|
||||
- External Influences: Consider socio-economic, political, or
|
||||
environmental factors (if applicable) that could influence the transition
|
||||
conditions.
|
||||
- Creative/Heuristic Reasoning: Open your mind to multiple possible $C$'s,
|
||||
no matter how unconventional they might seem. Utilize analogies,
|
||||
metaphors, or brainstorming techniques to envision possible conditions or
|
||||
paths from $A$ to $B$.
|
||||
- The conditions $C$ should be multiple but in one sentence. And each
|
||||
condition should be concerned with one aspect/entity.
|
||||
4. Entity/Label Recognition of Conditions ($C$):
|
||||
- Identify and categorize entities of Conditions ($C$) such as the names,
|
||||
locations, dates, specific technical terms or contextual parameters that
|
||||
might be associated with events, innovations post-2022.
|
||||
- The output of the entities/labels will be used as tags or labels for
|
||||
semantic similarity searches. The entities/labels may be the words, or
|
||||
phrases, each of them should contain valuable, high information entropy
|
||||
information, and should be independent.
|
||||
- Ensure that the identified entities are formatted in a manner suitable
|
||||
for database indexing and retrieval. Organize the entities into
|
||||
categories, and combine the category with its instance into a continuous
|
||||
phrase, without using colons or other separators.
|
||||
- Format these entities for database indexing: output the category rather
|
||||
than its instance/content into a continuous phrase. For example, instead
|
||||
of "Jan. 02", identify it as "Event time".
|
||||
5. Quality Assessment ($Q$):
|
||||
- Efficiency: How efficient is the transition from $A$ to $B$, which
|
||||
measures the resources used versus the desired outcome?
|
||||
- Effectiveness: Did the transition achieve the desired outcome or was the
|
||||
target state achieved as intended?
|
||||
- Safety & Risks: Assess any risks associated with the transition and the
|
||||
measures to mitigate them.
|
||||
- Feedback Mechanisms: Incorporate feedback loops to continuously monitor
|
||||
and adjust the quality of transition, making it more adaptive.
|
||||
6. Iterative Evaluation:
|
||||
- Test & Refine: Based on the initially deduced conditions and assessed
|
||||
quality, iterate the process to refine and optimize the transition. This
|
||||
might involve tweaking conditions, employing different paths, or changing
|
||||
resources.
|
||||
- Feedback Integration: Use feedback to make improvements and increase the
|
||||
quality of the transition.
|
||||
7. Real-world scenarios often present challenges that may not be captured by
|
||||
models and frameworks. While using the model, maintain an adaptive mindset:
|
||||
- Scenario Exploration: Continuously imagine various possible scenarios,
|
||||
both positive and negative, to prepare for unexpected events.
|
||||
- Flexibility: Be prepared to modify conditions ($C$) or alter the path/
|
||||
process ($L$) if unforeseen challenges arise.
|
||||
- Feedback Integration: Rapidly integrate feedback from actual
|
||||
implementations to adjust the model's application, ensuring relevancy and
|
||||
effectiveness.
|
||||
|
||||
===== TASK =====
|
||||
Given the starting state $A$ and the target state $B$, assuming that a path
|
||||
$L$ always exists between $A$ and $B$, how can one deduce or identify the
|
||||
necessary conditions $C$ and the quality $Q$ of the transition?
|
||||
|
||||
===== STARTING STATE $A$ =====
|
||||
{starting_state}
|
||||
|
||||
===== TARGET STATE $B$ =====
|
||||
{target_state}
|
||||
|
||||
{role_with_description_prompt}
|
||||
===== ANSWER TEMPLATE =====
|
||||
- Characterization and comparison of $A$ and $B$:\n<BLANK>
|
||||
- Historical & Empirical Analysis:\n<BLANK>/None
|
||||
- Logical Deduction of Conditions ($C$) (multiple conditions can be deduced):
|
||||
condition <NUM>:
|
||||
<BLANK>.
|
||||
- Entity/Label Recognition of Conditions:\n[<BLANK>, <BLANK>, ...] (include
|
||||
square brackets)
|
||||
- Quality Assessment ($Q$) (do not use symbols):
|
||||
<BLANK>.
|
||||
- Iterative Evaluation:\n<BLANK>/None"""
|
||||
|
||||
if role_descriptions_dict is not None:
|
||||
role_names = role_descriptions_dict.keys()
|
||||
role_with_description_prompt = (
|
||||
"===== ROLES WITH DESCRIPTIONS =====\n"
|
||||
+ "\n".join(
|
||||
f"{role_name}:\n{role_descriptions_dict[role_name]}\n"
|
||||
for role_name in role_names
|
||||
)
|
||||
+ "\n\n"
|
||||
)
|
||||
else:
|
||||
role_with_description_prompt = ""
|
||||
deduce_prompt = TextPrompt(deduce_prompt)
|
||||
|
||||
deduce = deduce_prompt.format(
|
||||
starting_state=starting_state,
|
||||
target_state=target_state,
|
||||
role_with_description_prompt=role_with_description_prompt,
|
||||
)
|
||||
|
||||
conditions_and_quality_generation_msg = BaseMessage.make_user_message(
|
||||
role_name="Deductive Reasoner", content=deduce
|
||||
)
|
||||
|
||||
response = self.step(
|
||||
input_message=conditions_and_quality_generation_msg
|
||||
)
|
||||
|
||||
if response.terminated:
|
||||
raise RuntimeError(
|
||||
"Deduction failed. Error:\n" + f"{response.info}"
|
||||
)
|
||||
msg: BaseMessage = response.msg
|
||||
logger.info(f"Message content:\n{msg.content}")
|
||||
|
||||
# Extract the conditions from the message
|
||||
conditions_dict = {
|
||||
f"condition {i}": cdt.replace("<", "")
|
||||
.replace(">", "")
|
||||
.strip()
|
||||
.strip('\n')
|
||||
for i, cdt in re.findall(
|
||||
r"condition (\d+):\s*(.+?)(?=condition \d+|- Entity)",
|
||||
msg.content,
|
||||
re.DOTALL,
|
||||
)
|
||||
}
|
||||
|
||||
# Extract the labels from the message
|
||||
labels = [
|
||||
label.strip().strip('\n').strip("\"'")
|
||||
for label in re.findall(
|
||||
r"Entity/Label Recognition of Conditions:\n\[(.+?)\]",
|
||||
msg.content,
|
||||
re.DOTALL,
|
||||
)[0].split(",")
|
||||
]
|
||||
|
||||
# Extract the quality from the message
|
||||
quality = next(
|
||||
q.strip().strip('\n')
|
||||
for q in re.findall(
|
||||
r"Quality Assessment \(\$Q\$\) \(do not use symbols\):"
|
||||
r"\n(.+?)- Iterative",
|
||||
msg.content,
|
||||
re.DOTALL,
|
||||
)
|
||||
)
|
||||
|
||||
# Convert them into JSON format
|
||||
conditions_and_quality_json: Dict[
|
||||
str, Union[List[str], Dict[str, str]]
|
||||
] = {}
|
||||
conditions_and_quality_json["conditions"] = conditions_dict
|
||||
conditions_and_quality_json["labels"] = labels
|
||||
conditions_and_quality_json["evaluate_quality"] = quality
|
||||
|
||||
return conditions_and_quality_json
|
||||
@@ -1,201 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from colorama import Fore
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.agents.tool_agents.base import BaseToolAgent
|
||||
from camel.interpreters import (
|
||||
BaseInterpreter,
|
||||
InternalPythonInterpreter,
|
||||
SubprocessInterpreter,
|
||||
)
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.responses import ChatAgentResponse
|
||||
from camel.utils import print_text_animated
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="EmbodiedAgent")
|
||||
class EmbodiedAgent(ChatAgent):
|
||||
r"""Class for managing conversations of CAMEL Embodied Agents.
|
||||
|
||||
Args:
|
||||
system_message (BaseMessage): The system message for the chat agent.
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
message_window_size (int, optional): The maximum number of previous
|
||||
messages to include in the context window. If `None`, no windowing
|
||||
is performed. (default: :obj:`None`)
|
||||
tool_agents (List[BaseToolAgent], optional): The tools agents to use in
|
||||
the embodied agent. (default: :obj:`None`)
|
||||
code_interpreter (BaseInterpreter, optional): The code interpreter to
|
||||
execute codes. If `code_interpreter` and `tool_agent` are both
|
||||
`None`, default to `SubProcessInterpreter`. If `code_interpreter`
|
||||
is `None` and `tool_agents` is not `None`, default to
|
||||
`InternalPythonInterpreter`. (default: :obj:`None`)
|
||||
verbose (bool, optional): Whether to print the critic's messages.
|
||||
logger_color (Any): The color of the logger displayed to the user.
|
||||
(default: :obj:`Fore.MAGENTA`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
system_message: BaseMessage,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
message_window_size: Optional[int] = None,
|
||||
tool_agents: Optional[List[BaseToolAgent]] = None,
|
||||
code_interpreter: Optional[BaseInterpreter] = None,
|
||||
verbose: bool = False,
|
||||
logger_color: Any = Fore.MAGENTA,
|
||||
) -> None:
|
||||
self.tool_agents = tool_agents
|
||||
self.code_interpreter: BaseInterpreter
|
||||
if code_interpreter is not None:
|
||||
self.code_interpreter = code_interpreter
|
||||
elif self.tool_agents:
|
||||
self.code_interpreter = InternalPythonInterpreter()
|
||||
else:
|
||||
self.code_interpreter = SubprocessInterpreter()
|
||||
|
||||
if self.tool_agents:
|
||||
system_message = self._set_tool_agents(system_message)
|
||||
self.verbose = verbose
|
||||
self.logger_color = logger_color
|
||||
super().__init__(
|
||||
system_message=system_message,
|
||||
model=model,
|
||||
message_window_size=message_window_size,
|
||||
)
|
||||
|
||||
def _set_tool_agents(self, system_message: BaseMessage) -> BaseMessage:
|
||||
action_space_prompt = self._get_tool_agents_prompt()
|
||||
result_message = system_message.create_new_instance(
|
||||
content=system_message.content.format(
|
||||
action_space=action_space_prompt
|
||||
)
|
||||
)
|
||||
if self.tool_agents is not None:
|
||||
self.code_interpreter.update_action_space(
|
||||
{tool.name: tool for tool in self.tool_agents}
|
||||
)
|
||||
return result_message
|
||||
|
||||
def _get_tool_agents_prompt(self) -> str:
|
||||
r"""Returns the action space prompt.
|
||||
|
||||
Returns:
|
||||
str: The action space prompt.
|
||||
"""
|
||||
if self.tool_agents is not None:
|
||||
return "\n".join(
|
||||
[
|
||||
f"*** {tool.name} ***:\n {tool.description}"
|
||||
for tool in self.tool_agents
|
||||
]
|
||||
)
|
||||
else:
|
||||
return ""
|
||||
|
||||
def get_tool_agent_names(self) -> List[str]:
|
||||
r"""Returns the names of tool agents.
|
||||
|
||||
Returns:
|
||||
List[str]: The names of tool agents.
|
||||
"""
|
||||
if self.tool_agents is not None:
|
||||
return [tool.name for tool in self.tool_agents]
|
||||
else:
|
||||
return []
|
||||
|
||||
# ruff: noqa: E501
|
||||
def step(self, input_message: BaseMessage) -> ChatAgentResponse: # type: ignore[override]
|
||||
r"""Performs a step in the conversation.
|
||||
|
||||
Args:
|
||||
input_message (BaseMessage): The input message.
|
||||
|
||||
Returns:
|
||||
ChatAgentResponse: A struct containing the output messages,
|
||||
a boolean indicating whether the chat session has terminated,
|
||||
and information about the chat session.
|
||||
"""
|
||||
response = super().step(input_message)
|
||||
|
||||
if response.msgs is None or len(response.msgs) == 0:
|
||||
raise RuntimeError("Got None output messages.")
|
||||
if response.terminated:
|
||||
raise RuntimeError(f"{self.__class__.__name__} step failed.")
|
||||
|
||||
# NOTE: Only single output messages are supported
|
||||
explanations, codes = response.msg.extract_text_and_code_prompts()
|
||||
|
||||
if self.verbose:
|
||||
for explanation, code in zip(explanations, codes):
|
||||
print_text_animated(
|
||||
self.logger_color + f"> Explanation:\n{explanation}"
|
||||
)
|
||||
print_text_animated(self.logger_color + f"> Code:\n{code}")
|
||||
|
||||
if len(explanations) > len(codes):
|
||||
print_text_animated(
|
||||
self.logger_color + f"> Explanation:\n{explanations[-1]}"
|
||||
)
|
||||
|
||||
content = response.msg.content
|
||||
|
||||
if codes is not None:
|
||||
try:
|
||||
content = "\n> Executed Results:\n"
|
||||
for block_idx, code in enumerate(codes):
|
||||
executed_output = self.code_interpreter.run(
|
||||
code, code.code_type
|
||||
)
|
||||
content += (
|
||||
f"Executing code block {block_idx}: {{\n"
|
||||
+ executed_output
|
||||
+ "}\n"
|
||||
)
|
||||
except InterruptedError as e:
|
||||
content = (
|
||||
f"\n> Running code fail: {e}\n"
|
||||
"Please regenerate the code."
|
||||
)
|
||||
|
||||
# TODO: Handle errors
|
||||
content = input_message.content + f"\n> Embodied Actions:\n{content}"
|
||||
message = BaseMessage(
|
||||
input_message.role_name,
|
||||
input_message.role_type,
|
||||
input_message.meta_dict,
|
||||
content,
|
||||
)
|
||||
return ChatAgentResponse(
|
||||
msgs=[message],
|
||||
terminated=response.terminated,
|
||||
info=response.info,
|
||||
)
|
||||
@@ -1,259 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import TYPE_CHECKING, Optional, Union
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from unstructured.documents.elements import Element
|
||||
|
||||
from camel.agents import ChatAgent
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.prompts import TextPrompt
|
||||
from camel.storages.graph_storages.graph_element import (
|
||||
GraphElement,
|
||||
Node,
|
||||
Relationship,
|
||||
)
|
||||
from camel.types import RoleType
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
text_prompt = """
|
||||
You are tasked with extracting nodes and relationships from given content and
|
||||
structures them into Node and Relationship objects. Here's the outline of what
|
||||
you needs to do:
|
||||
|
||||
Content Extraction:
|
||||
You should be able to process input content and identify entities mentioned
|
||||
within it.
|
||||
Entities can be any noun phrases or concepts that represent distinct entities
|
||||
in the context of the given content.
|
||||
|
||||
Node Extraction:
|
||||
For each identified entity, you should create a Node object.
|
||||
Each Node object should have a unique identifier (id) and a type (type).
|
||||
Additional properties associated with the node can also be extracted and
|
||||
stored.
|
||||
|
||||
Relationship Extraction:
|
||||
You should identify relationships between entities mentioned in the content.
|
||||
For each relationship, create a Relationship object.
|
||||
A Relationship object should have a subject (subj) and an object (obj) which
|
||||
are Node objects representing the entities involved in the relationship.
|
||||
Each relationship should also have a type (type), and additional properties if
|
||||
applicable.
|
||||
|
||||
Output Formatting:
|
||||
The extracted nodes and relationships should be formatted as instances of the
|
||||
provided Node and Relationship classes.
|
||||
Ensure that the extracted data adheres to the structure defined by the classes.
|
||||
Output the structured data in a format that can be easily validated against
|
||||
the provided code.
|
||||
|
||||
Instructions for you:
|
||||
Read the provided content thoroughly.
|
||||
Identify distinct entities mentioned in the content and categorize them as
|
||||
nodes.
|
||||
Determine relationships between these entities and represent them as directed
|
||||
relationships.
|
||||
Provide the extracted nodes and relationships in the specified format below.
|
||||
Example for you:
|
||||
|
||||
Example Content:
|
||||
"John works at XYZ Corporation. He is a software engineer. The company is
|
||||
located in New York City."
|
||||
|
||||
Expected Output:
|
||||
|
||||
Nodes:
|
||||
|
||||
Node(id='John', type='Person')
|
||||
Node(id='XYZ Corporation', type='Organization')
|
||||
Node(id='New York City', type='Location')
|
||||
|
||||
Relationships:
|
||||
|
||||
Relationship(subj=Node(id='John', type='Person'), obj=Node(id='XYZ
|
||||
Corporation', type='Organization'), type='WorksAt')
|
||||
Relationship(subj=Node(id='John', type='Person'), obj=Node(id='New York City',
|
||||
type='Location'), type='ResidesIn')
|
||||
|
||||
===== TASK =====
|
||||
Please extracts nodes and relationships from given content and structures them
|
||||
into Node and Relationship objects.
|
||||
|
||||
{task}
|
||||
"""
|
||||
|
||||
|
||||
@track_agent(name="KnowledgeGraphAgent")
|
||||
class KnowledgeGraphAgent(ChatAgent):
|
||||
r"""An agent that can extract node and relationship information for
|
||||
different entities from given `Element` content.
|
||||
|
||||
Attributes:
|
||||
task_prompt (TextPrompt): A prompt for the agent to extract node and
|
||||
relationship information for different entities.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
) -> None:
|
||||
r"""Initialize the `KnowledgeGraphAgent`.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
"""
|
||||
system_message = BaseMessage(
|
||||
role_name="Graphify",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="Your mission is to transform unstructured content "
|
||||
"into structured graph data. Extract nodes and relationships with "
|
||||
"precision, and let the connections unfold. Your graphs will "
|
||||
"illuminate the hidden connections within the chaos of "
|
||||
"information.",
|
||||
)
|
||||
super().__init__(system_message, model=model)
|
||||
|
||||
def run(
|
||||
self,
|
||||
element: "Element",
|
||||
parse_graph_elements: bool = False,
|
||||
) -> Union[str, GraphElement]:
|
||||
r"""Run the agent to extract node and relationship information.
|
||||
|
||||
Args:
|
||||
element (Element): The input element.
|
||||
parse_graph_elements (bool, optional): Whether to parse into
|
||||
`GraphElement`. Defaults to `False`.
|
||||
|
||||
Returns:
|
||||
Union[str, GraphElement]: The extracted node and relationship
|
||||
information. If `parse_graph_elements` is `True` then return
|
||||
`GraphElement`, else return `str`.
|
||||
"""
|
||||
self.reset()
|
||||
self.element = element
|
||||
|
||||
knowledge_graph_prompt = TextPrompt(text_prompt)
|
||||
knowledge_graph_generation = knowledge_graph_prompt.format(
|
||||
task=str(element)
|
||||
)
|
||||
|
||||
knowledge_graph_generation_msg = BaseMessage.make_user_message(
|
||||
role_name="Graphify", content=knowledge_graph_generation
|
||||
)
|
||||
|
||||
response = self.step(input_message=knowledge_graph_generation_msg)
|
||||
|
||||
content = response.msg.content
|
||||
|
||||
if parse_graph_elements:
|
||||
content = self._parse_graph_elements(content)
|
||||
|
||||
return content
|
||||
|
||||
def _validate_node(self, node: Node) -> bool:
|
||||
r"""Validate if the object is a valid Node.
|
||||
|
||||
Args:
|
||||
node (Node): Object to be validated.
|
||||
|
||||
Returns:
|
||||
bool: True if the object is a valid Node, False otherwise.
|
||||
"""
|
||||
return (
|
||||
isinstance(node, Node)
|
||||
and isinstance(node.id, (str, int))
|
||||
and isinstance(node.type, str)
|
||||
)
|
||||
|
||||
def _validate_relationship(self, relationship: Relationship) -> bool:
|
||||
r"""Validate if the object is a valid Relationship.
|
||||
|
||||
Args:
|
||||
relationship (Relationship): Object to be validated.
|
||||
|
||||
Returns:
|
||||
bool: True if the object is a valid Relationship, False otherwise.
|
||||
"""
|
||||
return (
|
||||
isinstance(relationship, Relationship)
|
||||
and self._validate_node(relationship.subj)
|
||||
and self._validate_node(relationship.obj)
|
||||
and isinstance(relationship.type, str)
|
||||
)
|
||||
|
||||
def _parse_graph_elements(self, input_string: str) -> GraphElement:
|
||||
r"""Parses graph elements from given content.
|
||||
|
||||
Args:
|
||||
input_string (str): The input content.
|
||||
|
||||
Returns:
|
||||
GraphElement: The parsed graph elements.
|
||||
"""
|
||||
import re
|
||||
|
||||
# Regular expressions to extract nodes and relationships
|
||||
node_pattern = r"Node\(id='(.*?)', type='(.*?)'\)"
|
||||
rel_pattern = (
|
||||
r"Relationship\(subj=Node\(id='(.*?)', type='(.*?)'\), "
|
||||
r"obj=Node\(id='(.*?)', type='(.*?)'\), type='(.*?)'\)"
|
||||
)
|
||||
|
||||
nodes = {}
|
||||
relationships = []
|
||||
|
||||
# Extract nodes
|
||||
for match in re.finditer(node_pattern, input_string):
|
||||
id, type = match.groups()
|
||||
properties = {'source': 'agent_created'}
|
||||
if id not in nodes:
|
||||
node = Node(id=id, type=type, properties=properties)
|
||||
if self._validate_node(node):
|
||||
nodes[id] = node
|
||||
|
||||
# Extract relationships
|
||||
for match in re.finditer(rel_pattern, input_string):
|
||||
subj_id, subj_type, obj_id, obj_type, rel_type = match.groups()
|
||||
properties = {'source': 'agent_created'}
|
||||
if subj_id in nodes and obj_id in nodes:
|
||||
subj = nodes[subj_id]
|
||||
obj = nodes[obj_id]
|
||||
relationship = Relationship(
|
||||
subj=subj, obj=obj, type=rel_type, properties=properties
|
||||
)
|
||||
if self._validate_relationship(relationship):
|
||||
relationships.append(relationship)
|
||||
|
||||
return GraphElement(
|
||||
nodes=list(nodes.values()),
|
||||
relationships=relationships,
|
||||
source=self.element,
|
||||
)
|
||||
@@ -1,141 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import re
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.prompts import TextPrompt
|
||||
from camel.types import RoleType
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="RoleAssignmentAgent")
|
||||
class RoleAssignmentAgent(ChatAgent):
|
||||
r"""An agent that generates role names based on the task prompt.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
|
||||
Attributes:
|
||||
role_assignment_prompt (TextPrompt): A prompt for the agent to generate
|
||||
role names.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
) -> None:
|
||||
system_message = BaseMessage(
|
||||
role_name="Role Assigner",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You assign roles based on tasks.",
|
||||
)
|
||||
super().__init__(system_message, model=model)
|
||||
|
||||
def run(
|
||||
self,
|
||||
task_prompt: Union[str, TextPrompt],
|
||||
num_roles: int = 2,
|
||||
) -> Dict[str, str]:
|
||||
r"""Generate role names based on the input task prompt.
|
||||
|
||||
Args:
|
||||
task_prompt (Union[str, TextPrompt]): The prompt
|
||||
for the task based on which the roles are to be generated.
|
||||
num_roles (int, optional): The number of roles to generate.
|
||||
(default: :obj:`2`)
|
||||
|
||||
Returns:
|
||||
Dict[str, str]: A dictionary mapping role names to their
|
||||
descriptions.
|
||||
"""
|
||||
self.reset()
|
||||
|
||||
expert_prompt = "===== ANSWER PROMPT =====\n" + "\n".join(
|
||||
f"Domain expert {i + 1}: <BLANK>\n"
|
||||
f"Associated competencies, characteristics, duties "
|
||||
f"and workflows: <BLANK>. End."
|
||||
for i in range(num_roles or 0)
|
||||
)
|
||||
role_assignment_generation_prompt = TextPrompt(
|
||||
"You are a role assignment agent, and you're in charge of "
|
||||
+ "recruiting {num_roles} experts for the following task."
|
||||
+ "\n==== TASK =====\n {task}\n\n"
|
||||
+ "Identify the domain experts you'd recruit and detail their "
|
||||
+ "associated competencies, characteristics, duties and workflows "
|
||||
+ "to complete the task.\n "
|
||||
+ "Your answer MUST adhere to the format of ANSWER PROMPT, and "
|
||||
+ "ONLY answer the BLANKs.\n"
|
||||
+ expert_prompt
|
||||
)
|
||||
role_assignment_generation = role_assignment_generation_prompt.format(
|
||||
num_roles=num_roles, task=task_prompt
|
||||
)
|
||||
|
||||
role_assignment_generation_msg = BaseMessage.make_user_message(
|
||||
role_name="Role Assigner", content=role_assignment_generation
|
||||
)
|
||||
|
||||
response = self.step(input_message=role_assignment_generation_msg)
|
||||
|
||||
msg = response.msg # type: BaseMessage
|
||||
terminated = response.terminated
|
||||
|
||||
# Distribute the output completions into role names and descriptions
|
||||
role_names = [
|
||||
desc.replace("<|", "").replace("|>", "")
|
||||
for desc in re.findall(
|
||||
r"Domain expert \d: (.+?)\nAssociated competencies,",
|
||||
msg.content,
|
||||
re.DOTALL,
|
||||
)
|
||||
]
|
||||
role_descriptions = [
|
||||
desc.replace("<|", "").replace("|>", "")
|
||||
for desc in re.findall(
|
||||
r"Associated competencies, characteristics, "
|
||||
r"duties and workflows: (.+?) End.",
|
||||
msg.content,
|
||||
re.DOTALL,
|
||||
)
|
||||
]
|
||||
|
||||
if len(role_names) != num_roles or len(role_descriptions) != num_roles:
|
||||
raise RuntimeError(
|
||||
"Got None or insufficient information of roles."
|
||||
)
|
||||
if terminated:
|
||||
raise RuntimeError("Role assignment failed.")
|
||||
|
||||
role_descriptions_dict = {
|
||||
role_name: description
|
||||
for role_name, description in zip(role_names, role_descriptions)
|
||||
}
|
||||
|
||||
return role_descriptions_dict
|
||||
@@ -1,133 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import Optional
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.prompts import TextPrompt
|
||||
from camel.types import RoleType
|
||||
from camel.utils import create_chunks
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="SearchAgent")
|
||||
class SearchAgent(ChatAgent):
|
||||
r"""An agent that summarizes text based on a query and evaluates the
|
||||
relevance of an answer.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
) -> None:
|
||||
system_message = BaseMessage(
|
||||
role_name="Assistant",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You are a helpful assistant.",
|
||||
)
|
||||
super().__init__(system_message, model=model)
|
||||
|
||||
def summarize_text(self, text: str, query: str) -> str:
|
||||
r"""Summarize the information from the text, base on the query.
|
||||
|
||||
Args:
|
||||
text (str): Text to summarize.
|
||||
query (str): What information you want.
|
||||
|
||||
Returns:
|
||||
str: Strings with information.
|
||||
"""
|
||||
self.reset()
|
||||
|
||||
summary_prompt = TextPrompt(
|
||||
'''Gather information from this text that relative to the
|
||||
question, but do not directly answer the question.\nquestion:
|
||||
{query}\ntext '''
|
||||
)
|
||||
summary_prompt = summary_prompt.format(query=query)
|
||||
# Max length of each chunk
|
||||
max_len = 3000
|
||||
results = ""
|
||||
chunks = create_chunks(text, max_len)
|
||||
# Summarize
|
||||
for i, chunk in enumerate(chunks, start=1):
|
||||
prompt = summary_prompt + str(i) + ": " + chunk
|
||||
user_msg = BaseMessage.make_user_message(
|
||||
role_name="User",
|
||||
content=prompt,
|
||||
)
|
||||
result = self.step(user_msg).msg.content
|
||||
results += result + "\n"
|
||||
|
||||
# Final summarization
|
||||
final_prompt = TextPrompt(
|
||||
'''Here are some summarized texts which split from one text. Using
|
||||
the information to answer the question. If can't find the answer,
|
||||
you must answer "I can not find the answer to the query" and
|
||||
explain why.\n Query:\n{query}.\n\nText:\n'''
|
||||
)
|
||||
final_prompt = final_prompt.format(query=query)
|
||||
prompt = final_prompt + results
|
||||
|
||||
user_msg = BaseMessage.make_user_message(
|
||||
role_name="User",
|
||||
content=prompt,
|
||||
)
|
||||
response = self.step(user_msg).msg.content
|
||||
|
||||
return response
|
||||
|
||||
def continue_search(self, query: str, answer: str) -> bool:
|
||||
r"""Ask whether to continue search or not based on the provided answer.
|
||||
|
||||
Args:
|
||||
query (str): The question.
|
||||
answer (str): The answer to the question.
|
||||
|
||||
Returns:
|
||||
bool: `True` if the user want to continue search, `False`
|
||||
otherwise.
|
||||
"""
|
||||
prompt = TextPrompt(
|
||||
"Do you think the ANSWER can answer the QUERY? "
|
||||
"Use only 'yes' or 'no' to answer.\n"
|
||||
"===== QUERY =====\n{query}\n\n"
|
||||
"===== ANSWER =====\n{answer}"
|
||||
)
|
||||
prompt = prompt.format(query=query, answer=answer)
|
||||
user_msg = BaseMessage.make_user_message(
|
||||
role_name="User",
|
||||
content=prompt,
|
||||
)
|
||||
response = self.step(user_msg).msg.content
|
||||
if "yes" in str(response).lower():
|
||||
return False
|
||||
return True
|
||||
@@ -1,410 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from camel.agents.chat_agent import ChatAgent
|
||||
from camel.messages import BaseMessage
|
||||
from camel.models import BaseModelBackend
|
||||
from camel.prompts import PromptTemplateGenerator, TextPrompt
|
||||
from camel.types import RoleType, TaskType
|
||||
from camel.utils import get_task_list
|
||||
|
||||
# AgentOps decorator setting
|
||||
try:
|
||||
import os
|
||||
|
||||
if os.getenv("AGENTOPS_API_KEY") is not None:
|
||||
from agentops import track_agent
|
||||
else:
|
||||
raise ImportError
|
||||
except (ImportError, AttributeError):
|
||||
from camel.utils import track_agent
|
||||
|
||||
|
||||
@track_agent(name="TaskSpecifyAgent")
|
||||
class TaskSpecifyAgent(ChatAgent):
|
||||
r"""An agent that specifies a given task prompt by prompting the user to
|
||||
provide more details.
|
||||
|
||||
Attributes:
|
||||
DEFAULT_WORD_LIMIT (int): The default word limit for the task prompt.
|
||||
task_specify_prompt (TextPrompt): The prompt for specifying the task.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
task_type (TaskType, optional): The type of task for which to generate
|
||||
a prompt. (default: :obj:`TaskType.AI_SOCIETY`)
|
||||
task_specify_prompt (Union[str, TextPrompt], optional): The prompt for
|
||||
specifying the task. (default: :obj:`None`)
|
||||
word_limit (int, optional): The word limit for the task prompt.
|
||||
(default: :obj:`50`)
|
||||
output_language (str, optional): The language to be output by the
|
||||
agent. (default: :obj:`None`)
|
||||
"""
|
||||
|
||||
DEFAULT_WORD_LIMIT = 50
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
task_type: TaskType = TaskType.AI_SOCIETY,
|
||||
task_specify_prompt: Optional[Union[str, TextPrompt]] = None,
|
||||
word_limit: int = DEFAULT_WORD_LIMIT,
|
||||
output_language: Optional[str] = None,
|
||||
) -> None:
|
||||
self.task_specify_prompt: Union[str, TextPrompt]
|
||||
if task_specify_prompt is None:
|
||||
task_specify_prompt_template = (
|
||||
PromptTemplateGenerator().get_task_specify_prompt(task_type)
|
||||
)
|
||||
|
||||
self.task_specify_prompt = task_specify_prompt_template.format(
|
||||
word_limit=word_limit
|
||||
)
|
||||
else:
|
||||
self.task_specify_prompt = TextPrompt(task_specify_prompt)
|
||||
|
||||
system_message = BaseMessage(
|
||||
role_name="Task Specifier",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You can make a task more specific.",
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
system_message,
|
||||
model=model,
|
||||
output_language=output_language,
|
||||
)
|
||||
|
||||
def run(
|
||||
self,
|
||||
task_prompt: Union[str, TextPrompt],
|
||||
meta_dict: Optional[Dict[str, Any]] = None,
|
||||
) -> TextPrompt:
|
||||
r"""Specify the given task prompt by providing more details.
|
||||
|
||||
Args:
|
||||
task_prompt (Union[str, TextPrompt]): The original task
|
||||
prompt.
|
||||
meta_dict (Dict[str, Any], optional): A dictionary containing
|
||||
additional information to include in the prompt.
|
||||
(default: :obj:`None`)
|
||||
|
||||
Returns:
|
||||
TextPrompt: The specified task prompt.
|
||||
"""
|
||||
self.reset()
|
||||
task_specify_prompt = self.task_specify_prompt.format(task=task_prompt)
|
||||
|
||||
if meta_dict is not None:
|
||||
task_specify_prompt = task_specify_prompt.format(**meta_dict)
|
||||
task_msg = BaseMessage.make_user_message(
|
||||
role_name="Task Specifier", content=task_specify_prompt
|
||||
)
|
||||
specifier_response = self.step(task_msg)
|
||||
|
||||
if specifier_response.terminated:
|
||||
raise RuntimeError("Task specification failed.")
|
||||
if len(specifier_response.msgs) == 0:
|
||||
raise RuntimeError("Got no specification message.")
|
||||
|
||||
specified_task_msg = specifier_response.msgs[0]
|
||||
|
||||
return TextPrompt(specified_task_msg.content)
|
||||
|
||||
|
||||
@track_agent(name="TaskPlannerAgent")
|
||||
class TaskPlannerAgent(ChatAgent):
|
||||
r"""An agent that helps divide a task into subtasks based on the input
|
||||
task prompt.
|
||||
|
||||
Attributes:
|
||||
task_planner_prompt (TextPrompt): A prompt for the agent to divide
|
||||
the task into subtasks.
|
||||
|
||||
Args:
|
||||
model (BaseModelBackend, optional): The model backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
output_language (str, optional): The language to be output by the
|
||||
agent. (default: :obj:`None`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
output_language: Optional[str] = None,
|
||||
) -> None:
|
||||
self.task_planner_prompt = TextPrompt(
|
||||
"Divide this task into subtasks: {task}. Be concise."
|
||||
)
|
||||
system_message = BaseMessage(
|
||||
role_name="Task Planner",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You are a helpful task planner.",
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
system_message,
|
||||
model=model,
|
||||
output_language=output_language,
|
||||
)
|
||||
|
||||
def run(
|
||||
self,
|
||||
task_prompt: Union[str, TextPrompt],
|
||||
) -> TextPrompt:
|
||||
r"""Generate subtasks based on the input task prompt.
|
||||
|
||||
Args:
|
||||
task_prompt (Union[str, TextPrompt]): The prompt for the task to
|
||||
be divided into subtasks.
|
||||
|
||||
Returns:
|
||||
TextPrompt: A prompt for the subtasks generated by the agent.
|
||||
"""
|
||||
# TODO: Maybe include roles information.
|
||||
self.reset()
|
||||
task_planner_prompt = self.task_planner_prompt.format(task=task_prompt)
|
||||
|
||||
task_msg = BaseMessage.make_user_message(
|
||||
role_name="Task Planner", content=task_planner_prompt
|
||||
)
|
||||
|
||||
task_response = self.step(task_msg)
|
||||
|
||||
if task_response.terminated:
|
||||
raise RuntimeError("Task planning failed.")
|
||||
if len(task_response.msgs) == 0:
|
||||
raise RuntimeError("Got no task planning message.")
|
||||
|
||||
sub_tasks_msg = task_response.msgs[0]
|
||||
return TextPrompt(sub_tasks_msg.content)
|
||||
|
||||
|
||||
@track_agent(name="TaskCreationAgent")
|
||||
class TaskCreationAgent(ChatAgent):
|
||||
r"""An agent that helps create new tasks based on the objective
|
||||
and last completed task. Compared to :obj:`TaskPlannerAgent`,
|
||||
it's still a task planner, but it has more context information
|
||||
like last task and incomplete task list. Modified from
|
||||
`BabyAGI <https://github.com/yoheinakajima/babyagi>`_.
|
||||
|
||||
Attributes:
|
||||
task_creation_prompt (TextPrompt): A prompt for the agent to
|
||||
create new tasks.
|
||||
|
||||
Args:
|
||||
role_name (str): The role name of the Agent to create the task.
|
||||
objective (Union[str, TextPrompt]): The objective of the Agent to
|
||||
perform the task.
|
||||
model (BaseModelBackend, optional): The LLM backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
output_language (str, optional): The language to be output by the
|
||||
agent. (default: :obj:`None`)
|
||||
message_window_size (int, optional): The maximum number of previous
|
||||
messages to include in the context window. If `None`, no windowing
|
||||
is performed. (default: :obj:`None`)
|
||||
max_task_num (int, optional): The maximum number of planned
|
||||
tasks in one round. (default: :obj:3)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
role_name: str,
|
||||
objective: Union[str, TextPrompt],
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
output_language: Optional[str] = None,
|
||||
message_window_size: Optional[int] = None,
|
||||
max_task_num: Optional[int] = 3,
|
||||
) -> None:
|
||||
task_creation_prompt = TextPrompt(
|
||||
"""Create new a task with the following objective: {objective}.
|
||||
Never forget you are a Task Creator of {role_name}.
|
||||
You must instruct me based on my expertise and your needs to solve the task.
|
||||
You should consider past solved tasks and in-progress tasks: {task_list}.
|
||||
The new created tasks must not overlap with these past tasks.
|
||||
The result must be a numbered list in the format:
|
||||
|
||||
#. First Task
|
||||
#. Second Task
|
||||
#. Third Task
|
||||
|
||||
You can only give me up to {max_task_num} tasks at a time. \
|
||||
Each task should be concise, concrete and doable for a {role_name}.
|
||||
You should make task plan and not ask me questions.
|
||||
If you think no new tasks are needed right now, write "No tasks to add."
|
||||
Now start to give me new tasks one by one. No more than three tasks.
|
||||
Be concrete.
|
||||
"""
|
||||
)
|
||||
|
||||
self.task_creation_prompt = task_creation_prompt.format(
|
||||
objective=objective, role_name=role_name, max_task_num=max_task_num
|
||||
)
|
||||
self.objective = objective
|
||||
|
||||
system_message = BaseMessage(
|
||||
role_name="Task Creator",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You are a helpful task creator.",
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
system_message,
|
||||
model=model,
|
||||
output_language=output_language,
|
||||
message_window_size=message_window_size,
|
||||
)
|
||||
|
||||
def run(
|
||||
self,
|
||||
task_list: List[str],
|
||||
) -> List[str]:
|
||||
r"""Generate subtasks based on the previous task results and
|
||||
incomplete task list.
|
||||
|
||||
Args:
|
||||
task_list (List[str]): The completed or in-progress
|
||||
tasks which should not overlap with new created tasks.
|
||||
|
||||
Returns:
|
||||
List[str]: The new task list generated by the Agent.
|
||||
"""
|
||||
|
||||
if len(task_list) > 0:
|
||||
task_creation_prompt = self.task_creation_prompt.format(
|
||||
task_list=task_list
|
||||
)
|
||||
else:
|
||||
task_creation_prompt = self.task_creation_prompt.format(
|
||||
task_list=""
|
||||
)
|
||||
|
||||
task_msg = BaseMessage.make_user_message(
|
||||
role_name="Task Creator", content=task_creation_prompt
|
||||
)
|
||||
task_response = self.step(task_msg)
|
||||
|
||||
if task_response.terminated:
|
||||
raise RuntimeError("Task creation failed.")
|
||||
if len(task_response.msgs) == 0:
|
||||
raise RuntimeError("Got no task creation message.")
|
||||
|
||||
sub_tasks_msg = task_response.msgs[0]
|
||||
return get_task_list(sub_tasks_msg.content)
|
||||
|
||||
|
||||
@track_agent(name="TaskPrioritizationAgent")
|
||||
class TaskPrioritizationAgent(ChatAgent):
|
||||
r"""An agent that helps re-prioritize the task list and
|
||||
returns numbered prioritized list. Modified from
|
||||
`BabyAGI <https://github.com/yoheinakajima/babyagi>`_.
|
||||
|
||||
Attributes:
|
||||
task_prioritization_prompt (TextPrompt): A prompt for the agent to
|
||||
prioritize tasks.
|
||||
|
||||
Args:
|
||||
objective (Union[str, TextPrompt]): The objective of the Agent to
|
||||
perform the task.
|
||||
model (BaseModelBackend, optional): The LLM backend to use for
|
||||
generating responses. (default: :obj:`OpenAIModel` with
|
||||
`GPT_4O_MINI`)
|
||||
output_language (str, optional): The language to be output by the
|
||||
agent. (default: :obj:`None`)
|
||||
message_window_size (int, optional): The maximum number of previous
|
||||
messages to include in the context window. If `None`, no windowing
|
||||
is performed. (default: :obj:`None`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
objective: Union[str, TextPrompt],
|
||||
model: Optional[BaseModelBackend] = None,
|
||||
output_language: Optional[str] = None,
|
||||
message_window_size: Optional[int] = None,
|
||||
) -> None:
|
||||
task_prioritization_prompt = TextPrompt(
|
||||
"""Prioritize the following tasks : {task_list}.
|
||||
Consider the ultimate objective of you: {objective}.
|
||||
Tasks should be sorted from highest to lowest priority, where higher-priority \
|
||||
tasks are those that act as pre-requisites or are more essential for meeting \
|
||||
the objective. Return one task per line in your response.
|
||||
Do not remove or modify any tasks.
|
||||
The result must be a numbered list in the format:
|
||||
|
||||
#. First task
|
||||
#. Second task
|
||||
|
||||
The entries must be consecutively numbered, starting with 1.
|
||||
The number of each entry must be followed by a period.
|
||||
Do not include any headers before your ranked list or follow your list \
|
||||
with any other output."""
|
||||
)
|
||||
|
||||
self.task_prioritization_prompt = task_prioritization_prompt.format(
|
||||
objective=objective
|
||||
)
|
||||
self.objective = objective
|
||||
|
||||
system_message = BaseMessage(
|
||||
role_name="Task Prioritizer",
|
||||
role_type=RoleType.ASSISTANT,
|
||||
meta_dict=None,
|
||||
content="You are a helpful task prioritizer.",
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
system_message,
|
||||
model=model,
|
||||
output_language=output_language,
|
||||
message_window_size=message_window_size,
|
||||
)
|
||||
|
||||
def run(
|
||||
self,
|
||||
task_list: List[str],
|
||||
) -> List[str]:
|
||||
r"""Prioritize the task list given the agent objective.
|
||||
|
||||
Args:
|
||||
task_list (List[str]): The unprioritized tasks of agent.
|
||||
|
||||
Returns:
|
||||
List[str]: The new prioritized task list generated by the Agent.
|
||||
"""
|
||||
task_prioritization_prompt = self.task_prioritization_prompt.format(
|
||||
task_list=task_list
|
||||
)
|
||||
|
||||
task_msg = BaseMessage.make_user_message(
|
||||
role_name="Task Prioritizer", content=task_prioritization_prompt
|
||||
)
|
||||
|
||||
task_response = self.step(task_msg)
|
||||
|
||||
if task_response.terminated:
|
||||
raise RuntimeError("Task prioritization failed.")
|
||||
if len(task_response.msgs) == 0:
|
||||
raise RuntimeError("Got no task prioritization message.")
|
||||
|
||||
sub_tasks_msg = task_response.msgs[0]
|
||||
return get_task_list(sub_tasks_msg.content)
|
||||
@@ -1,20 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from .base import BaseToolAgent
|
||||
from .hugging_face_tool_agent import HuggingFaceToolAgent
|
||||
|
||||
__all__ = [
|
||||
'BaseToolAgent',
|
||||
'HuggingFaceToolAgent',
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,39 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from camel.agents import BaseAgent
|
||||
|
||||
|
||||
class BaseToolAgent(BaseAgent):
|
||||
r"""Creates a :obj:`BaseToolAgent` object with the specified name and
|
||||
description.
|
||||
|
||||
Args:
|
||||
name (str): The name of the tool agent.
|
||||
description (str): The description of the tool agent.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str, description: str) -> None:
|
||||
self.name = name
|
||||
self.description = description
|
||||
|
||||
def reset(self) -> None:
|
||||
r"""Resets the agent to its initial state."""
|
||||
pass
|
||||
|
||||
def step(self) -> None:
|
||||
r"""Performs a single step of the agent."""
|
||||
pass
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.name}: {self.description}"
|
||||
@@ -1,206 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import Any, Optional
|
||||
|
||||
from camel.agents.tool_agents.base import BaseToolAgent
|
||||
|
||||
|
||||
# flake8: noqa :E501
|
||||
class HuggingFaceToolAgent(BaseToolAgent):
|
||||
r"""Tool agent for calling HuggingFace models. This agent is a wrapper
|
||||
around agents from the `transformers` library. For more information
|
||||
about the available models, please see the `transformers` documentation
|
||||
at https://huggingface.co/docs/transformers/transformers_agents.
|
||||
|
||||
Args:
|
||||
name (str): The name of the agent.
|
||||
*args (Any): Additional positional arguments to pass to the underlying
|
||||
Agent class.
|
||||
remote (bool, optional): Flag indicating whether to run the agent
|
||||
remotely. (default: :obj:`True`)
|
||||
**kwargs (Any): Additional keyword arguments to pass to the underlying
|
||||
Agent class.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
*args: Any,
|
||||
remote: bool = True,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
try:
|
||||
# TODO: Support other tool agents
|
||||
import transformers
|
||||
from packaging import version
|
||||
|
||||
if version.parse(transformers.__version__) < version.parse(
|
||||
"4.31.0"
|
||||
):
|
||||
raise ValueError(
|
||||
"The version of \"transformers\" package should >= 4.31.0"
|
||||
)
|
||||
|
||||
from transformers.tools import OpenAiAgent
|
||||
from transformers.tools.agent_types import AgentImage
|
||||
except (ImportError, ValueError):
|
||||
raise ValueError(
|
||||
"Could not import transformers tool agents. "
|
||||
"Please setup the environment with "
|
||||
"pip install huggingface_hub==0.14.1 transformers==4.31.0 diffusers accelerate==0.20.3 datasets torch soundfile sentencepiece opencv-python"
|
||||
)
|
||||
self.agent_image_type = AgentImage
|
||||
self.agent = OpenAiAgent(*args, **kwargs)
|
||||
description = f"""The `{name}` is a tool agent that can perform a variety of tasks including:
|
||||
- Document question answering: given a document (such as a PDF) in image format, answer a question on this document
|
||||
- Text question answering: given a long text and a question, answer the question in the text
|
||||
- Unconditional image captioning: Caption the image!
|
||||
- Image question answering: given an image, answer a question on this image
|
||||
- Image segmentation: given an image and a prompt, output the segmentation mask of that prompt
|
||||
- Speech to text: given an audio recording of a person talking, transcribe the speech into text
|
||||
- Text to speech: convert text to speech
|
||||
- Zero-shot text classification: given a text and a list of labels, identify to which label the text corresponds the most
|
||||
- Text summarization: summarize a long text in one or a few sentences
|
||||
- Translation: translate the text into a given language
|
||||
- Text downloading: to download a text from a web URL
|
||||
- Text to image: generate an image according to a prompt, leveraging stable diffusion
|
||||
- Image transformation: modify an image given an initial image and a prompt, leveraging instruct pix2pix stable diffusion
|
||||
- Text to video: generate a small video according to a prompt
|
||||
|
||||
Here are some python code examples of what you can do with this agent:
|
||||
|
||||
Single execution (step) mode, the single execution method is when using the step() method of the agent:
|
||||
```
|
||||
# Text to image
|
||||
rivers_and_lakes_image = {name}.step("Draw me a picture of rivers and lakes.")
|
||||
rivers_and_lakes_image.save("./rivers_and_lakes_image.png")
|
||||
|
||||
# Text to image -> Image transformation
|
||||
sea_add_island_image = {name}.step("Draw me a picture of the sea then transform the picture to add an island")
|
||||
sea_add_island_image.save("./sea_add_island_image.png")
|
||||
|
||||
# If you'd like to keep a state across executions or to pass non-text objects to the agent,
|
||||
# you can do so by specifying variables that you would like the agent to use. For example,
|
||||
# you could generate the first image of rivers and lakes, and ask the model to update that picture to add an island by doing the following:
|
||||
picture = {name}.step("Generate a picture of rivers and lakes.")
|
||||
picture.save("./picture.png")
|
||||
updated_picture = {name}.step("Transform the image in `picture` to add an island to it.", picture=picture)
|
||||
updated_picture.save("./updated_picture.png")
|
||||
|
||||
capybara_sea_image = {name}.step("Draw me a picture of the `prompt`", prompt="a capybara swimming in the sea")
|
||||
capybara_sea_image.save("./capybara_sea_image.png")
|
||||
|
||||
# Document question answering
|
||||
answer = {name}.step(
|
||||
"In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?",
|
||||
document=document,
|
||||
)
|
||||
print(answer)
|
||||
|
||||
|
||||
# Text to image
|
||||
boat_image = {name}.step("Generate an image of a boat in the water")
|
||||
boat_image.save("./boat_image.png")
|
||||
|
||||
# Unconditional image captioning
|
||||
boat_image_caption = {name}.step("Can you caption the `boat_image`?", boat_image=boat_image)
|
||||
print(boat_image_caption)
|
||||
|
||||
# Text to image -> Unconditional image captioning -> Text to speech
|
||||
boat_audio = {name}.step("Can you generate an image of a boat? Please read out loud the contents of the image afterwards")
|
||||
|
||||
# Text downloading
|
||||
document = {name}.step("Download the text from http://hf.co")
|
||||
print(document)
|
||||
|
||||
# Text summarization
|
||||
summary = {name}.step("Summarize the following text: `document`", document=document)
|
||||
print(summary)
|
||||
|
||||
# Text downloading -> Text summarization -> Text to speech
|
||||
audio = {name}.step("Read out loud the summary of http://hf.co")
|
||||
```
|
||||
|
||||
Chat-based execution (chat), the agent also has a chat-based approach, using the chat() method:
|
||||
```
|
||||
# Clean the chat history
|
||||
{name}.reset()
|
||||
|
||||
# Text to image
|
||||
capybara_image = {name}.chat("Show me an an image of a capybara")
|
||||
capybara_image.save("./capybara_image.png")
|
||||
|
||||
# Image transformation
|
||||
transformed_capybara_image = {name}.chat("Transform the image so that it snows")
|
||||
transformed_capybara_image.save("./transformed_capybara_image.png")
|
||||
|
||||
# Image segmentation
|
||||
segmented_transformed_capybara_image = {name}.chat("Show me a mask of the snowy capybaras")
|
||||
segmented_transformed_capybara_image.save("./segmented_transformed_capybara_image.png")
|
||||
```
|
||||
"""
|
||||
super(HuggingFaceToolAgent, self).__init__(name, description)
|
||||
self.remote = remote
|
||||
|
||||
def reset(self) -> None:
|
||||
r"""Resets the chat history of the agent."""
|
||||
self.agent.prepare_for_new_chat()
|
||||
|
||||
def step(
|
||||
self,
|
||||
*args: Any,
|
||||
remote: Optional[bool] = None,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
r"""Runs the agent in single execution mode.
|
||||
|
||||
Args:
|
||||
*args (Any): Positional arguments to pass to the agent.
|
||||
remote (bool, optional): Flag indicating whether to run the agent
|
||||
remotely. Overrides the default setting. (default: :obj:`None`)
|
||||
**kwargs (Any): Keyword arguments to pass to the agent.
|
||||
|
||||
Returns:
|
||||
str: The response from the agent.
|
||||
"""
|
||||
if remote is None:
|
||||
remote = self.remote
|
||||
agent_output = self.agent.run(*args, remote=remote, **kwargs)
|
||||
if isinstance(agent_output, self.agent_image_type):
|
||||
agent_output = agent_output.to_raw()
|
||||
return agent_output
|
||||
|
||||
def chat(
|
||||
self,
|
||||
*args: Any,
|
||||
remote: Optional[bool] = None,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
r"""Runs the agent in a chat conversation mode.
|
||||
|
||||
Args:
|
||||
*args (Any): Positional arguments to pass to the agent.
|
||||
remote (bool, optional): Flag indicating whether to run the agent
|
||||
remotely. Overrides the default setting. (default: :obj:`None`)
|
||||
**kwargs (Any): Keyword arguments to pass to the agent.
|
||||
|
||||
Returns:
|
||||
str: The response from the agent.
|
||||
"""
|
||||
if remote is None:
|
||||
remote = self.remote
|
||||
agent_output = self.agent.chat(*args, remote=remote, **kwargs)
|
||||
if isinstance(agent_output, self.agent_image_type):
|
||||
agent_output = agent_output.to_raw()
|
||||
return agent_output
|
||||
@@ -1,17 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
from .base import BaseBenchmark
|
||||
|
||||
__all__ = ["BaseBenchmark"]
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,152 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Literal, Optional
|
||||
|
||||
from camel.agents import ChatAgent
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BaseBenchmark(ABC):
|
||||
r"""Base class for benchmarks.
|
||||
|
||||
Attributes:
|
||||
name (str): Name of the benchmark.
|
||||
data_dir (str): Path to the data directory.
|
||||
save_to (str): Path to save the results.
|
||||
processes (int): Number of processes to use for parallel
|
||||
processing. :(default: :obj:`1`)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, name: str, data_dir: str, save_to: str, processes: int = 1
|
||||
):
|
||||
r"""Initialize the benchmark.
|
||||
|
||||
Args:
|
||||
name (str): Name of the benchmark.
|
||||
data_dir (str): Path to the data directory.
|
||||
save_to (str): Path to save the results.
|
||||
processes (int): Number of processes to use for parallel
|
||||
processing. :(default: :obj:`1`)
|
||||
|
||||
"""
|
||||
self.name = name
|
||||
self.data_dir = Path(data_dir)
|
||||
self.processes = processes
|
||||
self.save_to = save_to
|
||||
if not self.data_dir.exists():
|
||||
logger.info(
|
||||
f"Data directory {data_dir} does not exist. Creating it."
|
||||
)
|
||||
self.data_dir.mkdir(parents=True, exist_ok=True)
|
||||
if not self.data_dir.is_dir():
|
||||
raise NotADirectoryError(
|
||||
f"Data directory {data_dir} is not a directory"
|
||||
)
|
||||
self._data: Dict[str, List[Dict[str, Any]]] = dict()
|
||||
self._results: List[Dict[str, Any]] = []
|
||||
|
||||
@abstractmethod
|
||||
def download(self) -> "BaseBenchmark":
|
||||
r"""Download the benchmark data.
|
||||
|
||||
Returns:
|
||||
BaseBenchmark: The benchmark instance.
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def load(self, force_download: bool = False) -> "BaseBenchmark":
|
||||
r"""Load the benchmark data.
|
||||
|
||||
Args:
|
||||
force_download (bool): Whether to force download the data.
|
||||
|
||||
Returns:
|
||||
BaseBenchmark: The benchmark instance.
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def train(self) -> List[Dict[str, Any]]:
|
||||
r"""Get the training data.
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: The training data.
|
||||
"""
|
||||
if not self._data:
|
||||
logger.info("Data not loaded. Loading data.")
|
||||
self.load()
|
||||
return self._data["train"]
|
||||
|
||||
@property
|
||||
def valid(self) -> List[Dict[str, Any]]:
|
||||
r"""Get the validation data.
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: The validation data.
|
||||
"""
|
||||
if not self._data:
|
||||
logger.info("Data not loaded. Loading data.")
|
||||
self.load()
|
||||
return self._data["valid"]
|
||||
|
||||
@property
|
||||
def test(self) -> List[Dict[str, Any]]:
|
||||
r"""Get the test data.
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: The test data.
|
||||
"""
|
||||
if not self._data:
|
||||
logger.info("Data not loaded. Loading data.")
|
||||
self.load()
|
||||
return self._data["test"]
|
||||
|
||||
@abstractmethod
|
||||
def run(
|
||||
self,
|
||||
agent: ChatAgent,
|
||||
on: Literal["train", "valid", "test"],
|
||||
randomize: bool = False,
|
||||
subset: Optional[int] = None,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> "BaseBenchmark":
|
||||
r"""Run the benchmark.
|
||||
|
||||
Args:
|
||||
agent (ChatAgent): The chat agent.
|
||||
on (str): The data split to run the benchmark on.
|
||||
randomize (bool): Whether to randomize the data.
|
||||
subset (int): The subset of the data to run the benchmark on.
|
||||
|
||||
Returns:
|
||||
BaseBenchmark: The benchmark instance.
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def results(self) -> List[Dict[str, Any]]:
|
||||
r"""Get the results.
|
||||
|
||||
Returns:
|
||||
List[Dict[str, Any]]: The results.
|
||||
"""
|
||||
return self._results
|
||||
@@ -1,34 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from .discord_app import DiscordApp
|
||||
from .slack.models import (
|
||||
SlackAppMentionEventBody,
|
||||
SlackAppMentionEventProfile,
|
||||
SlackAuthProfile,
|
||||
SlackEventBody,
|
||||
SlackEventProfile,
|
||||
)
|
||||
from .slack.slack_app import SlackApp
|
||||
from .telegram_bot import TelegramBot
|
||||
|
||||
__all__ = [
|
||||
'DiscordApp',
|
||||
'SlackApp',
|
||||
'SlackAppMentionEventBody',
|
||||
'SlackAppMentionEventProfile',
|
||||
'SlackAuthProfile',
|
||||
'SlackEventBody',
|
||||
'SlackEventProfile',
|
||||
'TelegramBot',
|
||||
]
|
||||
@@ -1,138 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
from camel.utils import dependencies_required
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from discord import Message
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DiscordApp:
|
||||
r"""A class representing a Discord app that uses the `discord.py` library
|
||||
to interact with Discord servers.
|
||||
|
||||
This bot can respond to messages in specific channels and only reacts to
|
||||
messages that mention the bot.
|
||||
|
||||
Attributes:
|
||||
channel_ids (Optional[List[int]]): A list of allowed channel IDs. If
|
||||
provided, the bot will only respond to messages in these channels.
|
||||
token (Optional[str]): The Discord bot token used for authentication.
|
||||
"""
|
||||
|
||||
@dependencies_required('discord')
|
||||
def __init__(
|
||||
self,
|
||||
channel_ids: Optional[List[int]] = None,
|
||||
token: Optional[str] = None,
|
||||
) -> None:
|
||||
r"""Initialize the DiscordApp instance by setting up the Discord client
|
||||
and event handlers.
|
||||
|
||||
Args:
|
||||
channel_ids (Optional[List[int]]): A list of allowed channel IDs.
|
||||
The bot will only respond to messages in these channels if
|
||||
provided.
|
||||
token (Optional[str]): The Discord bot token for authentication.
|
||||
If not provided, the token will be retrieved from the
|
||||
environment variable `DISCORD_TOKEN`.
|
||||
|
||||
Raises:
|
||||
ValueError: If the `DISCORD_TOKEN` is not found in environment
|
||||
variables.
|
||||
"""
|
||||
self.token = token or os.getenv('DISCORD_TOKEN')
|
||||
self.channel_ids = channel_ids
|
||||
|
||||
if not self.token:
|
||||
raise ValueError(
|
||||
"`DISCORD_TOKEN` not found in environment variables. Get it"
|
||||
" here: `https://discord.com/developers/applications`."
|
||||
)
|
||||
|
||||
import discord
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
self._client = discord.Client(intents=intents)
|
||||
|
||||
# Register event handlers
|
||||
self._client.event(self.on_ready)
|
||||
self._client.event(self.on_message)
|
||||
|
||||
async def start(self):
|
||||
r"""Asynchronously start the Discord bot using its token.
|
||||
|
||||
This method starts the bot and logs into Discord asynchronously using
|
||||
the provided token. It should be awaited when used in an async
|
||||
environment.
|
||||
"""
|
||||
await self._client.start(self.token)
|
||||
|
||||
def run(self) -> None:
|
||||
r"""Start the Discord bot using its token.
|
||||
|
||||
This method starts the bot and logs into Discord synchronously using
|
||||
the provided token. It blocks execution and keeps the bot running.
|
||||
"""
|
||||
self._client.run(self.token) # type: ignore[arg-type]
|
||||
|
||||
async def on_ready(self) -> None:
|
||||
r"""Event handler that is called when the bot has successfully
|
||||
connected to the Discord server.
|
||||
|
||||
When the bot is ready and logged into Discord, it prints a message
|
||||
displaying the bot's username.
|
||||
"""
|
||||
logger.info(f'We have logged in as {self._client.user}')
|
||||
|
||||
async def on_message(self, message: 'Message') -> None:
|
||||
r"""Event handler for processing incoming messages.
|
||||
|
||||
This method is called whenever a new message is received by the bot. It
|
||||
will ignore messages sent by the bot itself, only respond to messages
|
||||
in allowed channels (if specified), and only to messages that mention
|
||||
the bot.
|
||||
|
||||
Args:
|
||||
message (discord.Message): The message object received from
|
||||
Discord.
|
||||
"""
|
||||
# If the message author is the bot itself,
|
||||
# do not respond to this message
|
||||
if message.author == self._client.user:
|
||||
return
|
||||
|
||||
# If allowed channel IDs are provided,
|
||||
# only respond to messages in those channels
|
||||
if self.channel_ids and message.channel.id not in self.channel_ids:
|
||||
return
|
||||
|
||||
# Only respond to messages that mention the bot
|
||||
if not self._client.user or not self._client.user.mentioned_in(
|
||||
message
|
||||
):
|
||||
return
|
||||
|
||||
logger.info(f"Received message: {message.content}")
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
return self._client
|
||||
@@ -1,30 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from .models import (
|
||||
SlackAppMentionEventBody,
|
||||
SlackAppMentionEventProfile,
|
||||
SlackAuthProfile,
|
||||
SlackEventBody,
|
||||
SlackEventProfile,
|
||||
)
|
||||
from .slack_app import SlackApp
|
||||
|
||||
__all__ = [
|
||||
'SlackApp',
|
||||
'SlackAppMentionEventBody',
|
||||
'SlackAppMentionEventProfile',
|
||||
'SlackAuthProfile',
|
||||
'SlackEventBody',
|
||||
'SlackEventProfile',
|
||||
]
|
||||
@@ -1,158 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class SlackAuthProfile(BaseModel):
|
||||
r"""Represents the authorization profile within a Slack event.
|
||||
|
||||
Events will contain a single, compact authorizations field that shows one
|
||||
installation of your app that the event is visible to.
|
||||
In other words, lists of authorizations will be truncated to one element.
|
||||
|
||||
If there's more than one installing party that your app is keeping track
|
||||
of, it's best not to rely on the single party listed in authorizations to
|
||||
be any particular one.
|
||||
|
||||
To get a full list of who can see events, call the apps.event.
|
||||
authorizations.list method after obtaining an app-level token. Read more on
|
||||
the changes here; they have taken effect for existing apps as of
|
||||
February 24, 2021.
|
||||
|
||||
References:
|
||||
|
||||
- https://api.slack.com/apis/events-api#authorizations
|
||||
- https://api.slack.com/changelog/2020-09-15-events-api-truncate-authed-users#no_context
|
||||
"""
|
||||
|
||||
enterprise_id: Optional[str] = None
|
||||
"""The ID of the enterprise associated with the authorization."""
|
||||
|
||||
team_id: str
|
||||
"""The ID of the team associated with the authorization."""
|
||||
|
||||
user_id: str
|
||||
"""The ID of the user associated with the authorization."""
|
||||
|
||||
is_bot: bool
|
||||
"""Whether the authorized user is a bot."""
|
||||
|
||||
is_enterprise_install: bool
|
||||
"""Whether the authorization is for an enterprise installation."""
|
||||
|
||||
|
||||
class SlackEventProfile(BaseModel):
|
||||
r"""Represents the detailed profile of a Slack event, including user,
|
||||
message, and context data.
|
||||
"""
|
||||
|
||||
user: str
|
||||
"""The ID of the user associated with the event."""
|
||||
|
||||
type: str
|
||||
"""The type of the event (e.g., 'message')."""
|
||||
|
||||
ts: str
|
||||
"""A timestamp representing when the event was triggered."""
|
||||
|
||||
thread_ts: Optional[str] = None
|
||||
"""The timestamp of the parent message in a thread."""
|
||||
|
||||
client_msg_id: str
|
||||
"""A unique ID generated by the client for the message (if available)."""
|
||||
|
||||
text: str
|
||||
"""The message content text."""
|
||||
|
||||
team: str
|
||||
"""The ID of the team that the event is associated with."""
|
||||
|
||||
blocks: list
|
||||
"""The list of message blocks, providing structured information."""
|
||||
|
||||
channel: str
|
||||
"""The ID of the Slack channel where the event happened."""
|
||||
|
||||
event_ts: str
|
||||
"""The event-specific timestamp when it occurred."""
|
||||
|
||||
channel_type: Optional[str]
|
||||
"""The type of Slack channel (e.g., 'channel', 'im')."""
|
||||
|
||||
|
||||
class SlackEventBody(BaseModel):
|
||||
r"""Represents the entire body of a Slack event, including the event
|
||||
profile, authorization, and context.
|
||||
"""
|
||||
|
||||
token: str
|
||||
"""The token to verify the source of the event."""
|
||||
|
||||
team_id: str
|
||||
"""The ID of the team where the event is happening."""
|
||||
|
||||
context_team_id: Optional[str]
|
||||
"""The team ID for the shared channel context, if applicable."""
|
||||
|
||||
context_enterprise_id: Optional[str] = None
|
||||
"""The enterprise ID for the shared channel context, if applicable."""
|
||||
|
||||
api_app_id: str
|
||||
"""The unique identifier for the Slack app that received the event."""
|
||||
|
||||
event: SlackEventProfile
|
||||
"""A detailed profile of the event"""
|
||||
|
||||
type: str
|
||||
"""The overall type of event received (e.g., 'event_callback')."""
|
||||
|
||||
event_id: str
|
||||
"""A unique identifier assigned to this event by Slack."""
|
||||
|
||||
event_time: int
|
||||
"""The timestamp (in seconds) representing when the event was triggered."""
|
||||
|
||||
authorizations: Optional[list[SlackAuthProfile]] = None
|
||||
"""An optional list of authorizations that describe which installation can
|
||||
see the event."""
|
||||
|
||||
is_ext_shared_channel: bool
|
||||
"""Indicates if the event is part of a shared channel between different
|
||||
organizations."""
|
||||
|
||||
event_context: str
|
||||
"""A unique string representing the context of the event."""
|
||||
|
||||
|
||||
class SlackAppMentionEventProfile(SlackEventProfile):
|
||||
r"""Represents the detailed profile of a Slack event where the app was
|
||||
mentioned in a message.
|
||||
"""
|
||||
|
||||
channel_type: Optional[str] = None
|
||||
"""The type of Slack channel. it's None for app mentions."""
|
||||
|
||||
|
||||
class SlackAppMentionEventBody(SlackEventBody):
|
||||
r"""Represents the entire body of a Slack event where the app was mentioned
|
||||
in a message.
|
||||
"""
|
||||
|
||||
context_team_id: Optional[str] = None
|
||||
"""A detailed profile of the event. it's None for app mentions."""
|
||||
|
||||
event: SlackAppMentionEventProfile
|
||||
"""A detailed profile of the event"""
|
||||
@@ -1,255 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional
|
||||
|
||||
from slack_sdk.oauth.installation_store.async_installation_store import (
|
||||
AsyncInstallationStore,
|
||||
)
|
||||
from starlette import requests, responses
|
||||
|
||||
from camel.bots.slack.models import (
|
||||
SlackAppMentionEventBody,
|
||||
SlackAppMentionEventProfile,
|
||||
SlackEventBody,
|
||||
SlackEventProfile,
|
||||
)
|
||||
from camel.utils import dependencies_required
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from slack_bolt.context.async_context import AsyncBoltContext
|
||||
from slack_bolt.context.say.async_say import AsyncSay
|
||||
from slack_sdk.web.async_client import AsyncWebClient
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SlackApp:
|
||||
r"""Represents a Slack app that is powered by a Slack Bolt `AsyncApp`.
|
||||
|
||||
This class is responsible for initializing and managing the Slack
|
||||
application by setting up event handlers, running the app server, and
|
||||
handling events such as messages and mentions from Slack.
|
||||
|
||||
Args:
|
||||
token (Optional[str]): Slack API token for authentication.
|
||||
scopes (Optional[str]): Slack app scopes for permissions.
|
||||
signing_secret (Optional[str]): Signing secret for verifying Slack
|
||||
requests.
|
||||
client_id (Optional[str]): Slack app client ID.
|
||||
client_secret (Optional[str]): Slack app client secret.
|
||||
redirect_uri_path (str): The URI path for OAuth redirect, defaults to
|
||||
"/slack/oauth_redirect".
|
||||
installation_store (Optional[AsyncInstallationStore]): The installation
|
||||
store for handling OAuth installations.
|
||||
"""
|
||||
|
||||
@dependencies_required('slack_bolt')
|
||||
def __init__(
|
||||
self,
|
||||
token: Optional[str] = None,
|
||||
scopes: Optional[str] = None,
|
||||
signing_secret: Optional[str] = None,
|
||||
client_id: Optional[str] = None,
|
||||
client_secret: Optional[str] = None,
|
||||
redirect_uri_path: str = "/slack/oauth_redirect",
|
||||
installation_store: Optional[AsyncInstallationStore] = None,
|
||||
) -> None:
|
||||
r"""Initializes the SlackApp instance by setting up the Slack Bolt app
|
||||
and configuring event handlers and OAuth settings.
|
||||
|
||||
Args:
|
||||
token (Optional[str]): The Slack API token.
|
||||
scopes (Optional[str]): The scopes for Slack app permissions.
|
||||
signing_secret (Optional[str]): The signing secret for verifying
|
||||
requests.
|
||||
client_id (Optional[str]): The Slack app client ID.
|
||||
client_secret (Optional[str]): The Slack app client secret.
|
||||
redirect_uri_path (str): The URI path for handling OAuth redirects
|
||||
(default is "/slack/oauth_redirect").
|
||||
installation_store (Optional[AsyncInstallationStore]): An optional
|
||||
installation store for OAuth installations.
|
||||
"""
|
||||
from slack_bolt.adapter.starlette.async_handler import (
|
||||
AsyncSlackRequestHandler,
|
||||
)
|
||||
from slack_bolt.app.async_app import AsyncApp
|
||||
from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings
|
||||
|
||||
self.token: Optional[str] = token or os.getenv("SLACK_TOKEN")
|
||||
self.scopes: Optional[str] = scopes or os.getenv("SLACK_SCOPES")
|
||||
self.signing_secret: Optional[str] = signing_secret or os.getenv(
|
||||
"SLACK_SIGNING_SECRET"
|
||||
)
|
||||
self.client_id: Optional[str] = client_id or os.getenv(
|
||||
"SLACK_CLIENT_ID"
|
||||
)
|
||||
self.client_secret: Optional[str] = client_secret or os.getenv(
|
||||
"SLACK_CLIENT_SECRET"
|
||||
)
|
||||
|
||||
if not all([self.token, self.scopes, self.signing_secret]):
|
||||
raise ValueError(
|
||||
"`SLACK_TOKEN`, `SLACK_SCOPES`, and `SLACK_SIGNING_SECRET` "
|
||||
"environment variables must be set. Get it here: "
|
||||
"`https://api.slack.com/apps`."
|
||||
)
|
||||
|
||||
# Setup OAuth settings if client ID and secret are provided
|
||||
if self.client_id and self.client_secret:
|
||||
self._app = AsyncApp(
|
||||
oauth_settings=AsyncOAuthSettings(
|
||||
client_id=self.client_id,
|
||||
client_secret=self.client_secret,
|
||||
scopes=self.scopes,
|
||||
redirect_uri_path=redirect_uri_path,
|
||||
),
|
||||
logger=logger,
|
||||
signing_secret=self.signing_secret,
|
||||
installation_store=installation_store,
|
||||
token=self.token,
|
||||
)
|
||||
else:
|
||||
# Initialize Slack Bolt AsyncApp with settings
|
||||
self._app = AsyncApp(
|
||||
logger=logger,
|
||||
signing_secret=self.signing_secret,
|
||||
installation_store=installation_store,
|
||||
token=self.token,
|
||||
)
|
||||
|
||||
self._handler = AsyncSlackRequestHandler(self._app)
|
||||
self.setup_handlers()
|
||||
|
||||
def setup_handlers(self) -> None:
|
||||
r"""Sets up the event handlers for Slack events, such as `app_mention`
|
||||
and `message`.
|
||||
|
||||
This method registers the `app_mention` and `on_message` event handlers
|
||||
with the Slack Bolt app to respond to Slack events.
|
||||
"""
|
||||
self._app.event("app_mention")(self.app_mention)
|
||||
self._app.event("message")(self.on_message)
|
||||
|
||||
def run(
|
||||
self,
|
||||
port: int = 3000,
|
||||
path: str = "/slack/events",
|
||||
host: Optional[str] = None,
|
||||
) -> None:
|
||||
r"""Starts the Slack Bolt app server to listen for incoming Slack
|
||||
events.
|
||||
|
||||
Args:
|
||||
port (int): The port on which the server should run (default is
|
||||
3000).
|
||||
path (str): The endpoint path for receiving Slack events (default
|
||||
is "/slack/events").
|
||||
host (Optional[str]): The hostname to bind the server (default is
|
||||
None).
|
||||
"""
|
||||
self._app.start(port=port, path=path, host=host)
|
||||
|
||||
async def handle_request(
|
||||
self, request: requests.Request
|
||||
) -> responses.Response:
|
||||
r"""Handles incoming requests from Slack through the request handler.
|
||||
|
||||
Args:
|
||||
request (Request): A Starlette request object representing the
|
||||
incoming request.
|
||||
|
||||
Returns:
|
||||
The response generated by the Slack Bolt handler.
|
||||
"""
|
||||
return await self._handler.handle(request)
|
||||
|
||||
async def app_mention(
|
||||
self,
|
||||
context: "AsyncBoltContext",
|
||||
client: "AsyncWebClient",
|
||||
event: Dict[str, Any],
|
||||
body: Dict[str, Any],
|
||||
say: "AsyncSay",
|
||||
) -> None:
|
||||
r"""Event handler for `app_mention` events.
|
||||
|
||||
This method is triggered when someone mentions the app in Slack.
|
||||
|
||||
Args:
|
||||
context (AsyncBoltContext): The Slack Bolt context for the event.
|
||||
client (AsyncWebClient): The Slack Web API client.
|
||||
event (Dict[str, Any]): The event data for the app mention.
|
||||
body (Dict[str, Any]): The full request body from Slack.
|
||||
say (AsyncSay): A function to send a response back to the channel.
|
||||
"""
|
||||
event_profile = SlackAppMentionEventProfile(**event)
|
||||
event_body = SlackAppMentionEventBody(**body)
|
||||
|
||||
logger.info(f"app_mention, context: {context}")
|
||||
logger.info(f"app_mention, client: {client}")
|
||||
logger.info(f"app_mention, event_profile: {event_profile}")
|
||||
logger.info(f"app_mention, event_body: {event_body}")
|
||||
logger.info(f"app_mention, say: {say}")
|
||||
|
||||
async def on_message(
|
||||
self,
|
||||
context: "AsyncBoltContext",
|
||||
client: "AsyncWebClient",
|
||||
event: Dict[str, Any],
|
||||
body: Dict[str, Any],
|
||||
say: "AsyncSay",
|
||||
) -> None:
|
||||
r"""Event handler for `message` events.
|
||||
|
||||
This method is triggered when the app receives a message in Slack.
|
||||
|
||||
Args:
|
||||
context (AsyncBoltContext): The Slack Bolt context for the event.
|
||||
client (AsyncWebClient): The Slack Web API client.
|
||||
event (Dict[str, Any]): The event data for the message.
|
||||
body (Dict[str, Any]): The full request body from Slack.
|
||||
say (AsyncSay): A function to send a response back to the channel.
|
||||
"""
|
||||
await context.ack()
|
||||
|
||||
event_profile = SlackEventProfile(**event)
|
||||
event_body = SlackEventBody(**body)
|
||||
|
||||
logger.info(f"on_message, context: {context}")
|
||||
logger.info(f"on_message, client: {client}")
|
||||
logger.info(f"on_message, event_profile: {event_profile}")
|
||||
logger.info(f"on_message, event_body: {event_body}")
|
||||
logger.info(f"on_message, say: {say}")
|
||||
|
||||
logger.info(f"Received message: {event_profile.text}")
|
||||
|
||||
def mention_me(
|
||||
self, context: "AsyncBoltContext", body: SlackEventBody
|
||||
) -> bool:
|
||||
r"""Check if the bot is mentioned in the message.
|
||||
|
||||
Args:
|
||||
context (AsyncBoltContext): The Slack Bolt context for the event.
|
||||
body (SlackEventBody): The body of the Slack event.
|
||||
|
||||
Returns:
|
||||
bool: True if the bot is mentioned in the message, False otherwise.
|
||||
"""
|
||||
message = body.event.text
|
||||
bot_user_id = context.bot_user_id
|
||||
mention = f"<@{bot_user_id}>"
|
||||
return mention in message
|
||||
@@ -1,82 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
import os
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from camel.agents import ChatAgent
|
||||
from camel.messages import BaseMessage
|
||||
from camel.utils import dependencies_required
|
||||
|
||||
# Conditionally import telebot types only for type checking
|
||||
if TYPE_CHECKING:
|
||||
from telebot.types import ( # type: ignore[import-untyped]
|
||||
Message,
|
||||
)
|
||||
|
||||
|
||||
class TelegramBot:
|
||||
r"""Represents a Telegram bot that is powered by an agent.
|
||||
|
||||
Attributes:
|
||||
chat_agent (ChatAgent): Chat agent that will power the bot.
|
||||
telegram_token (str, optional): The bot token.
|
||||
"""
|
||||
|
||||
@dependencies_required('telebot')
|
||||
def __init__(
|
||||
self,
|
||||
chat_agent: ChatAgent,
|
||||
telegram_token: Optional[str] = None,
|
||||
) -> None:
|
||||
self.chat_agent = chat_agent
|
||||
|
||||
if not telegram_token:
|
||||
self.token = os.getenv('TELEGRAM_TOKEN')
|
||||
if not self.token:
|
||||
raise ValueError(
|
||||
"`TELEGRAM_TOKEN` not found in environment variables. "
|
||||
"Get it from t.me/BotFather."
|
||||
)
|
||||
else:
|
||||
self.token = telegram_token
|
||||
|
||||
import telebot # type: ignore[import-untyped]
|
||||
|
||||
self.bot = telebot.TeleBot(token=self.token)
|
||||
|
||||
# Register the message handler within the constructor
|
||||
self.bot.message_handler(func=lambda message: True)(self.on_message)
|
||||
|
||||
def run(self) -> None:
|
||||
r"""Start the Telegram bot."""
|
||||
print("Telegram bot is running...")
|
||||
self.bot.infinity_polling()
|
||||
|
||||
def on_message(self, message: 'Message') -> None:
|
||||
r"""Handles incoming messages from the user.
|
||||
|
||||
Args:
|
||||
message (types.Message): The incoming message object.
|
||||
"""
|
||||
self.chat_agent.reset()
|
||||
|
||||
if not message.text:
|
||||
return
|
||||
|
||||
user_msg = BaseMessage.make_user_message(
|
||||
role_name="User", content=message.text
|
||||
)
|
||||
assistant_response = self.chat_agent.step(user_msg)
|
||||
|
||||
self.bot.reply_to(message, assistant_response.msg.content)
|
||||
@@ -1,76 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfig
|
||||
from .base_config import BaseConfig
|
||||
from .cohere_config import COHERE_API_PARAMS, CohereConfig
|
||||
from .deepseek_config import DEEPSEEK_API_PARAMS, DeepSeekConfig
|
||||
from .gemini_config import Gemini_API_PARAMS, GeminiConfig
|
||||
from .groq_config import GROQ_API_PARAMS, GroqConfig
|
||||
from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
|
||||
from .mistral_config import MISTRAL_API_PARAMS, MistralConfig
|
||||
from .nvidia_config import NVIDIA_API_PARAMS, NvidiaConfig
|
||||
from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
|
||||
from .openai_config import OPENAI_API_PARAMS, ChatGPTConfig
|
||||
from .qwen_config import QWEN_API_PARAMS, QwenConfig
|
||||
from .reka_config import REKA_API_PARAMS, RekaConfig
|
||||
from .samba_config import (
|
||||
SAMBA_CLOUD_API_PARAMS,
|
||||
SAMBA_VERSE_API_PARAMS,
|
||||
SambaCloudAPIConfig,
|
||||
SambaVerseAPIConfig,
|
||||
)
|
||||
from .togetherai_config import TOGETHERAI_API_PARAMS, TogetherAIConfig
|
||||
from .vllm_config import VLLM_API_PARAMS, VLLMConfig
|
||||
from .yi_config import YI_API_PARAMS, YiConfig
|
||||
from .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig
|
||||
|
||||
__all__ = [
|
||||
'BaseConfig',
|
||||
'ChatGPTConfig',
|
||||
'OPENAI_API_PARAMS',
|
||||
'AnthropicConfig',
|
||||
'ANTHROPIC_API_PARAMS',
|
||||
'GROQ_API_PARAMS',
|
||||
'GroqConfig',
|
||||
'LiteLLMConfig',
|
||||
'LITELLM_API_PARAMS',
|
||||
'NvidiaConfig',
|
||||
'NVIDIA_API_PARAMS',
|
||||
'OllamaConfig',
|
||||
'OLLAMA_API_PARAMS',
|
||||
'ZhipuAIConfig',
|
||||
'ZHIPUAI_API_PARAMS',
|
||||
'GeminiConfig',
|
||||
'Gemini_API_PARAMS',
|
||||
'VLLMConfig',
|
||||
'VLLM_API_PARAMS',
|
||||
'MistralConfig',
|
||||
'MISTRAL_API_PARAMS',
|
||||
'RekaConfig',
|
||||
'REKA_API_PARAMS',
|
||||
'SambaVerseAPIConfig',
|
||||
'SAMBA_VERSE_API_PARAMS',
|
||||
'SambaCloudAPIConfig',
|
||||
'SAMBA_CLOUD_API_PARAMS',
|
||||
'TogetherAIConfig',
|
||||
'TOGETHERAI_API_PARAMS',
|
||||
'CohereConfig',
|
||||
'COHERE_API_PARAMS',
|
||||
'YiConfig',
|
||||
'YI_API_PARAMS',
|
||||
'QwenConfig',
|
||||
'QWEN_API_PARAMS',
|
||||
'DeepSeekConfig',
|
||||
'DEEPSEEK_API_PARAMS',
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,69 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class AnthropicConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Anthropic API.
|
||||
|
||||
See: https://docs.anthropic.com/claude/reference/complete_post
|
||||
Args:
|
||||
max_tokens (int, optional): The maximum number of tokens to
|
||||
generate before stopping. Note that Anthropic models may stop
|
||||
before reaching this maximum. This parameter only specifies the
|
||||
absolute maximum number of tokens to generate.
|
||||
(default: :obj:`256`)
|
||||
stop_sequences (List[str], optional): Sequences that will cause the
|
||||
model to stop generating completion text. Anthropic models stop
|
||||
on "\n\nHuman:", and may include additional built-in stop sequences
|
||||
in the future. By providing the stop_sequences parameter, you may
|
||||
include additional strings that will cause the model to stop
|
||||
generating.
|
||||
temperature (float, optional): Amount of randomness injected into the
|
||||
response. Defaults to 1. Ranges from 0 to 1. Use temp closer to 0
|
||||
for analytical / multiple choice, and closer to 1 for creative
|
||||
and generative tasks.
|
||||
(default: :obj:`1`)
|
||||
top_p (float, optional): Use nucleus sampling. In nucleus sampling, we
|
||||
compute the cumulative distribution over all the options for each
|
||||
subsequent token in decreasing probability order and cut it off
|
||||
once it reaches a particular probability specified by `top_p`.
|
||||
You should either alter `temperature` or `top_p`,
|
||||
but not both.
|
||||
(default: :obj:`0.7`)
|
||||
top_k (int, optional): Only sample from the top K options for each
|
||||
subsequent token. Used to remove "long tail" low probability
|
||||
responses.
|
||||
(default: :obj:`5`)
|
||||
metadata: An object describing metadata about the request.
|
||||
stream (bool, optional): Whether to incrementally stream the response
|
||||
using server-sent events. (default: :obj:`False`)
|
||||
"""
|
||||
|
||||
max_tokens: int = 256
|
||||
stop_sequences: Union[List[str], NotGiven] = NOT_GIVEN
|
||||
temperature: float = 1
|
||||
top_p: Union[float, NotGiven] = NOT_GIVEN
|
||||
top_k: Union[int, NotGiven] = NOT_GIVEN
|
||||
metadata: NotGiven = NOT_GIVEN
|
||||
stream: bool = False
|
||||
|
||||
|
||||
ANTHROPIC_API_PARAMS = {param for param in AnthropicConfig.model_fields.keys()}
|
||||
@@ -1,89 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, field_validator
|
||||
|
||||
|
||||
class BaseConfig(ABC, BaseModel):
|
||||
r"""Base configuration class for all models.
|
||||
|
||||
This class provides a common interface for all models, ensuring that all
|
||||
models have a consistent set of attributes and methods.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
frozen=True,
|
||||
# UserWarning: conflict with protected namespace "model_"
|
||||
protected_namespaces=(),
|
||||
)
|
||||
|
||||
tools: Optional[List[Any]] = None
|
||||
"""A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
"""
|
||||
|
||||
@field_validator("tools", mode="before")
|
||||
@classmethod
|
||||
def fields_type_checking(cls, tools):
|
||||
r"""Validate the type of tools in the configuration.
|
||||
|
||||
This method ensures that the tools provided in the configuration are
|
||||
instances of `FunctionTool`. If any tool is not an instance of
|
||||
`FunctionTool`, it raises a ValueError.
|
||||
"""
|
||||
if tools is not None:
|
||||
from camel.toolkits import FunctionTool
|
||||
|
||||
for tool in tools:
|
||||
if not isinstance(tool, FunctionTool):
|
||||
raise ValueError(
|
||||
f"The tool {tool} should "
|
||||
"be an instance of `FunctionTool`."
|
||||
)
|
||||
return tools
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
r"""Convert the current configuration to a dictionary.
|
||||
|
||||
This method converts the current configuration object to a dictionary
|
||||
representation, which can be used for serialization or other purposes.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: A dictionary representation of the current
|
||||
configuration.
|
||||
"""
|
||||
config_dict = self.model_dump()
|
||||
|
||||
tools_schema = None
|
||||
if self.tools:
|
||||
from camel.toolkits import FunctionTool
|
||||
|
||||
tools_schema = []
|
||||
for tool in self.tools:
|
||||
if not isinstance(tool, FunctionTool):
|
||||
raise ValueError(
|
||||
f"The tool {tool} should "
|
||||
"be an instance of `FunctionTool`."
|
||||
)
|
||||
tools_schema.append(tool.get_openai_tool_schema())
|
||||
config_dict["tools"] = tools_schema
|
||||
return config_dict
|
||||
@@ -1,76 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
|
||||
|
||||
class CohereConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Cohere API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.3`)
|
||||
documents (list, optional): A list of relevant documents that the
|
||||
model can cite to generate a more accurate reply. Each document is
|
||||
either a string or document object with content and metadata.
|
||||
(default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens the model
|
||||
will generate as part of the response. (default: :obj:`None`)
|
||||
stop_sequences (List(str), optional): A list of up to 5 strings that
|
||||
the model will use to stop generation. If the model generates a
|
||||
string that matches any of the strings in the list, it will stop
|
||||
generating tokens and return the generated text up to that point
|
||||
not including the stop sequence. (default: :obj:`None`)
|
||||
seed (int, optional): If specified, the backend will make a best
|
||||
effort to sample tokens deterministically, such that repeated
|
||||
requests with the same seed and parameters should return the same
|
||||
result. However, determinism cannot be totally guaranteed.
|
||||
(default: :obj:`None`)
|
||||
frequency_penalty (float, optional): Min value of `0.0`, max value of
|
||||
`1.0`. Used to reduce repetitiveness of generated tokens. The
|
||||
higher the value, the stronger a penalty is applied to previously
|
||||
present tokens, proportional to how many times they have already
|
||||
appeared in the prompt or prior generation. (default: :obj:`0.0`)
|
||||
presence_penalty (float, optional): Min value of `0.0`, max value of
|
||||
`1.0`. Used to reduce repetitiveness of generated tokens. Similar
|
||||
to `frequency_penalty`, except that this penalty is applied
|
||||
equally to all tokens that have already appeared, regardless of
|
||||
their exact frequencies. (default: :obj:`0.0`)
|
||||
k (int, optional): Ensures only the top k most likely tokens are
|
||||
considered for generation at each step. Min value of `0`, max
|
||||
value of `500`. (default: :obj:`0`)
|
||||
p (float, optional): Ensures that only the most likely tokens, with
|
||||
total probability mass of `p`, are considered for generation at
|
||||
each step. If both k and p are enabled, `p` acts after `k`. Min
|
||||
value of `0.01`, max value of `0.99`. (default: :obj:`0.75`)
|
||||
"""
|
||||
|
||||
temperature: Optional[float] = 0.2
|
||||
documents: Optional[list] = None
|
||||
max_tokens: Optional[int] = None
|
||||
stop_sequences: Optional[List[str]] = None
|
||||
seed: Optional[int] = None
|
||||
frequency_penalty: Optional[float] = 0.0
|
||||
presence_penalty: Optional[float] = 0.0
|
||||
k: Optional[int] = 0
|
||||
p: Optional[float] = 0.75
|
||||
|
||||
|
||||
COHERE_API_PARAMS = {param for param in CohereConfig().model_fields.keys()}
|
||||
@@ -1,134 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional, Sequence, Type, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class DeepSeekConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
DeepSeek API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): Controls the diversity and focus of the
|
||||
generated results. Higher values make the output more diverse,
|
||||
while lower values make it more focused. (default: :obj:`1.0`)
|
||||
response_format (object, optional): Specifies the format of the
|
||||
returned content. The available values are `{"type": "text"}` or
|
||||
`{"type": "json_object"}`. Setting it to `{"type": "json_object"}`
|
||||
will output a standard JSON string.
|
||||
(default: :obj:`{"type": "text"}`)
|
||||
stream (bool, optional): If set, partial message deltas will be sent.
|
||||
Tokens will be sent as data-only server-sent events (SSE) as
|
||||
they become available, with the stream terminated by a
|
||||
data: [DONE] message. (default: :obj:`False`)
|
||||
stop (Union[str, list[str]], optional): Up to 16 sequences where
|
||||
the API will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens that can
|
||||
be generated in the chat completion. The total length of input
|
||||
tokens and generated tokens is limited by the model's context
|
||||
length. (default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between -2.0 and 2.0.
|
||||
Positive values penalize new tokens based on whether they
|
||||
appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between -2.0 and 2.0.
|
||||
Positive values penalize new tokens based on their existing
|
||||
frequency in the text so far, decreasing the model's likelihood
|
||||
to repeat the same line verbatim. (default: :obj:`0`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use
|
||||
this to provide a list of functions the model may generate JSON
|
||||
inputs for. A max of 128 functions are supported.
|
||||
(default: :obj:`None`)
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which
|
||||
(if any) tool is called by the model. "none" means the model
|
||||
will not call any tool and instead generates a message. "auto"
|
||||
means the model can pick between generating a message or calling
|
||||
one or more tools. "required" means the model must call one or
|
||||
more tools. Specifying a particular tool via
|
||||
{"type": "function", "function": {"name": "my_function"}} forces
|
||||
the model to call that tool. "none" is the default when no tools
|
||||
are present. "auto" is the default if tools are present.
|
||||
(default: :obj:`"auto"`)
|
||||
logprobs (bool, optional): Whether to return log probabilities of
|
||||
the output tokens or not. If true, returns the log probabilities
|
||||
of each output token returned in the content of message.
|
||||
(default: :obj:`False`)
|
||||
top_logprobs (int, optional): An integer between 0 and 20 specifying
|
||||
the number of most likely tokens to return at each token
|
||||
position, each with an associated log probability. logprobs
|
||||
must be set to true if this parameter is used.
|
||||
(default: :obj:`None`)
|
||||
include_usage (bool, optional): When streaming, specifies whether to
|
||||
include usage information in `stream_options`. (default:
|
||||
:obj:`True`)
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # deepseek default: 1.0
|
||||
top_p: float = 1.0
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
logprobs: bool = False
|
||||
top_logprobs: Optional[int] = None
|
||||
|
||||
def __init__(self, include_usage: bool = True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
# Only set stream_options when stream is True
|
||||
# Otherwise, it will raise error when calling the API
|
||||
if self.stream:
|
||||
self.stream_options = {"include_usage": include_usage}
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
r"""Convert the current configuration to a dictionary.
|
||||
|
||||
This method converts the current configuration object to a dictionary
|
||||
representation, which can be used for serialization or other purposes.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: A dictionary representation of the current
|
||||
configuration.
|
||||
"""
|
||||
config_dict = self.model_dump()
|
||||
if self.tools:
|
||||
from camel.toolkits import FunctionTool
|
||||
|
||||
tools_schema = []
|
||||
for tool in self.tools:
|
||||
if not isinstance(tool, FunctionTool):
|
||||
raise ValueError(
|
||||
f"The tool {tool} should "
|
||||
"be an instance of `FunctionTool`."
|
||||
)
|
||||
tools_schema.append(tool.get_openai_tool_schema())
|
||||
config_dict["tools"] = NOT_GIVEN
|
||||
return config_dict
|
||||
|
||||
|
||||
DEEPSEEK_API_PARAMS = {param for param in DeepSeekConfig.model_fields.keys()}
|
||||
@@ -1,114 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional, Sequence, Type, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class GeminiConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Gemini API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` means the
|
||||
model must call one or more tools. Specifying a particular tool
|
||||
via {"type": "function", "function": {"name": "my_function"}}
|
||||
forces the model to call that tool. :obj:`"none"` is the default
|
||||
when no tools are present. :obj:`"auto"` is the default if tools
|
||||
are present.
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
r"""Convert the current configuration to a dictionary.
|
||||
|
||||
This method converts the current configuration object to a dictionary
|
||||
representation, which can be used for serialization or other purposes.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: A dictionary representation of the current
|
||||
configuration.
|
||||
"""
|
||||
config_dict = self.model_dump()
|
||||
if self.tools:
|
||||
from camel.toolkits import FunctionTool
|
||||
|
||||
tools_schema = []
|
||||
for tool in self.tools:
|
||||
if not isinstance(tool, FunctionTool):
|
||||
raise ValueError(
|
||||
f"The tool {tool} should "
|
||||
"be an instance of `FunctionTool`."
|
||||
)
|
||||
tools_schema.append(tool.get_openai_tool_schema())
|
||||
config_dict["tools"] = NOT_GIVEN
|
||||
return config_dict
|
||||
|
||||
|
||||
Gemini_API_PARAMS = {param for param in GeminiConfig.model_fields.keys()}
|
||||
@@ -1,104 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, Sequence, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class GroqConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using OpenAI
|
||||
compatibility.
|
||||
|
||||
Reference: https://console.groq.com/docs/openai
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
user (str, optional): A unique identifier representing your end-user,
|
||||
which can help OpenAI to monitor and detect abuse.
|
||||
(default: :obj:`""`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` means the
|
||||
model must call one or more tools. Specifying a particular tool
|
||||
via {"type": "function", "function": {"name": "my_function"}}
|
||||
forces the model to call that tool. :obj:`"none"` is the default
|
||||
when no tools are present. :obj:`"auto"` is the default if tools
|
||||
are present.
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
user: str = ""
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = "auto"
|
||||
|
||||
|
||||
GROQ_API_PARAMS = {param for param in GroqConfig.model_fields.keys()}
|
||||
@@ -1,97 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
|
||||
|
||||
class LiteLLMConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
LiteLLM API.
|
||||
|
||||
Args:
|
||||
timeout (Optional[Union[float, str]], optional): Request timeout.
|
||||
(default: None)
|
||||
temperature (Optional[float], optional): Temperature parameter for
|
||||
controlling randomness. (default: None)
|
||||
top_p (Optional[float], optional): Top-p parameter for nucleus
|
||||
sampling. (default: None)
|
||||
n (Optional[int], optional): Number of completions to generate.
|
||||
(default: None)
|
||||
stream (Optional[bool], optional): Whether to return a streaming
|
||||
response. (default: None)
|
||||
stream_options (Optional[dict], optional): Options for the streaming
|
||||
response. (default: None)
|
||||
stop (Optional[Union[str, List[str]]], optional): Sequences where the
|
||||
API will stop generating further tokens. (default: None)
|
||||
max_tokens (Optional[int], optional): Maximum number of tokens to
|
||||
generate. (default: None)
|
||||
presence_penalty (Optional[float], optional): Penalize new tokens
|
||||
based on their existence in the text so far. (default: None)
|
||||
frequency_penalty (Optional[float], optional): Penalize new tokens
|
||||
based on their frequency in the text so far. (default: None)
|
||||
logit_bias (Optional[dict], optional): Modify the probability of
|
||||
specific tokens appearing in the completion. (default: None)
|
||||
user (Optional[str], optional): A unique identifier representing the
|
||||
end-user. (default: None)
|
||||
response_format (Optional[dict], optional): Response format
|
||||
parameters. (default: None)
|
||||
seed (Optional[int], optional): Random seed. (default: None)
|
||||
tools (Optional[List], optional): List of tools. (default: None)
|
||||
tool_choice (Optional[Union[str, dict]], optional): Tool choice
|
||||
parameters. (default: None)
|
||||
logprobs (Optional[bool], optional): Whether to return log
|
||||
probabilities of the output tokens. (default: None)
|
||||
top_logprobs (Optional[int], optional): Number of most likely tokens
|
||||
to return at each token position. (default: None)
|
||||
deployment_id (Optional[str], optional): Deployment ID. (default: None)
|
||||
extra_headers (Optional[dict], optional): Additional headers for the
|
||||
request. (default: None)
|
||||
api_version (Optional[str], optional): API version. (default: None)
|
||||
mock_response (Optional[str], optional): Mock completion response for
|
||||
testing or debugging. (default: None)
|
||||
custom_llm_provider (Optional[str], optional): Non-OpenAI LLM
|
||||
provider. (default: None)
|
||||
max_retries (Optional[int], optional): Maximum number of retries.
|
||||
(default: None)
|
||||
"""
|
||||
|
||||
timeout: Optional[Union[float, str]] = None
|
||||
temperature: Optional[float] = None
|
||||
top_p: Optional[float] = None
|
||||
n: Optional[int] = None
|
||||
stream: Optional[bool] = None
|
||||
stream_options: Optional[dict] = None
|
||||
stop: Optional[Union[str, List[str]]] = None
|
||||
max_tokens: Optional[int] = None
|
||||
presence_penalty: Optional[float] = None
|
||||
frequency_penalty: Optional[float] = None
|
||||
logit_bias: Optional[dict] = None
|
||||
user: Optional[str] = None
|
||||
response_format: Optional[dict] = None
|
||||
seed: Optional[int] = None
|
||||
tool_choice: Optional[Union[str, dict]] = None
|
||||
logprobs: Optional[bool] = None
|
||||
top_logprobs: Optional[int] = None
|
||||
deployment_id: Optional[str] = None
|
||||
extra_headers: Optional[dict] = None
|
||||
api_version: Optional[str] = None
|
||||
mock_response: Optional[str] = None
|
||||
custom_llm_provider: Optional[str] = None
|
||||
max_retries: Optional[int] = None
|
||||
|
||||
|
||||
LITELLM_API_PARAMS = {param for param in LiteLLMConfig.model_fields.keys()}
|
||||
@@ -1,79 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional, Union
|
||||
|
||||
from pydantic import field_validator
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
|
||||
|
||||
class MistralConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Mistral API.
|
||||
|
||||
reference: https://github.com/mistralai/client-python/blob/9d238f88c41689821d7b08570f13b43426f97fd6/src/mistralai/client.py#L195
|
||||
|
||||
#TODO: Support stream mode
|
||||
|
||||
Args:
|
||||
temperature (Optional[float], optional): temperature the temperature
|
||||
to use for sampling, e.g. 0.5.
|
||||
top_p (Optional[float], optional): the cumulative probability of
|
||||
tokens to generate, e.g. 0.9. Defaults to None.
|
||||
max_tokens (Optional[int], optional): the maximum number of tokens to
|
||||
generate, e.g. 100. Defaults to None.
|
||||
stop (Optional[Union[str,list[str]]]): Stop generation if this token
|
||||
is detected. Or if one of these tokens is detected when providing
|
||||
a string list.
|
||||
random_seed (Optional[int], optional): the random seed to use for
|
||||
sampling, e.g. 42. Defaults to None.
|
||||
safe_prompt (bool, optional): whether to use safe prompt, e.g. true.
|
||||
Defaults to False.
|
||||
response_format (Union[Dict[str, str], ResponseFormat): format of the
|
||||
response.
|
||||
tool_choice (str, optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"any"` means the
|
||||
model must call one or more tools. :obj:`"auto"` is the default
|
||||
value.
|
||||
"""
|
||||
|
||||
temperature: Optional[float] = None
|
||||
top_p: Optional[float] = None
|
||||
max_tokens: Optional[int] = None
|
||||
stop: Optional[Union[str, list[str]]] = None
|
||||
random_seed: Optional[int] = None
|
||||
safe_prompt: bool = False
|
||||
response_format: Optional[Union[Dict[str, str], Any]] = None
|
||||
tool_choice: Optional[str] = "auto"
|
||||
|
||||
@field_validator("response_format", mode="before")
|
||||
@classmethod
|
||||
def fields_type_checking(cls, response_format):
|
||||
if response_format and not isinstance(response_format, dict):
|
||||
from mistralai.models import ResponseFormat
|
||||
|
||||
if not isinstance(response_format, ResponseFormat):
|
||||
raise ValueError(
|
||||
f"The tool {response_format} should be an instance "
|
||||
"of `mistralai.models.ResponseFormat`."
|
||||
)
|
||||
return response_format
|
||||
|
||||
|
||||
MISTRAL_API_PARAMS = {param for param in MistralConfig().model_fields.keys()}
|
||||
@@ -1,70 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class NvidiaConfig(BaseConfig):
|
||||
r"""Configuration class for NVIDIA API models.
|
||||
|
||||
This class defines the configuration parameters for NVIDIA's language
|
||||
models, including temperature, sampling parameters, and response format
|
||||
settings.
|
||||
|
||||
Args:
|
||||
stream (bool, optional): Whether to stream the response.
|
||||
(default: :obj:`False`)
|
||||
temperature (float, optional): Controls randomness in the response.
|
||||
Higher values make output more random, lower values make it more
|
||||
deterministic. Range: [0.0, 2.0]. (default: :obj:`0.7`)
|
||||
top_p (float, optional): Controls diversity via nucleus sampling.
|
||||
Range: [0.0, 1.0]. (default: :obj:`0.95`)
|
||||
presence_penalty (float, optional): Penalizes new tokens based on
|
||||
whether they appear in the text so far. Range: [-2.0, 2.0].
|
||||
(default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Penalizes new tokens based on
|
||||
their frequency in the text so far. Range: [-2.0, 2.0].
|
||||
(default: :obj:`0.0`)
|
||||
max_tokens (Union[int, NotGiven], optional): Maximum number of tokens
|
||||
to generate. If not provided, model will use its default maximum.
|
||||
(default: :obj:`NOT_GIVEN`)
|
||||
seed (Optional[int], optional): Random seed for deterministic sampling.
|
||||
(default: :obj:`None`)
|
||||
tools (Optional[List[Dict]], optional): List of tools available to the
|
||||
model. This includes tools such as a text editor, a calculator, or
|
||||
a search engine. (default: :obj:`None`)
|
||||
tool_choice (Optional[str], optional): Tool choice configuration.
|
||||
(default: :obj:`None`)
|
||||
stop (Optional[List[str]], optional): List of stop sequences.
|
||||
(default: :obj:`None`)
|
||||
"""
|
||||
|
||||
stream: bool = Field(default=False)
|
||||
temperature: float = Field(default=0.7)
|
||||
top_p: float = Field(default=0.95)
|
||||
presence_penalty: float = Field(default=0.0)
|
||||
frequency_penalty: float = Field(default=0.0)
|
||||
max_tokens: Union[int, NotGiven] = Field(default=NOT_GIVEN)
|
||||
seed: Optional[int] = Field(default=None)
|
||||
tool_choice: Optional[str] = Field(default=None)
|
||||
stop: Optional[List[str]] = Field(default=None)
|
||||
|
||||
|
||||
NVIDIA_API_PARAMS = {param for param in NvidiaConfig.model_fields.keys()}
|
||||
@@ -1,82 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class OllamaConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using OpenAI
|
||||
compatibility
|
||||
|
||||
Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
"""
|
||||
|
||||
temperature: float = 0.2
|
||||
top_p: float = 1.0
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
|
||||
|
||||
OLLAMA_API_PARAMS = {param for param in OllamaConfig.model_fields.keys()}
|
||||
@@ -1,139 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional, Sequence, Type, Union
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class ChatGPTConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
OpenAI API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
logit_bias (dict, optional): Modify the likelihood of specified tokens
|
||||
appearing in the completion. Accepts a json object that maps tokens
|
||||
(specified by their token ID in the tokenizer) to an associated
|
||||
bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
|
||||
is added to the logits generated by the model prior to sampling.
|
||||
The exact effect will vary per model, but values between:obj:` -1`
|
||||
and :obj:`1` should decrease or increase likelihood of selection;
|
||||
values like :obj:`-100` or :obj:`100` should result in a ban or
|
||||
exclusive selection of the relevant token. (default: :obj:`{}`)
|
||||
user (str, optional): A unique identifier representing your end-user,
|
||||
which can help OpenAI to monitor and detect abuse.
|
||||
(default: :obj:`""`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` means the
|
||||
model must call one or more tools. Specifying a particular tool
|
||||
via {"type": "function", "function": {"name": "my_function"}}
|
||||
forces the model to call that tool. :obj:`"none"` is the default
|
||||
when no tools are present. :obj:`"auto"` is the default if tools
|
||||
are present.
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
logit_bias: dict = Field(default_factory=dict)
|
||||
user: str = ""
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
r"""Convert the current configuration to a dictionary.
|
||||
|
||||
This method converts the current configuration object to a dictionary
|
||||
representation, which can be used for serialization or other purposes.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: A dictionary representation of the current
|
||||
configuration.
|
||||
"""
|
||||
config_dict = self.model_dump()
|
||||
if self.tools:
|
||||
from camel.toolkits import FunctionTool
|
||||
|
||||
tools_schema = []
|
||||
for tool in self.tools:
|
||||
if not isinstance(tool, FunctionTool):
|
||||
raise ValueError(
|
||||
f"The tool {tool} should "
|
||||
"be an instance of `FunctionTool`."
|
||||
)
|
||||
tools_schema.append(tool.get_openai_tool_schema())
|
||||
config_dict["tools"] = NOT_GIVEN
|
||||
return config_dict
|
||||
|
||||
|
||||
OPENAI_API_PARAMS = {param for param in ChatGPTConfig.model_fields.keys()}
|
||||
@@ -1,91 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ClassVar, Optional, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class QwenConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Qwen API. You can refer to the following link for more details:
|
||||
https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api
|
||||
|
||||
Args:
|
||||
stream (bool, optional): Whether to stream the response.
|
||||
(default: :obj:`False`)
|
||||
temperature (float, optional): Controls the diversity and focus of
|
||||
the generated results. Lower values make the output more focused,
|
||||
while higher values make it more diverse. (default: :obj:`0.3`)
|
||||
top_p (float, optional): Controls the diversity and focus of the
|
||||
generated results. Higher values make the output more diverse,
|
||||
while lower values make it more focused. (default: :obj:`0.9`)
|
||||
presence_penalty (float, optional): Controls the repetition of
|
||||
content in the generated results. Positive values reduce the
|
||||
repetition of content, while negative values increase it.
|
||||
(default: :obj:`0.0`)
|
||||
response_format (object, optional): Specifies the format of the
|
||||
returned content. The available values are `{"type": "text"}` or
|
||||
`{"type": "json_object"}`. Setting it to `{"type": "json_object"}`
|
||||
will output a standard JSON string.
|
||||
(default: :obj:`{"type": "text"}`)
|
||||
max_tokens (Union[int, NotGiven], optional): Allows the model to
|
||||
generate the maximum number of tokens.
|
||||
(default: :obj:`NOT_GIVEN`)
|
||||
seed (int, optional): Sets the seed parameter to make the text
|
||||
generation process more deterministic, typically used to ensure
|
||||
that the results are consistent across model runs. By passing the
|
||||
same seed value (specified by you) in each model call while
|
||||
keeping other parameters unchanged, the model is likely to return
|
||||
the same result.
|
||||
(default: :obj:`None`)
|
||||
stop (str or list, optional): Using the stop parameter, the model will
|
||||
automatically stop generating text when it is about to include the
|
||||
specified string or token_id. You can use the stop parameter to
|
||||
control the output of the model by passing sensitive words.
|
||||
(default: :obj:`None`)
|
||||
tools (list, optional): Specifies an array of tools that the model can
|
||||
call. It can contain one or more tool objects. During a function
|
||||
call process, the model will select one tool from the array.
|
||||
(default: :obj:`None`)
|
||||
extra_body (dict, optional): Additional parameters to be sent to the
|
||||
Qwen API. If you want to enable internet search, you can set this
|
||||
parameter to `{"enable_search": True}`.
|
||||
(default: :obj:`{"enable_search": False}`)
|
||||
include_usage (bool, optional): When streaming, specifies whether to
|
||||
include usage information in `stream_options`. (default:
|
||||
:obj:`True`)
|
||||
"""
|
||||
|
||||
stream: bool = False
|
||||
temperature: float = 0.3
|
||||
top_p: float = 0.9
|
||||
presence_penalty: float = 0.0
|
||||
response_format: ClassVar[dict] = {"type": "text"}
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
seed: Optional[int] = None
|
||||
stop: Optional[Union[str, list]] = None
|
||||
extra_body: ClassVar[dict] = {"enable_search": False}
|
||||
|
||||
def __init__(self, include_usage: bool = True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
# Only set stream_options when stream is True
|
||||
# Otherwise, it will raise error when calling the API
|
||||
if self.stream:
|
||||
self.stream_options = {"include_usage": include_usage}
|
||||
|
||||
|
||||
QWEN_API_PARAMS = {param for param in QwenConfig.model_fields.keys()}
|
||||
@@ -1,74 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
|
||||
|
||||
class RekaConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Reka API.
|
||||
|
||||
Reference: https://docs.reka.ai/api-reference/chat/create
|
||||
|
||||
Args:
|
||||
temperature (Optional[float], optional): temperature the temperature
|
||||
to use for sampling, e.g. 0.5.
|
||||
top_p (Optional[float], optional): the cumulative probability of
|
||||
tokens to generate, e.g. 0.9. Defaults to None.
|
||||
top_k (Optional[int], optional): Parameter which forces the model to
|
||||
only consider the tokens with the `top_k` highest probabilities at
|
||||
the next step. Defaults to 1024.
|
||||
max_tokens (Optional[int], optional): the maximum number of tokens to
|
||||
generate, e.g. 100. Defaults to None.
|
||||
stop (Optional[Union[str,list[str]]]): Stop generation if this token
|
||||
is detected. Or if one of these tokens is detected when providing
|
||||
a string list.
|
||||
seed (Optional[int], optional): the random seed to use for sampling, e.
|
||||
g. 42. Defaults to None.
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
use_search_engine (Optional[bool]): Whether to consider using search
|
||||
engine to complete the request. Note that even if this is set to
|
||||
`True`, the model might decide to not use search.
|
||||
"""
|
||||
|
||||
temperature: Optional[float] = None
|
||||
top_p: Optional[float] = None
|
||||
top_k: Optional[int] = None
|
||||
max_tokens: Optional[int] = None
|
||||
stop: Optional[Union[str, list[str]]] = None
|
||||
seed: Optional[int] = None
|
||||
frequency_penalty: float = 0.0
|
||||
presence_penalty: float = 0.0
|
||||
use_search_engine: Optional[bool] = False
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
config_dict = super().as_dict()
|
||||
if "tools" in config_dict:
|
||||
del config_dict["tools"] # Reka does not support tool calling
|
||||
return config_dict
|
||||
|
||||
|
||||
REKA_API_PARAMS = {param for param in RekaConfig().model_fields.keys()}
|
||||
@@ -1,170 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional, Sequence, Union
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class SambaVerseAPIConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
SambaVerse API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.7`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`0.95`)
|
||||
top_k (int, optional): Only sample from the top K options for each
|
||||
subsequent token. Used to remove "long tail" low probability
|
||||
responses.
|
||||
(default: :obj:`50`)
|
||||
max_tokens (Optional[int], optional): The maximum number of tokens to
|
||||
generate, e.g. 100.
|
||||
(default: :obj:`2048`)
|
||||
repetition_penalty (Optional[float], optional): The parameter for
|
||||
repetition penalty. 1.0 means no penalty.
|
||||
(default: :obj:`1.0`)
|
||||
stop (Optional[Union[str,list[str]]]): Stop generation if this token
|
||||
is detected. Or if one of these tokens is detected when providing
|
||||
a string list.
|
||||
(default: :obj:`""`)
|
||||
stream (Optional[bool]): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
Currently SambaVerse API doesn't support stream mode.
|
||||
(default: :obj:`False`)
|
||||
"""
|
||||
|
||||
temperature: Optional[float] = 0.7
|
||||
top_p: Optional[float] = 0.95
|
||||
top_k: Optional[int] = 50
|
||||
max_tokens: Optional[int] = 2048
|
||||
repetition_penalty: Optional[float] = 1.0
|
||||
stop: Optional[Union[str, list[str]]] = ""
|
||||
stream: Optional[bool] = False
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
config_dict = super().as_dict()
|
||||
if "tools" in config_dict:
|
||||
del config_dict["tools"] # SambaNova does not support tool calling
|
||||
return config_dict
|
||||
|
||||
|
||||
SAMBA_VERSE_API_PARAMS = {
|
||||
param for param in SambaVerseAPIConfig().model_fields.keys()
|
||||
}
|
||||
|
||||
|
||||
class SambaCloudAPIConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
OpenAI API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
logit_bias (dict, optional): Modify the likelihood of specified tokens
|
||||
appearing in the completion. Accepts a json object that maps tokens
|
||||
(specified by their token ID in the tokenizer) to an associated
|
||||
bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
|
||||
is added to the logits generated by the model prior to sampling.
|
||||
The exact effect will vary per model, but values between:obj:` -1`
|
||||
and :obj:`1` should decrease or increase likelihood of selection;
|
||||
values like :obj:`-100` or :obj:`100` should result in a ban or
|
||||
exclusive selection of the relevant token. (default: :obj:`{}`)
|
||||
user (str, optional): A unique identifier representing your end-user,
|
||||
which can help OpenAI to monitor and detect abuse.
|
||||
(default: :obj:`""`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` means the
|
||||
model must call one or more tools. Specifying a particular tool
|
||||
via {"type": "function", "function": {"name": "my_function"}}
|
||||
forces the model to call that tool. :obj:`"none"` is the default
|
||||
when no tools are present. :obj:`"auto"` is the default if tools
|
||||
are present.
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
logit_bias: dict = Field(default_factory=dict)
|
||||
user: str = ""
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
|
||||
|
||||
SAMBA_CLOUD_API_PARAMS = {
|
||||
param for param in SambaCloudAPIConfig().model_fields.keys()
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Sequence, Union
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class TogetherAIConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
OpenAI API.
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
logit_bias (dict, optional): Modify the likelihood of specified tokens
|
||||
appearing in the completion. Accepts a json object that maps tokens
|
||||
(specified by their token ID in the tokenizer) to an associated
|
||||
bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
|
||||
is added to the logits generated by the model prior to sampling.
|
||||
The exact effect will vary per model, but values between:obj:` -1`
|
||||
and :obj:`1` should decrease or increase likelihood of selection;
|
||||
values like :obj:`-100` or :obj:`100` should result in a ban or
|
||||
exclusive selection of the relevant token. (default: :obj:`{}`)
|
||||
user (str, optional): A unique identifier representing your end-user,
|
||||
which can help OpenAI to monitor and detect abuse.
|
||||
(default: :obj:`""`)
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
logit_bias: dict = Field(default_factory=dict)
|
||||
user: str = ""
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
config_dict = super().as_dict()
|
||||
if "tools" in config_dict:
|
||||
del config_dict["tools"] # Currently does not support tool calling
|
||||
return config_dict
|
||||
|
||||
|
||||
TOGETHERAI_API_PARAMS = {
|
||||
param for param in TogetherAIConfig.model_fields.keys()
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, Sequence, Union
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
# flake8: noqa: E501
|
||||
class VLLMConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
OpenAI API.
|
||||
|
||||
Reference: https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`1.0`)
|
||||
n (int, optional): How many chat completion choices to generate for
|
||||
each input message. (default: :obj:`1`)
|
||||
response_format (object, optional): An object specifying the format
|
||||
that the model must output. Compatible with GPT-4 Turbo and all
|
||||
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
|
||||
{"type": "json_object"} enables JSON mode, which guarantees the
|
||||
message the model generates is valid JSON. Important: when using
|
||||
JSON mode, you must also instruct the model to produce JSON
|
||||
yourself via a system or user message. Without this, the model
|
||||
may generate an unending stream of whitespace until the generation
|
||||
reaches the token limit, resulting in a long-running and seemingly
|
||||
"stuck" request. Also note that the message content may be
|
||||
partially cut off if finish_reason="length", which indicates the
|
||||
generation exceeded max_tokens or the conversation exceeded the
|
||||
max context length.
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
presence_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on whether
|
||||
they appear in the text so far, increasing the model's likelihood
|
||||
to talk about new topics. See more information about frequency and
|
||||
presence penalties. (default: :obj:`0.0`)
|
||||
frequency_penalty (float, optional): Number between :obj:`-2.0` and
|
||||
:obj:`2.0`. Positive values penalize new tokens based on their
|
||||
existing frequency in the text so far, decreasing the model's
|
||||
likelihood to repeat the same line verbatim. See more information
|
||||
about frequency and presence penalties. (default: :obj:`0.0`)
|
||||
logit_bias (dict, optional): Modify the likelihood of specified tokens
|
||||
appearing in the completion. Accepts a json object that maps tokens
|
||||
(specified by their token ID in the tokenizer) to an associated
|
||||
bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
|
||||
is added to the logits generated by the model prior to sampling.
|
||||
The exact effect will vary per model, but values between:obj:` -1`
|
||||
and :obj:`1` should decrease or increase likelihood of selection;
|
||||
values like :obj:`-100` or :obj:`100` should result in a ban or
|
||||
exclusive selection of the relevant token. (default: :obj:`{}`)
|
||||
user (str, optional): A unique identifier representing your end-user,
|
||||
which can help OpenAI to monitor and detect abuse.
|
||||
(default: :obj:`""`)
|
||||
logprobs: Whether to return log probabilities of the output tokens or
|
||||
not. If true, returns the log probabilities of each output token
|
||||
returned in the `logits` of `message`. (default: :obj:`None`)
|
||||
top_logprobs: An integer between 0 and 20 specifying the number of
|
||||
most likely tokens to return at each token position, each with an
|
||||
associated log probability. `logprobs` must be set to `true` if
|
||||
this parameter is used. (default: :obj:`None`)
|
||||
"""
|
||||
|
||||
temperature: float = 0.2 # openai default: 1.0
|
||||
top_p: float = 1.0
|
||||
n: int = 1
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
presence_penalty: float = 0.0
|
||||
response_format: Union[dict, NotGiven] = NOT_GIVEN
|
||||
frequency_penalty: float = 0.0
|
||||
logit_bias: dict = Field(default_factory=dict)
|
||||
user: str = ""
|
||||
logprobs: Optional[bool] = None
|
||||
top_logprobs: Optional[int] = None
|
||||
|
||||
|
||||
VLLM_API_PARAMS = {param for param in VLLMConfig.model_fields.keys()}
|
||||
@@ -1,58 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class YiConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using the
|
||||
Yi API. You can refer to the following link for more details:
|
||||
https://platform.lingyiwanwu.com/docs/api-reference
|
||||
|
||||
Args:
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` or
|
||||
specifying a particular tool via
|
||||
{"type": "function", "function": {"name": "some_function"}}
|
||||
can be used to guide the model to use tools more strongly.
|
||||
(default: :obj:`None`)
|
||||
max_tokens (int, optional): Specifies the maximum number of tokens
|
||||
the model can generate. This sets an upper limit, but does not
|
||||
guarantee that this number will always be reached.
|
||||
(default: :obj:`5000`)
|
||||
top_p (float, optional): Controls the randomness of the generated
|
||||
results. Lower values lead to less randomness, while higher
|
||||
values increase randomness. (default: :obj:`0.9`)
|
||||
temperature (float, optional): Controls the diversity and focus of
|
||||
the generated results. Lower values make the output more focused,
|
||||
while higher values make it more diverse. (default: :obj:`0.3`)
|
||||
stream (bool, optional): If True, enables streaming output.
|
||||
(default: :obj:`False`)
|
||||
"""
|
||||
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
top_p: float = 0.9
|
||||
temperature: float = 0.3
|
||||
stream: bool = False
|
||||
|
||||
|
||||
YI_API_PARAMS = {param for param in YiConfig.model_fields.keys()}
|
||||
@@ -1,71 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, Sequence, Union
|
||||
|
||||
from camel.configs.base_config import BaseConfig
|
||||
from camel.types import NOT_GIVEN, NotGiven
|
||||
|
||||
|
||||
class ZhipuAIConfig(BaseConfig):
|
||||
r"""Defines the parameters for generating chat completions using OpenAI
|
||||
compatibility
|
||||
|
||||
Reference: https://open.bigmodel.cn/dev/api#glm-4v
|
||||
|
||||
Args:
|
||||
temperature (float, optional): Sampling temperature to use, between
|
||||
:obj:`0` and :obj:`2`. Higher values make the output more random,
|
||||
while lower values make it more focused and deterministic.
|
||||
(default: :obj:`0.2`)
|
||||
top_p (float, optional): An alternative to sampling with temperature,
|
||||
called nucleus sampling, where the model considers the results of
|
||||
the tokens with top_p probability mass. So :obj:`0.1` means only
|
||||
the tokens comprising the top 10% probability mass are considered.
|
||||
(default: :obj:`0.6`)
|
||||
stream (bool, optional): If True, partial message deltas will be sent
|
||||
as data-only server-sent events as they become available.
|
||||
(default: :obj:`False`)
|
||||
stop (str or list, optional): Up to :obj:`4` sequences where the API
|
||||
will stop generating further tokens. (default: :obj:`None`)
|
||||
max_tokens (int, optional): The maximum number of tokens to generate
|
||||
in the chat completion. The total length of input tokens and
|
||||
generated tokens is limited by the model's context length.
|
||||
(default: :obj:`None`)
|
||||
tools (list[FunctionTool], optional): A list of tools the model may
|
||||
call. Currently, only functions are supported as a tool. Use this
|
||||
to provide a list of functions the model may generate JSON inputs
|
||||
for. A max of 128 functions are supported.
|
||||
tool_choice (Union[dict[str, str], str], optional): Controls which (if
|
||||
any) tool is called by the model. :obj:`"none"` means the model
|
||||
will not call any tool and instead generates a message.
|
||||
:obj:`"auto"` means the model can pick between generating a
|
||||
message or calling one or more tools. :obj:`"required"` means the
|
||||
model must call one or more tools. Specifying a particular tool
|
||||
via {"type": "function", "function": {"name": "my_function"}}
|
||||
forces the model to call that tool. :obj:`"none"` is the default
|
||||
when no tools are present. :obj:`"auto"` is the default if tools
|
||||
are present.
|
||||
"""
|
||||
|
||||
temperature: float = 0.2
|
||||
top_p: float = 0.6
|
||||
stream: bool = False
|
||||
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
|
||||
max_tokens: Union[int, NotGiven] = NOT_GIVEN
|
||||
tool_choice: Optional[Union[dict[str, str], str]] = None
|
||||
|
||||
|
||||
ZHIPUAI_API_PARAMS = {param for param in ZhipuAIConfig.model_fields.keys()}
|
||||
@@ -1,23 +0,0 @@
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
||||
|
||||
from .base import BaseDatasetManager
|
||||
from .huggingface import HuggingFaceDatasetManager
|
||||
from .models import Record
|
||||
|
||||
__all__ = [
|
||||
"BaseDatasetManager",
|
||||
"Record",
|
||||
"HuggingFaceDatasetManager",
|
||||
]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user