Skip to main content

This library provides you with an easy way to orchestrate AI Agents and Swarms on SwarmZero

Project description

SwarmZero SDK

This library provides you with an easy way to create and run AI Agents and Swarms of Agents.

Join our Discord community for support and discussion.

Supported Large Language Models (LLMs)

  • OpenAI
    • o1-preview
    • o1-mini
    • gpt-4o
    • gpt-4o-mini
    • gpt-4-turbo
    • gpt-4
    • gpt-3.5-turbo
    • gpt-3.5-turbo-instruct
  • Anthropic
    • claude-3-5-sonnet-20240620
    • claude-3-opus-20240229
    • claude-3-sonnet-20240229
    • claude-3-haiku-20240307
  • MistralAI
    • mistral-large-latest
    • mistral-small-latest
    • open-mixtral-8x22b
    • codestral-latest
  • Gemini
    • gemini-1.5-flash
    • gemini-pro
    • gemini-1.5-pro
    • gemini-1.0-pro
  • Popular open source LLMs
    • llama3.2
    • llama3.1
    • llava
    • gemma2:2b
    • gemma2
    • gemma:7b
    • gemma:2b
    • gemma2:27b
    • codegemma
    • mistral-nemo
    • deepseek-coder-v2
    • phi3:mini
    • phi3:medium
    • command-r
    • command-r-plus
    • and any supported by Ollama

Project Requirements

  • Python >= 3.11

Installation

You can either directly install from pip:

pip install git+https://github.com/swarmzero/swarmzero.git@main

Or add it to your requirements.txt file:

swarmzero @ git+https://github.com/swarmzero/swarmzero@main

Optional Dependencies

To install with the optional web3 dependencies, you can specify them as follows:

pip install git+https://github.com/swarmzero/swarmzero.git@main#egg=swarmzero[web3]

Or add it to your requirements.txt file:

swarmzero[web3] @ git+https://github.com/swarmzero/swarmzero@main

Environment Setup

You need to specify an OPENAI_API_KEY in a .env file in this directory.

Make a copy of the .env.example file and rename it to .env.

Configuration Setup

To use a configuration file with your Agent, follow these steps:

  1. Create a Configuration File:

  2. Create an SDK Context:

    • Create an instance of SDKContext with the path to your configuration file.
    • The SDKContext allows you to manage configurations, resources, and utilities across your SwarmZero Agents more efficiently.
    from swarmzero.sdk_context import SDKContext
    
    sdk_context = SDKContext(config_path="./swarmzero_config.toml")
    
  3. Specify the Configuration Path:

    • When creating a Agent instance, provide the relative or absolute path to your configuration file.
    • Agent will use the configuration from the SDK Context. If you have one agent you can directly pass the config_path it will create the sdk_context for you.
    from swarmzero import Agent
    
    simple_agent = Agent(
        name="Simple Agent",
        functions=[],
        instruction="your instructions for this agent's goal",
        # sdk_context=sdk_context
        config_path="./swarmzero_config.toml" 
    )
    

Usage

First import the Agent class:

from swarmzero import Agent

Load your environment variables:

from dotenv import load_dotenv
load_dotenv()

Then create a Agent instance:

my_agent = Agent(
    name="my_agent",
    functions=[],
    instruction="your instructions for this agent's goal",
)

Then, run your agent:

my_agent.run()

Finally, call the API endpoint, /api/v1/chat, to see the result:

curl --request POST \
  --url http://localhost:8000/api/v1/chat \
  --header 'Content-Type: multipart/form-data' \
  --form 'user_id="test"' \
  --form 'session_id="test"' \
  --form 'chat_data={ "messages": [ { "role": "user", "content": "Who is Satoshi Nakamoto?" } ] }'

Adding tools

You can create tools that help your agent handle more complex tasks. Here's an example:

import os
from typing import Optional, Dict
from web3 import Web3
from swarmzero import Agent
from dotenv import load_dotenv

load_dotenv()

rpc_url = os.getenv("RPC_URL") # add an ETH Mainnet HTTP RPC URL to your `.env` file

def get_transaction_receipt(transaction_hash: str) -> Optional[Dict]:
    """
    Fetches the receipt of a specified transaction on the Ethereum blockchain and returns it as a dictionary.

    :param transaction_hash: The hash of the transaction to fetch the receipt for.
    :return: A dictionary containing the transaction receipt details, or None if the transaction cannot be found.
    """
    web3 = Web3(Web3.HTTPProvider(rpc_url))

    if not web3.is_connected():
        print("unable to connect to Ethereum")
        return None

    try:
        transaction_receipt = web3.eth.get_transaction_receipt(transaction_hash)
        return dict(transaction_receipt)
    except Exception as e:
        print(f"an error occurred: {e}")
        return None

if __name__ == "__main__":
    my_agent = Agent(
        name="my_agent",
        functions=[get_transaction_receipt]
    )

    my_agent.run()

    """
    [1] send a request:

    ```
    curl --request POST \
    --url http://localhost:8000/api/v1/chat \
    --header 'Content-Type: multipart/form-data' \
    --form 'user_id="test"' \
    --form 'session_id="test"' \
    --form 'chat_data={ "messages": [ { "role": "user", "content": "Who is the sender of this transaction - 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060" } ] }'
    ```

    [2] result:

    The address that initiated the transaction with hash 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060 is 0xA1E4380A3B1f749673E270229993eE55F35663b4.
    """

Creating a Swarm

You can create a swarm of agents to collaborate on complex tasks. Here's an example of how to set up and use a swarm:

from swarmzero.swarm import Swarm
from swarmzero.agent import Agent
from swarmzero.sdk_context import SDKContext

import asyncio

# Create SDK Context
sdk_context = SDKContext(config_path="./swarmzero_config_example.toml")


def save_report():
    return "save_item_to_csv"


def search_on_web():
    return "search_on_web"


# Create individual agents
agent1 = Agent(name="Research Agent", instruction="Conduct research on given topics", sdk_context=sdk_context,
               functions=[search_on_web])
agent2 = Agent(name="Analysis Agent", instruction="Analyze data and provide insights", sdk_context=sdk_context,
               functions=[save_report])
agent3 = Agent(name="Report Agent", instruction="Compile findings into a report", sdk_context=sdk_context, functions=[])

# Create swarm
swarm = Swarm(name="Research Team", description="A swarm of agents that collaborate on research tasks",
              instruction="Be helpful and collaborative", functions=[], agents=[agent1, agent2, agent3])


async def chat_with_swarm():
    return await swarm.chat("Can you analyze the following data: [1, 2, 3, 4, 5]")


if __name__ == "__main__":
    asyncio.run(chat_with_swarm())

Adding Retriever

You can add retriever tools to create vector embeddings and retrieve semantic information. It will create vector index for every pdf documents under 'swarmzero-data/files/user' folder and can filter files with required_exts parameter.

  • SwarmZero agent supports ".md", '.mdx' ,".txt", '.csv', '.docx', '.pdf' file types.
  • SwarmZero agent supports 4 type of retriever (basic, chroma, pinecone-serverless, pinecone-pod) and controlled with retrieval_tool parameter.
from swarmzero import Agent
from dotenv import load_dotenv

load_dotenv()

if __name__ == "__main__":
    my_agent = Agent(
        name="retrieve-test",
        functions=[],
        retrieve = True,
        required_exts = ['.md'],
        retrieval_tool='chroma'
    )

    my_agent.run()

    """
    [1] send a request:

    ```
    curl --request POST \
    --url http://localhost:8000/api/v1/chat \
    --header 'Content-Type: multipart/form-data' \
    --form 'user_id="test"' \
    --form 'session_id="test"' \
    --form 'chat_data={ "messages": [ { "role": "user", "content": "Can you summarise the documents?" } ] }'
    ```
    """

Adding Sample Prompts

Users of your agent/swarm may not always be familiar with its abilities. Providing sample prompts allows them to explore what you have built. Here's how to add sample prompts which they can use before committing to use your agent/swarm.

Default

In your swarmzero_config.toml file, create a top level entry called [sample_prompts] and add a new array to the key prompts like this:

[sample_prompts]
prompts = [
  "What can you help me do?",
  "Which tools do you have access to?",
  "What are your capabilities?"
]

Specific agents in a swarm

[target_agent_id]
model = "gpt-3.5-turbo"
timeout = 15
environment = "dev"
enable_multi_modal = true
ollama_server_url = 'http://123.456.78.90:11434'
sample_prompts = [
    "What can you help me do?",
    "Which tools do you have access to?",
    "What are your capabilities?"
]

See ./swarmzero_config_example.toml for an example configuration file.

Tutorial

The complete tutorial can be found at ./tutorial.md.

Contributing

Setup

If you want to contribute to the codebase, you would need to set up your dev environment. Follow these steps:

  • Create a new file called .env
  • Copy the contents of .env.example into your new .env file
  • API keys for third-party tools are not provided.
    • OPENAI_API_KEY from OpenAI
  • If you don't have Poetry installed, you can install it using the following commands:
curl -sSL https://install.python-poetry.org | python3 -

export PATH="$HOME/.local/bin:$PATH"
  • Activate the Virtual Environment created by Poetry with the following command:
poetry shell
  • Install dependencies.
poetry install --no-root

Testing

  • Make sure you're in the tests/ directory:
cd tests/
  • Run the test suite:
pytest
  • Run tests for a specific module:
pytest tests/path/to/test_module.py
  • Run with verbose output:
pytest -v
  • Run with a detailed output of each test (including print statements):
pytest -s
  • Run with coverage report:
pip install coverage pytest-cov
pytest  --cov --cov-report=html

Reports file tests/htmlcov/index.html

API Doc

Open http://localhost:8000/docs with your browser to see the Swagger UI of the API.

Learn More

https://swarmzero.ai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swarmzero-0.0.1.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swarmzero-0.0.1-py3-none-any.whl (60.3 kB view details)

Uploaded Python 3

File details

Details for the file swarmzero-0.0.1.tar.gz.

File metadata

  • Download URL: swarmzero-0.0.1.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.10 Darwin/23.6.0

File hashes

Hashes for swarmzero-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fba0e9f55454384c277a6ff0a1b93d47245d2f9b66d7a5b279e3dd7d9891323f
MD5 6d820cb38a3850da28a9f174b7811539
BLAKE2b-256 86b4d43141e68bc24dd4b7b5ab22968154ec8ad6505d330f2b8336dc2554fcbd

See more details on using hashes here.

File details

Details for the file swarmzero-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: swarmzero-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 60.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.10 Darwin/23.6.0

File hashes

Hashes for swarmzero-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e33e1833e6f3eb358a675cd5ad28628e9950642ec147484ce031b5fa5198488
MD5 45803c3737c7e2470aa0de6734fe4f0f
BLAKE2b-256 5488e264db657b1eb33738b76c6ece7b8da94e565ce42b136b1f999ba22f8c55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page