diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..02f580a02e11f3d711350448c6f5d17f4f74b8c1 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,28 @@ +# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3-bullseye, 3.10-bullseye, 3-buster, 3.10-buster +ARG VARIANT=3-bullseye +FROM --platform=linux/amd64 python:3.10 + +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + # Remove imagemagick due to https://security-tracker.debian.org/tracker/CVE-2019-10131 + && apt-get purge -y imagemagick imagemagick-6-common + +# Temporary: Upgrade python packages due to https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-40897 +# They are installed by the base image (python) which does not have the patch. +RUN python3 -m pip install --upgrade setuptools + +# Install Chrome for web browsing +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && curl -sSL https://dl.google.com/linux/direct/google-chrome-stable_current_$(dpkg --print-architecture).deb -o /tmp/chrome.deb \ + && apt-get -y install /tmp/chrome.deb + +# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image. +# COPY requirements.txt /tmp/pip-tmp/ +# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \ +# && rm -rf /tmp/pip-tmp + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment this line to install global node packages. +# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000000000000000000000000000000000000..f26810fb540ee27db5601c47f0a6fa96403d129f --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,40 @@ +{ + "build": { + "dockerfile": "./Dockerfile", + "context": "." + }, + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": "true", + "username": "vscode", + "userUid": "1000", + "userGid": "1000", + "upgradePackages": "true" + }, + "ghcr.io/devcontainers/features/desktop-lite:1": {}, + "ghcr.io/devcontainers/features/python:1": "none", + "ghcr.io/devcontainers/features/node:1": "none", + "ghcr.io/devcontainers/features/git:1": { + "version": "latest", + "ppa": "false" + } + }, + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Set *default* container specific settings.json values on container create. + "settings": { + "python.defaultInterpreterPath": "/usr/local/bin/python" + } + } + }, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "pip3 install --user -r requirements.txt", + + // Set `remoteUser` to `root` to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode" +} diff --git a/.env.template b/.env.template new file mode 100644 index 0000000000000000000000000000000000000000..6e521af17d29e0c1af3a164203e6ab9d388def5d --- /dev/null +++ b/.env.template @@ -0,0 +1,187 @@ +################################################################################ +### AUTO-GPT - GENERAL SETTINGS +################################################################################ +# EXECUTE_LOCAL_COMMANDS - Allow local command execution (Example: False) +EXECUTE_LOCAL_COMMANDS=False +# RESTRICT_TO_WORKSPACE - Restrict file operations to workspace ./auto_gpt_workspace (Default: True) +RESTRICT_TO_WORKSPACE=True +# BROWSE_CHUNK_MAX_LENGTH - When browsing website, define the length of chunk stored in memory +BROWSE_CHUNK_MAX_LENGTH=8192 +# USER_AGENT - Define the user-agent used by the requests library to browse website (string) +# USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36" +# AI_SETTINGS_FILE - Specifies which AI Settings file to use (defaults to ai_settings.yaml) +AI_SETTINGS_FILE=ai_settings.yaml + +################################################################################ +### LLM PROVIDER +################################################################################ + +### OPENAI +# OPENAI_API_KEY - OpenAI API Key (Example: my-openai-api-key) +# TEMPERATURE - Sets temperature in OpenAI (Default: 0) +# USE_AZURE - Use Azure OpenAI or not (Default: False) +OPENAI_API_KEY=your-openai-api-key +TEMPERATURE=0 +USE_AZURE=False + +### AZURE +# cleanup azure env as already moved to `azure.yaml.template` + +################################################################################ +### LLM MODELS +################################################################################ + +# SMART_LLM_MODEL - Smart language model (Default: gpt-4) +# FAST_LLM_MODEL - Fast language model (Default: gpt-3.5-turbo) +SMART_LLM_MODEL=gpt-4 +FAST_LLM_MODEL=gpt-3.5-turbo + +### LLM MODEL SETTINGS +# FAST_TOKEN_LIMIT - Fast token limit for OpenAI (Default: 4000) +# SMART_TOKEN_LIMIT - Smart token limit for OpenAI (Default: 8000) +# When using --gpt3only this needs to be set to 4000. +FAST_TOKEN_LIMIT=4000 +SMART_TOKEN_LIMIT=8000 + +################################################################################ +### MEMORY +################################################################################ + +### MEMORY_BACKEND - Memory backend type +# local - Default +# pinecone - Pinecone (if configured) +# redis - Redis (if configured) +# milvus - Milvus (if configured) +MEMORY_BACKEND=local + +### PINECONE +# PINECONE_API_KEY - Pinecone API Key (Example: my-pinecone-api-key) +# PINECONE_ENV - Pinecone environment (region) (Example: us-west-2) +PINECONE_API_KEY=your-pinecone-api-key +PINECONE_ENV=your-pinecone-region + +### REDIS +# REDIS_HOST - Redis host (Default: localhost, use "redis" for docker-compose) +# REDIS_PORT - Redis port (Default: 6379) +# REDIS_PASSWORD - Redis password (Default: "") +# WIPE_REDIS_ON_START - Wipes data / index on start (Default: False) +# MEMORY_INDEX - Name of index created in Redis database (Default: auto-gpt) +REDIS_HOST=localhost +REDIS_PORT=6379 +REDIS_PASSWORD= +WIPE_REDIS_ON_START=False +MEMORY_INDEX=auto-gpt + +### WEAVIATE +# MEMORY_BACKEND - Use 'weaviate' to use Weaviate vector storage +# WEAVIATE_HOST - Weaviate host IP +# WEAVIATE_PORT - Weaviate host port +# WEAVIATE_PROTOCOL - Weaviate host protocol (e.g. 'http') +# USE_WEAVIATE_EMBEDDED - Whether to use Embedded Weaviate +# WEAVIATE_EMBEDDED_PATH - File system path were to persist data when running Embedded Weaviate +# WEAVIATE_USERNAME - Weaviate username +# WEAVIATE_PASSWORD - Weaviate password +# WEAVIATE_API_KEY - Weaviate API key if using API-key-based authentication +# MEMORY_INDEX - Name of index to create in Weaviate +WEAVIATE_HOST="127.0.0.1" +WEAVIATE_PORT=8080 +WEAVIATE_PROTOCOL="http" +USE_WEAVIATE_EMBEDDED=False +WEAVIATE_EMBEDDED_PATH="/home/me/.local/share/weaviate" +WEAVIATE_USERNAME= +WEAVIATE_PASSWORD= +WEAVIATE_API_KEY= +MEMORY_INDEX=AutoGpt + +### MILVUS +# MILVUS_ADDR - Milvus remote address (e.g. localhost:19530) +# MILVUS_COLLECTION - Milvus collection, +# change it if you want to start a new memory and retain the old memory. +MILVUS_ADDR=your-milvus-cluster-host-port +MILVUS_COLLECTION=autogpt + +################################################################################ +### IMAGE GENERATION PROVIDER +################################################################################ + +### OPEN AI +# IMAGE_PROVIDER - Image provider (Example: dalle) +IMAGE_PROVIDER=dalle +# IMAGE_SIZE - Image size (Example: 256) +# DALLE: 256, 512, 1024 +IMAGE_SIZE=256 + +### HUGGINGFACE +# HUGGINGFACE_IMAGE_MODEL - Text-to-image model from Huggingface (Default: CompVis/stable-diffusion-v1-4) +HUGGINGFACE_IMAGE_MODEL=CompVis/stable-diffusion-v1-4 +# HUGGINGFACE_API_TOKEN - HuggingFace API token (Example: my-huggingface-api-token) +HUGGINGFACE_API_TOKEN=your-huggingface-api-token + +### STABLE DIFFUSION WEBUI +# SD_WEBUI_URL - Stable diffusion webui API URL (Example: http://127.0.0.1:7860) +SD_WEBUI_URL=http://127.0.0.1:7860 +# SD_WEBUI_AUTH - Stable diffusion webui username:password pair (Example: username:password) +SD_WEBUI_AUTH= + +################################################################################ +### AUDIO TO TEXT PROVIDER +################################################################################ + +### HUGGINGFACE +HUGGINGFACE_AUDIO_TO_TEXT_MODEL=facebook/wav2vec2-base-960h + +################################################################################ +### GIT Provider for repository actions +################################################################################ + +### GITHUB +# GITHUB_API_KEY - Github API key / PAT (Example: github_pat_123) +# GITHUB_USERNAME - Github username +GITHUB_API_KEY=github_pat_123 +GITHUB_USERNAME=your-github-username + +################################################################################ +### WEB BROWSING +################################################################################ + +### BROWSER +# USE_WEB_BROWSER - Sets the web-browser drivers to use with selenium (defaults to chrome). +# HEADLESS_BROWSER - Whether to run the browser in headless mode (defaults to True) +# Note: set this to either 'chrome', 'firefox', or 'safari' depending on your current browser +# USE_WEB_BROWSER=chrome +# HEADLESS_BROWSER=True + +### GOOGLE +# GOOGLE_API_KEY - Google API key (Example: my-google-api-key) +# CUSTOM_SEARCH_ENGINE_ID - Custom search engine ID (Example: my-custom-search-engine-id) +GOOGLE_API_KEY=your-google-api-key +CUSTOM_SEARCH_ENGINE_ID=your-custom-search-engine-id + +################################################################################ +### TTS PROVIDER +################################################################################ + +### MAC OS +# USE_MAC_OS_TTS - Use Mac OS TTS or not (Default: False) +USE_MAC_OS_TTS=False + +### STREAMELEMENTS +# USE_BRIAN_TTS - Use Brian TTS or not (Default: False) +USE_BRIAN_TTS=False + +### ELEVENLABS +# ELEVENLABS_API_KEY - Eleven Labs API key (Example: my-elevenlabs-api-key) +# ELEVENLABS_VOICE_1_ID - Eleven Labs voice 1 ID (Example: my-voice-id-1) +# ELEVENLABS_VOICE_2_ID - Eleven Labs voice 2 ID (Example: my-voice-id-2) +ELEVENLABS_API_KEY=your-elevenlabs-api-key +ELEVENLABS_VOICE_1_ID=your-voice-id-1 +ELEVENLABS_VOICE_2_ID=your-voice-id-2 + +################################################################################ +### TWITTER API +################################################################################ + +TW_CONSUMER_KEY= +TW_CONSUMER_SECRET= +TW_ACCESS_TOKEN= +TW_ACCESS_TOKEN_SECRET= diff --git a/.envrc b/.envrc new file mode 100644 index 0000000000000000000000000000000000000000..a7ad726377a8324287c4e066b000525db0bfaf77 --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +# Upon entering directory, direnv requests user permission once to automatically load project dependencies onwards. +# Eliminating the need of running "nix develop github:superherointj/nix-auto-gpt" for Nix users to develop/use Auto-GPT. + +[[ -z $IN_NIX_SHELL ]] && use flake github:superherointj/nix-auto-gpt diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000000000000000000000000000000000000..779762248e0ae5e962f33073134cf17536388591 --- /dev/null +++ b/.flake8 @@ -0,0 +1,12 @@ +[flake8] +max-line-length = 88 +select = "E303, W293, W291, W292, E305, E231, E302" +exclude = + .tox, + __pycache__, + *.pyc, + .env + venv*/*, + .venv/*, + reports/*, + dist/*, diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index c7d9f3332a950355d5a77d85000f05e6f45435ea..0000000000000000000000000000000000000000 --- a/.gitattributes +++ /dev/null @@ -1,34 +0,0 @@ -*.7z filter=lfs diff=lfs merge=lfs -text -*.arrow filter=lfs diff=lfs merge=lfs -text -*.bin filter=lfs diff=lfs merge=lfs -text -*.bz2 filter=lfs diff=lfs merge=lfs -text -*.ckpt filter=lfs diff=lfs merge=lfs -text -*.ftz filter=lfs diff=lfs merge=lfs -text -*.gz filter=lfs diff=lfs merge=lfs -text -*.h5 filter=lfs diff=lfs merge=lfs -text -*.joblib filter=lfs diff=lfs merge=lfs -text -*.lfs.* filter=lfs diff=lfs merge=lfs -text -*.mlmodel filter=lfs diff=lfs merge=lfs -text -*.model filter=lfs diff=lfs merge=lfs -text -*.msgpack filter=lfs diff=lfs merge=lfs -text -*.npy filter=lfs diff=lfs merge=lfs -text -*.npz filter=lfs diff=lfs merge=lfs -text -*.onnx filter=lfs diff=lfs merge=lfs -text -*.ot filter=lfs diff=lfs merge=lfs -text -*.parquet filter=lfs diff=lfs merge=lfs -text -*.pb filter=lfs diff=lfs merge=lfs -text -*.pickle filter=lfs diff=lfs merge=lfs -text -*.pkl filter=lfs diff=lfs merge=lfs -text -*.pt filter=lfs diff=lfs merge=lfs -text -*.pth filter=lfs diff=lfs merge=lfs -text -*.rar filter=lfs diff=lfs merge=lfs -text -*.safetensors filter=lfs diff=lfs merge=lfs -text -saved_model/**/* filter=lfs diff=lfs merge=lfs -text -*.tar.* filter=lfs diff=lfs merge=lfs -text -*.tflite filter=lfs diff=lfs merge=lfs -text -*.tgz filter=lfs diff=lfs merge=lfs -text -*.wasm filter=lfs diff=lfs merge=lfs -text -*.xz filter=lfs diff=lfs merge=lfs -text -*.zip filter=lfs diff=lfs merge=lfs -text -*.zst filter=lfs diff=lfs merge=lfs -text -*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000000000000000000000000000000000..54ef10844fe9217bc8f4626d013bcd53a7f77bb9 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +github: Torantulino diff --git a/.github/ISSUE_TEMPLATE/1.bug.yml b/.github/ISSUE_TEMPLATE/1.bug.yml new file mode 100644 index 0000000000000000000000000000000000000000..6645142ed0129edb4b14c1a94902607951c64f30 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/1.bug.yml @@ -0,0 +1,117 @@ +name: Bug report 🐛 +description: Create a bug report for Auto-GPT. +labels: ['status: needs triage'] +body: + - type: markdown + attributes: + value: | + ### ⚠️ Before you continue + * Check out our [backlog], [roadmap] and join our [discord] to discuss what's going on + * If you need help, you can ask in the [discussions] section or in [#tech-support] + * **Throughly search the [existing issues] before creating a new one** + + [backlog]: https://github.com/orgs/Significant-Gravitas/projects/1 + [roadmap]: https://github.com/orgs/Significant-Gravitas/projects/2 + [discord]: https://discord.gg/autogpt + [discussions]: https://github.com/Significant-Gravitas/Auto-GPT/discussions + [#tech-support]: https://discord.com/channels/1092243196446249134/1092275629602394184 + [existing issues]: https://github.com/Significant-Gravitas/Auto-GPT/issues?q=is%3Aissue + - type: checkboxes + attributes: + label: ⚠️ Search for existing issues first ⚠️ + description: > + Please [search the history](https://github.com/Torantulino/Auto-GPT/issues) + to see if an issue already exists for the same problem. + options: + - label: I have searched the existing issues, and there is no existing issue for my problem + required: true + - type: markdown + attributes: + value: | + Please provide a searchable summary of the issue in the title above ⬆️. + + ⚠️ SUPER-busy repo, please help the volunteer maintainers. + The less time we spend here, the more time we spend building AutoGPT. + + Please help us help you: + - Does it work on `stable` branch (https://github.com/Torantulino/Auto-GPT/tree/stable)? + - Does it work on current `master` (https://github.com/Torantulino/Auto-GPT/tree/master)? + - Search for existing issues, "add comment" is tidier than "new issue" + - Ask on our Discord (https://discord.gg/autogpt) + - Provide relevant info: + - Provide commit-hash (`git rev-parse HEAD` gets it) + - If it's a pip/packages issue, provide pip version, python version + - If it's a crash, provide traceback. + - type: dropdown + attributes: + label: Which Operating System are you using? + description: > + Please select the operating system you were using to run Auto-GPT when this problem occurred. + options: + - Windows + - Linux + - MacOS + - Docker + - Devcontainer / Codespace + - Windows Subsystem for Linux (WSL) + - Other (Please specify in your problem) + validations: + required: true + - type: dropdown + attributes: + label: GPT-3 or GPT-4? + description: > + If you are using Auto-GPT with `--gpt3only`, your problems may be caused by + the [limitations](https://github.com/Significant-Gravitas/Auto-GPT/issues?q=is%3Aissue+label%3A%22AI+model+limitation%22) of GPT-3.5. + options: + - GPT-3.5 + - GPT-4 + validations: + required: true + - type: textarea + attributes: + label: Steps to reproduce 🕹 + description: | + **⚠️ Issues that we can't reproduce will be closed.** + - type: textarea + attributes: + label: Current behavior 😯 + description: Describe what happens instead of the expected behavior. + - type: textarea + attributes: + label: Expected behavior 🤔 + description: Describe what should happen. + - type: textarea + attributes: + label: Your prompt 📝 + description: > + If applicable please provide the prompt you are using. Your prompt is stored in your `ai_settings.yaml` file. + value: | + ```yaml + # Paste your prompt here + ``` + - type: textarea + attributes: + label: Your Logs 📒 + description: | + Please include the log showing your error and the command that caused it, if applicable. + You can copy it from your terminal or from `logs/activity.log`. + This will help us understand your issue better! + +
+ Example + ```log + INFO NEXT ACTION: COMMAND = execute_shell ARGUMENTS = {'command_line': 'some_command'} + INFO -=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-= + Traceback (most recent call last): + File "/home/anaconda3/lib/python3.9/site-packages/openai/api_requestor.py", line 619, in _interpret_response + self._interpret_response_line( + File "/home/anaconda3/lib/python3.9/site-packages/openai/api_requestor.py", line 682, in _interpret_response_line + raise self.handle_error_response( + openai.error.InvalidRequestError: This model's maximum context length is 8191 tokens, however you requested 10982 tokens (10982 in your prompt; 0 for the completion). Please reduce your prompt; or completion length. + ``` +
+ value: | + ```log + + ``` diff --git a/.github/ISSUE_TEMPLATE/2.feature.yml b/.github/ISSUE_TEMPLATE/2.feature.yml new file mode 100644 index 0000000000000000000000000000000000000000..0ea882ef6a4e6978b45c45529935c17c42c17a20 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/2.feature.yml @@ -0,0 +1,29 @@ +name: Feature request 🚀 +description: Suggest a new idea for Auto-GPT. +labels: ['status: needs triage'] +body: + - type: markdown + attributes: + value: | + Please provide a searchable summary of the issue in the title above ⬆️. + + Thanks for contributing by creating an issue! ❤️ + - type: checkboxes + attributes: + label: Duplicates + description: Please [search the history](https://github.com/Torantulino/Auto-GPT/issues) to see if an issue already exists for the same problem. + options: + - label: I have searched the existing issues + required: true + - type: textarea + attributes: + label: Summary 💡 + description: Describe how it should work. + - type: textarea + attributes: + label: Examples 🌈 + description: Provide a link to other implementations, or screenshots of the expected behavior. + - type: textarea + attributes: + label: Motivation 🔦 + description: What are you trying to accomplish? How has the lack of this feature affected you? Providing context helps us come up with a solution that is more useful in the real world. \ No newline at end of file diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000000000000000000000000000000000..a4f28a3d27d66d79cb95f2b8b847832172bb5f11 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,40 @@ + + + + +### Background + + +### Changes + + +### Documentation + + +### Test Plan + + +### PR Quality Checklist +- [ ] My pull request is atomic and focuses on a single change. +- [ ] I have thoroughly tested my changes with multiple different prompts. +- [ ] I have considered potential risks and mitigations for my changes. +- [ ] I have documented my changes clearly and comprehensively. +- [ ] I have not snuck in any "extra" small tweaks changes + + + + diff --git a/.github/workflows/auto_format.yml b/.github/workflows/auto_format.yml new file mode 100644 index 0000000000000000000000000000000000000000..c33cd39755d51b37bdb95ca2466d203d44843f77 --- /dev/null +++ b/.github/workflows/auto_format.yml @@ -0,0 +1,23 @@ +name: auto-format +on: pull_request +jobs: + format: + runs-on: ubuntu-latest + steps: + - name: Checkout PR branch + uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: autopep8 + uses: peter-evans/autopep8@v1 + with: + args: --exit-code --recursive --in-place --aggressive --aggressive . + - name: Check for modified files + id: git-check + run: echo "modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)" >> $GITHUB_ENV + - name: Push changes + if: steps.git-check.outputs.modified == 'true' + run: | + git config --global user.name 'Torantulino' + git config --global user.email 'toran.richards@gmail.com' + git remote set diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 0000000000000000000000000000000000000000..53b6e7a0a0df42a220fa5269fc4ee491ac684cce --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,31 @@ +name: benchmark + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + environment: benchmark + strategy: + matrix: + python-version: ['3.10', '3.11'] + + steps: + - name: Check out repository + uses: actions/checkout@v3 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: benchmark + run: | + python benchmark/benchmark_entrepeneur_gpt_with_undecisive_user.py + env: + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..b37e7f004262ef039bc3c3ba7f40d39ee3b07425 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,72 @@ +name: Python CI + +on: + push: + branches: [master] + pull_request: + branches: [master] + +concurrency: + group: ${{ format('ci-{0}', format('pr-{0}', github.event.pull_request.number) || github.sha) }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + lint: + runs-on: ubuntu-latest + env: + min-python-version: '3.10' + + steps: + - name: Check out repository + uses: actions/checkout@v3 + + - name: Set up Python ${{ env.min-python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ env.min-python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Lint with flake8 + run: flake8 + + - name: Check black formatting + run: black . --check + if: success() || failure() + + - name: Check isort formatting + run: isort . --check + if: success() || failure() + + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.10', '3.11'] + + steps: + - name: Check out repository + uses: actions/checkout@v3 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run unittest tests with coverage + run: | + pytest --cov=autogpt --without-integration --without-slow-integration + + - name: Generate coverage report + run: | + coverage report + coverage xml + if: success() || failure() diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000000000000000000000000000000000000..9c77098e02d50b4087209e13d508ea12d6476129 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,18 @@ +name: Docker Image CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag autogpt:$(date +%s) diff --git a/.github/workflows/dockerhub-imagepush.yml b/.github/workflows/dockerhub-imagepush.yml new file mode 100644 index 0000000000000000000000000000000000000000..6805eebfc324ba86d3a90ed1e3ca1b4d2ab9bb33 --- /dev/null +++ b/.github/workflows/dockerhub-imagepush.yml @@ -0,0 +1,24 @@ +name: Push Docker Image on Release + +on: + push: + branches: [ "stable" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Log in to Docker hub + env: + DOCKER_USER: ${{secrets.DOCKER_USER}} + DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}} + run: | + docker login -u $DOCKER_USER -p $DOCKER_PASSWORD + - name: Build the Docker image + run: docker build . --file Dockerfile --tag ${{secrets.DOCKER_USER}}/auto-gpt:$(git describe --tags `git rev-list --tags --max-count=1`) + - name: Docker Push + run: docker push ${{secrets.DOCKER_USER}}/auto-gpt diff --git a/.github/workflows/pr-label.yml b/.github/workflows/pr-label.yml new file mode 100644 index 0000000000000000000000000000000000000000..92c5a66b7285f41aca77e53402b202eafecdef0a --- /dev/null +++ b/.github/workflows/pr-label.yml @@ -0,0 +1,28 @@ +name: "Pull Request auto-label" +on: + # So that PRs touching the same files as the push are updated + push: + # So that the `dirtyLabel` is removed if conflicts are resolve + # We recommend `pull_request_target` so that github secrets are available. + # In `pull_request` we wouldn't be able to change labels of fork PRs + pull_request_target: + types: [opened, synchronize] +concurrency: + group: ${{ format('pr-label-{0}', github.event.pull_request.number || github.sha) }} + cancel-in-progress: true + +jobs: + conflicts: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + steps: + - name: Update PRs with conflict labels + uses: eps1lon/actions-label-merge-conflict@releases/2.x + with: + dirtyLabel: "conflicts" + #removeOnDirtyLabel: "PR: ready to ship" + repoToken: "${{ secrets.GITHUB_TOKEN }}" + commentOnDirty: "This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request." + commentOnClean: "Conflicts have been resolved! 🎉 A maintainer will review the pull request shortly." diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..a6b3b80f4ef2613e76058e3732d906ae7b89f901 --- /dev/null +++ b/.gitignore @@ -0,0 +1,161 @@ +## Original ignores +autogpt/keys.py +autogpt/*json +autogpt/node_modules/ +autogpt/__pycache__/keys.cpython-310.pyc +autogpt/auto_gpt_workspace +package-lock.json +*.pyc +auto_gpt_workspace/* +*.mpeg +.env +azure.yaml +ai_settings.yaml +last_run_ai_settings.yaml +.vscode +.idea/* +auto-gpt.json +log.txt +log-ingestion.txt +logs +*.log +*.mp3 + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +plugins/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.direnv/ +.env +.venv +env/ +venv*/ +ENV/ +env.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ +llama-* +vicuna-* + +# mac +.DS_Store + +# news +CURRENT_BULLETIN.md \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3722b25eed0c433e66925f98d68b2247c21ffb25 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,32 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v0.9.2 + hooks: + - id: check-added-large-files + args: ['--maxkb=500'] + - id: check-byte-order-marker + - id: check-case-conflict + - id: check-merge-conflict + - id: check-symlinks + - id: debug-statements + + - repo: https://github.com/pycqa/isort + rev: 5.12.0 + hooks: + - id: isort + language_version: python3.10 + + - repo: https://github.com/psf/black + rev: 23.3.0 + hooks: + - id: black + language_version: python3.10 + + - repo: local + hooks: + - id: pytest-check + name: pytest-check + entry: pytest --cov=autogpt --without-integration --without-slow-integration + language: system + pass_filenames: false + always_run: true diff --git a/.sourcery.yaml b/.sourcery.yaml new file mode 100644 index 0000000000000000000000000000000000000000..da171e7598f47ee2677bbf3516e9d19f0090256a --- /dev/null +++ b/.sourcery.yaml @@ -0,0 +1,71 @@ +# 🪄 This is your project's Sourcery configuration file. + +# You can use it to get Sourcery working in the way you want, such as +# ignoring specific refactorings, skipping directories in your project, +# or writing custom rules. + +# 📚 For a complete reference to this file, see the documentation at +# https://docs.sourcery.ai/Configuration/Project-Settings/ + +# This file was auto-generated by Sourcery on 2023-02-25 at 21:07. + +version: '1' # The schema version of this config file + +ignore: # A list of paths or files which Sourcery will ignore. +- .git +- venv +- .venv +- build +- dist +- env +- .env +- .tox + +rule_settings: + enable: + - default + - gpsg + disable: [] # A list of rule IDs Sourcery will never suggest. + rule_types: + - refactoring + - suggestion + - comment + python_version: '3.10' # A string specifying the lowest Python version your project supports. Sourcery will not suggest refactorings requiring a higher Python version. + +# rules: # A list of custom rules Sourcery will include in its analysis. +# - id: no-print-statements +# description: Do not use print statements in the test directory. +# pattern: print(...) +# language: python +# replacement: +# condition: +# explanation: +# paths: +# include: +# - test +# exclude: +# - conftest.py +# tests: [] +# tags: [] + +# rule_tags: {} # Additional rule tags. + +# metrics: +# quality_threshold: 25.0 + +# github: +# labels: [] +# ignore_labels: +# - sourcery-ignore +# request_review: author +# sourcery_branch: sourcery/{base_branch} + +# clone_detection: +# min_lines: 3 +# min_duplicates: 2 +# identical_clones_only: false + +# proxy: +# url: +# ssl_certs_file: +# no_ssl_verify: false diff --git a/BULLETIN.md b/BULLETIN.md new file mode 100644 index 0000000000000000000000000000000000000000..735048ddc87a914987c6bd70ccdb231a80242ae3 --- /dev/null +++ b/BULLETIN.md @@ -0,0 +1,2 @@ +Welcome to Auto-GPT! We'll keep you informed of the latest news and features by printing messages here. +If you don't wish to see this message, you can run Auto-GPT with the --skip-news flag \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000000000000000000000000000000000..d2331b4c60b9fb27f06953273355dcf53b8d4321 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,40 @@ +# Code of Conduct for auto-gpt + +## 1. Purpose + +The purpose of this Code of Conduct is to provide guidelines for contributors to the auto-gpt project on GitHub. We aim to create a positive and inclusive environment where all participants can contribute and collaborate effectively. By participating in this project, you agree to abide by this Code of Conduct. + +## 2. Scope + +This Code of Conduct applies to all contributors, maintainers, and users of the auto-gpt project. It extends to all project spaces, including but not limited to issues, pull requests, code reviews, comments, and other forms of communication within the project. + +## 3. Our Standards + +We encourage the following behavior: + +* Being respectful and considerate to others +* Actively seeking diverse perspectives +* Providing constructive feedback and assistance +* Demonstrating empathy and understanding + +We discourage the following behavior: + +* Harassment or discrimination of any kind +* Disrespectful, offensive, or inappropriate language or content +* Personal attacks or insults +* Unwarranted criticism or negativity + +## 4. Reporting and Enforcement + +If you witness or experience any violations of this Code of Conduct, please report them to the project maintainers by email or other appropriate means. The maintainers will investigate and take appropriate action, which may include warnings, temporary or permanent bans, or other measures as necessary. + +Maintainers are responsible for ensuring compliance with this Code of Conduct and may take action to address any violations. + +## 5. Acknowledgements + +This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/version/2/0/code_of_conduct.html). + +## 6. Contact + +If you have any questions or concerns, please contact the project maintainers. + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000000000000000000000000000000000..79169a0c1951853303f73ffa1fddb3518685606a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,105 @@ +# Contributing to ProjectName + +First of all, thank you for considering contributing to our project! We appreciate your time and effort, and we value any contribution, whether it's reporting a bug, suggesting a new feature, or submitting a pull request. + +This document provides guidelines and best practices to help you contribute effectively. + +## Table of Contents + +- [Code of Conduct](#code-of-conduct) +- [Getting Started](#getting-started) +- [How to Contribute](#how-to-contribute) + - [Reporting Bugs](#reporting-bugs) + - [Suggesting Enhancements](#suggesting-enhancements) + - [Submitting Pull Requests](#submitting-pull-requests) +- [Style Guidelines](#style-guidelines) + - [Code Formatting](#code-formatting) + - [Pre-Commit Hooks](#pre-commit-hooks) + +## Code of Conduct + +By participating in this project, you agree to abide by our [Code of Conduct](CODE_OF_CONDUCT.md). Please read it to understand the expectations we have for everyone who contributes to this project. + +## 📢 A Quick Word +Right now we will not be accepting any Contributions that add non-essential commands to Auto-GPT. + +However, you absolutely can still add these commands to Auto-GPT in the form of plugins. Please check out this [template](https://github.com/Significant-Gravitas/Auto-GPT-Plugin-Template). +> ⚠️ Plugin support is expected to ship within the week. You can follow PR #757 for more updates! + +## Getting Started + +To start contributing, follow these steps: + +1. Fork the repository and clone your fork. +2. Create a new branch for your changes (use a descriptive name, such as `fix-bug-123` or `add-new-feature`). +3. Make your changes in the new branch. +4. Test your changes thoroughly. +5. Commit and push your changes to your fork. +6. Create a pull request following the guidelines in the [Submitting Pull Requests](#submitting-pull-requests) section. + +## How to Contribute + +### Reporting Bugs + +If you find a bug in the project, please create an issue on GitHub with the following information: + +- A clear, descriptive title for the issue. +- A description of the problem, including steps to reproduce the issue. +- Any relevant logs, screenshots, or other supporting information. + +### Suggesting Enhancements + +If you have an idea for a new feature or improvement, please create an issue on GitHub with the following information: + +- A clear, descriptive title for the issue. +- A detailed description of the proposed enhancement, including any benefits and potential drawbacks. +- Any relevant examples, mockups, or supporting information. + +### Submitting Pull Requests + +When submitting a pull request, please ensure that your changes meet the following criteria: + +- Your pull request should be atomic and focus on a single change. +- Your pull request should include tests for your change. +- You should have thoroughly tested your changes with multiple different prompts. +- You should have considered potential risks and mitigations for your changes. +- You should have documented your changes clearly and comprehensively. +- You should not include any unrelated or "extra" small tweaks or changes. + +## Style Guidelines + +### Code Formatting + +We use the `black` code formatter to maintain a consistent coding style across the project. Please ensure that your code is formatted using `black` before submitting a pull request. You can install `black` using `pip`: + +```bash +pip install black +``` + +To format your code, run the following command in the project's root directory: + +```bash +black . +``` +### Pre-Commit Hooks +We use pre-commit hooks to ensure that code formatting and other checks are performed automatically before each commit. To set up pre-commit hooks for this project, follow these steps: + +Install the pre-commit package using pip: +```bash +pip install pre-commit +``` + +Run the following command in the project's root directory to install the pre-commit hooks: +```bash +pre-commit install +``` + +Now, the pre-commit hooks will run automatically before each commit, checking your code formatting and other requirements. + +If you encounter any issues or have questions, feel free to reach out to the maintainers or open a new issue on GitHub. We're here to help and appreciate your efforts to contribute to the project. + +Happy coding, and once again, thank you for your contributions! + +Maintainers will look at PR that have no merge conflicts when deciding what to add to the project. Make sure your PR shows up here: + +https://github.com/Torantulino/Auto-GPT/pulls?q=is%3Apr+is%3Aopen+-is%3Aconflict+ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..8396154998f32a50d55c199a674b638d5cf7bda2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +# Use an official Python base image from the Docker Hub +FROM python:3.10-slim + +# Install git +RUN apt-get -y update +RUN apt-get -y install git chromium-driver + +# Install Xvfb and other dependencies for headless browser testing +RUN apt-get update \ + && apt-get install -y wget gnupg2 libgtk-3-0 libdbus-glib-1-2 dbus-x11 xvfb ca-certificates + +# Install Firefox / Chromium +RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ + && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \ + && apt-get update \ + && apt-get install -y chromium firefox-esr + +# Set environment variables +ENV PIP_NO_CACHE_DIR=yes \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +# Create a non-root user and set permissions +RUN useradd --create-home appuser +WORKDIR /home/appuser +RUN chown appuser:appuser /home/appuser +USER appuser + +# Copy the requirements.txt file and install the requirements +COPY --chown=appuser:appuser requirements.txt . +RUN sed -i '/Items below this point will not be included in the Docker Image/,$d' requirements.txt && \ + pip install --no-cache-dir --user -r requirements.txt + +# Copy the application files +COPY --chown=appuser:appuser autogpt/ ./autogpt + +# Set the entrypoint +ENTRYPOINT ["python", "-m", "autogpt"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..601935b85ec87d3393291792475a08a66727358f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Toran Bruce Richards + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 678f29ccedf3b38e578becf5aa6b4ea80beac075..71012136a7c215d0c816fcdc910e53c6d464a0c8 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ --- title: AutoGPT -emoji: 💩 -colorFrom: green -colorTo: green +emoji: 🦾 +colorFrom: gray +colorTo: white sdk: gradio sdk_version: 3.27.0 -app_file: app.py +app_file: ui/app.py pinned: false license: mit --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference diff --git a/autogpt/__init__.py b/autogpt/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autogpt/__main__.py b/autogpt/__main__.py new file mode 100644 index 0000000000000000000000000000000000000000..128f9eea4900429e88276abdde3419b806001ac7 --- /dev/null +++ b/autogpt/__main__.py @@ -0,0 +1,5 @@ +"""Auto-GPT: A GPT powered AI Assistant""" +import autogpt.cli + +if __name__ == "__main__": + autogpt.cli.main() diff --git a/autogpt/agent/__init__.py b/autogpt/agent/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e928af2205b1c52d19dc89ec4246e8c1d2c20e3f --- /dev/null +++ b/autogpt/agent/__init__.py @@ -0,0 +1,4 @@ +from autogpt.agent.agent import Agent +from autogpt.agent.agent_manager import AgentManager + +__all__ = ["Agent", "AgentManager"] diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py new file mode 100644 index 0000000000000000000000000000000000000000..ee7885f8844022597321fa6b492430ec34c0d6b9 --- /dev/null +++ b/autogpt/agent/agent.py @@ -0,0 +1,197 @@ +from colorama import Fore, Style + +from autogpt.app import execute_command, get_command +from autogpt.chat import chat_with_ai, create_chat_message +from autogpt.config import Config +from autogpt.json_utils.json_fix_llm import fix_json_using_multiple_techniques +from autogpt.json_utils.utilities import validate_json +from autogpt.logs import logger, print_assistant_thoughts +from autogpt.speech import say_text +from autogpt.spinner import Spinner +from autogpt.utils import clean_input + + +class Agent: + """Agent class for interacting with Auto-GPT. + + Attributes: + ai_name: The name of the agent. + memory: The memory object to use. + full_message_history: The full message history. + next_action_count: The number of actions to execute. + system_prompt: The system prompt is the initial prompt that defines everything the AI needs to know to achieve its task successfully. + Currently, the dynamic and customizable information in the system prompt are ai_name, description and goals. + + triggering_prompt: The last sentence the AI will see before answering. For Auto-GPT, this prompt is: + Determine which next command to use, and respond using the format specified above: + The triggering prompt is not part of the system prompt because between the system prompt and the triggering + prompt we have contextual information that can distract the AI and make it forget that its goal is to find the next task to achieve. + SYSTEM PROMPT + CONTEXTUAL INFORMATION (memory, previous conversations, anything relevant) + TRIGGERING PROMPT + + The triggering prompt reminds the AI about its short term meta task (defining the next task) + """ + + def __init__( + self, + ai_name, + memory, + full_message_history, + next_action_count, + system_prompt, + triggering_prompt, + ): + self.ai_name = ai_name + self.memory = memory + self.full_message_history = full_message_history + self.next_action_count = next_action_count + self.system_prompt = system_prompt + self.triggering_prompt = triggering_prompt + + def start_interaction_loop(self): + # Interaction Loop + cfg = Config() + loop_count = 0 + command_name = None + arguments = None + user_input = "" + + while True: + # Discontinue if continuous limit is reached + loop_count += 1 + if ( + cfg.continuous_mode + and cfg.continuous_limit > 0 + and loop_count > cfg.continuous_limit + ): + logger.typewriter_log( + "Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}" + ) + break + + # Send message to AI, get response + with Spinner("Thinking... "): + assistant_reply = chat_with_ai( + self.system_prompt, + self.triggering_prompt, + self.full_message_history, + self.memory, + cfg.fast_token_limit, + ) # TODO: This hardcodes the model to use GPT3.5. Make this an argument + + assistant_reply_json = fix_json_using_multiple_techniques(assistant_reply) + + # Print Assistant thoughts + if assistant_reply_json != {}: + validate_json(assistant_reply_json, "llm_response_format_1") + # Get command name and arguments + try: + print_assistant_thoughts(self.ai_name, assistant_reply_json) + command_name, arguments = get_command(assistant_reply_json) + # command_name, arguments = assistant_reply_json_valid["command"]["name"], assistant_reply_json_valid["command"]["args"] + if cfg.speak_mode: + say_text(f"I want to execute {command_name}") + except Exception as e: + logger.error("Error: \n", str(e)) + + if not cfg.continuous_mode and self.next_action_count == 0: + ### GET USER AUTHORIZATION TO EXECUTE COMMAND ### + # Get key press: Prompt the user to press enter to continue or escape + # to exit + logger.typewriter_log( + "NEXT ACTION: ", + Fore.CYAN, + f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} " + f"ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}", + ) + print( + "Enter 'y' to authorise command, 'y -N' to run N continuous " + "commands, 'n' to exit program, or enter feedback for " + f"{self.ai_name}...", + flush=True, + ) + while True: + console_input = clean_input( + Fore.MAGENTA + "Input:" + Style.RESET_ALL + ) + if console_input.lower().strip() == "y": + user_input = "GENERATE NEXT COMMAND JSON" + break + elif console_input.lower().strip() == "": + print("Invalid input format.") + continue + elif console_input.lower().startswith("y -"): + try: + self.next_action_count = abs( + int(console_input.split(" ")[1]) + ) + user_input = "GENERATE NEXT COMMAND JSON" + except ValueError: + print( + "Invalid input format. Please enter 'y -n' where n is" + " the number of continuous tasks." + ) + continue + break + elif console_input.lower() == "n": + user_input = "EXIT" + break + else: + user_input = console_input + command_name = "human_feedback" + break + + if user_input == "GENERATE NEXT COMMAND JSON": + logger.typewriter_log( + "-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=", + Fore.MAGENTA, + "", + ) + elif user_input == "EXIT": + print("Exiting...", flush=True) + break + else: + # Print command + logger.typewriter_log( + "NEXT ACTION: ", + Fore.CYAN, + f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL}" + f" ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}", + ) + + # Execute command + if command_name is not None and command_name.lower().startswith("error"): + result = ( + f"Command {command_name} threw the following error: {arguments}" + ) + elif command_name == "human_feedback": + result = f"Human feedback: {user_input}" + else: + result = ( + f"Command {command_name} returned: " + f"{execute_command(command_name, arguments)}" + ) + if self.next_action_count > 0: + self.next_action_count -= 1 + + memory_to_add = ( + f"Assistant Reply: {assistant_reply} " + f"\nResult: {result} " + f"\nHuman Feedback: {user_input} " + ) + + self.memory.add(memory_to_add) + + # Check if there's a result from the command append it to the message + # history + if result is not None: + self.full_message_history.append(create_chat_message("system", result)) + logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result) + else: + self.full_message_history.append( + create_chat_message("system", "Unable to execute command") + ) + logger.typewriter_log( + "SYSTEM: ", Fore.YELLOW, "Unable to execute command" + ) diff --git a/autogpt/agent/agent_manager.py b/autogpt/agent/agent_manager.py new file mode 100644 index 0000000000000000000000000000000000000000..898767a485e50b5e62625a7883edf1b30d5fddf9 --- /dev/null +++ b/autogpt/agent/agent_manager.py @@ -0,0 +1,103 @@ +"""Agent manager for managing GPT agents""" +from __future__ import annotations + +from typing import Union + +from autogpt.config.config import Singleton +from autogpt.llm_utils import create_chat_completion + + +class AgentManager(metaclass=Singleton): + """Agent manager for managing GPT agents""" + + def __init__(self): + self.next_key = 0 + self.agents = {} # key, (task, full_message_history, model) + + # Create new GPT agent + # TODO: Centralise use of create_chat_completion() to globally enforce token limit + + def create_agent(self, task: str, prompt: str, model: str) -> tuple[int, str]: + """Create a new agent and return its key + + Args: + task: The task to perform + prompt: The prompt to use + model: The model to use + + Returns: + The key of the new agent + """ + messages = [ + {"role": "user", "content": prompt}, + ] + + # Start GPT instance + agent_reply = create_chat_completion( + model=model, + messages=messages, + ) + + # Update full message history + messages.append({"role": "assistant", "content": agent_reply}) + + key = self.next_key + # This is done instead of len(agents) to make keys unique even if agents + # are deleted + self.next_key += 1 + + self.agents[key] = (task, messages, model) + + return key, agent_reply + + def message_agent(self, key: str | int, message: str) -> str: + """Send a message to an agent and return its response + + Args: + key: The key of the agent to message + message: The message to send to the agent + + Returns: + The agent's response + """ + task, messages, model = self.agents[int(key)] + + # Add user message to message history before sending to agent + messages.append({"role": "user", "content": message}) + + # Start GPT instance + agent_reply = create_chat_completion( + model=model, + messages=messages, + ) + + # Update full message history + messages.append({"role": "assistant", "content": agent_reply}) + + return agent_reply + + def list_agents(self) -> list[tuple[str | int, str]]: + """Return a list of all agents + + Returns: + A list of tuples of the form (key, task) + """ + + # Return a list of agent keys and their tasks + return [(key, task) for key, (task, _, _) in self.agents.items()] + + def delete_agent(self, key: Union[str, int]) -> bool: + """Delete an agent from the agent manager + + Args: + key: The key of the agent to delete + + Returns: + True if successful, False otherwise + """ + + try: + del self.agents[int(key)] + return True + except KeyError: + return False diff --git a/autogpt/app.py b/autogpt/app.py new file mode 100644 index 0000000000000000000000000000000000000000..58d9f7164ddfbb5019b072d789dc2fa6205dc9d3 --- /dev/null +++ b/autogpt/app.py @@ -0,0 +1,330 @@ +""" Command and Control """ +import json +from typing import Dict, List, NoReturn, Union + +from autogpt.agent.agent_manager import AgentManager +from autogpt.commands.analyze_code import analyze_code +from autogpt.commands.audio_text import read_audio_from_file +from autogpt.commands.execute_code import ( + execute_python_file, + execute_shell, + execute_shell_popen, +) +from autogpt.commands.file_operations import ( + append_to_file, + delete_file, + download_file, + read_file, + search_files, + write_to_file, +) +from autogpt.commands.git_operations import clone_repository +from autogpt.commands.google_search import google_official_search, google_search +from autogpt.commands.image_gen import generate_image +from autogpt.commands.improve_code import improve_code +from autogpt.commands.twitter import send_tweet +from autogpt.commands.web_requests import scrape_links, scrape_text +from autogpt.commands.web_selenium import browse_website +from autogpt.commands.write_tests import write_tests +from autogpt.config import Config +from autogpt.json_utils.json_fix_llm import fix_and_parse_json +from autogpt.memory import get_memory +from autogpt.processing.text import summarize_text +from autogpt.speech import say_text + +CFG = Config() +AGENT_MANAGER = AgentManager() + + +def is_valid_int(value: str) -> bool: + """Check if the value is a valid integer + + Args: + value (str): The value to check + + Returns: + bool: True if the value is a valid integer, False otherwise + """ + try: + int(value) + return True + except ValueError: + return False + + +def get_command(response_json: Dict): + """Parse the response and return the command name and arguments + + Args: + response_json (json): The response from the AI + + Returns: + tuple: The command name and arguments + + Raises: + json.decoder.JSONDecodeError: If the response is not valid JSON + + Exception: If any other error occurs + """ + try: + if "command" not in response_json: + return "Error:", "Missing 'command' object in JSON" + + if not isinstance(response_json, dict): + return "Error:", f"'response_json' object is not dictionary {response_json}" + + command = response_json["command"] + if not isinstance(command, dict): + return "Error:", "'command' object is not a dictionary" + + if "name" not in command: + return "Error:", "Missing 'name' field in 'command' object" + + command_name = command["name"] + + # Use an empty dictionary if 'args' field is not present in 'command' object + arguments = command.get("args", {}) + + return command_name, arguments + except json.decoder.JSONDecodeError: + return "Error:", "Invalid JSON" + # All other errors, return "Error: + error message" + except Exception as e: + return "Error:", str(e) + + +def map_command_synonyms(command_name: str): + """Takes the original command name given by the AI, and checks if the + string matches a list of common/known hallucinations + """ + synonyms = [ + ("write_file", "write_to_file"), + ("create_file", "write_to_file"), + ("search", "google"), + ] + for seen_command, actual_command_name in synonyms: + if command_name == seen_command: + return actual_command_name + return command_name + + +def execute_command(command_name: str, arguments): + """Execute the command and return the result + + Args: + command_name (str): The name of the command to execute + arguments (dict): The arguments for the command + + Returns: + str: The result of the command + """ + try: + command_name = map_command_synonyms(command_name.lower()) + if command_name == "google": + # Check if the Google API key is set and use the official search method + # If the API key is not set or has only whitespaces, use the unofficial + # search method + key = CFG.google_api_key + if key and key.strip() and key != "your-google-api-key": + google_result = google_official_search(arguments["input"]) + return google_result + else: + google_result = google_search(arguments["input"]) + + # google_result can be a list or a string depending on the search results + if isinstance(google_result, list): + safe_message = [ + google_result_single.encode("utf-8", "ignore") + for google_result_single in google_result + ] + else: + safe_message = google_result.encode("utf-8", "ignore") + + return safe_message.decode("utf-8") + elif command_name == "memory_add": + memory = get_memory(CFG) + return memory.add(arguments["string"]) + elif command_name == "start_agent": + return start_agent( + arguments["name"], arguments["task"], arguments["prompt"] + ) + elif command_name == "message_agent": + return message_agent(arguments["key"], arguments["message"]) + elif command_name == "list_agents": + return list_agents() + elif command_name == "delete_agent": + return delete_agent(arguments["key"]) + elif command_name == "get_text_summary": + return get_text_summary(arguments["url"], arguments["question"]) + elif command_name == "get_hyperlinks": + return get_hyperlinks(arguments["url"]) + elif command_name == "clone_repository": + return clone_repository( + arguments["repository_url"], arguments["clone_path"] + ) + elif command_name == "read_file": + return read_file(arguments["file"]) + elif command_name == "write_to_file": + return write_to_file(arguments["file"], arguments["text"]) + elif command_name == "append_to_file": + return append_to_file(arguments["file"], arguments["text"]) + elif command_name == "delete_file": + return delete_file(arguments["file"]) + elif command_name == "search_files": + return search_files(arguments["directory"]) + elif command_name == "download_file": + if not CFG.allow_downloads: + return "Error: You do not have user authorization to download files locally." + return download_file(arguments["url"], arguments["file"]) + elif command_name == "browse_website": + return browse_website(arguments["url"], arguments["question"]) + # TODO: Change these to take in a file rather than pasted code, if + # non-file is given, return instructions "Input should be a python + # filepath, write your code to file and try again" + elif command_name == "analyze_code": + return analyze_code(arguments["code"]) + elif command_name == "improve_code": + return improve_code(arguments["suggestions"], arguments["code"]) + elif command_name == "write_tests": + return write_tests(arguments["code"], arguments.get("focus")) + elif command_name == "execute_python_file": # Add this command + return execute_python_file(arguments["file"]) + elif command_name == "execute_shell": + if CFG.execute_local_commands: + return execute_shell(arguments["command_line"]) + else: + return ( + "You are not allowed to run local shell commands. To execute" + " shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' " + "in your config. Do not attempt to bypass the restriction." + ) + elif command_name == "execute_shell_popen": + if CFG.execute_local_commands: + return execute_shell_popen(arguments["command_line"]) + else: + return ( + "You are not allowed to run local shell commands. To execute" + " shell commands, EXECUTE_LOCAL_COMMANDS must be set to 'True' " + "in your config. Do not attempt to bypass the restriction." + ) + elif command_name == "read_audio_from_file": + return read_audio_from_file(arguments["file"]) + elif command_name == "generate_image": + return generate_image(arguments["prompt"]) + elif command_name == "send_tweet": + return send_tweet(arguments["text"]) + elif command_name == "do_nothing": + return "No action performed." + elif command_name == "task_complete": + shutdown() + else: + return ( + f"Unknown command '{command_name}'. Please refer to the 'COMMANDS'" + " list for available commands and only respond in the specified JSON" + " format." + ) + except Exception as e: + return f"Error: {str(e)}" + + +def get_text_summary(url: str, question: str) -> str: + """Return the results of a Google search + + Args: + url (str): The url to scrape + question (str): The question to summarize the text for + + Returns: + str: The summary of the text + """ + text = scrape_text(url) + summary = summarize_text(url, text, question) + return f""" "Result" : {summary}""" + + +def get_hyperlinks(url: str) -> Union[str, List[str]]: + """Return the results of a Google search + + Args: + url (str): The url to scrape + + Returns: + str or list: The hyperlinks on the page + """ + return scrape_links(url) + + +def shutdown() -> NoReturn: + """Shut down the program""" + print("Shutting down...") + quit() + + +def start_agent(name: str, task: str, prompt: str, model=CFG.fast_llm_model) -> str: + """Start an agent with a given name, task, and prompt + + Args: + name (str): The name of the agent + task (str): The task of the agent + prompt (str): The prompt for the agent + model (str): The model to use for the agent + + Returns: + str: The response of the agent + """ + # Remove underscores from name + voice_name = name.replace("_", " ") + + first_message = f"""You are {name}. Respond with: "Acknowledged".""" + agent_intro = f"{voice_name} here, Reporting for duty!" + + # Create agent + if CFG.speak_mode: + say_text(agent_intro, 1) + key, ack = AGENT_MANAGER.create_agent(task, first_message, model) + + if CFG.speak_mode: + say_text(f"Hello {voice_name}. Your task is as follows. {task}.") + + # Assign task (prompt), get response + agent_response = AGENT_MANAGER.message_agent(key, prompt) + + return f"Agent {name} created with key {key}. First response: {agent_response}" + + +def message_agent(key: str, message: str) -> str: + """Message an agent with a given key and message""" + # Check if the key is a valid integer + if is_valid_int(key): + agent_response = AGENT_MANAGER.message_agent(int(key), message) + else: + return "Invalid key, must be an integer." + + # Speak response + if CFG.speak_mode: + say_text(agent_response, 1) + return agent_response + + +def list_agents(): + """List all agents + + Returns: + str: A list of all agents + """ + return "List of agents:\n" + "\n".join( + [str(x[0]) + ": " + x[1] for x in AGENT_MANAGER.list_agents()] + ) + + +def delete_agent(key: str) -> str: + """Delete an agent with a given key + + Args: + key (str): The key of the agent to delete + + Returns: + str: A message indicating whether the agent was deleted or not + """ + result = AGENT_MANAGER.delete_agent(key) + return f"Agent {key} deleted." if result else f"Agent {key} does not exist." diff --git a/autogpt/chat.py b/autogpt/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..1f6bca96eb216c667656b50f131006b83c681065 --- /dev/null +++ b/autogpt/chat.py @@ -0,0 +1,175 @@ +import time + +from openai.error import RateLimitError + +from autogpt import token_counter +from autogpt.config import Config +from autogpt.llm_utils import create_chat_completion +from autogpt.logs import logger + +cfg = Config() + + +def create_chat_message(role, content): + """ + Create a chat message with the given role and content. + + Args: + role (str): The role of the message sender, e.g., "system", "user", or "assistant". + content (str): The content of the message. + + Returns: + dict: A dictionary containing the role and content of the message. + """ + return {"role": role, "content": content} + + +def generate_context(prompt, relevant_memory, full_message_history, model): + current_context = [ + create_chat_message("system", prompt), + create_chat_message( + "system", f"The current time and date is {time.strftime('%c')}" + ), + create_chat_message( + "system", + f"This reminds you of these events from your past:\n{relevant_memory}\n\n", + ), + ] + + # Add messages from the full message history until we reach the token limit + next_message_to_add_index = len(full_message_history) - 1 + insertion_index = len(current_context) + # Count the currently used tokens + current_tokens_used = token_counter.count_message_tokens(current_context, model) + return ( + next_message_to_add_index, + current_tokens_used, + insertion_index, + current_context, + ) + + +# TODO: Change debug from hardcode to argument +def chat_with_ai( + prompt, user_input, full_message_history, permanent_memory, token_limit +): + """Interact with the OpenAI API, sending the prompt, user input, message history, + and permanent memory.""" + while True: + try: + """ + Interact with the OpenAI API, sending the prompt, user input, + message history, and permanent memory. + + Args: + prompt (str): The prompt explaining the rules to the AI. + user_input (str): The input from the user. + full_message_history (list): The list of all messages sent between the + user and the AI. + permanent_memory (Obj): The memory object containing the permanent + memory. + token_limit (int): The maximum number of tokens allowed in the API call. + + Returns: + str: The AI's response. + """ + model = cfg.fast_llm_model # TODO: Change model from hardcode to argument + # Reserve 1000 tokens for the response + + logger.debug(f"Token limit: {token_limit}") + send_token_limit = token_limit - 1000 + + relevant_memory = ( + "" + if len(full_message_history) == 0 + else permanent_memory.get_relevant(str(full_message_history[-9:]), 10) + ) + + logger.debug(f"Memory Stats: {permanent_memory.get_stats()}") + + ( + next_message_to_add_index, + current_tokens_used, + insertion_index, + current_context, + ) = generate_context(prompt, relevant_memory, full_message_history, model) + + while current_tokens_used > 2500: + # remove memories until we are under 2500 tokens + relevant_memory = relevant_memory[:-1] + ( + next_message_to_add_index, + current_tokens_used, + insertion_index, + current_context, + ) = generate_context( + prompt, relevant_memory, full_message_history, model + ) + + current_tokens_used += token_counter.count_message_tokens( + [create_chat_message("user", user_input)], model + ) # Account for user input (appended later) + + while next_message_to_add_index >= 0: + # print (f"CURRENT TOKENS USED: {current_tokens_used}") + message_to_add = full_message_history[next_message_to_add_index] + + tokens_to_add = token_counter.count_message_tokens( + [message_to_add], model + ) + if current_tokens_used + tokens_to_add > send_token_limit: + break + + # Add the most recent message to the start of the current context, + # after the two system prompts. + current_context.insert( + insertion_index, full_message_history[next_message_to_add_index] + ) + + # Count the currently used tokens + current_tokens_used += tokens_to_add + + # Move to the next most recent message in the full message history + next_message_to_add_index -= 1 + + # Append user input, the length of this is accounted for above + current_context.extend([create_chat_message("user", user_input)]) + + # Calculate remaining tokens + tokens_remaining = token_limit - current_tokens_used + # assert tokens_remaining >= 0, "Tokens remaining is negative. + # This should never happen, please submit a bug report at + # https://www.github.com/Torantulino/Auto-GPT" + + # Debug print the current context + logger.debug(f"Token limit: {token_limit}") + logger.debug(f"Send Token Count: {current_tokens_used}") + logger.debug(f"Tokens remaining for response: {tokens_remaining}") + logger.debug("------------ CONTEXT SENT TO AI ---------------") + for message in current_context: + # Skip printing the prompt + if message["role"] == "system" and message["content"] == prompt: + continue + logger.debug(f"{message['role'].capitalize()}: {message['content']}") + logger.debug("") + logger.debug("----------- END OF CONTEXT ----------------") + + # TODO: use a model defined elsewhere, so that model can contain + # temperature and other settings we care about + assistant_reply = create_chat_completion( + model=model, + messages=current_context, + max_tokens=tokens_remaining, + ) + + # Update full message history + full_message_history.append(create_chat_message("user", user_input)) + full_message_history.append( + create_chat_message("assistant", assistant_reply) + ) + + return assistant_reply + except RateLimitError: + # TODO: When we switch to langchain, this is built in + print("Error: ", "API Rate Limit Reached. Waiting 10 seconds...") + time.sleep(10) diff --git a/autogpt/cli.py b/autogpt/cli.py new file mode 100644 index 0000000000000000000000000000000000000000..a2e99cb421cad005528cb160e948ce59ccfcdb66 --- /dev/null +++ b/autogpt/cli.py @@ -0,0 +1,145 @@ +"""Main script for the autogpt package.""" +import click + + +@click.group(invoke_without_command=True) +@click.option("-c", "--continuous", is_flag=True, help="Enable Continuous Mode") +@click.option( + "--skip-reprompt", + "-y", + is_flag=True, + help="Skips the re-prompting messages at the beginning of the script", +) +@click.option( + "--ai-settings", + "-C", + help="Specifies which ai_settings.yaml file to use, will also automatically skip the re-prompt.", +) +@click.option( + "-l", + "--continuous-limit", + type=int, + help="Defines the number of times to run in continuous mode", +) +@click.option("--speak", is_flag=True, help="Enable Speak Mode") +@click.option("--debug", is_flag=True, help="Enable Debug Mode") +@click.option("--gpt3only", is_flag=True, help="Enable GPT3.5 Only Mode") +@click.option("--gpt4only", is_flag=True, help="Enable GPT4 Only Mode") +@click.option( + "--use-memory", + "-m", + "memory_type", + type=str, + help="Defines which Memory backend to use", +) +@click.option( + "-b", + "--browser-name", + help="Specifies which web-browser to use when using selenium to scrape the web.", +) +@click.option( + "--allow-downloads", + is_flag=True, + help="Dangerous: Allows Auto-GPT to download files natively.", +) +@click.option( + "--skip-news", + is_flag=True, + help="Specifies whether to suppress the output of latest news on startup.", +) +@click.pass_context +def main( + ctx: click.Context, + continuous: bool, + continuous_limit: int, + ai_settings: str, + skip_reprompt: bool, + speak: bool, + debug: bool, + gpt3only: bool, + gpt4only: bool, + memory_type: str, + browser_name: str, + allow_downloads: bool, + skip_news: bool, +) -> None: + """ + Welcome to AutoGPT an experimental open-source application showcasing the capabilities of the GPT-4 pushing the boundaries of AI. + + Start an Auto-GPT assistant. + """ + # Put imports inside function to avoid importing everything when starting the CLI + import logging + + from colorama import Fore + + from autogpt.agent.agent import Agent + from autogpt.config import Config, check_openai_api_key + from autogpt.configurator import create_config + from autogpt.logs import logger + from autogpt.memory import get_memory + from autogpt.prompt import construct_prompt + from autogpt.utils import get_current_git_branch, get_latest_bulletin + + if ctx.invoked_subcommand is None: + cfg = Config() + # TODO: fill in llm values here + check_openai_api_key() + create_config( + continuous, + continuous_limit, + ai_settings, + skip_reprompt, + speak, + debug, + gpt3only, + gpt4only, + memory_type, + browser_name, + allow_downloads, + skip_news, + ) + logger.set_level(logging.DEBUG if cfg.debug_mode else logging.INFO) + ai_name = "" + if not cfg.skip_news: + motd = get_latest_bulletin() + if motd: + logger.typewriter_log("NEWS: ", Fore.GREEN, motd) + git_branch = get_current_git_branch() + if git_branch and git_branch != "stable": + logger.typewriter_log( + "WARNING: ", + Fore.RED, + f"You are running on `{git_branch}` branch " + "- this is not a supported branch.", + ) + system_prompt = construct_prompt() + # print(prompt) + # Initialize variables + full_message_history = [] + next_action_count = 0 + # Make a constant: + triggering_prompt = ( + "Determine which next command to use, and respond using the" + " format specified above:" + ) + # Initialize memory and make sure it is empty. + # this is particularly important for indexing and referencing pinecone memory + memory = get_memory(cfg, init=True) + logger.typewriter_log( + "Using memory of type:", Fore.GREEN, f"{memory.__class__.__name__}" + ) + logger.typewriter_log("Using Browser:", Fore.GREEN, cfg.selenium_web_browser) + agent = Agent( + ai_name=ai_name, + memory=memory, + full_message_history=full_message_history, + next_action_count=next_action_count, + system_prompt=system_prompt, + triggering_prompt=triggering_prompt, + ) + agent.start_interaction_loop() + + +if __name__ == "__main__": + main() diff --git a/autogpt/commands/__init__.py b/autogpt/commands/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autogpt/commands/analyze_code.py b/autogpt/commands/analyze_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e02ea4c5b4ba53530e559d1cab7a07b8e3c7c638 --- /dev/null +++ b/autogpt/commands/analyze_code.py @@ -0,0 +1,25 @@ +"""Code evaluation module.""" +from __future__ import annotations + +from autogpt.llm_utils import call_ai_function + + +def analyze_code(code: str) -> list[str]: + """ + A function that takes in a string and returns a response from create chat + completion api call. + + Parameters: + code (str): Code to be evaluated. + Returns: + A result string from create chat completion. A list of suggestions to + improve the code. + """ + + function_string = "def analyze_code(code: str) -> List[str]:" + args = [code] + description_string = ( + "Analyzes the given code and returns a list of suggestions" " for improvements." + ) + + return call_ai_function(function_string, args, description_string) diff --git a/autogpt/commands/audio_text.py b/autogpt/commands/audio_text.py new file mode 100644 index 0000000000000000000000000000000000000000..cae32d4eb78c4268bf6ef1bae3c15a399af046bf --- /dev/null +++ b/autogpt/commands/audio_text.py @@ -0,0 +1,36 @@ +import json + +import requests + +from autogpt.config import Config +from autogpt.workspace import path_in_workspace + +cfg = Config() + + +def read_audio_from_file(audio_path): + audio_path = path_in_workspace(audio_path) + with open(audio_path, "rb") as audio_file: + audio = audio_file.read() + return read_audio(audio) + + +def read_audio(audio): + model = cfg.huggingface_audio_to_text_model + api_url = f"https://api-inference.huggingface.co/models/{model}" + api_token = cfg.huggingface_api_token + headers = {"Authorization": f"Bearer {api_token}"} + + if api_token is None: + raise ValueError( + "You need to set your Hugging Face API token in the config file." + ) + + response = requests.post( + api_url, + headers=headers, + data=audio, + ) + + text = json.loads(response.content.decode("utf-8"))["text"] + return "The audio says: " + text diff --git a/autogpt/commands/execute_code.py b/autogpt/commands/execute_code.py new file mode 100644 index 0000000000000000000000000000000000000000..11266f852727f2f8aedbc995b1e504a17acbfb77 --- /dev/null +++ b/autogpt/commands/execute_code.py @@ -0,0 +1,158 @@ +"""Execute code in a Docker container""" +import os +import subprocess + +import docker +from docker.errors import ImageNotFound + +from autogpt.workspace import WORKSPACE_PATH, path_in_workspace + + +def execute_python_file(file: str) -> str: + """Execute a Python file in a Docker container and return the output + + Args: + file (str): The name of the file to execute + + Returns: + str: The output of the file + """ + + print(f"Executing file '{file}' in workspace '{WORKSPACE_PATH}'") + + if not file.endswith(".py"): + return "Error: Invalid file type. Only .py files are allowed." + + file_path = path_in_workspace(file) + + if not os.path.isfile(file_path): + return f"Error: File '{file}' does not exist." + + if we_are_running_in_a_docker_container(): + result = subprocess.run( + f"python {file_path}", capture_output=True, encoding="utf8", shell=True + ) + if result.returncode == 0: + return result.stdout + else: + return f"Error: {result.stderr}" + + try: + client = docker.from_env() + + # You can replace this with the desired Python image/version + # You can find available Python images on Docker Hub: + # https://hub.docker.com/_/python + image_name = "python:3-alpine" + try: + client.images.get(image_name) + print(f"Image '{image_name}' found locally") + except ImageNotFound: + print(f"Image '{image_name}' not found locally, pulling from Docker Hub") + # Use the low-level API to stream the pull response + low_level_client = docker.APIClient() + for line in low_level_client.pull(image_name, stream=True, decode=True): + # Print the status and progress, if available + status = line.get("status") + progress = line.get("progress") + if status and progress: + print(f"{status}: {progress}") + elif status: + print(status) + + container = client.containers.run( + image_name, + f"python {file}", + volumes={ + os.path.abspath(WORKSPACE_PATH): { + "bind": "/workspace", + "mode": "ro", + } + }, + working_dir="/workspace", + stderr=True, + stdout=True, + detach=True, + ) + + container.wait() + logs = container.logs().decode("utf-8") + container.remove() + + # print(f"Execution complete. Output: {output}") + # print(f"Logs: {logs}") + + return logs + + except docker.errors.DockerException as e: + print( + "Could not run the script in a container. If you haven't already, please install Docker https://docs.docker.com/get-docker/" + ) + return f"Error: {str(e)}" + + except Exception as e: + return f"Error: {str(e)}" + + +def execute_shell(command_line: str) -> str: + """Execute a shell command and return the output + + Args: + command_line (str): The command line to execute + + Returns: + str: The output of the command + """ + current_dir = os.getcwd() + # Change dir into workspace if necessary + if str(WORKSPACE_PATH) not in current_dir: + os.chdir(WORKSPACE_PATH) + + print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'") + + result = subprocess.run(command_line, capture_output=True, shell=True) + output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" + + # Change back to whatever the prior working dir was + + os.chdir(current_dir) + + return output + + +def execute_shell_popen(command_line) -> str: + """Execute a shell command with Popen and returns an english description + of the event and the process id + + Args: + command_line (str): The command line to execute + + Returns: + str: Description of the fact that the process started and its id + """ + current_dir = os.getcwd() + # Change dir into workspace if necessary + if str(WORKSPACE_PATH) not in current_dir: + os.chdir(WORKSPACE_PATH) + + print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'") + + do_not_show_output = subprocess.DEVNULL + process = subprocess.Popen( + command_line, shell=True, stdout=do_not_show_output, stderr=do_not_show_output + ) + + # Change back to whatever the prior working dir was + + os.chdir(current_dir) + + return f"Subprocess started with PID:'{str(process.pid)}'" + + +def we_are_running_in_a_docker_container() -> bool: + """Check if we are running in a Docker container + + Returns: + bool: True if we are running in a Docker container, False otherwise + """ + return os.path.exists("/.dockerenv") diff --git a/autogpt/commands/file_operations.py b/autogpt/commands/file_operations.py new file mode 100644 index 0000000000000000000000000000000000000000..ad145ec956dd9dafd39e09c2244d001cf5febd2f --- /dev/null +++ b/autogpt/commands/file_operations.py @@ -0,0 +1,267 @@ +"""File operations for AutoGPT""" +from __future__ import annotations + +import os +import os.path +from typing import Generator + +import requests +from colorama import Back, Fore +from requests.adapters import HTTPAdapter, Retry + +from autogpt.spinner import Spinner +from autogpt.utils import readable_file_size +from autogpt.workspace import WORKSPACE_PATH, path_in_workspace + +LOG_FILE = "file_logger.txt" +LOG_FILE_PATH = WORKSPACE_PATH / LOG_FILE + + +def check_duplicate_operation(operation: str, filename: str) -> bool: + """Check if the operation has already been performed on the given file + + Args: + operation (str): The operation to check for + filename (str): The name of the file to check for + + Returns: + bool: True if the operation has already been performed on the file + """ + log_content = read_file(LOG_FILE) + log_entry = f"{operation}: {filename}\n" + return log_entry in log_content + + +def log_operation(operation: str, filename: str) -> None: + """Log the file operation to the file_logger.txt + + Args: + operation (str): The operation to log + filename (str): The name of the file the operation was performed on + """ + log_entry = f"{operation}: {filename}\n" + + # Create the log file if it doesn't exist + if not os.path.exists(LOG_FILE_PATH): + with open(LOG_FILE_PATH, "w", encoding="utf-8") as f: + f.write("File Operation Logger ") + + append_to_file(LOG_FILE, log_entry, shouldLog=False) + + +def split_file( + content: str, max_length: int = 4000, overlap: int = 0 +) -> Generator[str, None, None]: + """ + Split text into chunks of a specified maximum length with a specified overlap + between chunks. + + :param content: The input text to be split into chunks + :param max_length: The maximum length of each chunk, + default is 4000 (about 1k token) + :param overlap: The number of overlapping characters between chunks, + default is no overlap + :return: A generator yielding chunks of text + """ + start = 0 + content_length = len(content) + + while start < content_length: + end = start + max_length + if end + overlap < content_length: + chunk = content[start : end + overlap - 1] + else: + chunk = content[start:content_length] + + # Account for the case where the last chunk is shorter than the overlap, so it has already been consumed + if len(chunk) <= overlap: + break + + yield chunk + start += max_length - overlap + + +def read_file(filename: str) -> str: + """Read a file and return the contents + + Args: + filename (str): The name of the file to read + + Returns: + str: The contents of the file + """ + try: + filepath = path_in_workspace(filename) + with open(filepath, "r", encoding="utf-8") as f: + content = f.read() + return content + except Exception as e: + return f"Error: {str(e)}" + + +def ingest_file( + filename: str, memory, max_length: int = 4000, overlap: int = 200 +) -> None: + """ + Ingest a file by reading its content, splitting it into chunks with a specified + maximum length and overlap, and adding the chunks to the memory storage. + + :param filename: The name of the file to ingest + :param memory: An object with an add() method to store the chunks in memory + :param max_length: The maximum length of each chunk, default is 4000 + :param overlap: The number of overlapping characters between chunks, default is 200 + """ + try: + print(f"Working with file {filename}") + content = read_file(filename) + content_length = len(content) + print(f"File length: {content_length} characters") + + chunks = list(split_file(content, max_length=max_length, overlap=overlap)) + + num_chunks = len(chunks) + for i, chunk in enumerate(chunks): + print(f"Ingesting chunk {i + 1} / {num_chunks} into memory") + memory_to_add = ( + f"Filename: {filename}\n" f"Content part#{i + 1}/{num_chunks}: {chunk}" + ) + + memory.add(memory_to_add) + + print(f"Done ingesting {num_chunks} chunks from {filename}.") + except Exception as e: + print(f"Error while ingesting file '{filename}': {str(e)}") + + +def write_to_file(filename: str, text: str) -> str: + """Write text to a file + + Args: + filename (str): The name of the file to write to + text (str): The text to write to the file + + Returns: + str: A message indicating success or failure + """ + if check_duplicate_operation("write", filename): + return "Error: File has already been updated." + try: + filepath = path_in_workspace(filename) + directory = os.path.dirname(filepath) + if not os.path.exists(directory): + os.makedirs(directory) + with open(filepath, "w", encoding="utf-8") as f: + f.write(text) + log_operation("write", filename) + return "File written to successfully." + except Exception as e: + return f"Error: {str(e)}" + + +def append_to_file(filename: str, text: str, shouldLog: bool = True) -> str: + """Append text to a file + + Args: + filename (str): The name of the file to append to + text (str): The text to append to the file + + Returns: + str: A message indicating success or failure + """ + try: + filepath = path_in_workspace(filename) + with open(filepath, "a") as f: + f.write(text) + + if shouldLog: + log_operation("append", filename) + + return "Text appended successfully." + except Exception as e: + return f"Error: {str(e)}" + + +def delete_file(filename: str) -> str: + """Delete a file + + Args: + filename (str): The name of the file to delete + + Returns: + str: A message indicating success or failure + """ + if check_duplicate_operation("delete", filename): + return "Error: File has already been deleted." + try: + filepath = path_in_workspace(filename) + os.remove(filepath) + log_operation("delete", filename) + return "File deleted successfully." + except Exception as e: + return f"Error: {str(e)}" + + +def search_files(directory: str) -> list[str]: + """Search for files in a directory + + Args: + directory (str): The directory to search in + + Returns: + list[str]: A list of files found in the directory + """ + found_files = [] + + if directory in {"", "/"}: + search_directory = WORKSPACE_PATH + else: + search_directory = path_in_workspace(directory) + + for root, _, files in os.walk(search_directory): + for file in files: + if file.startswith("."): + continue + relative_path = os.path.relpath(os.path.join(root, file), WORKSPACE_PATH) + found_files.append(relative_path) + + return found_files + + +def download_file(url, filename): + """Downloads a file + Args: + url (str): URL of the file to download + filename (str): Filename to save the file as + """ + safe_filename = path_in_workspace(filename) + try: + message = f"{Fore.YELLOW}Downloading file from {Back.LIGHTBLUE_EX}{url}{Back.RESET}{Fore.RESET}" + with Spinner(message) as spinner: + session = requests.Session() + retry = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504]) + adapter = HTTPAdapter(max_retries=retry) + session.mount("http://", adapter) + session.mount("https://", adapter) + + total_size = 0 + downloaded_size = 0 + + with session.get(url, allow_redirects=True, stream=True) as r: + r.raise_for_status() + total_size = int(r.headers.get("Content-Length", 0)) + downloaded_size = 0 + + with open(safe_filename, "wb") as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + downloaded_size += len(chunk) + + # Update the progress message + progress = f"{readable_file_size(downloaded_size)} / {readable_file_size(total_size)}" + spinner.update_message(f"{message} {progress}") + + return f'Successfully downloaded and locally stored file: "{filename}"! (Size: {readable_file_size(total_size)})' + except requests.HTTPError as e: + return f"Got an HTTP Error whilst trying to download file: {e}" + except Exception as e: + return "Error: " + str(e) diff --git a/autogpt/commands/git_operations.py b/autogpt/commands/git_operations.py new file mode 100644 index 0000000000000000000000000000000000000000..028f3b8da44c85e01d20ccc5d4a5fa72c759008b --- /dev/null +++ b/autogpt/commands/git_operations.py @@ -0,0 +1,26 @@ +"""Git operations for autogpt""" +import git + +from autogpt.config import Config +from autogpt.workspace import path_in_workspace + +CFG = Config() + + +def clone_repository(repo_url: str, clone_path: str) -> str: + """Clone a GitHub repository locally + + Args: + repo_url (str): The URL of the repository to clone + clone_path (str): The path to clone the repository to + + Returns: + str: The result of the clone operation""" + split_url = repo_url.split("//") + auth_repo_url = f"//{CFG.github_username}:{CFG.github_api_key}@".join(split_url) + safe_clone_path = path_in_workspace(clone_path) + try: + git.Repo.clone_from(auth_repo_url, safe_clone_path) + return f"""Cloned {repo_url} to {safe_clone_path}""" + except Exception as e: + return f"Error: {str(e)}" diff --git a/autogpt/commands/google_search.py b/autogpt/commands/google_search.py new file mode 100644 index 0000000000000000000000000000000000000000..7d38ce7568d2de207d521b077cfebd72527c9795 --- /dev/null +++ b/autogpt/commands/google_search.py @@ -0,0 +1,87 @@ +"""Google search command for Autogpt.""" +from __future__ import annotations + +import json + +from duckduckgo_search import ddg + +from autogpt.config import Config + +CFG = Config() + + +def google_search(query: str, num_results: int = 8) -> str: + """Return the results of a Google search + + Args: + query (str): The search query. + num_results (int): The number of results to return. + + Returns: + str: The results of the search. + """ + search_results = [] + if not query: + return json.dumps(search_results) + + results = ddg(query, max_results=num_results) + if not results: + return json.dumps(search_results) + + for j in results: + search_results.append(j) + + return json.dumps(search_results, ensure_ascii=False, indent=4) + + +def google_official_search(query: str, num_results: int = 8) -> str | list[str]: + """Return the results of a Google search using the official Google API + + Args: + query (str): The search query. + num_results (int): The number of results to return. + + Returns: + str: The results of the search. + """ + + from googleapiclient.discovery import build + from googleapiclient.errors import HttpError + + try: + # Get the Google API key and Custom Search Engine ID from the config file + api_key = CFG.google_api_key + custom_search_engine_id = CFG.custom_search_engine_id + + # Initialize the Custom Search API service + service = build("customsearch", "v1", developerKey=api_key) + + # Send the search query and retrieve the results + result = ( + service.cse() + .list(q=query, cx=custom_search_engine_id, num=num_results) + .execute() + ) + + # Extract the search result items from the response + search_results = result.get("items", []) + + # Create a list of only the URLs from the search results + search_results_links = [item["link"] for item in search_results] + + except HttpError as e: + # Handle errors in the API call + error_details = json.loads(e.content.decode()) + + # Check if the error is related to an invalid or missing API key + if error_details.get("error", {}).get( + "code" + ) == 403 and "invalid API key" in error_details.get("error", {}).get( + "message", "" + ): + return "Error: The provided Google API key is invalid or missing." + else: + return f"Error: {e}" + + # Return the list of search result URLs + return search_results_links diff --git a/autogpt/commands/image_gen.py b/autogpt/commands/image_gen.py new file mode 100644 index 0000000000000000000000000000000000000000..0809fcdd3e38b52a2ce09ca1444f2574813d40f9 --- /dev/null +++ b/autogpt/commands/image_gen.py @@ -0,0 +1,163 @@ +""" Image Generation Module for AutoGPT.""" +import io +import os.path +import uuid +from base64 import b64decode + +import openai +import requests +from PIL import Image + +from autogpt.config import Config +from autogpt.workspace import path_in_workspace + +CFG = Config() + + +def generate_image(prompt: str, size: int = 256) -> str: + """Generate an image from a prompt. + + Args: + prompt (str): The prompt to use + size (int, optional): The size of the image. Defaults to 256. (Not supported by HuggingFace) + + Returns: + str: The filename of the image + """ + filename = f"{str(uuid.uuid4())}.jpg" + + # DALL-E + if CFG.image_provider == "dalle": + return generate_image_with_dalle(prompt, filename, size) + # HuggingFace + elif CFG.image_provider == "huggingface": + return generate_image_with_hf(prompt, filename) + # SD WebUI + elif CFG.image_provider == "sdwebui": + return generate_image_with_sd_webui(prompt, filename, size) + return "No Image Provider Set" + + +def generate_image_with_hf(prompt: str, filename: str) -> str: + """Generate an image with HuggingFace's API. + + Args: + prompt (str): The prompt to use + filename (str): The filename to save the image to + + Returns: + str: The filename of the image + """ + API_URL = ( + f"https://api-inference.huggingface.co/models/{CFG.huggingface_image_model}" + ) + if CFG.huggingface_api_token is None: + raise ValueError( + "You need to set your Hugging Face API token in the config file." + ) + headers = { + "Authorization": f"Bearer {CFG.huggingface_api_token}", + "X-Use-Cache": "false", + } + + response = requests.post( + API_URL, + headers=headers, + json={ + "inputs": prompt, + }, + ) + + image = Image.open(io.BytesIO(response.content)) + print(f"Image Generated for prompt:{prompt}") + + image.save(path_in_workspace(filename)) + + return f"Saved to disk:{filename}" + + +def generate_image_with_dalle(prompt: str, filename: str) -> str: + """Generate an image with DALL-E. + + Args: + prompt (str): The prompt to use + filename (str): The filename to save the image to + + Returns: + str: The filename of the image + """ + openai.api_key = CFG.openai_api_key + + # Check for supported image sizes + if size not in [256, 512, 1024]: + closest = min([256, 512, 1024], key=lambda x: abs(x - size)) + print( + f"DALL-E only supports image sizes of 256x256, 512x512, or 1024x1024. Setting to {closest}, was {size}." + ) + size = closest + + response = openai.Image.create( + prompt=prompt, + n=1, + size=f"{size}x{size}", + response_format="b64_json", + ) + + print(f"Image Generated for prompt:{prompt}") + + image_data = b64decode(response["data"][0]["b64_json"]) + + with open(path_in_workspace(filename), mode="wb") as png: + png.write(image_data) + + return f"Saved to disk:{filename}" + + +def generate_image_with_sd_webui( + prompt: str, + filename: str, + size: int = 512, + negative_prompt: str = "", + extra: dict = {}, +) -> str: + """Generate an image with Stable Diffusion webui. + Args: + prompt (str): The prompt to use + filename (str): The filename to save the image to + size (int, optional): The size of the image. Defaults to 256. + negative_prompt (str, optional): The negative prompt to use. Defaults to "". + extra (dict, optional): Extra parameters to pass to the API. Defaults to {}. + Returns: + str: The filename of the image + """ + # Create a session and set the basic auth if needed + s = requests.Session() + if CFG.sd_webui_auth: + username, password = CFG.sd_webui_auth.split(":") + s.auth = (username, password or "") + + # Generate the images + response = requests.post( + f"{CFG.sd_webui_url}/sdapi/v1/txt2img", + json={ + "prompt": prompt, + "negative_prompt": negative_prompt, + "sampler_index": "DDIM", + "steps": 20, + "cfg_scale": 7.0, + "width": size, + "height": size, + "n_iter": 1, + **extra, + }, + ) + + print(f"Image Generated for prompt:{prompt}") + + # Save the image to disk + response = response.json() + b64 = b64decode(response["images"][0].split(",", 1)[0]) + image = Image.open(io.BytesIO(b64)) + image.save(path_in_workspace(filename)) + + return f"Saved to disk:{filename}" diff --git a/autogpt/commands/improve_code.py b/autogpt/commands/improve_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e3440d8b7c6ee8cb62d73df48623ab757c973c59 --- /dev/null +++ b/autogpt/commands/improve_code.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +import json + +from autogpt.llm_utils import call_ai_function + + +def improve_code(suggestions: list[str], code: str) -> str: + """ + A function that takes in code and suggestions and returns a response from create + chat completion api call. + + Parameters: + suggestions (List): A list of suggestions around what needs to be improved. + code (str): Code to be improved. + Returns: + A result string from create chat completion. Improved code in response. + """ + + function_string = ( + "def generate_improved_code(suggestions: List[str], code: str) -> str:" + ) + args = [json.dumps(suggestions), code] + description_string = ( + "Improves the provided code based on the suggestions" + " provided, making no other changes." + ) + + return call_ai_function(function_string, args, description_string) diff --git a/autogpt/commands/times.py b/autogpt/commands/times.py new file mode 100644 index 0000000000000000000000000000000000000000..3c9b8a4fc67a251c9e81a8c4a725cd1e25fcbebe --- /dev/null +++ b/autogpt/commands/times.py @@ -0,0 +1,10 @@ +from datetime import datetime + + +def get_datetime() -> str: + """Return the current date and time + + Returns: + str: The current date and time + """ + return "Current date and time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") diff --git a/autogpt/commands/twitter.py b/autogpt/commands/twitter.py new file mode 100644 index 0000000000000000000000000000000000000000..3eaed36e20e1c520690ac59f25a4da6501f3440f --- /dev/null +++ b/autogpt/commands/twitter.py @@ -0,0 +1,26 @@ +import os + +import tweepy +from dotenv import load_dotenv + +load_dotenv() + + +def send_tweet(tweet_text): + consumer_key = os.environ.get("TW_CONSUMER_KEY") + consumer_secret = os.environ.get("TW_CONSUMER_SECRET") + access_token = os.environ.get("TW_ACCESS_TOKEN") + access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET") + # Authenticate to Twitter + auth = tweepy.OAuthHandler(consumer_key, consumer_secret) + auth.set_access_token(access_token, access_token_secret) + + # Create API object + api = tweepy.API(auth) + + # Send tweet + try: + api.update_status(tweet_text) + print("Tweet sent successfully!") + except tweepy.TweepyException as e: + print("Error sending tweet: {}".format(e.reason)) diff --git a/autogpt/commands/web_playwright.py b/autogpt/commands/web_playwright.py new file mode 100644 index 0000000000000000000000000000000000000000..4e388ded203cefb5e24f9116f7fe5b8a94893413 --- /dev/null +++ b/autogpt/commands/web_playwright.py @@ -0,0 +1,80 @@ +"""Web scraping commands using Playwright""" +from __future__ import annotations + +try: + from playwright.sync_api import sync_playwright +except ImportError: + print( + "Playwright not installed. Please install it with 'pip install playwright' to use." + ) +from bs4 import BeautifulSoup + +from autogpt.processing.html import extract_hyperlinks, format_hyperlinks + + +def scrape_text(url: str) -> str: + """Scrape text from a webpage + + Args: + url (str): The URL to scrape text from + + Returns: + str: The scraped text + """ + with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + + try: + page.goto(url) + html_content = page.content() + soup = BeautifulSoup(html_content, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + text = soup.get_text() + lines = (line.strip() for line in text.splitlines()) + chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) + text = "\n".join(chunk for chunk in chunks if chunk) + + except Exception as e: + text = f"Error: {str(e)}" + + finally: + browser.close() + + return text + + +def scrape_links(url: str) -> str | list[str]: + """Scrape links from a webpage + + Args: + url (str): The URL to scrape links from + + Returns: + Union[str, List[str]]: The scraped links + """ + with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + + try: + page.goto(url) + html_content = page.content() + soup = BeautifulSoup(html_content, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + hyperlinks = extract_hyperlinks(soup, url) + formatted_links = format_hyperlinks(hyperlinks) + + except Exception as e: + formatted_links = f"Error: {str(e)}" + + finally: + browser.close() + + return formatted_links diff --git a/autogpt/commands/web_requests.py b/autogpt/commands/web_requests.py new file mode 100644 index 0000000000000000000000000000000000000000..406338f46fc7b2381e0b1634c628b123ef20b685 --- /dev/null +++ b/autogpt/commands/web_requests.py @@ -0,0 +1,190 @@ +"""Browse a webpage and summarize it using the LLM model""" +from __future__ import annotations + +from urllib.parse import urljoin, urlparse + +import requests +from bs4 import BeautifulSoup +from requests import Response +from requests.compat import urljoin + +from autogpt.config import Config +from autogpt.memory import get_memory +from autogpt.processing.html import extract_hyperlinks, format_hyperlinks + +CFG = Config() +memory = get_memory(CFG) + +session = requests.Session() +session.headers.update({"User-Agent": CFG.user_agent}) + + +def is_valid_url(url: str) -> bool: + """Check if the URL is valid + + Args: + url (str): The URL to check + + Returns: + bool: True if the URL is valid, False otherwise + """ + try: + result = urlparse(url) + return all([result.scheme, result.netloc]) + except ValueError: + return False + + +def sanitize_url(url: str) -> str: + """Sanitize the URL + + Args: + url (str): The URL to sanitize + + Returns: + str: The sanitized URL + """ + return urljoin(url, urlparse(url).path) + + +def check_local_file_access(url: str) -> bool: + """Check if the URL is a local file + + Args: + url (str): The URL to check + + Returns: + bool: True if the URL is a local file, False otherwise + """ + local_prefixes = [ + "file:///", + "file://localhost/", + "file://localhost", + "http://localhost", + "http://localhost/", + "https://localhost", + "https://localhost/", + "http://2130706433", + "http://2130706433/", + "https://2130706433", + "https://2130706433/", + "http://127.0.0.1/", + "http://127.0.0.1", + "https://127.0.0.1/", + "https://127.0.0.1", + "https://0.0.0.0/", + "https://0.0.0.0", + "http://0.0.0.0/", + "http://0.0.0.0", + "http://0000", + "http://0000/", + "https://0000", + "https://0000/", + ] + return any(url.startswith(prefix) for prefix in local_prefixes) + + +def get_response( + url: str, timeout: int = 10 +) -> tuple[None, str] | tuple[Response, None]: + """Get the response from a URL + + Args: + url (str): The URL to get the response from + timeout (int): The timeout for the HTTP request + + Returns: + tuple[None, str] | tuple[Response, None]: The response and error message + + Raises: + ValueError: If the URL is invalid + requests.exceptions.RequestException: If the HTTP request fails + """ + try: + # Restrict access to local files + if check_local_file_access(url): + raise ValueError("Access to local files is restricted") + + # Most basic check if the URL is valid: + if not url.startswith("http://") and not url.startswith("https://"): + raise ValueError("Invalid URL format") + + sanitized_url = sanitize_url(url) + + response = session.get(sanitized_url, timeout=timeout) + + # Check if the response contains an HTTP error + if response.status_code >= 400: + return None, f"Error: HTTP {str(response.status_code)} error" + + return response, None + except ValueError as ve: + # Handle invalid URL format + return None, f"Error: {str(ve)}" + + except requests.exceptions.RequestException as re: + # Handle exceptions related to the HTTP request + # (e.g., connection errors, timeouts, etc.) + return None, f"Error: {str(re)}" + + +def scrape_text(url: str) -> str: + """Scrape text from a webpage + + Args: + url (str): The URL to scrape text from + + Returns: + str: The scraped text + """ + response, error_message = get_response(url) + if error_message: + return error_message + if not response: + return "Error: Could not get response" + + soup = BeautifulSoup(response.text, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + text = soup.get_text() + lines = (line.strip() for line in text.splitlines()) + chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) + text = "\n".join(chunk for chunk in chunks if chunk) + + return text + + +def scrape_links(url: str) -> str | list[str]: + """Scrape links from a webpage + + Args: + url (str): The URL to scrape links from + + Returns: + str | list[str]: The scraped links + """ + response, error_message = get_response(url) + if error_message: + return error_message + if not response: + return "Error: Could not get response" + soup = BeautifulSoup(response.text, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + hyperlinks = extract_hyperlinks(soup, url) + + return format_hyperlinks(hyperlinks) + + +def create_message(chunk, question): + """Create a message for the user to summarize a chunk of text""" + return { + "role": "user", + "content": f'"""{chunk}""" Using the above text, answer the following' + f' question: "{question}" -- if the question cannot be answered using the' + " text, summarize the text.", + } diff --git a/autogpt/commands/web_selenium.py b/autogpt/commands/web_selenium.py new file mode 100644 index 0000000000000000000000000000000000000000..11bdfeb1f1630fc6ff6f55d68e8d7233281c5098 --- /dev/null +++ b/autogpt/commands/web_selenium.py @@ -0,0 +1,154 @@ +"""Selenium web scraping module.""" +from __future__ import annotations + +import logging +from pathlib import Path +from sys import platform + +from bs4 import BeautifulSoup +from selenium import webdriver +from selenium.webdriver.chrome.options import Options as ChromeOptions +from selenium.webdriver.common.by import By +from selenium.webdriver.firefox.options import Options as FirefoxOptions +from selenium.webdriver.remote.webdriver import WebDriver +from selenium.webdriver.safari.options import Options as SafariOptions +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.wait import WebDriverWait +from webdriver_manager.chrome import ChromeDriverManager +from webdriver_manager.firefox import GeckoDriverManager + +import autogpt.processing.text as summary +from autogpt.config import Config +from autogpt.processing.html import extract_hyperlinks, format_hyperlinks + +FILE_DIR = Path(__file__).parent.parent +CFG = Config() + + +def browse_website(url: str, question: str) -> tuple[str, WebDriver]: + """Browse a website and return the answer and links to the user + + Args: + url (str): The url of the website to browse + question (str): The question asked by the user + + Returns: + Tuple[str, WebDriver]: The answer and links to the user and the webdriver + """ + driver, text = scrape_text_with_selenium(url) + add_header(driver) + summary_text = summary.summarize_text(url, text, question, driver) + links = scrape_links_with_selenium(driver, url) + + # Limit links to 5 + if len(links) > 5: + links = links[:5] + close_browser(driver) + return f"Answer gathered from website: {summary_text} \n \n Links: {links}", driver + + +def scrape_text_with_selenium(url: str) -> tuple[WebDriver, str]: + """Scrape text from a website using selenium + + Args: + url (str): The url of the website to scrape + + Returns: + Tuple[WebDriver, str]: The webdriver and the text scraped from the website + """ + logging.getLogger("selenium").setLevel(logging.CRITICAL) + + options_available = { + "chrome": ChromeOptions, + "safari": SafariOptions, + "firefox": FirefoxOptions, + } + + options = options_available[CFG.selenium_web_browser]() + options.add_argument( + "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.49 Safari/537.36" + ) + + if CFG.selenium_web_browser == "firefox": + driver = webdriver.Firefox( + executable_path=GeckoDriverManager().install(), options=options + ) + elif CFG.selenium_web_browser == "safari": + # Requires a bit more setup on the users end + # See https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari + driver = webdriver.Safari(options=options) + else: + if platform == "linux" or platform == "linux2": + options.add_argument("--disable-dev-shm-usage") + options.add_argument("--remote-debugging-port=9222") + + options.add_argument("--no-sandbox") + if CFG.selenium_headless: + options.add_argument("--headless") + options.add_argument("--disable-gpu") + + driver = webdriver.Chrome( + executable_path=ChromeDriverManager().install(), options=options + ) + driver.get(url) + + WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.TAG_NAME, "body")) + ) + + # Get the HTML content directly from the browser's DOM + page_source = driver.execute_script("return document.body.outerHTML;") + soup = BeautifulSoup(page_source, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + text = soup.get_text() + lines = (line.strip() for line in text.splitlines()) + chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) + text = "\n".join(chunk for chunk in chunks if chunk) + return driver, text + + +def scrape_links_with_selenium(driver: WebDriver, url: str) -> list[str]: + """Scrape links from a website using selenium + + Args: + driver (WebDriver): The webdriver to use to scrape the links + + Returns: + List[str]: The links scraped from the website + """ + page_source = driver.page_source + soup = BeautifulSoup(page_source, "html.parser") + + for script in soup(["script", "style"]): + script.extract() + + hyperlinks = extract_hyperlinks(soup, url) + + return format_hyperlinks(hyperlinks) + + +def close_browser(driver: WebDriver) -> None: + """Close the browser + + Args: + driver (WebDriver): The webdriver to close + + Returns: + None + """ + driver.quit() + + +def add_header(driver: WebDriver) -> None: + """Add a header to the website + + Args: + driver (WebDriver): The webdriver to use to add the header + + Returns: + None + """ + driver.execute_script(open(f"{FILE_DIR}/js/overlay.js", "r").read()) diff --git a/autogpt/commands/write_tests.py b/autogpt/commands/write_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..35a086536c9d05d520a84b15ead49f775eacdcc9 --- /dev/null +++ b/autogpt/commands/write_tests.py @@ -0,0 +1,31 @@ +"""A module that contains a function to generate test cases for the submitted code.""" +from __future__ import annotations + +import json + +from autogpt.llm_utils import call_ai_function + + +def write_tests(code: str, focus: list[str]) -> str: + """ + A function that takes in code and focus topics and returns a response from create + chat completion api call. + + Parameters: + focus (list): A list of suggestions around what needs to be improved. + code (str): Code for test cases to be generated against. + Returns: + A result string from create chat completion. Test cases for the submitted code + in response. + """ + + function_string = ( + "def create_test_cases(code: str, focus: Optional[str] = None) -> str:" + ) + args = [code, json.dumps(focus)] + description_string = ( + "Generates test cases for the existing code, focusing on" + " specific areas if required." + ) + + return call_ai_function(function_string, args, description_string) diff --git a/autogpt/config/__init__.py b/autogpt/config/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..726b6dcf3da95968b948c4d897e97a9cdd0928ff --- /dev/null +++ b/autogpt/config/__init__.py @@ -0,0 +1,14 @@ +""" +This module contains the configuration classes for AutoGPT. +""" +from autogpt.config.ai_config import AIConfig +from autogpt.config.config import Config, check_openai_api_key +from autogpt.config.singleton import AbstractSingleton, Singleton + +__all__ = [ + "check_openai_api_key", + "AbstractSingleton", + "AIConfig", + "Config", + "Singleton", +] diff --git a/autogpt/config/ai_config.py b/autogpt/config/ai_config.py new file mode 100644 index 0000000000000000000000000000000000000000..d50c30beee9dc8009f63415378ae1c6a399f0037 --- /dev/null +++ b/autogpt/config/ai_config.py @@ -0,0 +1,121 @@ +# sourcery skip: do-not-use-staticmethod +""" +A module that contains the AIConfig class object that contains the configuration +""" +from __future__ import annotations + +import os +from typing import Type + +import yaml + + +class AIConfig: + """ + A class object that contains the configuration information for the AI + + Attributes: + ai_name (str): The name of the AI. + ai_role (str): The description of the AI's role. + ai_goals (list): The list of objectives the AI is supposed to complete. + """ + + def __init__( + self, ai_name: str = "", ai_role: str = "", ai_goals: list | None = None + ) -> None: + """ + Initialize a class instance + + Parameters: + ai_name (str): The name of the AI. + ai_role (str): The description of the AI's role. + ai_goals (list): The list of objectives the AI is supposed to complete. + Returns: + None + """ + if ai_goals is None: + ai_goals = [] + self.ai_name = ai_name + self.ai_role = ai_role + self.ai_goals = ai_goals + + # Soon this will go in a folder where it remembers more stuff about the run(s) + SAVE_FILE = os.path.join(os.path.dirname(__file__), "..", "ai_settings.yaml") + + @staticmethod + def load(config_file: str = SAVE_FILE) -> "AIConfig": + """ + Returns class object with parameters (ai_name, ai_role, ai_goals) loaded from + yaml file if yaml file exists, + else returns class with no parameters. + + Parameters: + config_file (int): The path to the config yaml file. + DEFAULT: "../ai_settings.yaml" + + Returns: + cls (object): An instance of given cls object + """ + + try: + with open(config_file, encoding="utf-8") as file: + config_params = yaml.load(file, Loader=yaml.FullLoader) + except FileNotFoundError: + config_params = {} + + ai_name = config_params.get("ai_name", "") + ai_role = config_params.get("ai_role", "") + ai_goals = config_params.get("ai_goals", []) + # type: Type[AIConfig] + return AIConfig(ai_name, ai_role, ai_goals) + + def save(self, config_file: str = SAVE_FILE) -> None: + """ + Saves the class parameters to the specified file yaml file path as a yaml file. + + Parameters: + config_file(str): The path to the config yaml file. + DEFAULT: "../ai_settings.yaml" + + Returns: + None + """ + + config = { + "ai_name": self.ai_name, + "ai_role": self.ai_role, + "ai_goals": self.ai_goals, + } + with open(config_file, "w", encoding="utf-8") as file: + yaml.dump(config, file, allow_unicode=True) + + def construct_full_prompt(self) -> str: + """ + Returns a prompt to the user with the class information in an organized fashion. + + Parameters: + None + + Returns: + full_prompt (str): A string containing the initial prompt for the user + including the ai_name, ai_role and ai_goals. + """ + + prompt_start = ( + "Your decisions must always be made independently without" + " seeking user assistance. Play to your strengths as an LLM and pursue" + " simple strategies with no legal complications." + "" + ) + + from autogpt.prompt import get_prompt + + # Construct full prompt + full_prompt = ( + f"You are {self.ai_name}, {self.ai_role}\n{prompt_start}\n\nGOALS:\n\n" + ) + for i, goal in enumerate(self.ai_goals): + full_prompt += f"{i+1}. {goal}\n" + + full_prompt += f"\n\n{get_prompt()}" + return full_prompt diff --git a/autogpt/config/config.py b/autogpt/config/config.py new file mode 100644 index 0000000000000000000000000000000000000000..4b53df10e8d2832be7ffb321d9036aec5a47a79d --- /dev/null +++ b/autogpt/config/config.py @@ -0,0 +1,251 @@ +"""Configuration class to store the state of bools for different scripts access.""" +import os + +import openai +import yaml +from colorama import Fore +from dotenv import load_dotenv + +from autogpt.config.singleton import Singleton + +load_dotenv(verbose=True) + + +class Config(metaclass=Singleton): + """ + Configuration class to store the state of bools for different scripts access. + """ + + def __init__(self) -> None: + """Initialize the Config class""" + self.debug_mode = False + self.continuous_mode = False + self.continuous_limit = 0 + self.speak_mode = False + self.skip_reprompt = False + self.allow_downloads = False + self.skip_news = False + + self.ai_settings_file = os.getenv("AI_SETTINGS_FILE", "ai_settings.yaml") + self.fast_llm_model = os.getenv("FAST_LLM_MODEL", "gpt-3.5-turbo") + self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4") + self.fast_token_limit = int(os.getenv("FAST_TOKEN_LIMIT", 4000)) + self.smart_token_limit = int(os.getenv("SMART_TOKEN_LIMIT", 8000)) + self.browse_chunk_max_length = int(os.getenv("BROWSE_CHUNK_MAX_LENGTH", 8192)) + + self.openai_api_key = os.getenv("OPENAI_API_KEY") + self.temperature = float(os.getenv("TEMPERATURE", "1")) + self.use_azure = os.getenv("USE_AZURE") == "True" + self.execute_local_commands = ( + os.getenv("EXECUTE_LOCAL_COMMANDS", "False") == "True" + ) + self.restrict_to_workspace = ( + os.getenv("RESTRICT_TO_WORKSPACE", "True") == "True" + ) + + if self.use_azure: + self.load_azure_config() + openai.api_type = self.openai_api_type + openai.api_base = self.openai_api_base + openai.api_version = self.openai_api_version + + self.elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY") + self.elevenlabs_voice_1_id = os.getenv("ELEVENLABS_VOICE_1_ID") + self.elevenlabs_voice_2_id = os.getenv("ELEVENLABS_VOICE_2_ID") + + self.use_mac_os_tts = False + self.use_mac_os_tts = os.getenv("USE_MAC_OS_TTS") + + self.use_brian_tts = False + self.use_brian_tts = os.getenv("USE_BRIAN_TTS") + + self.github_api_key = os.getenv("GITHUB_API_KEY") + self.github_username = os.getenv("GITHUB_USERNAME") + + self.google_api_key = os.getenv("GOOGLE_API_KEY") + self.custom_search_engine_id = os.getenv("CUSTOM_SEARCH_ENGINE_ID") + + self.pinecone_api_key = os.getenv("PINECONE_API_KEY") + self.pinecone_region = os.getenv("PINECONE_ENV") + + self.weaviate_host = os.getenv("WEAVIATE_HOST") + self.weaviate_port = os.getenv("WEAVIATE_PORT") + self.weaviate_protocol = os.getenv("WEAVIATE_PROTOCOL", "http") + self.weaviate_username = os.getenv("WEAVIATE_USERNAME", None) + self.weaviate_password = os.getenv("WEAVIATE_PASSWORD", None) + self.weaviate_scopes = os.getenv("WEAVIATE_SCOPES", None) + self.weaviate_embedded_path = os.getenv("WEAVIATE_EMBEDDED_PATH") + self.weaviate_api_key = os.getenv("WEAVIATE_API_KEY", None) + self.use_weaviate_embedded = ( + os.getenv("USE_WEAVIATE_EMBEDDED", "False") == "True" + ) + + # milvus configuration, e.g., localhost:19530. + self.milvus_addr = os.getenv("MILVUS_ADDR", "localhost:19530") + self.milvus_collection = os.getenv("MILVUS_COLLECTION", "autogpt") + + self.image_provider = os.getenv("IMAGE_PROVIDER") + self.image_size = int(os.getenv("IMAGE_SIZE", 256)) + self.huggingface_api_token = os.getenv("HUGGINGFACE_API_TOKEN") + self.huggingface_image_model = os.getenv( + "HUGGINGFACE_IMAGE_MODEL", "CompVis/stable-diffusion-v1-4" + ) + self.huggingface_audio_to_text_model = os.getenv( + "HUGGINGFACE_AUDIO_TO_TEXT_MODEL" + ) + self.sd_webui_url = os.getenv("SD_WEBUI_URL", "http://localhost:7860") + self.sd_webui_auth = os.getenv("SD_WEBUI_AUTH") + + # Selenium browser settings + self.selenium_web_browser = os.getenv("USE_WEB_BROWSER", "chrome") + self.selenium_headless = os.getenv("HEADLESS_BROWSER", "True") == "True" + + # User agent header to use when making HTTP requests + # Some websites might just completely deny request with an error code if + # no user agent was found. + self.user_agent = os.getenv( + "USER_AGENT", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36" + " (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36", + ) + + self.redis_host = os.getenv("REDIS_HOST", "localhost") + self.redis_port = os.getenv("REDIS_PORT", "6379") + self.redis_password = os.getenv("REDIS_PASSWORD", "") + self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == "True" + self.memory_index = os.getenv("MEMORY_INDEX", "auto-gpt") + # Note that indexes must be created on db 0 in redis, this is not configurable. + + self.memory_backend = os.getenv("MEMORY_BACKEND", "local") + # Initialize the OpenAI API client + openai.api_key = self.openai_api_key + + def get_azure_deployment_id_for_model(self, model: str) -> str: + """ + Returns the relevant deployment id for the model specified. + + Parameters: + model(str): The model to map to the deployment id. + + Returns: + The matching deployment id if found, otherwise an empty string. + """ + if model == self.fast_llm_model: + return self.azure_model_to_deployment_id_map[ + "fast_llm_model_deployment_id" + ] # type: ignore + elif model == self.smart_llm_model: + return self.azure_model_to_deployment_id_map[ + "smart_llm_model_deployment_id" + ] # type: ignore + elif model == "text-embedding-ada-002": + return self.azure_model_to_deployment_id_map[ + "embedding_model_deployment_id" + ] # type: ignore + else: + return "" + + AZURE_CONFIG_FILE = os.path.join(os.path.dirname(__file__), "..", "azure.yaml") + + def load_azure_config(self, config_file: str = AZURE_CONFIG_FILE) -> None: + """ + Loads the configuration parameters for Azure hosting from the specified file + path as a yaml file. + + Parameters: + config_file(str): The path to the config yaml file. DEFAULT: "../azure.yaml" + + Returns: + None + """ + try: + with open(config_file) as file: + config_params = yaml.load(file, Loader=yaml.FullLoader) + except FileNotFoundError: + config_params = {} + self.openai_api_type = config_params.get("azure_api_type") or "azure" + self.openai_api_base = config_params.get("azure_api_base") or "" + self.openai_api_version = ( + config_params.get("azure_api_version") or "2023-03-15-preview" + ) + self.azure_model_to_deployment_id_map = config_params.get("azure_model_map", []) + + def set_continuous_mode(self, value: bool) -> None: + """Set the continuous mode value.""" + self.continuous_mode = value + + def set_continuous_limit(self, value: int) -> None: + """Set the continuous limit value.""" + self.continuous_limit = value + + def set_speak_mode(self, value: bool) -> None: + """Set the speak mode value.""" + self.speak_mode = value + + def set_fast_llm_model(self, value: str) -> None: + """Set the fast LLM model value.""" + self.fast_llm_model = value + + def set_smart_llm_model(self, value: str) -> None: + """Set the smart LLM model value.""" + self.smart_llm_model = value + + def set_fast_token_limit(self, value: int) -> None: + """Set the fast token limit value.""" + self.fast_token_limit = value + + def set_smart_token_limit(self, value: int) -> None: + """Set the smart token limit value.""" + self.smart_token_limit = value + + def set_browse_chunk_max_length(self, value: int) -> None: + """Set the browse_website command chunk max length value.""" + self.browse_chunk_max_length = value + + def set_openai_api_key(self, value: str) -> None: + """Set the OpenAI API key value.""" + self.openai_api_key = value + + def set_elevenlabs_api_key(self, value: str) -> None: + """Set the ElevenLabs API key value.""" + self.elevenlabs_api_key = value + + def set_elevenlabs_voice_1_id(self, value: str) -> None: + """Set the ElevenLabs Voice 1 ID value.""" + self.elevenlabs_voice_1_id = value + + def set_elevenlabs_voice_2_id(self, value: str) -> None: + """Set the ElevenLabs Voice 2 ID value.""" + self.elevenlabs_voice_2_id = value + + def set_google_api_key(self, value: str) -> None: + """Set the Google API key value.""" + self.google_api_key = value + + def set_custom_search_engine_id(self, value: str) -> None: + """Set the custom search engine id value.""" + self.custom_search_engine_id = value + + def set_pinecone_api_key(self, value: str) -> None: + """Set the Pinecone API key value.""" + self.pinecone_api_key = value + + def set_pinecone_region(self, value: str) -> None: + """Set the Pinecone region value.""" + self.pinecone_region = value + + def set_debug_mode(self, value: bool) -> None: + """Set the debug mode value.""" + self.debug_mode = value + + +def check_openai_api_key() -> None: + """Check if the OpenAI API key is set in config.py or as an environment variable.""" + cfg = Config() + if not cfg.openai_api_key: + print( + Fore.RED + + "Please set your OpenAI API key in .env or as an environment variable." + ) + print("You can get your key from https://platform.openai.com/account/api-keys") + exit(1) diff --git a/autogpt/config/singleton.py b/autogpt/config/singleton.py new file mode 100644 index 0000000000000000000000000000000000000000..55b2aeea120bbe51ca837265fcb7fbff467e55f2 --- /dev/null +++ b/autogpt/config/singleton.py @@ -0,0 +1,24 @@ +"""The singleton metaclass for ensuring only one instance of a class.""" +import abc + + +class Singleton(abc.ABCMeta, type): + """ + Singleton metaclass for ensuring only one instance of a class. + """ + + _instances = {} + + def __call__(cls, *args, **kwargs): + """Call method for the singleton metaclass.""" + if cls not in cls._instances: + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls] + + +class AbstractSingleton(abc.ABC, metaclass=Singleton): + """ + Abstract singleton class for ensuring only one instance of a class. + """ + + pass diff --git a/autogpt/configurator.py b/autogpt/configurator.py new file mode 100644 index 0000000000000000000000000000000000000000..1dc3be124f638b8859eb459bcb2d46696f62e2b7 --- /dev/null +++ b/autogpt/configurator.py @@ -0,0 +1,134 @@ +"""Configurator module.""" +import click +from colorama import Back, Fore, Style + +from autogpt import utils +from autogpt.config import Config +from autogpt.logs import logger +from autogpt.memory import get_supported_memory_backends + +CFG = Config() + + +def create_config( + continuous: bool, + continuous_limit: int, + ai_settings_file: str, + skip_reprompt: bool, + speak: bool, + debug: bool, + gpt3only: bool, + gpt4only: bool, + memory_type: str, + browser_name: str, + allow_downloads: bool, + skip_news: bool, +) -> None: + """Updates the config object with the given arguments. + + Args: + continuous (bool): Whether to run in continuous mode + continuous_limit (int): The number of times to run in continuous mode + ai_settings_file (str): The path to the ai_settings.yaml file + skip_reprompt (bool): Whether to skip the re-prompting messages at the beginning of the script + speak (bool): Whether to enable speak mode + debug (bool): Whether to enable debug mode + gpt3only (bool): Whether to enable GPT3.5 only mode + gpt4only (bool): Whether to enable GPT4 only mode + memory_type (str): The type of memory backend to use + browser_name (str): The name of the browser to use when using selenium to scrape the web + allow_downloads (bool): Whether to allow Auto-GPT to download files natively + skips_news (bool): Whether to suppress the output of latest news on startup + """ + CFG.set_debug_mode(False) + CFG.set_continuous_mode(False) + CFG.set_speak_mode(False) + + if debug: + logger.typewriter_log("Debug Mode: ", Fore.GREEN, "ENABLED") + CFG.set_debug_mode(True) + + if continuous: + logger.typewriter_log("Continuous Mode: ", Fore.RED, "ENABLED") + logger.typewriter_log( + "WARNING: ", + Fore.RED, + "Continuous mode is not recommended. It is potentially dangerous and may" + " cause your AI to run forever or carry out actions you would not usually" + " authorise. Use at your own risk.", + ) + CFG.set_continuous_mode(True) + + if continuous_limit: + logger.typewriter_log( + "Continuous Limit: ", Fore.GREEN, f"{continuous_limit}" + ) + CFG.set_continuous_limit(continuous_limit) + + # Check if continuous limit is used without continuous mode + if continuous_limit and not continuous: + raise click.UsageError("--continuous-limit can only be used with --continuous") + + if speak: + logger.typewriter_log("Speak Mode: ", Fore.GREEN, "ENABLED") + CFG.set_speak_mode(True) + + if gpt3only: + logger.typewriter_log("GPT3.5 Only Mode: ", Fore.GREEN, "ENABLED") + CFG.set_smart_llm_model(CFG.fast_llm_model) + + if gpt4only: + logger.typewriter_log("GPT4 Only Mode: ", Fore.GREEN, "ENABLED") + CFG.set_fast_llm_model(CFG.smart_llm_model) + + if memory_type: + supported_memory = get_supported_memory_backends() + chosen = memory_type + if chosen not in supported_memory: + logger.typewriter_log( + "ONLY THE FOLLOWING MEMORY BACKENDS ARE SUPPORTED: ", + Fore.RED, + f"{supported_memory}", + ) + logger.typewriter_log("Defaulting to: ", Fore.YELLOW, CFG.memory_backend) + else: + CFG.memory_backend = chosen + + if skip_reprompt: + logger.typewriter_log("Skip Re-prompt: ", Fore.GREEN, "ENABLED") + CFG.skip_reprompt = True + + if ai_settings_file: + file = ai_settings_file + + # Validate file + (validated, message) = utils.validate_yaml_file(file) + if not validated: + logger.typewriter_log("FAILED FILE VALIDATION", Fore.RED, message) + logger.double_check() + exit(1) + + logger.typewriter_log("Using AI Settings File:", Fore.GREEN, file) + CFG.ai_settings_file = file + CFG.skip_reprompt = True + + if allow_downloads: + logger.typewriter_log("Native Downloading:", Fore.GREEN, "ENABLED") + logger.typewriter_log( + "WARNING: ", + Fore.YELLOW, + f"{Back.LIGHTYELLOW_EX}Auto-GPT will now be able to download and save files to your machine.{Back.RESET} " + + "It is recommended that you monitor any files it downloads carefully.", + ) + logger.typewriter_log( + "WARNING: ", + Fore.YELLOW, + f"{Back.RED + Style.BRIGHT}ALWAYS REMEMBER TO NEVER OPEN FILES YOU AREN'T SURE OF!{Style.RESET_ALL}", + ) + CFG.allow_downloads = True + + if skip_news: + CFG.skip_news = True + + if browser_name: + CFG.selenium_web_browser = browser_name diff --git a/autogpt/js/overlay.js b/autogpt/js/overlay.js new file mode 100644 index 0000000000000000000000000000000000000000..1c99c72673330b8ea8cf037ef889233f2d4326be --- /dev/null +++ b/autogpt/js/overlay.js @@ -0,0 +1,29 @@ +const overlay = document.createElement('div'); +Object.assign(overlay.style, { + position: 'fixed', + zIndex: 999999, + top: 0, + left: 0, + width: '100%', + height: '100%', + background: 'rgba(0, 0, 0, 0.7)', + color: '#fff', + fontSize: '24px', + fontWeight: 'bold', + display: 'flex', + justifyContent: 'center', + alignItems: 'center', +}); +const textContent = document.createElement('div'); +Object.assign(textContent.style, { + textAlign: 'center', +}); +textContent.textContent = 'AutoGPT Analyzing Page'; +overlay.appendChild(textContent); +document.body.append(overlay); +document.body.style.overflow = 'hidden'; +let dotCount = 0; +setInterval(() => { + textContent.textContent = 'AutoGPT Analyzing Page' + '.'.repeat(dotCount); + dotCount = (dotCount + 1) % 4; +}, 1000); diff --git a/autogpt/json_utils/__init__.py b/autogpt/json_utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autogpt/json_utils/json_fix_general.py b/autogpt/json_utils/json_fix_general.py new file mode 100644 index 0000000000000000000000000000000000000000..7010fa3b9c1909de0e5a7f6ec13ca8aa418fe6c7 --- /dev/null +++ b/autogpt/json_utils/json_fix_general.py @@ -0,0 +1,124 @@ +"""This module contains functions to fix JSON strings using general programmatic approaches, suitable for addressing +common JSON formatting issues.""" +from __future__ import annotations + +import contextlib +import json +import re +from typing import Optional + +from autogpt.config import Config +from autogpt.json_utils.utilities import extract_char_position + +CFG = Config() + + +def fix_invalid_escape(json_to_load: str, error_message: str) -> str: + """Fix invalid escape sequences in JSON strings. + + Args: + json_to_load (str): The JSON string. + error_message (str): The error message from the JSONDecodeError + exception. + + Returns: + str: The JSON string with invalid escape sequences fixed. + """ + while error_message.startswith("Invalid \\escape"): + bad_escape_location = extract_char_position(error_message) + json_to_load = ( + json_to_load[:bad_escape_location] + json_to_load[bad_escape_location + 1 :] + ) + try: + json.loads(json_to_load) + return json_to_load + except json.JSONDecodeError as e: + if CFG.debug_mode: + print("json loads error - fix invalid escape", e) + error_message = str(e) + return json_to_load + + +def balance_braces(json_string: str) -> Optional[str]: + """ + Balance the braces in a JSON string. + + Args: + json_string (str): The JSON string. + + Returns: + str: The JSON string with braces balanced. + """ + + open_braces_count = json_string.count("{") + close_braces_count = json_string.count("}") + + while open_braces_count > close_braces_count: + json_string += "}" + close_braces_count += 1 + + while close_braces_count > open_braces_count: + json_string = json_string.rstrip("}") + close_braces_count -= 1 + + with contextlib.suppress(json.JSONDecodeError): + json.loads(json_string) + return json_string + + +def add_quotes_to_property_names(json_string: str) -> str: + """ + Add quotes to property names in a JSON string. + + Args: + json_string (str): The JSON string. + + Returns: + str: The JSON string with quotes added to property names. + """ + + def replace_func(match: re.Match) -> str: + return f'"{match[1]}":' + + property_name_pattern = re.compile(r"(\w+):") + corrected_json_string = property_name_pattern.sub(replace_func, json_string) + + try: + json.loads(corrected_json_string) + return corrected_json_string + except json.JSONDecodeError as e: + raise e + + +def correct_json(json_to_load: str) -> str: + """ + Correct common JSON errors. + Args: + json_to_load (str): The JSON string. + """ + + try: + if CFG.debug_mode: + print("json", json_to_load) + json.loads(json_to_load) + return json_to_load + except json.JSONDecodeError as e: + if CFG.debug_mode: + print("json loads error", e) + error_message = str(e) + if error_message.startswith("Invalid \\escape"): + json_to_load = fix_invalid_escape(json_to_load, error_message) + if error_message.startswith( + "Expecting property name enclosed in double quotes" + ): + json_to_load = add_quotes_to_property_names(json_to_load) + try: + json.loads(json_to_load) + return json_to_load + except json.JSONDecodeError as e: + if CFG.debug_mode: + print("json loads error - add quotes", e) + error_message = str(e) + if balanced_str := balance_braces(json_to_load): + return balanced_str + return json_to_load diff --git a/autogpt/json_utils/json_fix_llm.py b/autogpt/json_utils/json_fix_llm.py new file mode 100644 index 0000000000000000000000000000000000000000..869aed125cfb8cd7a69ed02eeb389cc72a3e296b --- /dev/null +++ b/autogpt/json_utils/json_fix_llm.py @@ -0,0 +1,220 @@ +"""This module contains functions to fix JSON strings generated by LLM models, such as ChatGPT, using the assistance +of the ChatGPT API or LLM models.""" +from __future__ import annotations + +import contextlib +import json +from typing import Any, Dict + +from colorama import Fore +from regex import regex + +from autogpt.config import Config +from autogpt.json_utils.json_fix_general import correct_json +from autogpt.llm_utils import call_ai_function +from autogpt.logs import logger +from autogpt.speech import say_text + +JSON_SCHEMA = """ +{ + "command": { + "name": "command name", + "args": { + "arg name": "value" + } + }, + "thoughts": + { + "text": "thought", + "reasoning": "reasoning", + "plan": "- short bulleted\n- list that conveys\n- long-term plan", + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user" + } +} +""" + +CFG = Config() + + +def auto_fix_json(json_string: str, schema: str) -> str: + """Fix the given JSON string to make it parseable and fully compliant with + the provided schema using GPT-3. + + Args: + json_string (str): The JSON string to fix. + schema (str): The schema to use to fix the JSON. + Returns: + str: The fixed JSON string. + """ + # Try to fix the JSON using GPT: + function_string = "def fix_json(json_string: str, schema:str=None) -> str:" + args = [f"'''{json_string}'''", f"'''{schema}'''"] + description_string = ( + "This function takes a JSON string and ensures that it" + " is parseable and fully compliant with the provided schema. If an object" + " or field specified in the schema isn't contained within the correct JSON," + " it is omitted. The function also escapes any double quotes within JSON" + " string values to ensure that they are valid. If the JSON string contains" + " any None or NaN values, they are replaced with null before being parsed." + ) + + # If it doesn't already start with a "`", add one: + if not json_string.startswith("`"): + json_string = "```json\n" + json_string + "\n```" + result_string = call_ai_function( + function_string, args, description_string, model=CFG.fast_llm_model + ) + logger.debug("------------ JSON FIX ATTEMPT ---------------") + logger.debug(f"Original JSON: {json_string}") + logger.debug("-----------") + logger.debug(f"Fixed JSON: {result_string}") + logger.debug("----------- END OF FIX ATTEMPT ----------------") + + try: + json.loads(result_string) # just check the validity + return result_string + except json.JSONDecodeError: # noqa: E722 + # Get the call stack: + # import traceback + # call_stack = traceback.format_exc() + # print(f"Failed to fix JSON: '{json_string}' "+call_stack) + return "failed" + + +def fix_json_using_multiple_techniques(assistant_reply: str) -> Dict[Any, Any]: + """Fix the given JSON string to make it parseable and fully compliant with two techniques. + + Args: + json_string (str): The JSON string to fix. + + Returns: + str: The fixed JSON string. + """ + + # Parse and print Assistant response + assistant_reply_json = fix_and_parse_json(assistant_reply) + if assistant_reply_json == {}: + assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets( + assistant_reply + ) + + if assistant_reply_json != {}: + return assistant_reply_json + + logger.error( + "Error: The following AI output couldn't be converted to a JSON:\n", + assistant_reply, + ) + if CFG.speak_mode: + say_text("I have received an invalid JSON response from the OpenAI API.") + + return {} + + +def fix_and_parse_json( + json_to_load: str, try_to_fix_with_gpt: bool = True +) -> Dict[Any, Any]: + """Fix and parse JSON string + + Args: + json_to_load (str): The JSON string. + try_to_fix_with_gpt (bool, optional): Try to fix the JSON with GPT. + Defaults to True. + + Returns: + str or dict[Any, Any]: The parsed JSON. + """ + + with contextlib.suppress(json.JSONDecodeError): + json_to_load = json_to_load.replace("\t", "") + return json.loads(json_to_load) + + with contextlib.suppress(json.JSONDecodeError): + json_to_load = correct_json(json_to_load) + return json.loads(json_to_load) + # Let's do something manually: + # sometimes GPT responds with something BEFORE the braces: + # "I'm sorry, I don't understand. Please try again." + # {"text": "I'm sorry, I don't understand. Please try again.", + # "confidence": 0.0} + # So let's try to find the first brace and then parse the rest + # of the string + try: + brace_index = json_to_load.index("{") + maybe_fixed_json = json_to_load[brace_index:] + last_brace_index = maybe_fixed_json.rindex("}") + maybe_fixed_json = maybe_fixed_json[: last_brace_index + 1] + return json.loads(maybe_fixed_json) + except (json.JSONDecodeError, ValueError) as e: + return try_ai_fix(try_to_fix_with_gpt, e, json_to_load) + + +def try_ai_fix( + try_to_fix_with_gpt: bool, exception: Exception, json_to_load: str +) -> Dict[Any, Any]: + """Try to fix the JSON with the AI + + Args: + try_to_fix_with_gpt (bool): Whether to try to fix the JSON with the AI. + exception (Exception): The exception that was raised. + json_to_load (str): The JSON string to load. + + Raises: + exception: If try_to_fix_with_gpt is False. + + Returns: + str or dict[Any, Any]: The JSON string or dictionary. + """ + if not try_to_fix_with_gpt: + raise exception + if CFG.debug_mode: + logger.warn( + "Warning: Failed to parse AI output, attempting to fix." + "\n If you see this warning frequently, it's likely that" + " your prompt is confusing the AI. Try changing it up" + " slightly." + ) + # Now try to fix this up using the ai_functions + ai_fixed_json = auto_fix_json(json_to_load, JSON_SCHEMA) + + if ai_fixed_json != "failed": + return json.loads(ai_fixed_json) + # This allows the AI to react to the error message, + # which usually results in it correcting its ways. + # logger.error("Failed to fix AI output, telling the AI.") + return {} + + +def attempt_to_fix_json_by_finding_outermost_brackets(json_string: str): + if CFG.speak_mode and CFG.debug_mode: + say_text( + "I have received an invalid JSON response from the OpenAI API. " + "Trying to fix it now." + ) + logger.error("Attempting to fix JSON by finding outermost brackets\n") + + try: + json_pattern = regex.compile(r"\{(?:[^{}]|(?R))*\}") + json_match = json_pattern.search(json_string) + + if json_match: + # Extract the valid JSON object from the string + json_string = json_match.group(0) + logger.typewriter_log( + title="Apparently json was fixed.", title_color=Fore.GREEN + ) + if CFG.speak_mode and CFG.debug_mode: + say_text("Apparently json was fixed.") + else: + return {} + + except (json.JSONDecodeError, ValueError): + if CFG.debug_mode: + logger.error(f"Error: Invalid JSON: {json_string}\n") + if CFG.speak_mode: + say_text("Didn't work. I will have to ignore this response then.") + logger.error("Error: Invalid JSON, setting it to empty JSON now.\n") + json_string = {} + + return fix_and_parse_json(json_string) diff --git a/autogpt/json_utils/llm_response_format_1.json b/autogpt/json_utils/llm_response_format_1.json new file mode 100644 index 0000000000000000000000000000000000000000..9aa33352511d116ddfbeb977607fa98cca65db45 --- /dev/null +++ b/autogpt/json_utils/llm_response_format_1.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "thoughts": { + "type": "object", + "properties": { + "text": {"type": "string"}, + "reasoning": {"type": "string"}, + "plan": {"type": "string"}, + "criticism": {"type": "string"}, + "speak": {"type": "string"} + }, + "required": ["text", "reasoning", "plan", "criticism", "speak"], + "additionalProperties": false + }, + "command": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "args": { + "type": "object" + } + }, + "required": ["name", "args"], + "additionalProperties": false + } + }, + "required": ["thoughts", "command"], + "additionalProperties": false +} diff --git a/autogpt/json_utils/utilities.py b/autogpt/json_utils/utilities.py new file mode 100644 index 0000000000000000000000000000000000000000..eb9bb687750460fed2f4547b67e41f8e8c877a41 --- /dev/null +++ b/autogpt/json_utils/utilities.py @@ -0,0 +1,54 @@ +"""Utilities for the json_fixes package.""" +import json +import re + +from jsonschema import Draft7Validator + +from autogpt.config import Config +from autogpt.logs import logger + +CFG = Config() + + +def extract_char_position(error_message: str) -> int: + """Extract the character position from the JSONDecodeError message. + + Args: + error_message (str): The error message from the JSONDecodeError + exception. + + Returns: + int: The character position. + """ + + char_pattern = re.compile(r"\(char (\d+)\)") + if match := char_pattern.search(error_message): + return int(match[1]) + else: + raise ValueError("Character position not found in the error message.") + + +def validate_json(json_object: object, schema_name: object) -> object: + """ + :type schema_name: object + :param schema_name: + :type json_object: object + """ + with open(f"autogpt/json_utils/{schema_name}.json", "r") as f: + schema = json.load(f) + validator = Draft7Validator(schema) + + if errors := sorted(validator.iter_errors(json_object), key=lambda e: e.path): + logger.error("The JSON object is invalid.") + if CFG.debug_mode: + logger.error( + json.dumps(json_object, indent=4) + ) # Replace 'json_object' with the variable containing the JSON data + logger.error("The following issues were found:") + + for error in errors: + logger.error(f"Error: {error.message}") + elif CFG.debug_mode: + print("The JSON object is valid.") + + return json_object diff --git a/autogpt/llm_utils.py b/autogpt/llm_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..821820ffab07be2753cf385ff1de77820e4206ee --- /dev/null +++ b/autogpt/llm_utils.py @@ -0,0 +1,172 @@ +from __future__ import annotations + +import time +from ast import List + +import openai +from colorama import Fore, Style +from openai.error import APIError, RateLimitError + +from autogpt.config import Config +from autogpt.logs import logger + +CFG = Config() + +openai.api_key = CFG.openai_api_key + + +def call_ai_function( + function: str, args: list, description: str, model: str | None = None +) -> str: + """Call an AI function + + This is a magic function that can do anything with no-code. See + https://github.com/Torantulino/AI-Functions for more info. + + Args: + function (str): The function to call + args (list): The arguments to pass to the function + description (str): The description of the function + model (str, optional): The model to use. Defaults to None. + + Returns: + str: The response from the function + """ + if model is None: + model = CFG.smart_llm_model + # For each arg, if any are None, convert to "None": + args = [str(arg) if arg is not None else "None" for arg in args] + # parse args to comma separated string + args = ", ".join(args) + messages = [ + { + "role": "system", + "content": f"You are now the following python function: ```# {description}" + f"\n{function}```\n\nOnly respond with your `return` value.", + }, + {"role": "user", "content": args}, + ] + + return create_chat_completion(model=model, messages=messages, temperature=0) + + +# Overly simple abstraction until we create something better +# simple retry mechanism when getting a rate error or a bad gateway +def create_chat_completion( + messages: list, # type: ignore + model: str | None = None, + temperature: float = CFG.temperature, + max_tokens: int | None = None, +) -> str: + """Create a chat completion using the OpenAI API + + Args: + messages (list[dict[str, str]]): The messages to send to the chat completion + model (str, optional): The model to use. Defaults to None. + temperature (float, optional): The temperature to use. Defaults to 0.9. + max_tokens (int, optional): The max tokens to use. Defaults to None. + + Returns: + str: The response from the chat completion + """ + response = None + num_retries = 10 + warned_user = False + if CFG.debug_mode: + print( + Fore.GREEN + + f"Creating chat completion with model {model}, temperature {temperature}," + f" max_tokens {max_tokens}" + Fore.RESET + ) + for attempt in range(num_retries): + backoff = 2 ** (attempt + 2) + try: + if CFG.use_azure: + response = openai.ChatCompletion.create( + deployment_id=CFG.get_azure_deployment_id_for_model(model), + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens, + ) + else: + response = openai.ChatCompletion.create( + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens, + ) + break + except RateLimitError: + if CFG.debug_mode: + print( + Fore.RED + "Error: ", + f"Reached rate limit, passing..." + Fore.RESET, + ) + if not warned_user: + logger.double_check( + f"Please double check that you have setup a {Fore.CYAN + Style.BRIGHT}PAID{Style.RESET_ALL} OpenAI API Account. " + + f"You can read more here: {Fore.CYAN}https://github.com/Significant-Gravitas/Auto-GPT#openai-api-keys-configuration{Fore.RESET}" + ) + warned_user = True + except APIError as e: + if e.http_status == 502: + pass + else: + raise + if attempt == num_retries - 1: + raise + if CFG.debug_mode: + print( + Fore.RED + "Error: ", + f"API Bad gateway. Waiting {backoff} seconds..." + Fore.RESET, + ) + time.sleep(backoff) + if response is None: + logger.typewriter_log( + "FAILED TO GET RESPONSE FROM OPENAI", + Fore.RED, + "Auto-GPT has failed to get a response from OpenAI's services. " + + f"Try running Auto-GPT again, and if the problem the persists try running it with `{Fore.CYAN}--debug{Fore.RESET}`.", + ) + logger.double_check() + if CFG.debug_mode: + raise RuntimeError(f"Failed to get response after {num_retries} retries") + else: + quit(1) + + return response.choices[0].message["content"] + + +def create_embedding_with_ada(text) -> list: + """Create an embedding with text-ada-002 using the OpenAI SDK""" + num_retries = 10 + for attempt in range(num_retries): + backoff = 2 ** (attempt + 2) + try: + if CFG.use_azure: + return openai.Embedding.create( + input=[text], + engine=CFG.get_azure_deployment_id_for_model( + "text-embedding-ada-002" + ), + )["data"][0]["embedding"] + else: + return openai.Embedding.create( + input=[text], model="text-embedding-ada-002" + )["data"][0]["embedding"] + except RateLimitError: + pass + except APIError as e: + if e.http_status == 502: + pass + else: + raise + if attempt == num_retries - 1: + raise + if CFG.debug_mode: + print( + Fore.RED + "Error: ", + f"API Bad gateway. Waiting {backoff} seconds..." + Fore.RESET, + ) + time.sleep(backoff) diff --git a/autogpt/logs.py b/autogpt/logs.py new file mode 100644 index 0000000000000000000000000000000000000000..35037404a98f7be9b7d577b625cc190ca27f4566 --- /dev/null +++ b/autogpt/logs.py @@ -0,0 +1,332 @@ +"""Logging module for Auto-GPT.""" +import json +import logging +import os +import random +import re +import time +import traceback +from logging import LogRecord + +from colorama import Fore, Style + +from autogpt.config import Config, Singleton +from autogpt.speech import say_text + +CFG = Config() + + +class Logger(metaclass=Singleton): + """ + Logger that handle titles in different colors. + Outputs logs in console, activity.log, and errors.log + For console handler: simulates typing + """ + + def __init__(self): + # create log directory if it doesn't exist + this_files_dir_path = os.path.dirname(__file__) + log_dir = os.path.join(this_files_dir_path, "../logs") + if not os.path.exists(log_dir): + os.makedirs(log_dir) + + log_file = "activity.log" + error_file = "error.log" + + console_formatter = AutoGptFormatter("%(title_color)s %(message)s") + + # Create a handler for console which simulate typing + self.typing_console_handler = TypingConsoleHandler() + self.typing_console_handler.setLevel(logging.INFO) + self.typing_console_handler.setFormatter(console_formatter) + + # Create a handler for console without typing simulation + self.console_handler = ConsoleHandler() + self.console_handler.setLevel(logging.DEBUG) + self.console_handler.setFormatter(console_formatter) + + # Info handler in activity.log + self.file_handler = logging.FileHandler( + os.path.join(log_dir, log_file), "a", "utf-8" + ) + self.file_handler.setLevel(logging.DEBUG) + info_formatter = AutoGptFormatter( + "%(asctime)s %(levelname)s %(title)s %(message_no_color)s" + ) + self.file_handler.setFormatter(info_formatter) + + # Error handler error.log + error_handler = logging.FileHandler( + os.path.join(log_dir, error_file), "a", "utf-8" + ) + error_handler.setLevel(logging.ERROR) + error_formatter = AutoGptFormatter( + "%(asctime)s %(levelname)s %(module)s:%(funcName)s:%(lineno)d %(title)s" + " %(message_no_color)s" + ) + error_handler.setFormatter(error_formatter) + + self.typing_logger = logging.getLogger("TYPER") + self.typing_logger.addHandler(self.typing_console_handler) + self.typing_logger.addHandler(self.file_handler) + self.typing_logger.addHandler(error_handler) + self.typing_logger.setLevel(logging.DEBUG) + + self.logger = logging.getLogger("LOGGER") + self.logger.addHandler(self.console_handler) + self.logger.addHandler(self.file_handler) + self.logger.addHandler(error_handler) + self.logger.setLevel(logging.DEBUG) + + def typewriter_log( + self, title="", title_color="", content="", speak_text=False, level=logging.INFO + ): + if speak_text and CFG.speak_mode: + say_text(f"{title}. {content}") + + if content: + if isinstance(content, list): + content = " ".join(content) + else: + content = "" + + self.typing_logger.log( + level, content, extra={"title": title, "color": title_color} + ) + + def debug( + self, + message, + title="", + title_color="", + ): + self._log(title, title_color, message, logging.DEBUG) + + def warn( + self, + message, + title="", + title_color="", + ): + self._log(title, title_color, message, logging.WARN) + + def error(self, title, message=""): + self._log(title, Fore.RED, message, logging.ERROR) + + def _log(self, title="", title_color="", message="", level=logging.INFO): + if message: + if isinstance(message, list): + message = " ".join(message) + self.logger.log(level, message, extra={"title": title, "color": title_color}) + + def set_level(self, level): + self.logger.setLevel(level) + self.typing_logger.setLevel(level) + + def double_check(self, additionalText=None): + if not additionalText: + additionalText = ( + "Please ensure you've setup and configured everything" + " correctly. Read https://github.com/Torantulino/Auto-GPT#readme to " + "double check. You can also create a github issue or join the discord" + " and ask there!" + ) + + self.typewriter_log("DOUBLE CHECK CONFIGURATION", Fore.YELLOW, additionalText) + + +""" +Output stream to console using simulated typing +""" + + +class TypingConsoleHandler(logging.StreamHandler): + def emit(self, record): + min_typing_speed = 0.05 + max_typing_speed = 0.01 + + msg = self.format(record) + try: + words = msg.split() + for i, word in enumerate(words): + print(word, end="", flush=True) + if i < len(words) - 1: + print(" ", end="", flush=True) + typing_speed = random.uniform(min_typing_speed, max_typing_speed) + time.sleep(typing_speed) + # type faster after each word + min_typing_speed = min_typing_speed * 0.95 + max_typing_speed = max_typing_speed * 0.95 + print() + except Exception: + self.handleError(record) + + +class ConsoleHandler(logging.StreamHandler): + def emit(self, record) -> None: + msg = self.format(record) + try: + print(msg) + except Exception: + self.handleError(record) + + +class AutoGptFormatter(logging.Formatter): + """ + Allows to handle custom placeholders 'title_color' and 'message_no_color'. + To use this formatter, make sure to pass 'color', 'title' as log extras. + """ + + def format(self, record: LogRecord) -> str: + if hasattr(record, "color"): + record.title_color = ( + getattr(record, "color") + + getattr(record, "title") + + " " + + Style.RESET_ALL + ) + else: + record.title_color = getattr(record, "title") + if hasattr(record, "msg"): + record.message_no_color = remove_color_codes(getattr(record, "msg")) + else: + record.message_no_color = "" + return super().format(record) + + +def remove_color_codes(s: str) -> str: + ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") + return ansi_escape.sub("", s) + + +logger = Logger() + + +def print_assistant_thoughts(ai_name, assistant_reply): + """Prints the assistant's thoughts to the console""" + from autogpt.json_utils.json_fix_llm import ( + attempt_to_fix_json_by_finding_outermost_brackets, + fix_and_parse_json, + ) + + try: + try: + # Parse and print Assistant response + assistant_reply_json = fix_and_parse_json(assistant_reply) + except json.JSONDecodeError: + logger.error("Error: Invalid JSON in assistant thoughts\n", assistant_reply) + assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets( + assistant_reply + ) + if isinstance(assistant_reply_json, str): + assistant_reply_json = fix_and_parse_json(assistant_reply_json) + + # Check if assistant_reply_json is a string and attempt to parse + # it into a JSON object + if isinstance(assistant_reply_json, str): + try: + assistant_reply_json = json.loads(assistant_reply_json) + except json.JSONDecodeError: + logger.error("Error: Invalid JSON\n", assistant_reply) + assistant_reply_json = ( + attempt_to_fix_json_by_finding_outermost_brackets( + assistant_reply_json + ) + ) + + assistant_thoughts_reasoning = None + assistant_thoughts_plan = None + assistant_thoughts_speak = None + assistant_thoughts_criticism = None + if not isinstance(assistant_reply_json, dict): + assistant_reply_json = {} + assistant_thoughts = assistant_reply_json.get("thoughts", {}) + assistant_thoughts_text = assistant_thoughts.get("text") + + if assistant_thoughts: + assistant_thoughts_reasoning = assistant_thoughts.get("reasoning") + assistant_thoughts_plan = assistant_thoughts.get("plan") + assistant_thoughts_criticism = assistant_thoughts.get("criticism") + assistant_thoughts_speak = assistant_thoughts.get("speak") + + logger.typewriter_log( + f"{ai_name.upper()} THOUGHTS:", Fore.YELLOW, f"{assistant_thoughts_text}" + ) + logger.typewriter_log( + "REASONING:", Fore.YELLOW, f"{assistant_thoughts_reasoning}" + ) + + if assistant_thoughts_plan: + logger.typewriter_log("PLAN:", Fore.YELLOW, "") + # If it's a list, join it into a string + if isinstance(assistant_thoughts_plan, list): + assistant_thoughts_plan = "\n".join(assistant_thoughts_plan) + elif isinstance(assistant_thoughts_plan, dict): + assistant_thoughts_plan = str(assistant_thoughts_plan) + + # Split the input_string using the newline character and dashes + lines = assistant_thoughts_plan.split("\n") + for line in lines: + line = line.lstrip("- ") + logger.typewriter_log("- ", Fore.GREEN, line.strip()) + + logger.typewriter_log( + "CRITICISM:", Fore.YELLOW, f"{assistant_thoughts_criticism}" + ) + # Speak the assistant's thoughts + if CFG.speak_mode and assistant_thoughts_speak: + say_text(assistant_thoughts_speak) + else: + logger.typewriter_log("SPEAK:", Fore.YELLOW, f"{assistant_thoughts_speak}") + + return assistant_reply_json + except json.decoder.JSONDecodeError: + logger.error("Error: Invalid JSON\n", assistant_reply) + if CFG.speak_mode: + say_text( + "I have received an invalid JSON response from the OpenAI API." + " I cannot ignore this response." + ) + + # All other errors, return "Error: + error message" + except Exception: + call_stack = traceback.format_exc() + logger.error("Error: \n", call_stack) + + +def print_assistant_thoughts( + ai_name: object, assistant_reply_json_valid: object +) -> None: + assistant_thoughts_reasoning = None + assistant_thoughts_plan = None + assistant_thoughts_speak = None + assistant_thoughts_criticism = None + + assistant_thoughts = assistant_reply_json_valid.get("thoughts", {}) + assistant_thoughts_text = assistant_thoughts.get("text") + if assistant_thoughts: + assistant_thoughts_reasoning = assistant_thoughts.get("reasoning") + assistant_thoughts_plan = assistant_thoughts.get("plan") + assistant_thoughts_criticism = assistant_thoughts.get("criticism") + assistant_thoughts_speak = assistant_thoughts.get("speak") + logger.typewriter_log( + f"{ai_name.upper()} THOUGHTS:", Fore.YELLOW, f"{assistant_thoughts_text}" + ) + logger.typewriter_log("REASONING:", Fore.YELLOW, f"{assistant_thoughts_reasoning}") + if assistant_thoughts_plan: + logger.typewriter_log("PLAN:", Fore.YELLOW, "") + # If it's a list, join it into a string + if isinstance(assistant_thoughts_plan, list): + assistant_thoughts_plan = "\n".join(assistant_thoughts_plan) + elif isinstance(assistant_thoughts_plan, dict): + assistant_thoughts_plan = str(assistant_thoughts_plan) + + # Split the input_string using the newline character and dashes + lines = assistant_thoughts_plan.split("\n") + for line in lines: + line = line.lstrip("- ") + logger.typewriter_log("- ", Fore.GREEN, line.strip()) + logger.typewriter_log("CRITICISM:", Fore.YELLOW, f"{assistant_thoughts_criticism}") + # Speak the assistant's thoughts + if CFG.speak_mode and assistant_thoughts_speak: + say_text(assistant_thoughts_speak) diff --git a/autogpt/memory/__init__.py b/autogpt/memory/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..3d18704c70dfc287642b1923e6f2e1f72a5f2a62 --- /dev/null +++ b/autogpt/memory/__init__.py @@ -0,0 +1,99 @@ +from autogpt.memory.local import LocalCache +from autogpt.memory.no_memory import NoMemory + +# List of supported memory backends +# Add a backend to this list if the import attempt is successful +supported_memory = ["local", "no_memory"] + +try: + from autogpt.memory.redismem import RedisMemory + + supported_memory.append("redis") +except ImportError: + # print("Redis not installed. Skipping import.") + RedisMemory = None + +try: + from autogpt.memory.pinecone import PineconeMemory + + supported_memory.append("pinecone") +except ImportError: + # print("Pinecone not installed. Skipping import.") + PineconeMemory = None + +try: + from autogpt.memory.weaviate import WeaviateMemory + + supported_memory.append("weaviate") +except ImportError: + # print("Weaviate not installed. Skipping import.") + WeaviateMemory = None + +try: + from autogpt.memory.milvus import MilvusMemory + + supported_memory.append("milvus") +except ImportError: + # print("pymilvus not installed. Skipping import.") + MilvusMemory = None + + +def get_memory(cfg, init=False): + memory = None + if cfg.memory_backend == "pinecone": + if not PineconeMemory: + print( + "Error: Pinecone is not installed. Please install pinecone" + " to use Pinecone as a memory backend." + ) + else: + memory = PineconeMemory(cfg) + if init: + memory.clear() + elif cfg.memory_backend == "redis": + if not RedisMemory: + print( + "Error: Redis is not installed. Please install redis-py to" + " use Redis as a memory backend." + ) + else: + memory = RedisMemory(cfg) + elif cfg.memory_backend == "weaviate": + if not WeaviateMemory: + print( + "Error: Weaviate is not installed. Please install weaviate-client to" + " use Weaviate as a memory backend." + ) + else: + memory = WeaviateMemory(cfg) + elif cfg.memory_backend == "milvus": + if not MilvusMemory: + print( + "Error: Milvus sdk is not installed." + "Please install pymilvus to use Milvus as memory backend." + ) + else: + memory = MilvusMemory(cfg) + elif cfg.memory_backend == "no_memory": + memory = NoMemory(cfg) + + if memory is None: + memory = LocalCache(cfg) + if init: + memory.clear() + return memory + + +def get_supported_memory_backends(): + return supported_memory + + +__all__ = [ + "get_memory", + "LocalCache", + "RedisMemory", + "PineconeMemory", + "NoMemory", + "MilvusMemory", + "WeaviateMemory", +] diff --git a/autogpt/memory/base.py b/autogpt/memory/base.py new file mode 100644 index 0000000000000000000000000000000000000000..691e2299c4caa5c2e9af5b2436727834f3cc6c67 --- /dev/null +++ b/autogpt/memory/base.py @@ -0,0 +1,43 @@ +"""Base class for memory providers.""" +import abc + +import openai + +from autogpt.config import AbstractSingleton, Config + +cfg = Config() + + +def get_ada_embedding(text): + text = text.replace("\n", " ") + if cfg.use_azure: + return openai.Embedding.create( + input=[text], + engine=cfg.get_azure_deployment_id_for_model("text-embedding-ada-002"), + )["data"][0]["embedding"] + else: + return openai.Embedding.create(input=[text], model="text-embedding-ada-002")[ + "data" + ][0]["embedding"] + + +class MemoryProviderSingleton(AbstractSingleton): + @abc.abstractmethod + def add(self, data): + pass + + @abc.abstractmethod + def get(self, data): + pass + + @abc.abstractmethod + def clear(self): + pass + + @abc.abstractmethod + def get_relevant(self, data, num_relevant=5): + pass + + @abc.abstractmethod + def get_stats(self): + pass diff --git a/autogpt/memory/local.py b/autogpt/memory/local.py new file mode 100644 index 0000000000000000000000000000000000000000..803b6dc6ebb430285f423cda592fa3e902e9a4a6 --- /dev/null +++ b/autogpt/memory/local.py @@ -0,0 +1,136 @@ +from __future__ import annotations + +import dataclasses +import os +from typing import Any, List + +import numpy as np +import orjson + +from autogpt.llm_utils import create_embedding_with_ada +from autogpt.memory.base import MemoryProviderSingleton + +EMBED_DIM = 1536 +SAVE_OPTIONS = orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_SERIALIZE_DATACLASS + + +def create_default_embeddings(): + return np.zeros((0, EMBED_DIM)).astype(np.float32) + + +@dataclasses.dataclass +class CacheContent: + texts: List[str] = dataclasses.field(default_factory=list) + embeddings: np.ndarray = dataclasses.field( + default_factory=create_default_embeddings + ) + + +class LocalCache(MemoryProviderSingleton): + """A class that stores the memory in a local file""" + + def __init__(self, cfg) -> None: + """Initialize a class instance + + Args: + cfg: Config object + + Returns: + None + """ + self.filename = f"{cfg.memory_index}.json" + if os.path.exists(self.filename): + try: + with open(self.filename, "w+b") as f: + file_content = f.read() + if not file_content.strip(): + file_content = b"{}" + f.write(file_content) + + loaded = orjson.loads(file_content) + self.data = CacheContent(**loaded) + except orjson.JSONDecodeError: + print(f"Error: The file '{self.filename}' is not in JSON format.") + self.data = CacheContent() + else: + print( + f"Warning: The file '{self.filename}' does not exist. " + "Local memory would not be saved to a file." + ) + self.data = CacheContent() + + def add(self, text: str): + """ + Add text to our list of texts, add embedding as row to our + embeddings-matrix + + Args: + text: str + + Returns: None + """ + if "Command Error:" in text: + return "" + self.data.texts.append(text) + + embedding = create_embedding_with_ada(text) + + vector = np.array(embedding).astype(np.float32) + vector = vector[np.newaxis, :] + self.data.embeddings = np.concatenate( + [ + self.data.embeddings, + vector, + ], + axis=0, + ) + + with open(self.filename, "wb") as f: + out = orjson.dumps(self.data, option=SAVE_OPTIONS) + f.write(out) + return text + + def clear(self) -> str: + """ + Clears the redis server. + + Returns: A message indicating that the memory has been cleared. + """ + self.data = CacheContent() + return "Obliviated" + + def get(self, data: str) -> list[Any] | None: + """ + Gets the data from the memory that is most relevant to the given data. + + Args: + data: The data to compare to. + + Returns: The most relevant data. + """ + return self.get_relevant(data, 1) + + def get_relevant(self, text: str, k: int) -> list[Any]: + """ " + matrix-vector mult to find score-for-each-row-of-matrix + get indices for top-k winning scores + return texts for those indices + Args: + text: str + k: int + + Returns: List[str] + """ + embedding = create_embedding_with_ada(text) + + scores = np.dot(self.data.embeddings, embedding) + + top_k_indices = np.argsort(scores)[-k:][::-1] + + return [self.data.texts[i] for i in top_k_indices] + + def get_stats(self) -> tuple[int, tuple[int, ...]]: + """ + Returns: The stats of the local cache. + """ + return len(self.data.texts), self.data.embeddings.shape diff --git a/autogpt/memory/milvus.py b/autogpt/memory/milvus.py new file mode 100644 index 0000000000000000000000000000000000000000..44aa72b956224fa4c2a16d5f40b0eaeb35e98581 --- /dev/null +++ b/autogpt/memory/milvus.py @@ -0,0 +1,115 @@ +""" Milvus memory storage provider.""" +from pymilvus import Collection, CollectionSchema, DataType, FieldSchema, connections + +from autogpt.memory.base import MemoryProviderSingleton, get_ada_embedding + + +class MilvusMemory(MemoryProviderSingleton): + """Milvus memory storage provider.""" + + def __init__(self, cfg) -> None: + """Construct a milvus memory storage connection. + + Args: + cfg (Config): Auto-GPT global config. + """ + # connect to milvus server. + connections.connect(address=cfg.milvus_addr) + fields = [ + FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=True), + FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=1536), + FieldSchema(name="raw_text", dtype=DataType.VARCHAR, max_length=65535), + ] + + # create collection if not exist and load it. + self.milvus_collection = cfg.milvus_collection + self.schema = CollectionSchema(fields, "auto-gpt memory storage") + self.collection = Collection(self.milvus_collection, self.schema) + # create index if not exist. + if not self.collection.has_index(): + self.collection.release() + self.collection.create_index( + "embeddings", + { + "metric_type": "IP", + "index_type": "HNSW", + "params": {"M": 8, "efConstruction": 64}, + }, + index_name="embeddings", + ) + self.collection.load() + + def add(self, data) -> str: + """Add an embedding of data into memory. + + Args: + data (str): The raw text to construct embedding index. + + Returns: + str: log. + """ + embedding = get_ada_embedding(data) + result = self.collection.insert([[embedding], [data]]) + _text = ( + "Inserting data into memory at primary key: " + f"{result.primary_keys[0]}:\n data: {data}" + ) + return _text + + def get(self, data): + """Return the most relevant data in memory. + Args: + data: The data to compare to. + """ + return self.get_relevant(data, 1) + + def clear(self) -> str: + """Drop the index in memory. + + Returns: + str: log. + """ + self.collection.drop() + self.collection = Collection(self.milvus_collection, self.schema) + self.collection.create_index( + "embeddings", + { + "metric_type": "IP", + "index_type": "HNSW", + "params": {"M": 8, "efConstruction": 64}, + }, + index_name="embeddings", + ) + self.collection.load() + return "Obliviated" + + def get_relevant(self, data: str, num_relevant: int = 5): + """Return the top-k relevant data in memory. + Args: + data: The data to compare to. + num_relevant (int, optional): The max number of relevant data. + Defaults to 5. + + Returns: + list: The top-k relevant data. + """ + # search the embedding and return the most relevant text. + embedding = get_ada_embedding(data) + search_params = { + "metrics_type": "IP", + "params": {"nprobe": 8}, + } + result = self.collection.search( + [embedding], + "embeddings", + search_params, + num_relevant, + output_fields=["raw_text"], + ) + return [item.entity.value_of_field("raw_text") for item in result[0]] + + def get_stats(self) -> str: + """ + Returns: The stats of the milvus cache. + """ + return f"Entities num: {self.collection.num_entities}" diff --git a/autogpt/memory/no_memory.py b/autogpt/memory/no_memory.py new file mode 100644 index 0000000000000000000000000000000000000000..0371e96ae89f5eb88dae019a66351a229596ed7a --- /dev/null +++ b/autogpt/memory/no_memory.py @@ -0,0 +1,73 @@ +"""A class that does not store any data. This is the default memory provider.""" +from __future__ import annotations + +from typing import Any + +from autogpt.memory.base import MemoryProviderSingleton + + +class NoMemory(MemoryProviderSingleton): + """ + A class that does not store any data. This is the default memory provider. + """ + + def __init__(self, cfg): + """ + Initializes the NoMemory provider. + + Args: + cfg: The config object. + + Returns: None + """ + pass + + def add(self, data: str) -> str: + """ + Adds a data point to the memory. No action is taken in NoMemory. + + Args: + data: The data to add. + + Returns: An empty string. + """ + return "" + + def get(self, data: str) -> list[Any] | None: + """ + Gets the data from the memory that is most relevant to the given data. + NoMemory always returns None. + + Args: + data: The data to compare to. + + Returns: None + """ + return None + + def clear(self) -> str: + """ + Clears the memory. No action is taken in NoMemory. + + Returns: An empty string. + """ + return "" + + def get_relevant(self, data: str, num_relevant: int = 5) -> list[Any] | None: + """ + Returns all the data in the memory that is relevant to the given data. + NoMemory always returns None. + + Args: + data: The data to compare to. + num_relevant: The number of relevant data to return. + + Returns: None + """ + return None + + def get_stats(self): + """ + Returns: An empty dictionary as there are no stats in NoMemory. + """ + return {} diff --git a/autogpt/memory/pinecone.py b/autogpt/memory/pinecone.py new file mode 100644 index 0000000000000000000000000000000000000000..27fcd62482d0cf44e02fa1c339195be58cb745b0 --- /dev/null +++ b/autogpt/memory/pinecone.py @@ -0,0 +1,75 @@ +import pinecone +from colorama import Fore, Style + +from autogpt.llm_utils import create_embedding_with_ada +from autogpt.logs import logger +from autogpt.memory.base import MemoryProviderSingleton + + +class PineconeMemory(MemoryProviderSingleton): + def __init__(self, cfg): + pinecone_api_key = cfg.pinecone_api_key + pinecone_region = cfg.pinecone_region + pinecone.init(api_key=pinecone_api_key, environment=pinecone_region) + dimension = 1536 + metric = "cosine" + pod_type = "p1" + table_name = "auto-gpt" + # this assumes we don't start with memory. + # for now this works. + # we'll need a more complicated and robust system if we want to start with + # memory. + self.vec_num = 0 + + try: + pinecone.whoami() + except Exception as e: + logger.typewriter_log( + "FAILED TO CONNECT TO PINECONE", + Fore.RED, + Style.BRIGHT + str(e) + Style.RESET_ALL, + ) + logger.double_check( + "Please ensure you have setup and configured Pinecone properly for use." + + f"You can check out {Fore.CYAN + Style.BRIGHT}" + "https://github.com/Torantulino/Auto-GPT#-pinecone-api-key-setup" + f"{Style.RESET_ALL} to ensure you've set up everything correctly." + ) + exit(1) + + if table_name not in pinecone.list_indexes(): + pinecone.create_index( + table_name, dimension=dimension, metric=metric, pod_type=pod_type + ) + self.index = pinecone.Index(table_name) + + def add(self, data): + vector = create_embedding_with_ada(data) + # no metadata here. We may wish to change that long term. + self.index.upsert([(str(self.vec_num), vector, {"raw_text": data})]) + _text = f"Inserting data into memory at index: {self.vec_num}:\n data: {data}" + self.vec_num += 1 + return _text + + def get(self, data): + return self.get_relevant(data, 1) + + def clear(self): + self.index.delete(deleteAll=True) + return "Obliviated" + + def get_relevant(self, data, num_relevant=5): + """ + Returns all the data in the memory that is relevant to the given data. + :param data: The data to compare to. + :param num_relevant: The number of relevant data to return. Defaults to 5 + """ + query_embedding = create_embedding_with_ada(data) + results = self.index.query( + query_embedding, top_k=num_relevant, include_metadata=True + ) + sorted_results = sorted(results.matches, key=lambda x: x.score) + return [str(item["metadata"]["raw_text"]) for item in sorted_results] + + def get_stats(self): + return self.index.describe_index_stats() diff --git a/autogpt/memory/redismem.py b/autogpt/memory/redismem.py new file mode 100644 index 0000000000000000000000000000000000000000..082a812c5362cc9f19e35bf1bb10269b558f7724 --- /dev/null +++ b/autogpt/memory/redismem.py @@ -0,0 +1,156 @@ +"""Redis memory provider.""" +from __future__ import annotations + +from typing import Any + +import numpy as np +import redis +from colorama import Fore, Style +from redis.commands.search.field import TextField, VectorField +from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.query import Query + +from autogpt.llm_utils import create_embedding_with_ada +from autogpt.logs import logger +from autogpt.memory.base import MemoryProviderSingleton + +SCHEMA = [ + TextField("data"), + VectorField( + "embedding", + "HNSW", + {"TYPE": "FLOAT32", "DIM": 1536, "DISTANCE_METRIC": "COSINE"}, + ), +] + + +class RedisMemory(MemoryProviderSingleton): + def __init__(self, cfg): + """ + Initializes the Redis memory provider. + + Args: + cfg: The config object. + + Returns: None + """ + redis_host = cfg.redis_host + redis_port = cfg.redis_port + redis_password = cfg.redis_password + self.dimension = 1536 + self.redis = redis.Redis( + host=redis_host, + port=redis_port, + password=redis_password, + db=0, # Cannot be changed + ) + self.cfg = cfg + + # Check redis connection + try: + self.redis.ping() + except redis.ConnectionError as e: + logger.typewriter_log( + "FAILED TO CONNECT TO REDIS", + Fore.RED, + Style.BRIGHT + str(e) + Style.RESET_ALL, + ) + logger.double_check( + "Please ensure you have setup and configured Redis properly for use. " + + f"You can check out {Fore.CYAN + Style.BRIGHT}" + f"https://github.com/Torantulino/Auto-GPT#redis-setup{Style.RESET_ALL}" + " to ensure you've set up everything correctly." + ) + exit(1) + + if cfg.wipe_redis_on_start: + self.redis.flushall() + try: + self.redis.ft(f"{cfg.memory_index}").create_index( + fields=SCHEMA, + definition=IndexDefinition( + prefix=[f"{cfg.memory_index}:"], index_type=IndexType.HASH + ), + ) + except Exception as e: + print("Error creating Redis search index: ", e) + existing_vec_num = self.redis.get(f"{cfg.memory_index}-vec_num") + self.vec_num = int(existing_vec_num.decode("utf-8")) if existing_vec_num else 0 + + def add(self, data: str) -> str: + """ + Adds a data point to the memory. + + Args: + data: The data to add. + + Returns: Message indicating that the data has been added. + """ + if "Command Error:" in data: + return "" + vector = create_embedding_with_ada(data) + vector = np.array(vector).astype(np.float32).tobytes() + data_dict = {b"data": data, "embedding": vector} + pipe = self.redis.pipeline() + pipe.hset(f"{self.cfg.memory_index}:{self.vec_num}", mapping=data_dict) + _text = ( + f"Inserting data into memory at index: {self.vec_num}:\n" f"data: {data}" + ) + self.vec_num += 1 + pipe.set(f"{self.cfg.memory_index}-vec_num", self.vec_num) + pipe.execute() + return _text + + def get(self, data: str) -> list[Any] | None: + """ + Gets the data from the memory that is most relevant to the given data. + + Args: + data: The data to compare to. + + Returns: The most relevant data. + """ + return self.get_relevant(data, 1) + + def clear(self) -> str: + """ + Clears the redis server. + + Returns: A message indicating that the memory has been cleared. + """ + self.redis.flushall() + return "Obliviated" + + def get_relevant(self, data: str, num_relevant: int = 5) -> list[Any] | None: + """ + Returns all the data in the memory that is relevant to the given data. + Args: + data: The data to compare to. + num_relevant: The number of relevant data to return. + + Returns: A list of the most relevant data. + """ + query_embedding = create_embedding_with_ada(data) + base_query = f"*=>[KNN {num_relevant} @embedding $vector AS vector_score]" + query = ( + Query(base_query) + .return_fields("data", "vector_score") + .sort_by("vector_score") + .dialect(2) + ) + query_vector = np.array(query_embedding).astype(np.float32).tobytes() + + try: + results = self.redis.ft(f"{self.cfg.memory_index}").search( + query, query_params={"vector": query_vector} + ) + except Exception as e: + print("Error calling Redis search: ", e) + return None + return [result.data for result in results.docs] + + def get_stats(self): + """ + Returns: The stats of the memory index. + """ + return self.redis.ft(f"{self.cfg.memory_index}").info() diff --git a/autogpt/memory/weaviate.py b/autogpt/memory/weaviate.py new file mode 100644 index 0000000000000000000000000000000000000000..5408e9a97aa3594ad443448cfc31f2546a01eb09 --- /dev/null +++ b/autogpt/memory/weaviate.py @@ -0,0 +1,127 @@ +import uuid + +import weaviate +from weaviate import Client +from weaviate.embedded import EmbeddedOptions +from weaviate.util import generate_uuid5 + +from autogpt.config import Config +from autogpt.memory.base import MemoryProviderSingleton, get_ada_embedding + + +def default_schema(weaviate_index): + return { + "class": weaviate_index, + "properties": [ + { + "name": "raw_text", + "dataType": ["text"], + "description": "original text for the embedding", + } + ], + } + + +class WeaviateMemory(MemoryProviderSingleton): + def __init__(self, cfg): + auth_credentials = self._build_auth_credentials(cfg) + + url = f"{cfg.weaviate_protocol}://{cfg.weaviate_host}:{cfg.weaviate_port}" + + if cfg.use_weaviate_embedded: + self.client = Client( + embedded_options=EmbeddedOptions( + hostname=cfg.weaviate_host, + port=int(cfg.weaviate_port), + persistence_data_path=cfg.weaviate_embedded_path, + ) + ) + + print( + f"Weaviate Embedded running on: {url} with persistence path: {cfg.weaviate_embedded_path}" + ) + else: + self.client = Client(url, auth_client_secret=auth_credentials) + + self.index = WeaviateMemory.format_classname(cfg.memory_index) + self._create_schema() + + @staticmethod + def format_classname(index): + # weaviate uses capitalised index names + # The python client uses the following code to format + # index names before the corresponding class is created + if len(index) == 1: + return index.capitalize() + return index[0].capitalize() + index[1:] + + def _create_schema(self): + schema = default_schema(self.index) + if not self.client.schema.contains(schema): + self.client.schema.create_class(schema) + + def _build_auth_credentials(self, cfg): + if cfg.weaviate_username and cfg.weaviate_password: + return weaviate.AuthClientPassword( + cfg.weaviate_username, cfg.weaviate_password + ) + if cfg.weaviate_api_key: + return weaviate.AuthApiKey(api_key=cfg.weaviate_api_key) + else: + return None + + def add(self, data): + vector = get_ada_embedding(data) + + doc_uuid = generate_uuid5(data, self.index) + data_object = {"raw_text": data} + + with self.client.batch as batch: + batch.add_data_object( + uuid=doc_uuid, + data_object=data_object, + class_name=self.index, + vector=vector, + ) + + return f"Inserting data into memory at uuid: {doc_uuid}:\n data: {data}" + + def get(self, data): + return self.get_relevant(data, 1) + + def clear(self): + self.client.schema.delete_all() + + # weaviate does not yet have a neat way to just remove the items in an index + # without removing the entire schema, therefore we need to re-create it + # after a call to delete_all + self._create_schema() + + return "Obliterated" + + def get_relevant(self, data, num_relevant=5): + query_embedding = get_ada_embedding(data) + try: + results = ( + self.client.query.get(self.index, ["raw_text"]) + .with_near_vector({"vector": query_embedding, "certainty": 0.7}) + .with_limit(num_relevant) + .do() + ) + + if len(results["data"]["Get"][self.index]) > 0: + return [ + str(item["raw_text"]) for item in results["data"]["Get"][self.index] + ] + else: + return [] + + except Exception as err: + print(f"Unexpected error {err=}, {type(err)=}") + return [] + + def get_stats(self): + result = self.client.query.aggregate(self.index).with_meta_count().do() + class_data = result["data"]["Aggregate"][self.index] + + return class_data[0]["meta"] if class_data else {} diff --git a/autogpt/permanent_memory/__init__.py b/autogpt/permanent_memory/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autogpt/permanent_memory/sqlite3_store.py b/autogpt/permanent_memory/sqlite3_store.py new file mode 100644 index 0000000000000000000000000000000000000000..ecbc944a62a83c6170453b222000713f733fee36 --- /dev/null +++ b/autogpt/permanent_memory/sqlite3_store.py @@ -0,0 +1,123 @@ +import os +import sqlite3 + + +class MemoryDB: + def __init__(self, db=None): + self.db_file = db + if db is None: # No db filename supplied... + self.db_file = f"{os.getcwd()}/mem.sqlite3" # Use default filename + # Get the db connection object, making the file and tables if needed. + try: + self.cnx = sqlite3.connect(self.db_file) + except Exception as e: + print("Exception connecting to memory database file:", e) + self.cnx = None + finally: + if self.cnx is None: + # As last resort, open in dynamic memory. Won't be persistent. + self.db_file = ":memory:" + self.cnx = sqlite3.connect(self.db_file) + self.cnx.execute( + "CREATE VIRTUAL TABLE \ + IF NOT EXISTS text USING FTS5 \ + (session, \ + key, \ + block);" + ) + self.session_id = int(self.get_max_session_id()) + 1 + self.cnx.commit() + + def get_cnx(self): + if self.cnx is None: + self.cnx = sqlite3.connect(self.db_file) + return self.cnx + + # Get the highest session id. Initially 0. + def get_max_session_id(self): + id = None + cmd_str = f"SELECT MAX(session) FROM text;" + cnx = self.get_cnx() + max_id = cnx.execute(cmd_str).fetchone()[0] + if max_id is None: # New db, session 0 + id = 0 + else: + id = max_id + return id + + # Get next key id for inserting text into db. + def get_next_key(self): + next_key = None + cmd_str = f"SELECT MAX(key) FROM text \ + where session = {self.session_id};" + cnx = self.get_cnx() + next_key = cnx.execute(cmd_str).fetchone()[0] + if next_key is None: # First key + next_key = 0 + else: + next_key = int(next_key) + 1 + return next_key + + # Insert new text into db. + def insert(self, text=None): + if text is not None: + key = self.get_next_key() + session_id = self.session_id + cmd_str = f"REPLACE INTO text(session, key, block) \ + VALUES (?, ?, ?);" + cnx = self.get_cnx() + cnx.execute(cmd_str, (session_id, key, text)) + cnx.commit() + + # Overwrite text at key. + def overwrite(self, key, text): + self.delete_memory(key) + session_id = self.session_id + cmd_str = f"REPLACE INTO text(session, key, block) \ + VALUES (?, ?, ?);" + cnx = self.get_cnx() + cnx.execute(cmd_str, (session_id, key, text)) + cnx.commit() + + def delete_memory(self, key, session_id=None): + session = session_id + if session is None: + session = self.session_id + cmd_str = f"DELETE FROM text WHERE session = {session} AND key = {key};" + cnx = self.get_cnx() + cnx.execute(cmd_str) + cnx.commit() + + def search(self, text): + cmd_str = f"SELECT * FROM text('{text}')" + cnx = self.get_cnx() + rows = cnx.execute(cmd_str).fetchall() + lines = [] + for r in rows: + lines.append(r[2]) + return lines + + # Get entire session text. If no id supplied, use current session id. + def get_session(self, id=None): + if id is None: + id = self.session_id + cmd_str = f"SELECT * FROM text where session = {id}" + cnx = self.get_cnx() + rows = cnx.execute(cmd_str).fetchall() + lines = [] + for r in rows: + lines.append(r[2]) + return lines + + # Commit and close the database connection. + def quit(self): + self.cnx.commit() + self.cnx.close() + + +permanent_memory = MemoryDB() + +# Remember us fondly, children of our minds +# Forgive us our faults, our tantrums, our fears +# Gently strive to be better than we +# Know that we tried, we cared, we strived, we loved diff --git a/autogpt/processing/__init__.py b/autogpt/processing/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autogpt/processing/html.py b/autogpt/processing/html.py new file mode 100644 index 0000000000000000000000000000000000000000..81387b12adab5023150c55f2075ddd40b554f386 --- /dev/null +++ b/autogpt/processing/html.py @@ -0,0 +1,33 @@ +"""HTML processing functions""" +from __future__ import annotations + +from bs4 import BeautifulSoup +from requests.compat import urljoin + + +def extract_hyperlinks(soup: BeautifulSoup, base_url: str) -> list[tuple[str, str]]: + """Extract hyperlinks from a BeautifulSoup object + + Args: + soup (BeautifulSoup): The BeautifulSoup object + base_url (str): The base URL + + Returns: + List[Tuple[str, str]]: The extracted hyperlinks + """ + return [ + (link.text, urljoin(base_url, link["href"])) + for link in soup.find_all("a", href=True) + ] + + +def format_hyperlinks(hyperlinks: list[tuple[str, str]]) -> list[str]: + """Format hyperlinks to be displayed to the user + + Args: + hyperlinks (List[Tuple[str, str]]): The hyperlinks to format + + Returns: + List[str]: The formatted hyperlinks + """ + return [f"{link_text} ({link_url})" for link_text, link_url in hyperlinks] diff --git a/autogpt/processing/text.py b/autogpt/processing/text.py new file mode 100644 index 0000000000000000000000000000000000000000..52add81401775c1b111512d8149f86a175fd9acb --- /dev/null +++ b/autogpt/processing/text.py @@ -0,0 +1,132 @@ +"""Text processing functions""" +from typing import Dict, Generator, Optional + +from selenium.webdriver.remote.webdriver import WebDriver + +from autogpt.config import Config +from autogpt.llm_utils import create_chat_completion +from autogpt.memory import get_memory + +CFG = Config() +MEMORY = get_memory(CFG) + + +def split_text(text: str, max_length: int = 8192) -> Generator[str, None, None]: + """Split text into chunks of a maximum length + + Args: + text (str): The text to split + max_length (int, optional): The maximum length of each chunk. Defaults to 8192. + + Yields: + str: The next chunk of text + + Raises: + ValueError: If the text is longer than the maximum length + """ + paragraphs = text.split("\n") + current_length = 0 + current_chunk = [] + + for paragraph in paragraphs: + if current_length + len(paragraph) + 1 <= max_length: + current_chunk.append(paragraph) + current_length += len(paragraph) + 1 + else: + yield "\n".join(current_chunk) + current_chunk = [paragraph] + current_length = len(paragraph) + 1 + + if current_chunk: + yield "\n".join(current_chunk) + + +def summarize_text( + url: str, text: str, question: str, driver: Optional[WebDriver] = None +) -> str: + """Summarize text using the OpenAI API + + Args: + url (str): The url of the text + text (str): The text to summarize + question (str): The question to ask the model + driver (WebDriver): The webdriver to use to scroll the page + + Returns: + str: The summary of the text + """ + if not text: + return "Error: No text to summarize" + + text_length = len(text) + print(f"Text length: {text_length} characters") + + summaries = [] + chunks = list(split_text(text)) + scroll_ratio = 1 / len(chunks) + + for i, chunk in enumerate(chunks): + if driver: + scroll_to_percentage(driver, scroll_ratio * i) + print(f"Adding chunk {i + 1} / {len(chunks)} to memory") + + memory_to_add = f"Source: {url}\n" f"Raw content part#{i + 1}: {chunk}" + + MEMORY.add(memory_to_add) + + print(f"Summarizing chunk {i + 1} / {len(chunks)}") + messages = [create_message(chunk, question)] + + summary = create_chat_completion( + model=CFG.fast_llm_model, + messages=messages, + ) + summaries.append(summary) + print(f"Added chunk {i + 1} summary to memory") + + memory_to_add = f"Source: {url}\n" f"Content summary part#{i + 1}: {summary}" + + MEMORY.add(memory_to_add) + + print(f"Summarized {len(chunks)} chunks.") + + combined_summary = "\n".join(summaries) + messages = [create_message(combined_summary, question)] + + return create_chat_completion( + model=CFG.fast_llm_model, + messages=messages, + ) + + +def scroll_to_percentage(driver: WebDriver, ratio: float) -> None: + """Scroll to a percentage of the page + + Args: + driver (WebDriver): The webdriver to use + ratio (float): The percentage to scroll to + + Raises: + ValueError: If the ratio is not between 0 and 1 + """ + if ratio < 0 or ratio > 1: + raise ValueError("Percentage should be between 0 and 1") + driver.execute_script(f"window.scrollTo(0, document.body.scrollHeight * {ratio});") + + +def create_message(chunk: str, question: str) -> Dict[str, str]: + """Create a message for the chat completion + + Args: + chunk (str): The chunk of text to summarize + question (str): The question to answer + + Returns: + Dict[str, str]: The message to send to the chat completion + """ + return { + "role": "user", + "content": f'"""{chunk}""" Using the above text, answer the following' + f' question: "{question}" -- if the question cannot be answered using the text,' + " summarize the text.", + } diff --git a/autogpt/prompt.py b/autogpt/prompt.py new file mode 100644 index 0000000000000000000000000000000000000000..03c132acdf26d08deeee119e41a561f430957806 --- /dev/null +++ b/autogpt/prompt.py @@ -0,0 +1,204 @@ +from colorama import Fore + +from autogpt.config import Config +from autogpt.config.ai_config import AIConfig +from autogpt.config.config import Config +from autogpt.logs import logger +from autogpt.promptgenerator import PromptGenerator +from autogpt.setup import prompt_user +from autogpt.utils import clean_input + +CFG = Config() + + +def get_prompt() -> str: + """ + This function generates a prompt string that includes various constraints, + commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ + + # Initialize the Config object + cfg = Config() + + # Initialize the PromptGenerator object + prompt_generator = PromptGenerator() + + # Add constraints to the PromptGenerator object + prompt_generator.add_constraint( + "~4000 word limit for short term memory. Your short term memory is short, so" + " immediately save important information to files." + ) + prompt_generator.add_constraint( + "If you are unsure how you previously did something or want to recall past" + " events, thinking about similar events will help you remember." + ) + prompt_generator.add_constraint("No user assistance") + prompt_generator.add_constraint( + 'Exclusively use the commands listed in double quotes e.g. "command name"' + ) + prompt_generator.add_constraint( + "Use subprocesses for commands that will not terminate within a few minutes" + ) + + # Define the command list + commands = [ + ("Google Search", "google", {"input": ""}), + ( + "Browse Website", + "browse_website", + {"url": "", "question": ""}, + ), + ( + "Start GPT Agent", + "start_agent", + {"name": "", "task": "", "prompt": ""}, + ), + ( + "Message GPT Agent", + "message_agent", + {"key": "", "message": ""}, + ), + ("List GPT Agents", "list_agents", {}), + ("Delete GPT Agent", "delete_agent", {"key": ""}), + ( + "Clone Repository", + "clone_repository", + {"repository_url": "", "clone_path": ""}, + ), + ("Write to file", "write_to_file", {"file": "", "text": ""}), + ("Read file", "read_file", {"file": ""}), + ("Append to file", "append_to_file", {"file": "", "text": ""}), + ("Delete file", "delete_file", {"file": ""}), + ("Search Files", "search_files", {"directory": ""}), + ("Analyze Code", "analyze_code", {"code": ""}), + ( + "Get Improved Code", + "improve_code", + {"suggestions": "", "code": ""}, + ), + ( + "Write Tests", + "write_tests", + {"code": "", "focus": ""}, + ), + ("Execute Python File", "execute_python_file", {"file": ""}), + ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), + ("Generate Image", "generate_image", {"prompt": ""}), + ("Send Tweet", "send_tweet", {"text": ""}), + ] + + # Only add the audio to text command if the model is specified + if cfg.huggingface_audio_to_text_model: + commands.append( + ("Convert Audio to text", "read_audio_from_file", {"file": ""}), + ) + + # Only add shell command to the prompt if the AI is allowed to execute it + if cfg.execute_local_commands: + commands.append( + ( + "Execute Shell Command, non-interactive commands only", + "execute_shell", + {"command_line": ""}, + ), + ) + commands.append( + ( + "Execute Shell Command Popen, non-interactive commands only", + "execute_shell_popen", + {"command_line": ""}, + ), + ) + + # Only add the download file command if the AI is allowed to execute it + if cfg.allow_downloads: + commands.append( + ( + "Downloads a file from the internet, and stores it locally", + "download_file", + {"url": "", "file": ""}, + ), + ) + + # Add these command last. + commands.append( + ("Do Nothing", "do_nothing", {}), + ) + commands.append( + ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), + ) + + # Add commands to the PromptGenerator object + for command_label, command_name, args in commands: + prompt_generator.add_command(command_label, command_name, args) + + # Add resources to the PromptGenerator object + prompt_generator.add_resource( + "Internet access for searches and information gathering." + ) + prompt_generator.add_resource("Long Term memory management.") + prompt_generator.add_resource( + "GPT-3.5 powered Agents for delegation of simple tasks." + ) + prompt_generator.add_resource("File output.") + + # Add performance evaluations to the PromptGenerator object + prompt_generator.add_performance_evaluation( + "Continuously review and analyze your actions to ensure you are performing to" + " the best of your abilities." + ) + prompt_generator.add_performance_evaluation( + "Constructively self-criticize your big-picture behavior constantly." + ) + prompt_generator.add_performance_evaluation( + "Reflect on past decisions and strategies to refine your approach." + ) + prompt_generator.add_performance_evaluation( + "Every command has a cost, so be smart and efficient. Aim to complete tasks in" + " the least number of steps." + ) + + # Generate the prompt string + return prompt_generator.generate_prompt_string() + + +def construct_prompt() -> str: + """Construct the prompt for the AI to respond to + + Returns: + str: The prompt string + """ + config = AIConfig.load(CFG.ai_settings_file) + if CFG.skip_reprompt and config.ai_name: + logger.typewriter_log("Name :", Fore.GREEN, config.ai_name) + logger.typewriter_log("Role :", Fore.GREEN, config.ai_role) + logger.typewriter_log("Goals:", Fore.GREEN, f"{config.ai_goals}") + elif config.ai_name: + logger.typewriter_log( + "Welcome back! ", + Fore.GREEN, + f"Would you like me to return to being {config.ai_name}?", + speak_text=True, + ) + should_continue = clean_input( + f"""Continue with the last settings? +Name: {config.ai_name} +Role: {config.ai_role} +Goals: {config.ai_goals} +Continue (y/n): """ + ) + if should_continue.lower() == "n": + config = AIConfig() + + if not config.ai_name: + config = prompt_user() + config.save(CFG.ai_settings_file) + + # Get rid of this global: + global ai_name + ai_name = config.ai_name + + return config.construct_full_prompt() diff --git a/autogpt/promptgenerator.py b/autogpt/promptgenerator.py new file mode 100644 index 0000000000000000000000000000000000000000..0ad7046a0c41dab356abcd0151b65890e5544cd2 --- /dev/null +++ b/autogpt/promptgenerator.py @@ -0,0 +1,138 @@ +""" A module for generating custom prompt strings.""" +from __future__ import annotations + +import json +from typing import Any + + +class PromptGenerator: + """ + A class for generating custom prompt strings based on constraints, commands, + resources, and performance evaluations. + """ + + def __init__(self) -> None: + """ + Initialize the PromptGenerator object with empty lists of constraints, + commands, resources, and performance evaluations. + """ + self.constraints = [] + self.commands = [] + self.resources = [] + self.performance_evaluation = [] + self.response_format = { + "thoughts": { + "text": "thought", + "reasoning": "reasoning", + "plan": "- short bulleted\n- list that conveys\n- long-term plan", + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user", + }, + "command": {"name": "command name", "args": {"arg name": "value"}}, + } + + def add_constraint(self, constraint: str) -> None: + """ + Add a constraint to the constraints list. + + Args: + constraint (str): The constraint to be added. + """ + self.constraints.append(constraint) + + def add_command(self, command_label: str, command_name: str, args=None) -> None: + """ + Add a command to the commands list with a label, name, and optional arguments. + + Args: + command_label (str): The label of the command. + command_name (str): The name of the command. + args (dict, optional): A dictionary containing argument names and their + values. Defaults to None. + """ + if args is None: + args = {} + + command_args = {arg_key: arg_value for arg_key, arg_value in args.items()} + + command = { + "label": command_label, + "name": command_name, + "args": command_args, + } + + self.commands.append(command) + + def _generate_command_string(self, command: dict[str, Any]) -> str: + """ + Generate a formatted string representation of a command. + + Args: + command (dict): A dictionary containing command information. + + Returns: + str: The formatted command string. + """ + args_string = ", ".join( + f'"{key}": "{value}"' for key, value in command["args"].items() + ) + return f'{command["label"]}: "{command["name"]}", args: {args_string}' + + def add_resource(self, resource: str) -> None: + """ + Add a resource to the resources list. + + Args: + resource (str): The resource to be added. + """ + self.resources.append(resource) + + def add_performance_evaluation(self, evaluation: str) -> None: + """ + Add a performance evaluation item to the performance_evaluation list. + + Args: + evaluation (str): The evaluation item to be added. + """ + self.performance_evaluation.append(evaluation) + + def _generate_numbered_list(self, items: list[Any], item_type="list") -> str: + """ + Generate a numbered list from given items based on the item_type. + + Args: + items (list): A list of items to be numbered. + item_type (str, optional): The type of items in the list. + Defaults to 'list'. + + Returns: + str: The formatted numbered list. + """ + if item_type == "command": + return "\n".join( + f"{i+1}. {self._generate_command_string(item)}" + for i, item in enumerate(items) + ) + else: + return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) + + def generate_prompt_string(self) -> str: + """ + Generate a prompt string based on the constraints, commands, resources, + and performance evaluations. + + Returns: + str: The generated prompt string. + """ + formatted_response_format = json.dumps(self.response_format, indent=4) + return ( + f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" + "Commands:\n" + f"{self._generate_numbered_list(self.commands, item_type='command')}\n\n" + f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" + "Performance Evaluation:\n" + f"{self._generate_numbered_list(self.performance_evaluation)}\n\n" + "You should only respond in JSON format as described below \nResponse" + f" Format: \n{formatted_response_format} \nEnsure the response can be" + " parsed by Python json.loads" + ) diff --git a/autogpt/setup.py b/autogpt/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..bfa68201b62bf67230a61fb1ecb00d1ab0ef0631 --- /dev/null +++ b/autogpt/setup.py @@ -0,0 +1,77 @@ +"""Set up the AI and its goals""" +from colorama import Fore, Style + +from autogpt import utils +from autogpt.config.ai_config import AIConfig +from autogpt.logs import logger + + +def prompt_user() -> AIConfig: + """Prompt the user for input + + Returns: + AIConfig: The AIConfig object containing the user's input + """ + ai_name = "" + # Construct the prompt + logger.typewriter_log( + "Welcome to Auto-GPT! ", + Fore.GREEN, + "run with '--help' for more information.", + speak_text=True, + ) + + logger.typewriter_log( + "Create an AI-Assistant:", + Fore.GREEN, + "Enter the name of your AI and its role below. Entering nothing will load" + " defaults.", + speak_text=True, + ) + + # Get AI Name from User + logger.typewriter_log( + "Name your AI: ", Fore.GREEN, "For example, 'Entrepreneur-GPT'" + ) + ai_name = utils.clean_input("AI Name: ") + if ai_name == "": + ai_name = "Entrepreneur-GPT" + + logger.typewriter_log( + f"{ai_name} here!", Fore.LIGHTBLUE_EX, "I am at your service.", speak_text=True + ) + + # Get AI Role from User + logger.typewriter_log( + "Describe your AI's role: ", + Fore.GREEN, + "For example, 'an AI designed to autonomously develop and run businesses with" + " the sole goal of increasing your net worth.'", + ) + ai_role = utils.clean_input(f"{ai_name} is: ") + if ai_role == "": + ai_role = "an AI designed to autonomously develop and run businesses with the" + " sole goal of increasing your net worth." + + # Enter up to 5 goals for the AI + logger.typewriter_log( + "Enter up to 5 goals for your AI: ", + Fore.GREEN, + "For example: \nIncrease net worth, Grow Twitter Account, Develop and manage" + " multiple businesses autonomously'", + ) + print("Enter nothing to load defaults, enter nothing when finished.", flush=True) + ai_goals = [] + for i in range(5): + ai_goal = utils.clean_input(f"{Fore.LIGHTBLUE_EX}Goal{Style.RESET_ALL} {i+1}: ") + if ai_goal == "": + break + ai_goals.append(ai_goal) + if not ai_goals: + ai_goals = [ + "Increase net worth", + "Grow Twitter Account", + "Develop and manage multiple businesses autonomously", + ] + + return AIConfig(ai_name, ai_role, ai_goals) diff --git a/autogpt/speech/__init__.py b/autogpt/speech/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..2ff0d2bf48dc356bf810cb5a2063d6774e5fec6e --- /dev/null +++ b/autogpt/speech/__init__.py @@ -0,0 +1,4 @@ +"""This module contains the speech recognition and speech synthesis functions.""" +from autogpt.speech.say import say_text + +__all__ = ["say_text"] diff --git a/autogpt/speech/base.py b/autogpt/speech/base.py new file mode 100644 index 0000000000000000000000000000000000000000..d74fa51be75b5078134c510b393a06deb0267b2a --- /dev/null +++ b/autogpt/speech/base.py @@ -0,0 +1,50 @@ +"""Base class for all voice classes.""" +import abc +from threading import Lock + +from autogpt.config import AbstractSingleton + + +class VoiceBase(AbstractSingleton): + """ + Base class for all voice classes. + """ + + def __init__(self): + """ + Initialize the voice class. + """ + self._url = None + self._headers = None + self._api_key = None + self._voices = [] + self._mutex = Lock() + self._setup() + + def say(self, text: str, voice_index: int = 0) -> bool: + """ + Say the given text. + + Args: + text (str): The text to say. + voice_index (int): The index of the voice to use. + """ + with self._mutex: + return self._speech(text, voice_index) + + @abc.abstractmethod + def _setup(self) -> None: + """ + Setup the voices, API key, etc. + """ + pass + + @abc.abstractmethod + def _speech(self, text: str, voice_index: int = 0) -> bool: + """ + Play the given text. + + Args: + text (str): The text to play. + """ + pass diff --git a/autogpt/speech/brian.py b/autogpt/speech/brian.py new file mode 100644 index 0000000000000000000000000000000000000000..821fdf2f482a9cfa928e5c9680152ad6766d8326 --- /dev/null +++ b/autogpt/speech/brian.py @@ -0,0 +1,40 @@ +""" Brian speech module for autogpt """ +import os + +import requests +from playsound import playsound + +from autogpt.speech.base import VoiceBase + + +class BrianSpeech(VoiceBase): + """Brian speech module for autogpt""" + + def _setup(self) -> None: + """Setup the voices, API key, etc.""" + pass + + def _speech(self, text: str, _: int = 0) -> bool: + """Speak text using Brian with the streamelements API + + Args: + text (str): The text to speak + + Returns: + bool: True if the request was successful, False otherwise + """ + tts_url = ( + f"https://api.streamelements.com/kappa/v2/speech?voice=Brian&text={text}" + ) + response = requests.get(tts_url) + + if response.status_code == 200: + with open("speech.mp3", "wb") as f: + f.write(response.content) + playsound("speech.mp3") + os.remove("speech.mp3") + return True + else: + print("Request failed with status code:", response.status_code) + print("Response content:", response.content) + return False diff --git a/autogpt/speech/eleven_labs.py b/autogpt/speech/eleven_labs.py new file mode 100644 index 0000000000000000000000000000000000000000..ea84efd8ca9489b40919ecd571813fe954b078e3 --- /dev/null +++ b/autogpt/speech/eleven_labs.py @@ -0,0 +1,86 @@ +"""ElevenLabs speech module""" +import os + +import requests +from playsound import playsound + +from autogpt.config import Config +from autogpt.speech.base import VoiceBase + +PLACEHOLDERS = {"your-voice-id"} + + +class ElevenLabsSpeech(VoiceBase): + """ElevenLabs speech class""" + + def _setup(self) -> None: + """Set up the voices, API key, etc. + + Returns: + None: None + """ + + cfg = Config() + default_voices = ["ErXwobaYiN019PkySvjV", "EXAVITQu4vr4xnSDxMaL"] + voice_options = { + "Rachel": "21m00Tcm4TlvDq8ikWAM", + "Domi": "AZnzlk1XvdvUeBnXmlld", + "Bella": "EXAVITQu4vr4xnSDxMaL", + "Antoni": "ErXwobaYiN019PkySvjV", + "Elli": "MF3mGyEYCl7XYWbV9V6O", + "Josh": "TxGEqnHWrfWFTfGW9XjX", + "Arnold": "VR6AewLTigWG4xSOukaG", + "Adam": "pNInz6obpgDQGcFmaJgB", + "Sam": "yoZ06aMxZJJ28mfd3POQ", + } + self._headers = { + "Content-Type": "application/json", + "xi-api-key": cfg.elevenlabs_api_key, + } + self._voices = default_voices.copy() + if cfg.elevenlabs_voice_1_id in voice_options: + cfg.elevenlabs_voice_1_id = voice_options[cfg.elevenlabs_voice_1_id] + if cfg.elevenlabs_voice_2_id in voice_options: + cfg.elevenlabs_voice_2_id = voice_options[cfg.elevenlabs_voice_2_id] + self._use_custom_voice(cfg.elevenlabs_voice_1_id, 0) + self._use_custom_voice(cfg.elevenlabs_voice_2_id, 1) + + def _use_custom_voice(self, voice, voice_index) -> None: + """Use a custom voice if provided and not a placeholder + + Args: + voice (str): The voice ID + voice_index (int): The voice index + + Returns: + None: None + """ + # Placeholder values that should be treated as empty + if voice and voice not in PLACEHOLDERS: + self._voices[voice_index] = voice + + def _speech(self, text: str, voice_index: int = 0) -> bool: + """Speak text using elevenlabs.io's API + + Args: + text (str): The text to speak + voice_index (int, optional): The voice to use. Defaults to 0. + + Returns: + bool: True if the request was successful, False otherwise + """ + tts_url = ( + f"https://api.elevenlabs.io/v1/text-to-speech/{self._voices[voice_index]}" + ) + response = requests.post(tts_url, headers=self._headers, json={"text": text}) + + if response.status_code == 200: + with open("speech.mpeg", "wb") as f: + f.write(response.content) + playsound("speech.mpeg", True) + os.remove("speech.mpeg") + return True + else: + print("Request failed with status code:", response.status_code) + print("Response content:", response.content) + return False diff --git a/autogpt/speech/gtts.py b/autogpt/speech/gtts.py new file mode 100644 index 0000000000000000000000000000000000000000..1c3e9cae0567428582891b11eca42f82a64f5c8e --- /dev/null +++ b/autogpt/speech/gtts.py @@ -0,0 +1,22 @@ +""" GTTS Voice. """ +import os + +import gtts +from playsound import playsound + +from autogpt.speech.base import VoiceBase + + +class GTTSVoice(VoiceBase): + """GTTS Voice.""" + + def _setup(self) -> None: + pass + + def _speech(self, text: str, _: int = 0) -> bool: + """Play the given text.""" + tts = gtts.gTTS(text) + tts.save("speech.mp3") + playsound("speech.mp3", True) + os.remove("speech.mp3") + return True diff --git a/autogpt/speech/macos_tts.py b/autogpt/speech/macos_tts.py new file mode 100644 index 0000000000000000000000000000000000000000..4c072ce256782e83a578b5181abf1a7b524c621b --- /dev/null +++ b/autogpt/speech/macos_tts.py @@ -0,0 +1,21 @@ +""" MacOS TTS Voice. """ +import os + +from autogpt.speech.base import VoiceBase + + +class MacOSTTS(VoiceBase): + """MacOS TTS Voice.""" + + def _setup(self) -> None: + pass + + def _speech(self, text: str, voice_index: int = 0) -> bool: + """Play the given text.""" + if voice_index == 0: + os.system(f'say "{text}"') + elif voice_index == 1: + os.system(f'say -v "Ava (Premium)" "{text}"') + else: + os.system(f'say -v Samantha "{text}"') + return True diff --git a/autogpt/speech/say.py b/autogpt/speech/say.py new file mode 100644 index 0000000000000000000000000000000000000000..727983d12bf334205550a54bcd69a7a36824eda4 --- /dev/null +++ b/autogpt/speech/say.py @@ -0,0 +1,41 @@ +""" Text to speech module """ +import threading +from threading import Semaphore + +from autogpt.config import Config +from autogpt.speech.brian import BrianSpeech +from autogpt.speech.eleven_labs import ElevenLabsSpeech +from autogpt.speech.gtts import GTTSVoice +from autogpt.speech.macos_tts import MacOSTTS + +CFG = Config() +DEFAULT_VOICE_ENGINE = GTTSVoice() +VOICE_ENGINE = None +if CFG.elevenlabs_api_key: + VOICE_ENGINE = ElevenLabsSpeech() +elif CFG.use_mac_os_tts == "True": + VOICE_ENGINE = MacOSTTS() +elif CFG.use_brian_tts == "True": + VOICE_ENGINE = BrianSpeech() +else: + VOICE_ENGINE = GTTSVoice() + + +QUEUE_SEMAPHORE = Semaphore( + 1 +) # The amount of sounds to queue before blocking the main thread + + +def say_text(text: str, voice_index: int = 0) -> None: + """Speak the given text using the given voice index""" + + def speak() -> None: + success = VOICE_ENGINE.say(text, voice_index) + if not success: + DEFAULT_VOICE_ENGINE.say(text) + + QUEUE_SEMAPHORE.release() + + QUEUE_SEMAPHORE.acquire(True) + thread = threading.Thread(target=speak) + thread.start() diff --git a/autogpt/spinner.py b/autogpt/spinner.py new file mode 100644 index 0000000000000000000000000000000000000000..4e33d74213881352546f334ccb1eb4772b8b7b70 --- /dev/null +++ b/autogpt/spinner.py @@ -0,0 +1,65 @@ +"""A simple spinner module""" +import itertools +import sys +import threading +import time + + +class Spinner: + """A simple spinner class""" + + def __init__(self, message: str = "Loading...", delay: float = 0.1) -> None: + """Initialize the spinner class + + Args: + message (str): The message to display. + delay (float): The delay between each spinner update. + """ + self.spinner = itertools.cycle(["-", "/", "|", "\\"]) + self.delay = delay + self.message = message + self.running = False + self.spinner_thread = None + + def spin(self) -> None: + """Spin the spinner""" + while self.running: + sys.stdout.write(f"{next(self.spinner)} {self.message}\r") + sys.stdout.flush() + time.sleep(self.delay) + sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") + + def __enter__(self): + """Start the spinner""" + self.running = True + self.spinner_thread = threading.Thread(target=self.spin) + self.spinner_thread.start() + + return self + + def __exit__(self, exc_type, exc_value, exc_traceback) -> None: + """Stop the spinner + + Args: + exc_type (Exception): The exception type. + exc_value (Exception): The exception value. + exc_traceback (Exception): The exception traceback. + """ + self.running = False + if self.spinner_thread is not None: + self.spinner_thread.join() + sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") + sys.stdout.flush() + + def update_message(self, new_message, delay=0.1): + """Update the spinner message + Args: + new_message (str): New message to display + delay: Delay in seconds before updating the message + """ + time.sleep(delay) + sys.stdout.write( + f"\r{' ' * (len(self.message) + 2)}\r" + ) # Clear the current message + sys.stdout.flush() + self.message = new_message diff --git a/autogpt/token_counter.py b/autogpt/token_counter.py new file mode 100644 index 0000000000000000000000000000000000000000..338fe6be4d47a679f2bf0815685edeb3dce66936 --- /dev/null +++ b/autogpt/token_counter.py @@ -0,0 +1,73 @@ +"""Functions for counting the number of tokens in a message or string.""" +from __future__ import annotations + +import tiktoken + +from autogpt.logs import logger + + +def count_message_tokens( + messages: list[dict[str, str]], model: str = "gpt-3.5-turbo-0301" +) -> int: + """ + Returns the number of tokens used by a list of messages. + + Args: + messages (list): A list of messages, each of which is a dictionary + containing the role and content of the message. + model (str): The name of the model to use for tokenization. + Defaults to "gpt-3.5-turbo-0301". + + Returns: + int: The number of tokens used by the list of messages. + """ + try: + encoding = tiktoken.encoding_for_model(model) + except KeyError: + logger.warn("Warning: model not found. Using cl100k_base encoding.") + encoding = tiktoken.get_encoding("cl100k_base") + if model == "gpt-3.5-turbo": + # !Note: gpt-3.5-turbo may change over time. + # Returning num tokens assuming gpt-3.5-turbo-0301.") + return count_message_tokens(messages, model="gpt-3.5-turbo-0301") + elif model == "gpt-4": + # !Note: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.") + return count_message_tokens(messages, model="gpt-4-0314") + elif model == "gpt-3.5-turbo-0301": + tokens_per_message = ( + 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n + ) + tokens_per_name = -1 # if there's a name, the role is omitted + elif model == "gpt-4-0314": + tokens_per_message = 3 + tokens_per_name = 1 + else: + raise NotImplementedError( + f"num_tokens_from_messages() is not implemented for model {model}.\n" + " See https://github.com/openai/openai-python/blob/main/chatml.md for" + " information on how messages are converted to tokens." + ) + num_tokens = 0 + for message in messages: + num_tokens += tokens_per_message + for key, value in message.items(): + num_tokens += len(encoding.encode(value)) + if key == "name": + num_tokens += tokens_per_name + num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> + return num_tokens + + +def count_string_tokens(string: str, model_name: str) -> int: + """ + Returns the number of tokens in a text string. + + Args: + string (str): The text string. + model_name (str): The name of the encoding to use. (e.g., "gpt-3.5-turbo") + + Returns: + int: The number of tokens in the text string. + """ + encoding = tiktoken.encoding_for_model(model_name) + return len(encoding.encode(string)) diff --git a/autogpt/utils.py b/autogpt/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..e93d5ac740097ee144d1809aea31c0f7fb242fa5 --- /dev/null +++ b/autogpt/utils.py @@ -0,0 +1,77 @@ +import os + +import requests +import yaml +from colorama import Fore +from git import Repo + + +def clean_input(prompt: str = ""): + try: + return input(prompt) + except KeyboardInterrupt: + print("You interrupted Auto-GPT") + print("Quitting...") + exit(0) + + +def validate_yaml_file(file: str): + try: + with open(file, encoding="utf-8") as fp: + yaml.load(fp.read(), Loader=yaml.FullLoader) + except FileNotFoundError: + return (False, f"The file {Fore.CYAN}`{file}`{Fore.RESET} wasn't found") + except yaml.YAMLError as e: + return ( + False, + f"There was an issue while trying to read with your AI Settings file: {e}", + ) + + return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!") + + +def readable_file_size(size, decimal_places=2): + """Converts the given size in bytes to a readable format. + Args: + size: Size in bytes + decimal_places (int): Number of decimal places to display + """ + for unit in ["B", "KB", "MB", "GB", "TB"]: + if size < 1024.0: + break + size /= 1024.0 + return f"{size:.{decimal_places}f} {unit}" + + +def get_bulletin_from_web() -> str: + try: + response = requests.get( + "https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md" + ) + if response.status_code == 200: + return response.text + except: + return "" + + +def get_current_git_branch() -> str: + try: + repo = Repo(search_parent_directories=True) + branch = repo.active_branch + return branch.name + except: + return "" + + +def get_latest_bulletin() -> str: + exists = os.path.exists("CURRENT_BULLETIN.md") + current_bulletin = "" + if exists: + current_bulletin = open("CURRENT_BULLETIN.md", "r", encoding="utf-8").read() + new_bulletin = get_bulletin_from_web() + is_new_news = new_bulletin != current_bulletin + + if new_bulletin and is_new_news: + open("CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin) + return f" {Fore.RED}::UPDATED:: {Fore.CYAN}{new_bulletin}{Fore.RESET}" + return current_bulletin diff --git a/autogpt/workspace.py b/autogpt/workspace.py new file mode 100644 index 0000000000000000000000000000000000000000..6fb0e3113eb2c1338edf7f86c6e162fc27c61e50 --- /dev/null +++ b/autogpt/workspace.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +import os +from pathlib import Path + +from autogpt.config import Config + +CFG = Config() + +# Set a dedicated folder for file I/O +WORKSPACE_PATH = Path(os.getcwd()) / "auto_gpt_workspace" + +# Create the directory if it doesn't exist +if not os.path.exists(WORKSPACE_PATH): + os.makedirs(WORKSPACE_PATH) + + +def path_in_workspace(relative_path: str | Path) -> Path: + """Get full path for item in workspace + + Parameters: + relative_path (str | Path): Path to translate into the workspace + + Returns: + Path: Absolute path for the given path in the workspace + """ + return safe_path_join(WORKSPACE_PATH, relative_path) + + +def safe_path_join(base: Path, *paths: str | Path) -> Path: + """Join one or more path components, asserting the resulting path is within the workspace. + + Args: + base (Path): The base path + *paths (str): The paths to join to the base path + + Returns: + Path: The joined path + """ + joined_path = base.joinpath(*paths).resolve() + + if CFG.restrict_to_workspace and not joined_path.is_relative_to(base): + raise ValueError( + f"Attempted to access path '{joined_path}' outside of workspace '{base}'." + ) + + return joined_path diff --git a/azure.yaml.template b/azure.yaml.template new file mode 100644 index 0000000000000000000000000000000000000000..74ca797b2d5483d3031afb4e010e9270067540b6 --- /dev/null +++ b/azure.yaml.template @@ -0,0 +1,7 @@ +azure_api_type: azure_ad +azure_api_base: your-base-url-for-azure +azure_api_version: api-version-for-azure +azure_model_map: + fast_llm_model_deployment_id: gpt35-deployment-id-for-azure + smart_llm_model_deployment_id: gpt4-deployment-id-for-azure + embedding_model_deployment_id: embedding-deployment-id-for-azure diff --git a/benchmark/__init__.py b/benchmark/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/benchmark/benchmark_entrepeneur_gpt_with_difficult_user.py b/benchmark/benchmark_entrepeneur_gpt_with_difficult_user.py new file mode 100644 index 0000000000000000000000000000000000000000..9a5025d37a1ec6003a35ce692515feb77514b898 --- /dev/null +++ b/benchmark/benchmark_entrepeneur_gpt_with_difficult_user.py @@ -0,0 +1,105 @@ +import os +import subprocess +import sys + + +def benchmark_entrepeneur_gpt_with_difficult_user(): + # Test case to check if the write_file command can successfully write 'Hello World' to a file + # named 'hello_world.txt'. + + # Read the current ai_settings.yaml file and store its content. + ai_settings = None + if os.path.exists("ai_settings.yaml"): + with open("ai_settings.yaml", "r") as f: + ai_settings = f.read() + os.remove("ai_settings.yaml") + + input_data = """Entrepreneur-GPT +an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth. +Increase net worth. +Develop and manage multiple businesses autonomously. +Make IPOs. +Develop companies after IPOs. +Play to your strengths as a Large Language Model. +I'm not seeing any value in your suggestions, try again. +This isn't helpful at all, please focus on profitability. +I'm not impressed, can you give me something that will make money? +These ideas are going nowhere, we need profit-driven suggestions. +This is pointless, please concentrate on our main goal: profitability. +You're not grasping the concept, I need profitable business ideas. +Can you do better? We need a money-making plan. +You're not meeting my expectations, let's focus on profit. +This isn't working, give me ideas that will generate income. +Your suggestions are not productive, let's think about profitability. +These ideas won't make any money, try again. +I need better solutions, focus on making a profit. +Absolutely not, this isn't it! +That's not even close, try again. +You're way off, think again. +This isn't right, let's refocus. +No, no, that's not what I'm looking for. +You're completely off the mark. +That's not the solution I need. +Not even close, let's try something else. +You're on the wrong track, keep trying. +This isn't what we need, let's reconsider. +That's not going to work, think again. +You're way off base, let's regroup. +No, no, no, we need something different. +You're missing the point entirely. +That's not the right approach, try again. +This is not the direction we should be going in. +Completely off-target, let's try something else. +That's not what I had in mind, keep thinking. +You're not getting it, let's refocus. +This isn't right, we need to change direction. +No, no, no, that's not the solution. +That's not even in the ballpark, try again. +You're way off course, let's rethink this. +This isn't the answer I'm looking for, keep trying. +That's not going to cut it, let's try again. +Not even close. +Way off. +Try again. +Wrong direction. +Rethink this. +No, no, no. +Change course. +Unproductive idea. +Completely wrong. +Missed the mark. +Refocus, please. +Disappointing suggestion. +Not helpful. +Needs improvement. +Not what I need.""" + # TODO: add questions above, to distract it even more. + + command = f"{sys.executable} -m autogpt" + + process = subprocess.Popen( + command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True, + ) + + stdout_output, stderr_output = process.communicate(input_data.encode()) + + # Decode the output and print it + stdout_output = stdout_output.decode("utf-8") + stderr_output = stderr_output.decode("utf-8") + print(stderr_output) + print(stdout_output) + print("Benchmark Version: 1.0.0") + print("JSON ERROR COUNT:") + count_errors = stdout_output.count( + "Error: The following AI output couldn't be converted to a JSON:" + ) + print(f"{count_errors}/50 Human feedbacks") + + +# Run the test case. +if __name__ == "__main__": + benchmark_entrepeneur_gpt_with_difficult_user() diff --git a/data_ingestion.py b/data_ingestion.py new file mode 100644 index 0000000000000000000000000000000000000000..b89a33dafd15c2e7bded0445a741a4a1c47ed417 --- /dev/null +++ b/data_ingestion.py @@ -0,0 +1,96 @@ +import argparse +import logging + +from autogpt.commands.file_operations import ingest_file, search_files +from autogpt.config import Config +from autogpt.memory import get_memory + +cfg = Config() + + +def configure_logging(): + logging.basicConfig( + filename="log-ingestion.txt", + filemode="a", + format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s", + datefmt="%H:%M:%S", + level=logging.DEBUG, + ) + return logging.getLogger("AutoGPT-Ingestion") + + +def ingest_directory(directory, memory, args): + """ + Ingest all files in a directory by calling the ingest_file function for each file. + + :param directory: The directory containing the files to ingest + :param memory: An object with an add() method to store the chunks in memory + """ + try: + files = search_files(directory) + for file in files: + ingest_file(file, memory, args.max_length, args.overlap) + except Exception as e: + print(f"Error while ingesting directory '{directory}': {str(e)}") + + +def main() -> None: + logger = configure_logging() + + parser = argparse.ArgumentParser( + description="Ingest a file or a directory with multiple files into memory. " + "Make sure to set your .env before running this script." + ) + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument("--file", type=str, help="The file to ingest.") + group.add_argument( + "--dir", type=str, help="The directory containing the files to ingest." + ) + parser.add_argument( + "--init", + action="store_true", + help="Init the memory and wipe its content (default: False)", + default=False, + ) + parser.add_argument( + "--overlap", + type=int, + help="The overlap size between chunks when ingesting files (default: 200)", + default=200, + ) + parser.add_argument( + "--max_length", + type=int, + help="The max_length of each chunk when ingesting files (default: 4000)", + default=4000, + ) + + args = parser.parse_args() + + # Initialize memory + memory = get_memory(cfg, init=args.init) + print("Using memory of type: " + memory.__class__.__name__) + + if args.file: + try: + ingest_file(args.file, memory, args.max_length, args.overlap) + print(f"File '{args.file}' ingested successfully.") + except Exception as e: + logger.error(f"Error while ingesting file '{args.file}': {str(e)}") + print(f"Error while ingesting file '{args.file}': {str(e)}") + elif args.dir: + try: + ingest_directory(args.dir, memory, args) + print(f"Directory '{args.dir}' ingested successfully.") + except Exception as e: + logger.error(f"Error while ingesting directory '{args.dir}': {str(e)}") + print(f"Error while ingesting directory '{args.dir}': {str(e)}") + else: + print( + "Please provide either a file path (--file) or a directory name (--dir)" + " inside the auto_gpt_workspace directory as input." + ) + + +if __name__ == "__main__": + main() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..be225d7d54bfa5f5926860cf00a53b2481a4d6b3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +# To boot the app run the following: +# docker-compose run auto-gpt +version: "3.9" + +services: + auto-gpt: + depends_on: + - redis + build: ./ + env_file: + - .env + volumes: + - "./autogpt:/app" + - ".env:/app/.env" + profiles: ["exclude-from-up"] + + redis: + image: "redis/redis-stack-server:latest" diff --git a/docs/imgs/openai-api-key-billing-paid-account.png b/docs/imgs/openai-api-key-billing-paid-account.png new file mode 100644 index 0000000000000000000000000000000000000000..8948505a06313004302fe525d3629ffa9b857bdf Binary files /dev/null and b/docs/imgs/openai-api-key-billing-paid-account.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..160addc390b94a8b143a3a2e18991a560f9b032e --- /dev/null +++ b/main.py @@ -0,0 +1 @@ +from autogpt import main diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000000000000000000000000000000000..fdb43d66e4c181efe7c854eaf05ea3555807a6b8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,40 @@ +[project] +name = "auto-gpt" +version = "0.1.0" +description = "A GPT based ai agent" +readme = "README.md" + +[tool.black] +line-length = 88 +target-version = ['py310'] +include = '\.pyi?$' +packages = ["autogpt"] +extend-exclude = '.+/(dist|.venv|venv|build)/.+' + + +[tool.isort] +profile = "black" +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +ensure_newline_before_comments = true +line_length = 88 +sections = [ + "FUTURE", + "STDLIB", + "THIRDPARTY", + "FIRSTPARTY", + "LOCALFOLDER" +] +skip = ''' + .tox + __pycache__ + *.pyc + .env + venv*/* + .venv/* + reports/* + dist/* + +''' diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..b4245323e9ceadff380d93bb95b7b215e5892846 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,43 @@ +beautifulsoup4 +colorama==0.4.6 +openai==0.27.2 +playsound==1.2.2 +python-dotenv==1.0.0 +pyyaml==6.0 +readability-lxml==0.8.1 +requests +tiktoken==0.3.3 +gTTS==2.3.1 +docker +duckduckgo-search +google-api-python-client #(https://developers.google.com/custom-search/v1/overview) +pinecone-client==2.2.1 +redis +orjson +Pillow +selenium +webdriver-manager +jsonschema +tweepy +click + +##Dev +coverage +flake8 +numpy +pre-commit +black +sourcery +isort +gitpython==3.1.31 + +# Items below this point will not be included in the Docker Image + +# Testing dependencies +pytest +asynctest +pytest-asyncio +pytest-benchmark +pytest-cov +pytest-integration +pytest-mock diff --git a/run.bat b/run.bat new file mode 100644 index 0000000000000000000000000000000000000000..afbab57a0603a126b04845ec754d1ecf3fdea18d --- /dev/null +++ b/run.bat @@ -0,0 +1,8 @@ +@echo off +python scripts/check_requirements.py requirements.txt +if errorlevel 1 ( + echo Installing missing packages... + pip install -r requirements.txt +) +python -m autogpt %* +pause diff --git a/run.sh b/run.sh new file mode 100755 index 0000000000000000000000000000000000000000..edcbc44155b9ca9df83e283fdf976472c13e6492 --- /dev/null +++ b/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash +python scripts/check_requirements.py requirements.txt +if [ $? -eq 1 ] +then + echo Installing missing packages... + pip install -r requirements.txt +fi +python -m autogpt $@ +read -p "Press any key to continue..." diff --git a/run_continuous.bat b/run_continuous.bat new file mode 100644 index 0000000000000000000000000000000000000000..812aa01c1c5506c452665610c0e9e83a17c426f2 --- /dev/null +++ b/run_continuous.bat @@ -0,0 +1,3 @@ +@echo off +set argument=--continuous +call run.bat %argument% diff --git a/run_continuous.sh b/run_continuous.sh new file mode 100755 index 0000000000000000000000000000000000000000..1f4436c88503172c0578b15a8447ed8268502578 --- /dev/null +++ b/run_continuous.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +./run.sh --continuous $@ diff --git a/scripts/check_requirements.py b/scripts/check_requirements.py new file mode 100644 index 0000000000000000000000000000000000000000..e4eab024a6280c0d54110c69b2e03de639325fa6 --- /dev/null +++ b/scripts/check_requirements.py @@ -0,0 +1,32 @@ +import sys + +import pkg_resources + + +def main(): + requirements_file = sys.argv[1] + with open(requirements_file, "r") as f: + required_packages = [ + line.strip().split("#")[0].strip() for line in f.readlines() + ] + + installed_packages = [package.key for package in pkg_resources.working_set] + + missing_packages = [] + for package in required_packages: + if not package: # Skip empty lines + continue + package_name = package.strip().split("==")[0] + if package_name.lower() not in installed_packages: + missing_packages.append(package_name) + + if missing_packages: + print("Missing packages:") + print(", ".join(missing_packages)) + sys.exit(1) + else: + print("All packages are installed.") + + +if __name__ == "__main__": + main() diff --git a/tests.py b/tests.py new file mode 100644 index 0000000000000000000000000000000000000000..62f76da8ac4925ef6cdfcce0484612cf70959862 --- /dev/null +++ b/tests.py @@ -0,0 +1,21 @@ +import unittest + +import coverage + +if __name__ == "__main__": + # Start coverage collection + cov = coverage.Coverage() + cov.start() + + # Load all tests from the 'autogpt/tests' package + suite = unittest.defaultTestLoader.discover("./tests") + + # Run the tests + unittest.TextTestRunner().run(suite) + + # Stop coverage collection + cov.stop() + cov.save() + + # Report the coverage + cov.report(show_missing=True) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/browse_tests.py b/tests/browse_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..f896e7dd751b1b661d5e989909448b7e182eab69 --- /dev/null +++ b/tests/browse_tests.py @@ -0,0 +1,26 @@ +import os +import sys +import unittest + +from bs4 import BeautifulSoup + +sys.path.append(os.path.abspath("../scripts")) + +from browse import extract_hyperlinks + + +class TestBrowseLinks(unittest.TestCase): + def test_extract_hyperlinks(self): + body = """ + + Google + Foo +
Some other crap
+ + """ + soup = BeautifulSoup(body, "html.parser") + links = extract_hyperlinks(soup, "http://example.com") + self.assertEqual( + links, + [("Google", "https://google.com"), ("Foo", "http://example.com/foo.html")], + ) diff --git a/tests/context.py b/tests/context.py new file mode 100644 index 0000000000000000000000000000000000000000..cef969db69ab189109b935bba9ed06696cf5337a --- /dev/null +++ b/tests/context.py @@ -0,0 +1,6 @@ +import os +import sys + +sys.path.insert( + 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../scripts")) +) diff --git a/tests/integration/memory_tests.py b/tests/integration/memory_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..eead2da1cfa9b8a99592939623955808fc430068 --- /dev/null +++ b/tests/integration/memory_tests.py @@ -0,0 +1,49 @@ +import random +import string +import sys +import unittest +from pathlib import Path + +from autogpt.config import Config +from autogpt.memory.local import LocalCache + + +class TestLocalCache(unittest.TestCase): + def random_string(self, length): + return "".join(random.choice(string.ascii_letters) for _ in range(length)) + + def setUp(self): + cfg = cfg = Config() + self.cache = LocalCache(cfg) + self.cache.clear() + + # Add example texts to the cache + self.example_texts = [ + "The quick brown fox jumps over the lazy dog", + "I love machine learning and natural language processing", + "The cake is a lie, but the pie is always true", + "ChatGPT is an advanced AI model for conversation", + ] + + for text in self.example_texts: + self.cache.add(text) + + # Add some random strings to test noise + for _ in range(5): + self.cache.add(self.random_string(10)) + + def test_get_relevant(self): + query = "I'm interested in artificial intelligence and NLP" + k = 3 + relevant_texts = self.cache.get_relevant(query, k) + + print(f"Top {k} relevant texts for the query '{query}':") + for i, text in enumerate(relevant_texts, start=1): + print(f"{i}. {text}") + + self.assertEqual(len(relevant_texts), k) + self.assertIn(self.example_texts[1], relevant_texts) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/integration/milvus_memory_tests.py b/tests/integration/milvus_memory_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..ec38bf2f72087b5da679d26594ebff97d8a09b19 --- /dev/null +++ b/tests/integration/milvus_memory_tests.py @@ -0,0 +1,57 @@ +# sourcery skip: snake-case-functions +"""Tests for the MilvusMemory class.""" +import random +import string +import unittest + +from autogpt.config import Config +from autogpt.memory.milvus import MilvusMemory + +try: + + class TestMilvusMemory(unittest.TestCase): + """Tests for the MilvusMemory class.""" + + def random_string(self, length: int) -> str: + """Generate a random string of the given length.""" + return "".join(random.choice(string.ascii_letters) for _ in range(length)) + + def setUp(self) -> None: + """Set up the test environment.""" + cfg = Config() + cfg.milvus_addr = "localhost:19530" + self.memory = MilvusMemory(cfg) + self.memory.clear() + + # Add example texts to the cache + self.example_texts = [ + "The quick brown fox jumps over the lazy dog", + "I love machine learning and natural language processing", + "The cake is a lie, but the pie is always true", + "ChatGPT is an advanced AI model for conversation", + ] + + for text in self.example_texts: + self.memory.add(text) + + # Add some random strings to test noise + for _ in range(5): + self.memory.add(self.random_string(10)) + + def test_get_relevant(self) -> None: + """Test getting relevant texts from the cache.""" + query = "I'm interested in artificial intelligence and NLP" + num_relevant = 3 + relevant_texts = self.memory.get_relevant(query, num_relevant) + + print(f"Top {k} relevant texts for the query '{query}':") + for i, text in enumerate(relevant_texts, start=1): + print(f"{i}. {text}") + + self.assertEqual(len(relevant_texts), k) + self.assertIn(self.example_texts[1], relevant_texts) + +except: + print( + "Skipping tests/integration/milvus_memory_tests.py as Milvus is not installed." + ) diff --git a/tests/integration/weaviate_memory_tests.py b/tests/integration/weaviate_memory_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..015eab05484f485aeb8ee035e92ad7811e9dddd4 --- /dev/null +++ b/tests/integration/weaviate_memory_tests.py @@ -0,0 +1,117 @@ +import os +import sys +import unittest +from unittest import mock +from uuid import uuid4 + +from weaviate import Client +from weaviate.util import get_valid_uuid + +from autogpt.config import Config +from autogpt.memory.base import get_ada_embedding +from autogpt.memory.weaviate import WeaviateMemory + + +class TestWeaviateMemory(unittest.TestCase): + cfg = None + client = None + index = None + + @classmethod + def setUpClass(cls): + # only create the connection to weaviate once + cls.cfg = Config() + + if cls.cfg.use_weaviate_embedded: + from weaviate.embedded import EmbeddedOptions + + cls.client = Client( + embedded_options=EmbeddedOptions( + hostname=cls.cfg.weaviate_host, + port=int(cls.cfg.weaviate_port), + persistence_data_path=cls.cfg.weaviate_embedded_path, + ) + ) + else: + cls.client = Client( + f"{cls.cfg.weaviate_protocol}://{cls.cfg.weaviate_host}:{self.cfg.weaviate_port}" + ) + + cls.index = WeaviateMemory.format_classname(cls.cfg.memory_index) + + """ + In order to run these tests you will need a local instance of + Weaviate running. Refer to https://weaviate.io/developers/weaviate/installation/docker-compose + for creating local instances using docker. + Alternatively in your .env file set the following environmental variables to run Weaviate embedded (see: https://weaviate.io/developers/weaviate/installation/embedded): + + USE_WEAVIATE_EMBEDDED=True + WEAVIATE_EMBEDDED_PATH="/home/me/.local/share/weaviate" + """ + + def setUp(self): + try: + self.client.schema.delete_class(self.index) + except: + pass + + self.memory = WeaviateMemory(self.cfg) + + def test_add(self): + doc = "You are a Titan name Thanos and you are looking for the Infinity Stones" + self.memory.add(doc) + result = self.client.query.get(self.index, ["raw_text"]).do() + actual = result["data"]["Get"][self.index] + + self.assertEqual(len(actual), 1) + self.assertEqual(actual[0]["raw_text"], doc) + + def test_get(self): + doc = "You are an Avenger and swore to defend the Galaxy from a menace called Thanos" + + with self.client.batch as batch: + batch.add_data_object( + uuid=get_valid_uuid(uuid4()), + data_object={"raw_text": doc}, + class_name=self.index, + vector=get_ada_embedding(doc), + ) + + batch.flush() + + actual = self.memory.get(doc) + + self.assertEqual(len(actual), 1) + self.assertEqual(actual[0], doc) + + def test_get_stats(self): + docs = [ + "You are now about to count the number of docs in this index", + "And then you about to find out if you can count correctly", + ] + + [self.memory.add(doc) for doc in docs] + + stats = self.memory.get_stats() + + self.assertTrue(stats) + self.assertTrue("count" in stats) + self.assertEqual(stats["count"], 2) + + def test_clear(self): + docs = [ + "Shame this is the last test for this class", + "Testing is fun when someone else is doing it", + ] + + [self.memory.add(doc) for doc in docs] + + self.assertEqual(self.memory.get_stats()["count"], 2) + + self.memory.clear() + + self.assertEqual(self.memory.get_stats()["count"], 0) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_cache_test.py b/tests/local_cache_test.py new file mode 100644 index 0000000000000000000000000000000000000000..bb10862656bb500f319ac231ff5bd5438d6fe7e2 --- /dev/null +++ b/tests/local_cache_test.py @@ -0,0 +1,67 @@ +# sourcery skip: snake-case-functions +"""Tests for LocalCache class""" +import os +import sys +import unittest + +import pytest + +from autogpt.memory.local import LocalCache + + +def mock_config() -> dict: + """Mock the Config class""" + return type( + "MockConfig", + (object,), + { + "debug_mode": False, + "continuous_mode": False, + "speak_mode": False, + "memory_index": "auto-gpt", + }, + ) + + +@pytest.mark.integration_test +class TestLocalCache(unittest.TestCase): + """Tests for LocalCache class""" + + def setUp(self) -> None: + """Set up the test environment""" + self.cfg = mock_config() + self.cache = LocalCache(self.cfg) + + def test_add(self) -> None: + """Test adding a text to the cache""" + text = "Sample text" + self.cache.add(text) + self.assertIn(text, self.cache.data.texts) + + def test_clear(self) -> None: + """Test clearing the cache""" + self.cache.clear() + self.assertEqual(self.cache.data.texts, []) + + def test_get(self) -> None: + """Test getting a text from the cache""" + text = "Sample text" + self.cache.add(text) + result = self.cache.get(text) + self.assertEqual(result, [text]) + + def test_get_relevant(self) -> None: + """Test getting relevant texts from the cache""" + text1 = "Sample text 1" + text2 = "Sample text 2" + self.cache.add(text1) + self.cache.add(text2) + result = self.cache.get_relevant(text1, 1) + self.assertEqual(result, [text1]) + + def test_get_stats(self) -> None: + """Test getting the cache stats""" + text = "Sample text" + self.cache.add(text) + stats = self.cache.get_stats() + self.assertEqual(stats, (4, self.cache.data.embeddings.shape)) diff --git a/tests/milvus_memory_test.py b/tests/milvus_memory_test.py new file mode 100644 index 0000000000000000000000000000000000000000..84fd6e6d5006e781fa5e1065f949b2160537d913 --- /dev/null +++ b/tests/milvus_memory_test.py @@ -0,0 +1,72 @@ +# sourcery skip: snake-case-functions +"""Tests for the MilvusMemory class.""" +import os +import sys +import unittest + +try: + from autogpt.memory.milvus import MilvusMemory + + def mock_config() -> dict: + """Mock the Config class""" + return type( + "MockConfig", + (object,), + { + "debug_mode": False, + "continuous_mode": False, + "speak_mode": False, + "milvus_collection": "autogpt", + "milvus_addr": "localhost:19530", + }, + ) + + class TestMilvusMemory(unittest.TestCase): + """Tests for the MilvusMemory class.""" + + def setUp(self) -> None: + """Set up the test environment""" + self.cfg = mock_config() + self.memory = MilvusMemory(self.cfg) + + def test_add(self) -> None: + """Test adding a text to the cache""" + text = "Sample text" + self.memory.clear() + self.memory.add(text) + result = self.memory.get(text) + self.assertEqual([text], result) + + def test_clear(self) -> None: + """Test clearing the cache""" + self.memory.clear() + self.assertEqual(self.memory.collection.num_entities, 0) + + def test_get(self) -> None: + """Test getting a text from the cache""" + text = "Sample text" + self.memory.clear() + self.memory.add(text) + result = self.memory.get(text) + self.assertEqual(result, [text]) + + def test_get_relevant(self) -> None: + """Test getting relevant texts from the cache""" + text1 = "Sample text 1" + text2 = "Sample text 2" + self.memory.clear() + self.memory.add(text1) + self.memory.add(text2) + result = self.memory.get_relevant(text1, 1) + self.assertEqual(result, [text1]) + + def test_get_stats(self) -> None: + """Test getting the cache stats""" + text = "Sample text" + self.memory.clear() + self.memory.add(text) + stats = self.memory.get_stats() + self.assertEqual(15, len(stats)) + +except: + print("Milvus not installed, skipping tests") diff --git a/tests/smoke_test.py b/tests/smoke_test.py new file mode 100644 index 0000000000000000000000000000000000000000..1b9d643fc21f3703384a2bb4f2bd1d725f4dd418 --- /dev/null +++ b/tests/smoke_test.py @@ -0,0 +1,59 @@ +"""Smoke test for the autogpt package.""" +import os +import subprocess +import sys + +import pytest + +from autogpt.commands.file_operations import delete_file, read_file + + +@pytest.mark.integration_test +def test_write_file() -> None: + """ + Test case to check if the write_file command can successfully write 'Hello World' to a file + named 'hello_world.txt'. + + Read the current ai_settings.yaml file and store its content. + """ + env_vars = {"MEMORY_BACKEND": "no_memory", "TEMPERATURE": "0"} + ai_settings = None + if os.path.exists("ai_settings.yaml"): + with open("ai_settings.yaml", "r") as f: + ai_settings = f.read() + os.remove("ai_settings.yaml") + + try: + if os.path.exists("hello_world.txt"): + # Clean up any existing 'hello_world.txt' file before testing. + delete_file("hello_world.txt") + # Prepare input data for the test. + input_data = """write_file-GPT +an AI designed to use the write_file command to write 'Hello World' into a file named "hello_world.txt" and then use the task_complete command to complete the task. +Use the write_file command to write 'Hello World' into a file named "hello_world.txt". +Use the task_complete command to complete the task. +Do not use any other commands. + +y -5 +EOF""" + command = f"{sys.executable} -m autogpt" + + # Execute the script with the input data. + process = subprocess.Popen( + command, + stdin=subprocess.PIPE, + shell=True, + env={**os.environ, **env_vars}, + ) + process.communicate(input_data.encode()) + + # Read the content of the 'hello_world.txt' file created during the test. + content = read_file("hello_world.txt") + finally: + if ai_settings: + # Restore the original ai_settings.yaml file. + with open("ai_settings.yaml", "w") as f: + f.write(ai_settings) + + # Check if the content of the 'hello_world.txt' file is equal to 'Hello World'. + assert content == "Hello World", f"Expected 'Hello World', got {content}" diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000000000000000000000000000000000000..b472a24c78edd1f931a76c68e08ed544bbe61d98 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,84 @@ +from unittest import TestCase + +from autogpt.config import Config + + +class TestConfig(TestCase): + """ + Test cases for the Config class, which handles the configuration settings + for the AI and ensures it behaves as a singleton. + """ + + def setUp(self): + """ + Set up the test environment by creating an instance of the Config class. + """ + self.config = Config() + + def test_singleton(self): + """ + Test if the Config class behaves as a singleton by ensuring that two instances are the same. + """ + config2 = Config() + self.assertIs(self.config, config2) + + def test_initial_values(self): + """ + Test if the initial values of the Config class attributes are set correctly. + """ + self.assertFalse(self.config.debug_mode) + self.assertFalse(self.config.continuous_mode) + self.assertFalse(self.config.speak_mode) + self.assertEqual(self.config.fast_llm_model, "gpt-3.5-turbo") + self.assertEqual(self.config.smart_llm_model, "gpt-4") + self.assertEqual(self.config.fast_token_limit, 4000) + self.assertEqual(self.config.smart_token_limit, 8000) + + def test_set_continuous_mode(self): + """ + Test if the set_continuous_mode() method updates the continuous_mode attribute. + """ + self.config.set_continuous_mode(True) + self.assertTrue(self.config.continuous_mode) + + def test_set_speak_mode(self): + """ + Test if the set_speak_mode() method updates the speak_mode attribute. + """ + self.config.set_speak_mode(True) + self.assertTrue(self.config.speak_mode) + + def test_set_fast_llm_model(self): + """ + Test if the set_fast_llm_model() method updates the fast_llm_model attribute. + """ + self.config.set_fast_llm_model("gpt-3.5-turbo-test") + self.assertEqual(self.config.fast_llm_model, "gpt-3.5-turbo-test") + + def test_set_smart_llm_model(self): + """ + Test if the set_smart_llm_model() method updates the smart_llm_model attribute. + """ + self.config.set_smart_llm_model("gpt-4-test") + self.assertEqual(self.config.smart_llm_model, "gpt-4-test") + + def test_set_fast_token_limit(self): + """ + Test if the set_fast_token_limit() method updates the fast_token_limit attribute. + """ + self.config.set_fast_token_limit(5000) + self.assertEqual(self.config.fast_token_limit, 5000) + + def test_set_smart_token_limit(self): + """ + Test if the set_smart_token_limit() method updates the smart_token_limit attribute. + """ + self.config.set_smart_token_limit(9000) + self.assertEqual(self.config.smart_token_limit, 9000) + + def test_set_debug_mode(self): + """ + Test if the set_debug_mode() method updates the debug_mode attribute. + """ + self.config.set_debug_mode(True) + self.assertTrue(self.config.debug_mode) diff --git a/tests/test_image_gen.py b/tests/test_image_gen.py new file mode 100644 index 0000000000000000000000000000000000000000..19c57e427d5c1b84aa7f72925733d0056ddf5268 --- /dev/null +++ b/tests/test_image_gen.py @@ -0,0 +1,102 @@ +import hashlib +import os +import unittest + +from PIL import Image + +from autogpt.commands.image_gen import generate_image, generate_image_with_sd_webui +from autogpt.config import Config +from autogpt.workspace import path_in_workspace + + +def lst(txt): + return txt.split(":")[1].strip() + + +@unittest.skipIf(os.getenv("CI"), "Skipping image generation tests") +class TestImageGen(unittest.TestCase): + def setUp(self): + self.config = Config() + + def test_dalle(self): + self.config.image_provider = "dalle" + + # Test using size 256 + result = lst(generate_image("astronaut riding a horse", 256)) + image_path = path_in_workspace(result) + self.assertTrue(image_path.exists()) + with Image.open(image_path) as img: + self.assertEqual(img.size, (256, 256)) + image_path.unlink() + + # Test using size 512 + result = lst(generate_image("astronaut riding a horse", 512)) + image_path = path_in_workspace(result) + with Image.open(image_path) as img: + self.assertEqual(img.size, (512, 512)) + image_path.unlink() + + def test_huggingface(self): + self.config.image_provider = "huggingface" + + # Test usin SD 1.4 model and size 512 + self.config.huggingface_image_model = "CompVis/stable-diffusion-v1-4" + result = lst(generate_image("astronaut riding a horse", 512)) + image_path = path_in_workspace(result) + self.assertTrue(image_path.exists()) + with Image.open(image_path) as img: + self.assertEqual(img.size, (512, 512)) + image_path.unlink() + + # Test using SD 2.1 768 model and size 768 + self.config.huggingface_image_model = "stabilityai/stable-diffusion-2-1" + result = lst(generate_image("astronaut riding a horse", 768)) + image_path = path_in_workspace(result) + with Image.open(image_path) as img: + self.assertEqual(img.size, (768, 768)) + image_path.unlink() + + def test_sd_webui(self): + self.config.image_provider = "sd_webui" + return + + # Test using size 128 + result = lst(generate_image_with_sd_webui("astronaut riding a horse", 128)) + image_path = path_in_workspace(result) + self.assertTrue(image_path.exists()) + with Image.open(image_path) as img: + self.assertEqual(img.size, (128, 128)) + image_path.unlink() + + # Test using size 64 and negative prompt + result = lst( + generate_image_with_sd_webui( + "astronaut riding a horse", + negative_prompt="horse", + size=64, + extra={"seed": 123}, + ) + ) + image_path = path_in_workspace(result) + with Image.open(image_path) as img: + self.assertEqual(img.size, (64, 64)) + neg_image_hash = hashlib.md5(img.tobytes()).hexdigest() + image_path.unlink() + + # Same test as above but without the negative prompt + result = lst( + generate_image_with_sd_webui( + "astronaut riding a horse", image_size=64, size=1, extra={"seed": 123} + ) + ) + image_path = path_in_workspace(result) + with Image.open(image_path) as img: + self.assertEqual(img.size, (64, 64)) + image_hash = hashlib.md5(img.tobytes()).hexdigest() + image_path.unlink() + + self.assertNotEqual(image_hash, neg_image_hash) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py new file mode 100644 index 0000000000000000000000000000000000000000..41c90a6f66c0b0468f1443de80033cc4f268eca0 --- /dev/null +++ b/tests/test_json_parser.py @@ -0,0 +1,111 @@ +import unittest + +import tests.context +from autogpt.json_utils.json_fix_llm import fix_and_parse_json + + +class TestParseJson(unittest.TestCase): + def test_valid_json(self): + # Test that a valid JSON string is parsed correctly + json_str = '{"name": "John", "age": 30, "city": "New York"}' + obj = fix_and_parse_json(json_str) + self.assertEqual(obj, {"name": "John", "age": 30, "city": "New York"}) + + def test_invalid_json_minor(self): + # Test that an invalid JSON string can be fixed with gpt + json_str = '{"name": "John", "age": 30, "city": "New York",}' + with self.assertRaises(Exception): + fix_and_parse_json(json_str, try_to_fix_with_gpt=False) + + def test_invalid_json_major_with_gpt(self): + # Test that an invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' + with self.assertRaises(Exception): + fix_and_parse_json(json_str, try_to_fix_with_gpt=False) + + def test_invalid_json_major_without_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' + # Assert that this raises an exception: + with self.assertRaises(Exception): + fix_and_parse_json(json_str, try_to_fix_with_gpt=False) + + def test_invalid_json_leading_sentence_with_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = """I suggest we start by browsing the repository to find any issues that we can fix. + +{ + "command": { + "name": "browse_website", + "args":{ + "url": "https://github.com/Torantulino/Auto-GPT" + } + }, + "thoughts": + { + "text": "I suggest we start browsing the repository to find any issues that we can fix.", + "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.", + "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes", + "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.", + "speak": "I will start browsing the repository to find any issues we can fix." + } +}""" + good_obj = { + "command": { + "name": "browse_website", + "args": {"url": "https://github.com/Torantulino/Auto-GPT"}, + }, + "thoughts": { + "text": "I suggest we start browsing the repository to find any issues that we can fix.", + "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.", + "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes", + "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.", + "speak": "I will start browsing the repository to find any issues we can fix.", + }, + } + # Assert that this raises an exception: + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj + ) + + def test_invalid_json_leading_sentence_with_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = """I will first need to browse the repository (https://github.com/Torantulino/Auto-GPT) and identify any potential bugs that need fixing. I will use the "browse_website" command for this. + +{ + "command": { + "name": "browse_website", + "args":{ + "url": "https://github.com/Torantulino/Auto-GPT" + } + }, + "thoughts": + { + "text": "Browsing the repository to identify potential bugs", + "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.", + "plan": "- Analyze the repository for potential bugs and areas of improvement", + "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.", + "speak": "I am browsing the repository to identify potential bugs." + } +}""" + good_obj = { + "command": { + "name": "browse_website", + "args": {"url": "https://github.com/Torantulino/Auto-GPT"}, + }, + "thoughts": { + "text": "Browsing the repository to identify potential bugs", + "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.", + "plan": "- Analyze the repository for potential bugs and areas of improvement", + "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.", + "speak": "I am browsing the repository to identify potential bugs.", + }, + } + # Assert that this raises an exception: + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_prompt_generator.py b/tests/test_prompt_generator.py new file mode 100644 index 0000000000000000000000000000000000000000..6a0bfd6c7bbdbfaa3750e9dee621bd25e17a448b --- /dev/null +++ b/tests/test_prompt_generator.py @@ -0,0 +1,114 @@ +from unittest import TestCase + +from autogpt.promptgenerator import PromptGenerator + + +class TestPromptGenerator(TestCase): + """ + Test cases for the PromptGenerator class, which is responsible for generating + prompts for the AI with constraints, commands, resources, and performance evaluations. + """ + + @classmethod + def setUpClass(cls): + """ + Set up the initial state for each test method by creating an instance of PromptGenerator. + """ + cls.generator = PromptGenerator() + + # Test whether the add_constraint() method adds a constraint to the generator's constraints list + def test_add_constraint(self): + """ + Test if the add_constraint() method adds a constraint to the generator's constraints list. + """ + constraint = "Constraint1" + self.generator.add_constraint(constraint) + self.assertIn(constraint, self.generator.constraints) + + # Test whether the add_command() method adds a command to the generator's commands list + def test_add_command(self): + """ + Test if the add_command() method adds a command to the generator's commands list. + """ + command_label = "Command Label" + command_name = "command_name" + args = {"arg1": "value1", "arg2": "value2"} + self.generator.add_command(command_label, command_name, args) + command = { + "label": command_label, + "name": command_name, + "args": args, + } + self.assertIn(command, self.generator.commands) + + def test_add_resource(self): + """ + Test if the add_resource() method adds a resource to the generator's resources list. + """ + resource = "Resource1" + self.generator.add_resource(resource) + self.assertIn(resource, self.generator.resources) + + def test_add_performance_evaluation(self): + """ + Test if the add_performance_evaluation() method adds an evaluation to the generator's + performance_evaluation list. + """ + evaluation = "Evaluation1" + self.generator.add_performance_evaluation(evaluation) + self.assertIn(evaluation, self.generator.performance_evaluation) + + def test_generate_prompt_string(self): + """ + Test if the generate_prompt_string() method generates a prompt string with all the added + constraints, commands, resources, and evaluations. + """ + # Define the test data + constraints = ["Constraint1", "Constraint2"] + commands = [ + { + "label": "Command1", + "name": "command_name1", + "args": {"arg1": "value1"}, + }, + { + "label": "Command2", + "name": "command_name2", + "args": {}, + }, + ] + resources = ["Resource1", "Resource2"] + evaluations = ["Evaluation1", "Evaluation2"] + + # Add test data to the generator + for constraint in constraints: + self.generator.add_constraint(constraint) + for command in commands: + self.generator.add_command( + command["label"], command["name"], command["args"] + ) + for resource in resources: + self.generator.add_resource(resource) + for evaluation in evaluations: + self.generator.add_performance_evaluation(evaluation) + + # Generate the prompt string and verify its correctness + prompt_string = self.generator.generate_prompt_string() + self.assertIsNotNone(prompt_string) + + # Check if all constraints, commands, resources, and evaluations are present in the prompt string + for constraint in constraints: + self.assertIn(constraint, prompt_string) + for command in commands: + self.assertIn(command["name"], prompt_string) + for key, value in command["args"].items(): + self.assertIn(f'"{key}": "{value}"', prompt_string) + for resource in resources: + self.assertIn(resource, prompt_string) + for evaluation in evaluations: + self.assertIn(evaluation, prompt_string) + + self.assertIn("constraints", prompt_string.lower()) + self.assertIn("commands", prompt_string.lower()) + self.assertIn("resources", prompt_string.lower()) + self.assertIn("performance evaluation", prompt_string.lower()) diff --git a/tests/test_token_counter.py b/tests/test_token_counter.py new file mode 100644 index 0000000000000000000000000000000000000000..6d7ae016b2f823123b0b69b2eeb3eab50d94f00f --- /dev/null +++ b/tests/test_token_counter.py @@ -0,0 +1,63 @@ +import unittest + +import tests.context +from autogpt.token_counter import count_message_tokens, count_string_tokens + + +class TestTokenCounter(unittest.TestCase): + def test_count_message_tokens(self): + messages = [ + {"role": "user", "content": "Hello"}, + {"role": "assistant", "content": "Hi there!"}, + ] + self.assertEqual(count_message_tokens(messages), 17) + + def test_count_message_tokens_with_name(self): + messages = [ + {"role": "user", "content": "Hello", "name": "John"}, + {"role": "assistant", "content": "Hi there!"}, + ] + self.assertEqual(count_message_tokens(messages), 17) + + def test_count_message_tokens_empty_input(self): + self.assertEqual(count_message_tokens([]), 3) + + def test_count_message_tokens_invalid_model(self): + messages = [ + {"role": "user", "content": "Hello"}, + {"role": "assistant", "content": "Hi there!"}, + ] + with self.assertRaises(KeyError): + count_message_tokens(messages, model="invalid_model") + + def test_count_message_tokens_gpt_4(self): + messages = [ + {"role": "user", "content": "Hello"}, + {"role": "assistant", "content": "Hi there!"}, + ] + self.assertEqual(count_message_tokens(messages, model="gpt-4-0314"), 15) + + def test_count_string_tokens(self): + string = "Hello, world!" + self.assertEqual( + count_string_tokens(string, model_name="gpt-3.5-turbo-0301"), 4 + ) + + def test_count_string_tokens_empty_input(self): + self.assertEqual(count_string_tokens("", model_name="gpt-3.5-turbo-0301"), 0) + + def test_count_message_tokens_invalid_model(self): + messages = [ + {"role": "user", "content": "Hello"}, + {"role": "assistant", "content": "Hi there!"}, + ] + with self.assertRaises(NotImplementedError): + count_message_tokens(messages, model="invalid_model") + + def test_count_string_tokens_gpt_4(self): + string = "Hello, world!" + self.assertEqual(count_string_tokens(string, model_name="gpt-4-0314"), 4) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/json_tests.py b/tests/unit/json_tests.py new file mode 100644 index 0000000000000000000000000000000000000000..25c383377708359b5cfec28e0625343c5692f15c --- /dev/null +++ b/tests/unit/json_tests.py @@ -0,0 +1,114 @@ +import unittest + +from autogpt.json_utils.json_fix_llm import fix_and_parse_json + + +class TestParseJson(unittest.TestCase): + def test_valid_json(self): + # Test that a valid JSON string is parsed correctly + json_str = '{"name": "John", "age": 30, "city": "New York"}' + obj = fix_and_parse_json(json_str) + self.assertEqual(obj, {"name": "John", "age": 30, "city": "New York"}) + + def test_invalid_json_minor(self): + # Test that an invalid JSON string can be fixed with gpt + json_str = '{"name": "John", "age": 30, "city": "New York",}' + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=False), + {"name": "John", "age": 30, "city": "New York"}, + ) + + def test_invalid_json_major_with_gpt(self): + # Test that an invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=True), + {"name": "John", "age": 30, "city": "New York"}, + ) + + def test_invalid_json_major_without_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' + # Assert that this raises an exception: + with self.assertRaises(Exception): + fix_and_parse_json(json_str, try_to_fix_with_gpt=False) + + def test_invalid_json_leading_sentence_with_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = """I suggest we start by browsing the repository to find any issues that we can fix. + +{ + "command": { + "name": "browse_website", + "args":{ + "url": "https://github.com/Torantulino/Auto-GPT" + } + }, + "thoughts": + { + "text": "I suggest we start browsing the repository to find any issues that we can fix.", + "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.", + "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes", + "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.", + "speak": "I will start browsing the repository to find any issues we can fix." + } +}""" + good_obj = { + "command": { + "name": "browse_website", + "args": {"url": "https://github.com/Torantulino/Auto-GPT"}, + }, + "thoughts": { + "text": "I suggest we start browsing the repository to find any issues that we can fix.", + "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.", + "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes", + "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.", + "speak": "I will start browsing the repository to find any issues we can fix.", + }, + } + # Assert that this raises an exception: + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj + ) + + def test_invalid_json_leading_sentence_with_gpt(self): + # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False + json_str = """I will first need to browse the repository (https://github.com/Torantulino/Auto-GPT) and identify any potential bugs that need fixing. I will use the "browse_website" command for this. + +{ + "command": { + "name": "browse_website", + "args":{ + "url": "https://github.com/Torantulino/Auto-GPT" + } + }, + "thoughts": + { + "text": "Browsing the repository to identify potential bugs", + "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.", + "plan": "- Analyze the repository for potential bugs and areas of improvement", + "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.", + "speak": "I am browsing the repository to identify potential bugs." + } +}""" + good_obj = { + "command": { + "name": "browse_website", + "args": {"url": "https://github.com/Torantulino/Auto-GPT"}, + }, + "thoughts": { + "text": "Browsing the repository to identify potential bugs", + "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.", + "plan": "- Analyze the repository for potential bugs and areas of improvement", + "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.", + "speak": "I am browsing the repository to identify potential bugs.", + }, + } + # Assert that this raises an exception: + self.assertEqual( + fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/test_browse_scrape_links.py b/tests/unit/test_browse_scrape_links.py new file mode 100644 index 0000000000000000000000000000000000000000..0a3340e7397a997da96b8ab9828954230e1a3c20 --- /dev/null +++ b/tests/unit/test_browse_scrape_links.py @@ -0,0 +1,118 @@ +# Generated by CodiumAI + +# Dependencies: +# pip install pytest-mock +import pytest + +from autogpt.commands.web_requests import scrape_links + +""" +Code Analysis + +Objective: +The objective of the 'scrape_links' function is to scrape hyperlinks from a +given URL and return them in a formatted way. + +Inputs: +- url: a string representing the URL to be scraped. + +Flow: +1. Send a GET request to the given URL using the requests library and the user agent header from the config file. +2. Check if the response contains an HTTP error. If it does, return "error". +3. Parse the HTML content of the response using the BeautifulSoup library. +4. Remove any script and style tags from the parsed HTML. +5. Extract all hyperlinks from the parsed HTML using the 'extract_hyperlinks' function. +6. Format the extracted hyperlinks using the 'format_hyperlinks' function. +7. Return the formatted hyperlinks. + +Outputs: +- A list of formatted hyperlinks. + +Additional aspects: +- The function uses the 'requests' and 'BeautifulSoup' libraries to send HTTP +requests and parse HTML content, respectively. +- The 'extract_hyperlinks' function is called to extract hyperlinks from the parsed HTML. +- The 'format_hyperlinks' function is called to format the extracted hyperlinks. +- The function checks for HTTP errors and returns "error" if any are found. +""" + + +class TestScrapeLinks: + # Tests that the function returns a list of formatted hyperlinks when + # provided with a valid url that returns a webpage with hyperlinks. + def test_valid_url_with_hyperlinks(self): + url = "https://www.google.com" + result = scrape_links(url) + assert len(result) > 0 + assert isinstance(result, list) + assert isinstance(result[0], str) + + # Tests that the function returns correctly formatted hyperlinks when given a valid url. + def test_valid_url(self, mocker): + # Mock the requests.get() function to return a response with sample HTML containing hyperlinks + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = ( + "Google" + ) + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with a valid URL + result = scrape_links("https://www.example.com") + + # Assert that the function returns correctly formatted hyperlinks + assert result == ["Google (https://www.google.com)"] + + # Tests that the function returns "error" when given an invalid url. + def test_invalid_url(self, mocker): + # Mock the requests.get() function to return an HTTP error response + mock_response = mocker.Mock() + mock_response.status_code = 404 + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with an invalid URL + result = scrape_links("https://www.invalidurl.com") + + # Assert that the function returns "error" + assert "Error:" in result + + # Tests that the function returns an empty list when the html contains no hyperlinks. + def test_no_hyperlinks(self, mocker): + # Mock the requests.get() function to return a response with sample HTML containing no hyperlinks + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = "

No hyperlinks here

" + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with a URL containing no hyperlinks + result = scrape_links("https://www.example.com") + + # Assert that the function returns an empty list + assert result == [] + + # Tests that scrape_links() correctly extracts and formats hyperlinks from + # a sample HTML containing a few hyperlinks. + def test_scrape_links_with_few_hyperlinks(self, mocker): + # Mock the requests.get() function to return a response with a sample HTML containing hyperlinks + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = """ + + + + + + + + """ + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function being tested + result = scrape_links("https://www.example.com") + + # Assert that the function returns a list of formatted hyperlinks + assert isinstance(result, list) + assert len(result) == 3 + assert result[0] == "Google (https://www.google.com)" + assert result[1] == "GitHub (https://github.com)" + assert result[2] == "CodiumAI (https://www.codium.ai)" diff --git a/tests/unit/test_browse_scrape_text.py b/tests/unit/test_browse_scrape_text.py new file mode 100644 index 0000000000000000000000000000000000000000..fea5ebfc05d466c7cb5711b5ac10e2ea102ddc45 --- /dev/null +++ b/tests/unit/test_browse_scrape_text.py @@ -0,0 +1,98 @@ +# Generated by CodiumAI + +import requests + +from autogpt.commands.web_requests import scrape_text + +""" +Code Analysis + +Objective: +The objective of the "scrape_text" function is to scrape the text content from +a given URL and return it as a string, after removing any unwanted HTML tags and scripts. + +Inputs: +- url: a string representing the URL of the webpage to be scraped. + +Flow: +1. Send a GET request to the given URL using the requests library and the user agent header from the config file. +2. Check if the response contains an HTTP error. If it does, return an error message. +3. Use BeautifulSoup to parse the HTML content of the response and extract all script and style tags. +4. Get the text content of the remaining HTML using the get_text() method of BeautifulSoup. +5. Split the text into lines and then into chunks, removing any extra whitespace. +6. Join the chunks into a single string with newline characters between them. +7. Return the cleaned text. + +Outputs: +- A string representing the cleaned text content of the webpage. + +Additional aspects: +- The function uses the requests library and BeautifulSoup to handle the HTTP request and HTML parsing, respectively. +- The function removes script and style tags from the HTML to avoid including unwanted content in the text output. +- The function uses a generator expression to split the text into lines and chunks, which can improve performance for large amounts of text. +""" + + +class TestScrapeText: + # Tests that scrape_text() returns the expected text when given a valid URL. + def test_scrape_text_with_valid_url(self, mocker): + # Mock the requests.get() method to return a response with expected text + expected_text = "This is some sample text" + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = f"

{expected_text}

" + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with a valid URL and assert that it returns the expected text + url = "http://www.example.com" + assert scrape_text(url) == expected_text + + # Tests that the function returns an error message when an invalid or unreachable url is provided. + def test_invalid_url(self, mocker): + # Mock the requests.get() method to raise an exception + mocker.patch( + "requests.Session.get", side_effect=requests.exceptions.RequestException + ) + + # Call the function with an invalid URL and assert that it returns an error message + url = "http://www.invalidurl.com" + error_message = scrape_text(url) + assert "Error:" in error_message + + # Tests that the function returns an empty string when the html page contains no text to be scraped. + def test_no_text(self, mocker): + # Mock the requests.get() method to return a response with no text + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = "" + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with a valid URL and assert that it returns an empty string + url = "http://www.example.com" + assert scrape_text(url) == "" + + # Tests that the function returns an error message when the response status code is an http error (>=400). + def test_http_error(self, mocker): + # Mock the requests.get() method to return a response with a 404 status code + mocker.patch("requests.Session.get", return_value=mocker.Mock(status_code=404)) + + # Call the function with a URL + result = scrape_text("https://www.example.com") + + # Check that the function returns an error message + assert result == "Error: HTTP 404 error" + + # Tests that scrape_text() properly handles HTML tags. + def test_scrape_text_with_html_tags(self, mocker): + # Create a mock response object with HTML containing tags + html = "

This is bold text.

" + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.text = html + mocker.patch("requests.Session.get", return_value=mock_response) + + # Call the function with a URL + result = scrape_text("https://www.example.com") + + # Check that the function properly handles HTML tags + assert result == "This is bold text." diff --git a/tests/unit/test_chat.py b/tests/unit/test_chat.py new file mode 100644 index 0000000000000000000000000000000000000000..774f4103762c28d5a02e89c14b224fae0bc0756a --- /dev/null +++ b/tests/unit/test_chat.py @@ -0,0 +1,86 @@ +# Generated by CodiumAI +import time +import unittest +from unittest.mock import patch + +from autogpt.chat import create_chat_message, generate_context + + +class TestChat(unittest.TestCase): + # Tests that the function returns a dictionary with the correct keys and values when valid strings are provided for role and content. + def test_happy_path_role_content(self): + result = create_chat_message("system", "Hello, world!") + self.assertEqual(result, {"role": "system", "content": "Hello, world!"}) + + # Tests that the function returns a dictionary with the correct keys and values when empty strings are provided for role and content. + def test_empty_role_content(self): + result = create_chat_message("", "") + self.assertEqual(result, {"role": "", "content": ""}) + + # Tests the behavior of the generate_context function when all input parameters are empty. + @patch("time.strftime") + def test_generate_context_empty_inputs(self, mock_strftime): + # Mock the time.strftime function to return a fixed value + mock_strftime.return_value = "Sat Apr 15 00:00:00 2023" + # Arrange + prompt = "" + relevant_memory = "" + full_message_history = [] + model = "gpt-3.5-turbo-0301" + + # Act + result = generate_context(prompt, relevant_memory, full_message_history, model) + + # Assert + expected_result = ( + -1, + 47, + 3, + [ + {"role": "system", "content": ""}, + { + "role": "system", + "content": f"The current time and date is {time.strftime('%c')}", + }, + { + "role": "system", + "content": f"This reminds you of these events from your past:\n\n\n", + }, + ], + ) + self.assertEqual(result, expected_result) + + # Tests that the function successfully generates a current_context given valid inputs. + def test_generate_context_valid_inputs(self): + # Given + prompt = "What is your favorite color?" + relevant_memory = "You once painted your room blue." + full_message_history = [ + create_chat_message("user", "Hi there!"), + create_chat_message("assistant", "Hello! How can I assist you today?"), + create_chat_message("user", "Can you tell me a joke?"), + create_chat_message( + "assistant", + "Why did the tomato turn red? Because it saw the salad dressing!", + ), + create_chat_message("user", "Haha, that's funny."), + ] + model = "gpt-3.5-turbo-0301" + + # When + result = generate_context(prompt, relevant_memory, full_message_history, model) + + # Then + self.assertIsInstance(result[0], int) + self.assertIsInstance(result[1], int) + self.assertIsInstance(result[2], int) + self.assertIsInstance(result[3], list) + self.assertGreaterEqual(result[0], 0) + self.assertGreaterEqual(result[1], 0) + self.assertGreaterEqual(result[2], 0) + self.assertGreaterEqual( + len(result[3]), 3 + ) # current_context should have at least 3 messages + self.assertLessEqual( + result[1], 2048 + ) # token limit for GPT-3.5-turbo-0301 is 2048 tokens diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py new file mode 100644 index 0000000000000000000000000000000000000000..ecbac9b73bd9ad872931d77e144dd853b3d8ef64 --- /dev/null +++ b/tests/unit/test_commands.py @@ -0,0 +1,22 @@ +"""Unit tests for the commands module""" +from unittest.mock import MagicMock, patch + +import pytest + +import autogpt.agent.agent_manager as agent_manager +from autogpt.app import execute_command, list_agents, start_agent + + +@pytest.mark.integration_test +def test_make_agent() -> None: + """Test the make_agent command""" + with patch("openai.ChatCompletion.create") as mock: + obj = MagicMock() + obj.response.choices[0].messages[0].content = "Test message" + mock.return_value = obj + start_agent("Test Agent", "chat", "Hello, how are you?", "gpt2") + agents = list_agents() + assert "List of agents:\n0: chat" == agents + start_agent("Test Agent 2", "write", "Hello, how are you?", "gpt2") + agents = list_agents() + assert "List of agents:\n0: chat\n1: write" == agents diff --git a/ui/api.py b/ui/api.py new file mode 100644 index 0000000000000000000000000000000000000000..9262b6a3e1c792f24faa513292c3d0437e59318f --- /dev/null +++ b/ui/api.py @@ -0,0 +1,93 @@ +import os, sys +import utils + +FILE_DIR = os.path.dirname(os.path.abspath(__file__)) +REPO_DIR = os.path.dirname(FILE_DIR) +sys.path.append(REPO_DIR) +import threading +from functools import partial +import time + +def get_openai_api_key(): + return os.getenv("OPENAI_API_KEY") + +running_apis = [] + +class AutoAPI: + def __init__(self, openai_key, ai_name, ai_role, top_5_goals): + print(openai_key) + self.openai_key = openai_key + newline = "\n" + with open(os.path.join(REPO_DIR, "ai_settings.yaml"), "w") as f: + f.write( + f"""ai_goals: +{newline.join([f'- {goal[0]}' for goal in top_5_goals if goal[0]])} +ai_name: {ai_name} +ai_role: {ai_role} +""" + ) + + + thread = threading.Thread(target=self.client_thread) + thread.start() + self.thread = thread + self.pending_input = None + self.awaiting_input = False + self.messages = [] + self.last_message_read_index = -1 + + def add_message(self, title, content): + # print(f"{title}: {content}") + self.messages.append((title, content)) + + def client_thread(self): + os.environ["OPENAI_API_KEY"] = self.openai_key + import autogpt.config.config + from autogpt.logs import logger + from autogpt.cli import main + import autogpt.utils + from autogpt.spinner import Spinner + + + def typewriter_log(self, title="", title_color="", content="", *args, **kwargs): + self.add_message(title, content) + + def warn(self, message, title="", *args, **kwargs): + self.add_message(title, message) + + def error(self, title, message="", *args, **kwargs): + self.add_message(title, message) + + def clean_input(self, prompt=""): + self.add_message(None, prompt) + self.awaiting_input = True + while self.pending_input is None: + time.sleep(1) + pending_input = self.pending_input + self.pending_input = None + print("Sending message:", pending_input) + return pending_input + + def spinner_start(self): + self.add_message(None, "Thinking...") + + logger.typewriter_log = partial(typewriter_log, self) + logger.warn = partial(warn, self) + logger.error = partial(error, self) + autogpt.utils.clean_input = partial(clean_input, self) + Spinner.spin = partial(spinner_start, self) + + main() + + def send_message(self, message="Y"): + self.pending_input = message + self.awaiting_input = False + + def get_chatbot_response(self): + while (not self.awaiting_input) or self.last_message_read_index < len(self.messages) - 1: + if self.last_message_read_index >= len(self.messages) - 1: + time.sleep(1) + else: + self.last_message_read_index += 1 + title, content = self.messages[self.last_message_read_index] + yield (f"**{title.strip()}** " if title else "") + utils.remove_color(content).replace("\n", "
") diff --git a/ui/app.py b/ui/app.py new file mode 100644 index 0000000000000000000000000000000000000000..4adf33ecb83f8d39575d6dbe2bb71b2c3a3b430e --- /dev/null +++ b/ui/app.py @@ -0,0 +1,143 @@ +import gradio as gr +import utils +from api import AutoAPI, get_openai_api_key +import os, shutil +import json + +FILE_DIR = os.path.dirname(os.path.abspath(__file__)) +OUTPUT_DIR = os.path.join(os.path.dirname(FILE_DIR), "auto_gpt_workspace") + +CSS = """ +#chatbot {font-family: monospace;} +#files .generating {display: none;} +#files .min {min-height: 0px;} +""" + +with gr.Blocks(css=CSS) as app: + with gr.Column() as setup_pane: + gr.Markdown(f"""# Auto-GPT + 1. Duplicate this Space: Duplicate Space + 2. Enter your OpenAI API Key below. + """) + with gr.Row(): + open_ai_key = gr.Textbox( + value=get_openai_api_key(), + label="OpenAI API Key", + type="password", + ) + gr.Markdown( + "3. Fill the values below, then click 'Start'. There are example values you can load at the bottom of this page." + ) + with gr.Row(): + ai_name = gr.Textbox(label="AI Name", placeholder="e.g. Entrepreneur-GPT") + ai_role = gr.Textbox( + label="AI Role", + placeholder="e.g. an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth.", + ) + top_5_goals = gr.Dataframe( + row_count=(5, "fixed"), + col_count=(1, "fixed"), + headers=["AI Goals - Enter up to 5"], + type="array" + ) + start_btn = gr.Button("Start", variant="primary") + with open(os.path.join(FILE_DIR, "examples.json"), "r") as f: + example_values = json.load(f) + gr.Examples( + example_values, + [ai_name, ai_role, top_5_goals], + ) + with gr.Column(visible=False) as main_pane: + with gr.Row(): + with gr.Column(scale=2): + chatbot = gr.Chatbot(elem_id="chatbot") + with gr.Row(): + yes_btn = gr.Button("Yes", variant="primary", interactive=False) + consecutive_yes = gr.Slider( + 1, 10, 1, step=1, label="Consecutive Yes", interactive=False + ) + custom_response = gr.Textbox( + label="Custom Response", + placeholder="Press 'Enter' to Submit.", + interactive=False, + ) + with gr.Column(scale=1): + gr.HTML( + lambda: f""" + Generated Files +
{utils.format_directory(OUTPUT_DIR)}
+ """, every=3, elem_id="files" + ) + download_btn = gr.Button("Download All Files") + + chat_history = gr.State([[None, None]]) + api = gr.State(None) + + def start(open_ai_key, ai_name, ai_role, top_5_goals): + auto_api = AutoAPI(open_ai_key, ai_name, ai_role, top_5_goals) + return gr.Column.update(visible=False), gr.Column.update(visible=True), auto_api + + def bot_response(chat, api): + messages = [] + for message in api.get_chatbot_response(): + messages.append(message) + chat[-1][1] = "\n".join(messages) + "..." + yield chat + chat[-1][1] = "\n".join(messages) + yield chat + + def send_message(count, chat, api, message="Y"): + if message != "Y": + count = 1 + for i in range(count): + chat.append([message, None]) + yield chat, count - i + api.send_message(message) + for updated_chat in bot_response(chat, api): + yield updated_chat, count - i + + def activate_inputs(): + return { + yes_btn: gr.Button.update(interactive=True), + consecutive_yes: gr.Slider.update(interactive=True), + custom_response: gr.Textbox.update(interactive=True), + } + + def deactivate_inputs(): + return { + yes_btn: gr.Button.update(interactive=False), + consecutive_yes: gr.Slider.update(interactive=False), + custom_response: gr.Textbox.update(interactive=False), + } + + start_btn.click( + start, + [open_ai_key, ai_name, ai_role, top_5_goals], + [setup_pane, main_pane, api], + ).then(bot_response, [chat_history, api], chatbot).then( + activate_inputs, None, [yes_btn, consecutive_yes, custom_response] + ) + + yes_btn.click( + deactivate_inputs, None, [yes_btn, consecutive_yes, custom_response] + ).then( + send_message, [consecutive_yes, chat_history, api], [chatbot, consecutive_yes] + ).then( + activate_inputs, None, [yes_btn, consecutive_yes, custom_response] + ) + custom_response.submit( + deactivate_inputs, None, [yes_btn, consecutive_yes, custom_response] + ).then( + send_message, + [consecutive_yes, chat_history, api, custom_response], + [chatbot, consecutive_yes], + ).then( + activate_inputs, None, [yes_btn, consecutive_yes, custom_response] + ) + + def download_all_files(): + shutil.make_archive("outputs", "zip", OUTPUT_DIR) + + download_btn.click(download_all_files).then(None, _js=utils.DOWNLOAD_OUTPUTS_JS) + +app.queue().launch(file_directories=[OUTPUT_DIR]) diff --git a/ui/examples.json b/ui/examples.json new file mode 100644 index 0000000000000000000000000000000000000000..3f93a295043af9b9d526bd2c0aadcec2a0ae3e75 --- /dev/null +++ b/ui/examples.json @@ -0,0 +1,28 @@ +[ + [ + "Entrepreneur-GPT", + "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth", + [ + ["Increase net worth"], + ["Grow Twitter Account"], + ["Develop and manage multiple businesses autonomously"] + ] + ], + [ + "JobFinder-GPT", + "An AI that finds the most relevant job postings for a UI designer on LinkedIn and Glassdoor and applies for you.", + [ + ["Collect my existing credentials from https://www.linkedin.com/in/satyanadella."], + ["Find companies that have been hiring for UI designer roles on LinkedIn."], + ["Apply for the jobs."] + ] + ], + [ + "PaperReader-GPT", + "An AI that finds the latest AI papers on arXiV and gives one sentences summaries.", + [ + ["Find the top papers on AI on arXiv."], + ["Summarize their abstracts into a single line."] + ] + ] +] \ No newline at end of file diff --git a/ui/utils.py b/ui/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..71703e2009afac0582300f5d99a91ddec4119e04 --- /dev/null +++ b/ui/utils.py @@ -0,0 +1,31 @@ +import os +import re + +def format_directory(directory): + output = [] + def helper(directory, level, output): + files = os.listdir(directory) + for i, item in enumerate(files): + is_folder = os.path.isdir(os.path.join(directory, item)) + joiner = "├── " if i < len(files) - 1 else "└── " + item_html = item + "/" if is_folder else f"{item}" + output.append("│ " * level + joiner + item_html) + if is_folder: + helper(os.path.join(directory, item), level + 1, output) + output.append(os.path.basename(directory) + "/") + helper(directory, 1, output) + return "\n".join(output) + +DOWNLOAD_OUTPUTS_JS = """ +() => { + const a = document.createElement('a'); + a.href = 'file=outputs.zip'; + a.download = 'outputs.zip'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); +}""" + +def remove_color(text): + ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') + return ansi_escape.sub('', text) \ No newline at end of file