GPTSwarm

repository·main·Indexed 21 days ago

https://github.com/metauto-ai/gptswarm

A graph-based framework for LLM-based agents that enables customized and automatic self-organization of agent swarms with self-improvement capabilities. It supports the creation of custom operations by subclassing Node and defining agent topologies via the Graph class. The framework includes support for OpenAI, local LLMs via LM Studio, and mock backends, with integrated tools for web search and file analysis.

Tokens
5.2K
Snippets
21
Records
23
Agent score
76%

What's inside gptswarm

  1. Run tests for GPTSwarm

    main

    Use pytest to run the test suite. You can run full tests, tests using a mock LLM to avoid API costs, or specific test functions. If using Poetry, prefix commands with poetry run to automatically use the virtual environment.

    Common test patterns:

    • Mock LLM: Use -m mock_llm to run tests without calling external LLM APIs.
    • Logging: Use the -s flag to see logging output in the console.
    • Specific Tests: Use -k to filter by function name.
    # Quick no-API test (mock LLM) without logging
    pytest -m mock_llm test/
    
    # Quick no-API test (mock LLM) with logging
    pytest -s -m mock_llm test/
    
    # Standard tests without logging
    pytest test/
    
    # Standard tests with logging
    pytest -s test/
    
    # Test a specific function by name
    pytest -s test/swarm/graph/test_swarm.py -k 'test_raises'
    
    # Running via poetry
    poetry run pytest -m mock_llm
  2. Reproduce HumanEval experiments

    main

    To run node optimization that improves the demonstration examples of each node, use the experiments/run_humaneval.py script with the --learn_demonstration flag set to True.

    PYTHONPATH=. python experiments/run_humaneval.py --learn_demonstration True
  3. Run GPTSwarm with a local LLM via LM Studio

    main

    GPTSwarm supports local LLM inference using LM Studio.

    1. Download and start the LM Studio desktop app.
    2. Load a model from Huggingface.
    3. Start the local server.
    4. In your GPTSwarm code, set model_name='lmstudio' to route requests to the local server.
  4. Install GPTSwarm via Conda and Poetry

    main

    To set up the GPTSwarm development environment, clone the repository and use Conda to create a Python 3.10 environment. Then, use Poetry to install the required dependencies.

    # Clone the repository
    git clone https://github.com/metauto-ai/GPTSwarm.git
    cd GPTSwarm/
    
    # Create and activate the environment
    conda create -n swarm python=3.10
    conda activate swarm
    
    # Install dependencies using poetry
    pip install poetry
    poetry install
    git clone https://github.com/metauto-ai/GPTSwarm.git
    cd GPTSwarm/
    
    conda create -n swarm python=3.10
    conda activate swarm
    pip install poetry
    poetry install
  5. Reproduce MMLU experiments

    main

    You can run various modes for the MMLU (Massive Multitask Language Understanding) experiments using the experiments/run_mmlu.py script. Ensure PYTHONPATH=. is set to allow the script to locate the gptswarm package.

    Available modes:

    • DirectAnswer: The baseline execution.
    • FullConnectedSwarm: A fully-connected swarm ablation with a specified number of truthful agents.
    • RandomSwarm: A randomly-connected swarm ablation with a specified number of truthful agents.
    • OptimizedSwarm: The main experiment featuring optimization and eventual evaluation.
    # Run the baseline
    PYTHONPATH=. python experiments/run_mmlu.py --mode=DirectAnswer
    
    # Run fully-connected swarm ablation
    PYTHONPATH=. python experiments/run_mmlu.py --num-truthful-agents=3 --mode=FullConnectedSwarm
    
    # Run randomly-connected swarm ablation
    PYTHONPATH=. python experiments/run_mmlu.py --num-truthful-agents=3 --mode=RandomSwarm
    
    # Run the main experiment with optimization
    PYTHONPATH=. python experiments/run_mmlu.py --num-truthful-agents=3 --mode=OptimizedSwarm
  6. Package and publish GPTSwarm

    main

    To build and publish the package using Poetry, you must first configure your PyPI tokens and repository settings. This guide demonstrates publishing to TestPyPI before the main PyPI repository.

    # Configure tokens
    poetry config pypi-token.pypi "<your-token>"
    poetry config repositories.test-pypi https://test.pypi.org/legacy/
    poetry config pypi-token.test-pypi "<your-token>"
    
    # Prepare and build
    poetry version prerelease
    poetry build
    
    # Publish to TestPyPI
    poetry publish -r test-pypi
    
    # Publish to main PyPI
    poetry publish
    
    # Verify installation from TestPyPI
    pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple gptswarm==0.1.3a1
  7. Configure the package for local development

    main

    You can set up the GPTSwarm development environment using either Poetry (recommended) or Setuptools.

    Using Poetry

    Use Poetry to install the necessary dependencies and the gptswarm package into your current environment.

    Using Setuptools

    Install the package in editable mode so that changes to the repository are immediately reflected in the installed package.

    ### Using Poetry
    ```bash
    # Install needed packages and GPTSwarm
    poetry install
    
    # Install needed and uninstall packages not listed in the lock file
    poetry install --sync
    
    # Ensure dev packages are installed
    poetry install --with=dev --sync

    Using Setuptools

    # Install as a symbolic link (editable mode)
    pip install -e .
    
    # Install with developer tools
    pip install -e .[dev]
  8. Run code coverage

    main

    To measure how much of the codebase is covered by tests, use the coverage tool. This sequence erases previous results, runs the tests, generates an HTML report, and opens it in your browser.

    coverage erase
    coverage run --source=. -m pytest .
    coverage html -i
    open htmlcov/index.html
  9. Configure API keys in .env

    main

    GPTSwarm requires API keys for LLM backends and web search engines. Copy the .env.template file to .env and populate the following keys:

    • OPENAI_API_KEY: Required for the OpenAI LLM backend.
    • BING_API_KEY: For Bing Web Search.
    • GOOGLE_API_KEY: For Google Web Search.
    • SEARCHAPI_API_KEY: For SearchAPI Web Search.

    Search Engine Priority: The system automatically selects a search engine based on availability in this order:

    1. Bing API (if BING_API_KEY is set)
    2. Search API (if SEARCHAPI_API_KEY is set)
    3. Google API (if GOOGLE_API_KEY is set)
    OPENAI_API_KEY=""
    BING_API_KEY=""
    GOOGLE_API_KEY=""
    SEARCHAPI_API_KEY=""