Merge branch 'kyegomez:master' into master

pull/382/head
pliny 1 year ago committed by GitHub
commit 616b3b2723
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -40,7 +40,7 @@ jobs:
# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
- name: Run Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@5cc54a75f9ad88159bb54046196d920e40e367a5
uses: codacy/codacy-analysis-cli-action@33d455949345bddfdb845fba76b57b70cc83754b
with:
# Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository
# You can also omit the token and run the tools that support default configurations

@ -25,7 +25,7 @@ jobs:
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@2f6f737ca5f74c637829c0f5c3acd0e29ea5e8bf
uses: pypa/gh-action-pypi-publish@e53eb8b103ffcb59469888563dc324e3c8ba6f06
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

4
.gitignore vendored

@ -11,11 +11,13 @@ dataframe/
static/generated
runs
chroma
Unit Testing Agent_state.json
swarms/__pycache__
venv
.DS_Store
Cargo.lock
.DS_STORE
Cargo.lock
swarms/agents/.DS_Store
_build

@ -0,0 +1,15 @@
[package]
name = "swarms-runtime" # The name of your project
version = "0.1.0" # The current version, adhering to semantic versioning
edition = "2021" # Specifies which edition of Rust you're using, e.g., 2018 or 2021
authors = ["Your Name <your.email@example.com>"] # Optional: specify the package authors
license = "MIT" # Optional: the license for your project
description = "A brief description of my project" # Optional: a short description of your project
[dependencies]
cpython = "0.5"
rayon = "1.5"
[dependencies.pyo3]
version = "0.20.3"
features = ["extension-module", "auto-initialize"]

@ -0,0 +1,23 @@
# Use an official CUDA runtime as a parent image
FROM nvidia/cuda:11.4.2-runtime-ubuntu20.04
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN apt-get update && apt-get install -y \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN pip3 install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
# ENV NAME World
# Run app.py when the container launches
CMD ["python3", "example.py"]

@ -28,7 +28,7 @@ Run example in Collab: <a target="_blank" href="https://colab.research.google.co
</a>
### `Agent`
A fully plug in and play Autonomous agent powered by an LLM extended by a long term memory database, and equipped with function calling for tool usage! By passing in an LLM you can create a fully autonomous agent with extreme customization and reliability ready for real-world task automation!
A fully plug-and-play autonomous agent powered by an LLM extended by a long-term memory database, and equipped with function calling for tool usage! By passing in an LLM, you can create a fully autonomous agent with extreme customization and reliability, ready for real-world task automation!
Features:
@ -44,7 +44,7 @@ import os
from dotenv import load_dotenv
# Import the OpenAIChat model and the Agent struct
from swarms import OpenAIChat, Agent
from swarms import Agent, OpenAIChat
# Load the environment variables
load_dotenv()
@ -54,10 +54,7 @@ api_key = os.environ.get("OPENAI_API_KEY")
# Initialize the language model
llm = OpenAIChat(
temperature=0.5,
model_name="gpt-4",
openai_api_key=api_key,
max_tokens=4000
temperature=0.5, model_name="gpt-4", openai_api_key=api_key, max_tokens=4000
)
@ -66,25 +63,17 @@ agent = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True)
# Run the workflow on a task
agent.run("Generate a 10,000 word blog on health and wellness.")
```
### `ToolAgent`
ToolAgent is an agent that outputs JSON using any model from huggingface. It takes in an example schema with fields and then you provide it with a simple task and it'll output json! Perfect for function calling, parallel, and multi-step tool usage!
✅ Versatility: The ToolAgent class is designed to be flexible and adaptable. It can be used with any model and tokenizer, making it suitable for a wide range of tasks. This versatility means that you can use ToolAgent as a foundation for any tool that requires language model processing.
✅ Ease of Use: With its simple and intuitive interface, ToolAgent makes it easy to perform complex tasks. Just initialize it with your model, tokenizer, and JSON schema, and then call the run method with your task. This ease of use allows you to focus on your task, not on setting up your tools.
✅ Customizability: ToolAgent accepts variable length arguments and keyword arguments, allowing you to customize its behavior to suit your needs. Whether you need to adjust the temperature of the model's output, limit the number of tokens, or tweak any other parameter, ToolAgent has you covered. This customizability ensures that ToolAgent can adapt to your specific requirements.
ToolAgent is an agent that outputs JSON using any model from huggingface. It takes an example schema and performs a task, outputting JSON. It is versatile, easy to use, and customizable.
```python
# Import necessary libraries
from transformers import AutoModelForCausalLM, AutoTokenizer
from swarms import ToolAgent
# Load the pre-trained model and tokenizer
@ -113,13 +102,11 @@ generated_data = agent.run(task)
# Print the generated data
print(generated_data)
```
### `Worker`
The `Worker` is a simple all-in-one agent equipped with an LLM, tools, and RAG. Get started below:
The `Worker` is a simple all-in-one agent equipped with an LLM, tools, and RAG for low level tasks.
✅ Plug in and Play LLM. Utilize any LLM from anywhere and any framework
@ -130,8 +117,10 @@ The `Worker` is a simple all-in-one agent equipped with an LLM, tools, and RAG.
```python
# Importing necessary modules
import os
from dotenv import load_dotenv
from swarms import Worker, OpenAIChat, tool
from swarms import OpenAIChat, Worker, tool
# Loading environment variables from .env file
load_dotenv()
@ -157,14 +146,10 @@ worker = Worker(
)
# Running the worker with a prompt
out = worker.run(
"Hello, how are you? Create an image of how your are doing!"
)
out = worker.run("Hello, how are you? Create an image of how your are doing!")
# Printing the output
print(out)
```
------
@ -181,9 +166,11 @@ Sequential Workflow enables you to sequentially execute tasks with `Agent` and t
```python
import os
from swarms import OpenAIChat, Agent, SequentialWorkflow
from dotenv import load_dotenv
from swarms import Agent, OpenAIChat, SequentialWorkflow
load_dotenv()
# Load the environment variables
@ -192,10 +179,7 @@ api_key = os.getenv("OPENAI_API_KEY")
# Initialize the language agent
llm = OpenAIChat(
temperature=0.5,
model_name="gpt-4",
openai_api_key=api_key,
max_tokens=4000
temperature=0.5, model_name="gpt-4", openai_api_key=api_key, max_tokens=4000
)
@ -213,12 +197,14 @@ workflow = SequentialWorkflow(max_loops=1)
# Add tasks to the workflow
workflow.add(
agent1, "Generate a 10,000 word blog on health and wellness.",
agent1,
"Generate a 10,000 word blog on health and wellness.",
)
# Suppose the next task takes the output of the first task as input
workflow.add(
agent2, "Summarize the generated blog",
agent2,
"Summarize the generated blog",
)
# Run the workflow
@ -237,8 +223,10 @@ for task in workflow.tasks:
```python
import os
from dotenv import load_dotenv
from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent
from swarms import Agent, ConcurrentWorkflow, OpenAIChat, Task
# Load environment variables from .env file
load_dotenv()
@ -260,7 +248,6 @@ workflow.add(tasks=[task1, task2, task3])
# Run the workflow
workflow.run()
```
### `RecursiveWorkflow`
@ -268,8 +255,10 @@ workflow.run()
```python
import os
from dotenv import load_dotenv
from swarms import OpenAIChat, Task, RecursiveWorkflow, Agent
from swarms import Agent, OpenAIChat, RecursiveWorkflow, Task
# Load environment variables from .env file
load_dotenv()
@ -293,8 +282,6 @@ workflow.add(task3)
# Run the workflow
workflow.run()
```
@ -310,7 +297,7 @@ import os
from dotenv import load_dotenv
from swarms import Anthropic, Gemini, Mixtral, OpenAIChat, ModelParallelizer
from swarms import Anthropic, Gemini, Mixtral, ModelParallelizer, OpenAIChat
load_dotenv()
@ -352,10 +339,7 @@ import os
from dotenv import load_dotenv
from swarms import (
OpenAIChat,
Conversation,
)
from swarms import Conversation, OpenAIChat
conv = Conversation(
time_enabled=True,
@ -370,6 +354,7 @@ api_key = os.environ.get("OPENAI_API_KEY")
# Initialize the language model
llm = OpenAIChat(openai_api_key=api_key, model_name="gpt-4")
# Run the language model in a loop
def interactive_conversation(llm):
conv = Conversation()
@ -378,9 +363,7 @@ def interactive_conversation(llm):
conv.add("user", user_input)
if user_input.lower() == "quit":
break
task = (
conv.return_history_as_string()
) # Get the conversation history
task = conv.return_history_as_string() # Get the conversation history
out = llm(task)
conv.add("assistant", out)
print(
@ -392,7 +375,6 @@ def interactive_conversation(llm):
# Replace with your LLM instance
interactive_conversation(llm)
```
@ -411,7 +393,7 @@ import os
from dotenv import load_dotenv
# Import the OpenAIChat model and the Agent struct
from swarms import OpenAIChat, Agent, SwarmNetwork
from swarms import Agent, OpenAIChat, SwarmNetwork
# Load the environment variables
load_dotenv()
@ -448,11 +430,8 @@ print(out)
# Run all the agents in the swarm network on a task
out = swarmnet.run_many_agents(
"Generate a 10,000 word blog on health and wellness."
)
out = swarmnet.run_many_agents("Generate a 10,000 word blog on health and wellness.")
print(out)
```
@ -519,8 +498,6 @@ else:
# Output the result of the task
print(f"Task result: {task.result}")
```
---
@ -541,14 +518,7 @@ from dotenv import load_dotenv
from transformers import AutoModelForCausalLM, AutoTokenizer
# Import the models, structs, and telemetry modules
from swarms import (
Gemini,
GPT4VisionAPI,
Mixtral,
OpenAI,
ToolAgent,
BlocksList,
)
from swarms import BlocksList, Gemini, GPT4VisionAPI, Mixtral, OpenAI, ToolAgent
# Load the environment variables
load_dotenv()
@ -558,9 +528,7 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
gemini_api_key = os.getenv("GEMINI_API_KEY")
# Tool Agent
model = AutoModelForCausalLM.from_pretrained(
"databricks/dolly-v2-12b"
)
model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b")
tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b")
json_schema = {
"type": "object",
@ -571,9 +539,7 @@ json_schema = {
"courses": {"type": "array", "items": {"type": "string"}},
},
}
toolagent = ToolAgent(
model=model, tokenizer=tokenizer, json_schema=json_schema
)
toolagent = ToolAgent(model=model, tokenizer=tokenizer, json_schema=json_schema)
# Blocks List which enables you to build custom swarms by adding classes or functions
swarm = BlocksList(
@ -625,9 +591,7 @@ blocks_by_parent_name = swarm.get_by_parent_name(swarm.name)
blocks_by_parent_type = swarm.get_by_parent_type(type(swarm).__name__)
# Get blocks by parent description
blocks_by_parent_description = swarm.get_by_parent_description(
swarm.description
)
blocks_by_parent_description = swarm.get_by_parent_description(swarm.description)
# Run the block in the swarm
inference = swarm.run_block(toolagent, "Hello World")
@ -642,19 +606,21 @@ Here's a production grade swarm ready for real-world deployment in a factory and
```python
from swarms.structs import Agent
import os
from dotenv import load_dotenv
from swarms.models import GPT4VisionAPI
from swarms.prompts.logistics import (
Efficiency_Agent_Prompt,
Health_Security_Agent_Prompt,
Quality_Control_Agent_Prompt,
Productivity_Agent_Prompt,
Quality_Control_Agent_Prompt,
Safety_Agent_Prompt,
Security_Agent_Prompt,
Sustainability_Agent_Prompt,
Efficiency_Agent_Prompt,
)
from swarms.structs import Agent
# Load ENV
load_dotenv()
@ -692,9 +658,7 @@ productivity_agent = Agent(
)
# Initiailize safety agent
safety_agent = Agent(
llm=llm, sop=Safety_Agent_Prompt, max_loops=1, multi_modal=True
)
safety_agent = Agent(llm=llm, sop=Safety_Agent_Prompt, max_loops=1, multi_modal=True)
# Init the security agent
security_agent = Agent(
@ -754,7 +718,9 @@ Run the agent with multiple modalities useful for various real-world tasks in ma
```python
# Description: This is an example of how to use the Agent class to run a multi-modal workflow
import os
from dotenv import load_dotenv
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.structs import Agent
@ -781,17 +747,11 @@ img = "assembly_line.jpg"
## Initialize the workflow
agent = Agent(
llm=llm,
max_loops="auto",
autosave=True,
dashboard=True,
multi_modal=True
llm=llm, max_loops="auto", autosave=True, dashboard=True, multi_modal=True
)
# Run the workflow on a task
agent.run(task=task, img=img)
```
---
@ -863,14 +823,10 @@ model = QwenVLMultiModal(
)
# Run the model
response = model(
"Hello, how are you?", "https://example.com/image.jpg"
)
response = model("Hello, how are you?", "https://example.com/image.jpg")
# Print the response
print(response)
```
@ -888,7 +844,6 @@ out = model.run("Analyze the reciepts in this image", "docs.jpg")
# Print the output
print(out)
```
@ -929,8 +884,6 @@ model.set_max_length(200)
# Clear the chat history of the model
model.clear_chat_history()
```
## Radically Simple AI Model APIs
@ -947,9 +900,7 @@ We provide a vast array of language and multi-modal model APIs for you to genera
from swarms.models import Anthropic
# Initialize an instance of the Anthropic class
model = Anthropic(
anthropic_api_key=""
)
model = Anthropic(anthropic_api_key="")
# Using the run method
completion_1 = model.run("What is the capital of France?")
@ -958,7 +909,6 @@ print(completion_1)
# Using the __call__ method
completion_2 = model("How far is the moon from the earth?", stop=["miles", "km"])
print(completion_2)
```
@ -970,12 +920,16 @@ from swarms.models import HuggingfaceLLM
custom_config = {
"quantize": True,
"quantization_config": {"load_in_4bit": True},
"verbose": True
"verbose": True,
}
inference = HuggingfaceLLM(model_id="NousResearch/Nous-Hermes-2-Vision-Alpha", **custom_config)
inference = HuggingfaceLLM(
model_id="NousResearch/Nous-Hermes-2-Vision-Alpha", **custom_config
)
# Generate text based on a prompt
prompt_text = "Create a list of known biggest risks of structural collapse with references"
prompt_text = (
"Create a list of known biggest risks of structural collapse with references"
)
generated_text = inference(prompt_text)
print(generated_text)
```
@ -1033,7 +987,6 @@ task = "A person is walking on the street."
# Generate the video!
video_path = zeroscope(task)
print(video_path)
```
@ -1187,11 +1140,61 @@ Join our growing community around the world, for real-time support, ideas, and d
## Discovery Call
Book a discovery call to learn how Swarms can lower your operating costs by 40% with swarms of autonomous agents in lightspeed. [Click here to book a time that works for you!](https://calendly.com/swarm-corp/30min?month=2023-11)
## Accelerate Backlog
Help us accelerate our backlog by supporting us financially! Note, we're an open source corporation and so all the revenue we generate is through donations at the moment ;)
<a href="https://polar.sh/kyegomez"><img src="https://polar.sh/embed/fund-our-backlog.svg?org=kyegomez" /></a>
## File Structure
The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`.
```sh
├── __init__.py
├── agents
├── artifacts
├── chunkers
├── cli
├── loaders
├── memory
├── models
├── prompts
├── structs
├── telemetry
├── tokenizers
├── tools
├── utils
└── workers
```
## Docker Instructions
This application uses Docker with CUDA support. To build and run the Docker container, follow these steps:
### Prerequisites
- Make sure you have [Docker installed](https://docs.docker.com/get-docker/) on your machine.
- Ensure your machine has an NVIDIA GPU and [NVIDIA Docker support](https://github.com/NVIDIA/nvidia-docker) installed.
### Building the Docker Image
To build the Docker image, navigate to the root directory containing the `Dockerfile` and run the following command:
```bash
docker build --gpus all -t swarms
```
### Running the Docker Container
To run the Docker container, use the following command:
`docker run --gpus all -p 4000:80 swarms`
Replace swarms with the name of your Docker image, and replace 4000:80 with your actual port mapping. The format is hostPort:containerPort.
Now, your application should be running with CUDA support!
## Swarm Newsletter 🤖 🤖 🤖 📧
Sign up to the Swarm newsletter to receive updates on the latest Autonomous agent research papers, step by step guides on creating multi-agent app, and much more Swarmie goodiness 😊

@ -64,6 +64,7 @@ Initialize the `llm` (Language Learning Model) with your OpenAI API key:
```python
from swarms.models import OpenAIChat
llm = OpenAIChat(
openai_api_key="Your_OpenAI_API_Key",
temperature=0.5,
@ -74,6 +75,7 @@ Initialize the bot with the `llm`:
```python
from apps.discord import Bot
bot = Bot(llm=llm)
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

@ -52,7 +52,6 @@
## **Introdution**
Swarms provides automation-as-a-service through swarms of autonomous agents that work together as a team. We enable our customers to build, deploy, and scale production-grade multi-agent applications to automate real-world tasks.
### **Vision**
Our vision for 2024 is to provide the most reliable infrastructure for deploying autonomous agents into the real world through the Swarm Cloud, our premier cloud platform for the scalable deployment of Multi-Modal Autonomous Agents. The platform focuses on delivering maximum value to users by only taking a small fee when utilizing the agents for the hosted compute power needed to host the agents.
@ -69,15 +68,33 @@ The team has thousands of hours building and optimizing autonomous agents. Leade
Key milestones: get 80K framework users in January 2024, start contracts in target verticals, introduce commercial products in 2025 with various pricing models.
### **Resources**
#### **Pre-Seed Pitch Deck**
- [Here is our pitch deck for our preseed round](https://drive.google.com/file/d/1c76gK5UIdrfN4JOSpSlvVBEOpzR9emWc/view?usp=sharing)
### **The Swarm Corporation Memo**
#### **The Swarm Corporation Memo**
To learn more about our mission, vision, plans for GTM, and much more please refer to the [Swarm Memo here](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing)
## **Financial Documents**
This section is dedicated entirely for corporate documents.
- [Cap Table](https://docs.google.com/spreadsheets/d/1wuTWbfhYaY5Xp6nSQ9R0wDtSpwSS9coHxsjKd0UbIDc/edit?usp=sharing)
- [Cashflow Prediction Sheet](https://docs.google.com/spreadsheets/d/1HQEHCIXXMHajXMl5sj8MEfcQtWfOnD7GjHtNiocpD60/edit?usp=sharing)
------
## **Product**
Swarms is an open source framework for developers in python to enable seamless, reliable, and scalable multi-agent orchestration through modularity, customization, and precision.
[Here is the official Swarms Github Page:](https://github.com/kyegomez/swarms)
- [Swarms Github Page:](https://github.com/kyegomez/swarms)
- [Swarms Memo](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit)
- [Swarms Project Board](https://github.com/users/kyegomez/projects/1)
- [Swarms Website](https://www.swarms.world/g)
### Product Growth Metrics
| Name | Description | Link |
@ -93,3 +110,4 @@ Swarms is an open source framework for developers in python to enable seamless,
| Github Traffic Metrics | Metrics related to traffic, such as views and clones on Github. | [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) |
| Issues with the framework | Current open issues for the product on Github. | [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) |

@ -1,7 +1,110 @@
This page summarizes questions we were asked on [Discord](https://discord.gg/gnWRz88eym), Hacker News, and Reddit. Feel free to post a question to [Discord](https://discord.gg/gnWRz88eym) or open a discussion on our [Github Page](https://github.com/kyegomez) or hit us up directly: [kye@apac.ai](mailto:hello@swarms.ai).
### FAQ on Swarm Intelligence and Multi-Agent Systems
## 1. How is Swarms different from LangChain?
#### What is an agent in the context of AI and swarm intelligence?
Swarms is an open source alternative to LangChain and differs in its approach to creating LLM pipelines and DAGs. In addition to agents, it uses more general-purpose DAGs and pipelines. A close proxy might be *Airflow for LLMs*. Swarms still implements chain of thought logic for prompt tasks that use "tools" but it also supports any type of input / output (images, audio, etc.).
In artificial intelligence (AI), an agent refers to an LLM with some objective to accomplish.
In swarm intelligence, each agent interacts with other agents and possibly the environment to achieve complex collective behaviors or solve problems more efficiently than individual agents could on their own.
#### What do you need Swarms at all?
Individual agents are limited by a vast array of issues such as context window loss, single task execution, hallucination, and no collaboration.
#### How does a swarm work?
A swarm works through the principles of decentralized control, local interactions, and simple rules followed by each agent. Unlike centralized systems, where a single entity dictates the behavior of all components, in a swarm, each agent makes its own decisions based on local information and interactions with nearby agents. These local interactions lead to the emergence of complex, organized behaviors or solutions at the collective level, enabling the swarm to tackle tasks efficiently.
#### Why do you need more agents in a swarm?
More agents in a swarm can enhance its problem-solving capabilities, resilience, and efficiency. With more agents:
- **Diversity and Specialization**: The swarm can leverage a wider range of skills, knowledge, and perspectives, allowing for more creative and effective solutions to complex problems.
- **Scalability**: Adding more agents can increase the swarm's capacity to handle larger tasks or multiple tasks simultaneously.
- **Robustness**: A larger number of agents enhances the system's redundancy and fault tolerance, as the failure of a few agents has a minimal impact on the overall performance of the swarm.
#### Isn't it more expensive to use more agents?
While deploying more agents can initially increase costs, especially in terms of computational resources, hosting, and potentially API usage, there are several factors and strategies that can mitigate these expenses:
- **Efficiency at Scale**: Larger swarms can often solve problems more quickly or effectively, reducing the overall computational time and resources required.
- **Optimization and Caching**: Implementing optimizations and caching strategies can reduce redundant computations, lowering the workload on individual agents and the overall system.
- **Dynamic Scaling**: Utilizing cloud services that offer dynamic scaling can ensure you only pay for the resources you need when you need them, optimizing cost-efficiency.
#### Can swarms make decisions better than individual agents?
Yes, swarms can make better decisions than individual agents for several reasons:
- **Collective Intelligence**: Swarms combine the knowledge and insights of multiple agents, leading to more informed and well-rounded decision-making processes.
- **Error Correction**: The collaborative nature of swarms allows for error checking and correction among agents, reducing the likelihood of mistakes.
- **Adaptability**: Swarms are highly adaptable to changing environments or requirements, as the collective can quickly reorganize or shift strategies based on new information.
#### How do agents in a swarm communicate?
Communication in a swarm can vary based on the design and purpose of the system but generally involves either direct or indirect interactions:
- **Direct Communication**: Agents exchange information directly through messaging, signals, or other communication protocols designed for the system.
- **Indirect Communication**: Agents influence each other through the environment, a method known as stigmergy. Actions by one agent alter the environment, which in turn influences the behavior of other agents.
#### Are swarms only useful in computational tasks?
While swarms are often associated with computational tasks, their applications extend far beyond. Swarms can be utilized in:
- **Robotics**: Coordinating multiple robots for tasks like search and rescue, exploration, or surveillance.
- **Environmental Monitoring**: Using sensor networks to monitor pollution, wildlife, or climate conditions.
- **Social Sciences**: Modeling social behaviors or economic systems to understand complex societal dynamics.
- **Healthcare**: Coordinating care strategies in hospital settings or managing pandemic responses through distributed data analysis.
#### How do you ensure the security of a swarm system?
Security in swarm systems involves:
- **Encryption**: Ensuring all communications between agents are encrypted to prevent unauthorized access or manipulation.
- **Authentication**: Implementing strict authentication mechanisms to verify the identity of each agent in the swarm.
- **Resilience to Attacks**: Designing the swarm to continue functioning effectively even if some agents are compromised or attacked, utilizing redundancy and fault tolerance strategies.
#### How do individual agents within a swarm share insights without direct learning mechanisms like reinforcement learning?
In the context of pre-trained Large Language Models (LLMs) that operate within a swarm, sharing insights typically involves explicit communication and data exchange protocols rather than direct learning mechanisms like reinforcement learning. Here's how it can work:
- **Shared Databases and Knowledge Bases**: Agents can write to and read from a shared database or knowledge base where insights, generated content, and relevant data are stored. This allows agents to benefit from the collective experience of the swarm by accessing information that other agents have contributed.
- **APIs for Information Exchange**: Custom APIs can facilitate the exchange of information between agents. Through these APIs, agents can request specific information or insights from others within the swarm, effectively sharing knowledge without direct learning.
#### How do you balance the autonomy of individual LLMs with the need for coherent collective behavior in a swarm?
Balancing autonomy with collective coherence in a swarm of LLMs involves:
- **Central Coordination Mechanism**: Implementing a lightweight central coordination mechanism that can assign tasks, distribute information, and collect outputs from individual LLMs. This ensures that while each LLM operates autonomously, their actions are aligned with the swarm's overall objectives.
- **Standardized Communication Protocols**: Developing standardized protocols for how LLMs communicate and share information ensures that even though each agent works autonomously, the information exchange remains coherent and aligned with the collective goals.
#### How do LLM swarms adapt to changing environments or tasks without machine learning techniques?
Adaptation in LLM swarms, without relying on machine learning techniques for dynamic learning, can be achieved through:
- **Dynamic Task Allocation**: A central system or distributed algorithm can dynamically allocate tasks to different LLMs based on the changing environment or requirements. This ensures that the most suitable LLMs are addressing tasks for which they are best suited as conditions change.
- **Pre-trained Versatility**: Utilizing a diverse set of pre-trained LLMs with different specialties or training data allows the swarm to select the most appropriate agent for a task as the requirements evolve.
- **In Context Learning**: In context learning is another mechanism that can be employed within LLM swarms to adapt to changing environments or tasks. This approach involves leveraging the collective knowledge and experiences of the swarm to facilitate learning and improve performance. Here's how it can work:
#### Can LLM swarms operate in physical environments, or are they limited to digital spaces?
LLM swarms primarily operate in digital spaces, given their nature as software entities. However, they can interact with physical environments indirectly through interfaces with sensors, actuaries, or other devices connected to the Internet of Things (IoT). For example, LLMs can process data from physical sensors and control devices based on their outputs, enabling applications like smart home management or autonomous vehicle navigation.
#### Without direct learning from each other, how do agents in a swarm improve over time?
Improvement over time in a swarm of pre-trained LLMs, without direct learning from each other, can be achieved through:
- **Human Feedback**: Incorporating feedback from human operators or users can guide adjustments to the usage patterns or selection criteria of LLMs within the swarm, optimizing performance based on observed outcomes.
- **Periodic Re-training and Updating**: The individual LLMs can be periodically re-trained or updated by their developers based on collective insights and feedback from their deployment within swarms. While this does not involve direct learning from each encounter, it allows the LLMs to improve over time based on aggregated experiences.
These adjustments to the FAQ reflect the specific context of pre-trained LLMs operating within a swarm, focusing on communication, coordination, and adaptation mechanisms that align with their capabilities and constraints.
#### Conclusion
Swarms represent a powerful paradigm in AI, offering innovative solutions to complex, dynamic problems through collective intelligence and decentralized control. While challenges exist, particularly regarding cost and security, strategic design and management can leverage the strengths of swarm intelligence to achieve remarkable efficiency, adaptability, and robustness in a wide range of applications.

@ -46,6 +46,7 @@ You can also specify the conversation style:
```python
from bing_chat import ConversationStyle
response = chat("Tell me a joke", style=ConversationStyle.creative)
print(response)
```

@ -111,12 +111,13 @@ from swarms.models import OpenAIChat # Zephr, Mistral
from swarms.structs import Agent
api_key = "" # Initialize the language model (LLM)
llm = OpenAIChat(openai_api_key=api_key, temperature=0.5, max_tokens=3000)# Initialize the Agent object
llm = OpenAIChat(
openai_api_key=api_key, temperature=0.5, max_tokens=3000
) # Initialize the Agent object
agent = Agent(llm=llm, max_loops=5) # Run the agent
out = agent.run("Create an financial analysis on the following metrics")
print(out)
```
### [3. Initializing the Agent Object](https://github.com/kyegomez/swarms)

@ -35,7 +35,6 @@ The abstraction provided in `revgpt.py` is designed to simplify your interaction
1. **Import the Necessary Modules:**
```python
import os
from dotenv import load_dotenv
from revgpt import AbstractChatGPT
```

@ -28,8 +28,8 @@ The provided code showcases a system built around a worker node that utilizes va
The code begins with import statements, bringing in necessary modules and classes. Key imports include the `OpenAIChat` class, which represents a language model, and several custom agents and tools from the `swarms` package.
```python
import os
import interpreter # Assuming this is a custom module
from swarms.agents.hf_agents import HFAgent
from swarms.agents.omni_modal_agent import OmniModalAgent
from swarms.models import OpenAIChat
@ -59,11 +59,7 @@ All defined tools are appended to a list called `tools`. This list is later used
```python
# Append tools to a list
tools = [
hf_agent,
omni_agent,
compile
]
tools = [hf_agent, omni_agent, compile]
```
### Initializing a Worker Node
@ -263,8 +259,6 @@ response = node.run(task)
# Print the response
print(response)
```

@ -53,8 +53,8 @@ Voila! Youre now ready to summon your Worker.
Heres a simple way to invoke the Worker and give it a task:
```python
from swarms.models import OpenAIChat
from swarms import Worker
from swarms.models import OpenAIChat
llm = OpenAIChat(
# enter your api key
@ -75,8 +75,6 @@ node = Worker(
task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."
response = node.run(task)
print(response)
```

@ -0,0 +1,55 @@
# The Limits of Individual Agents
![Reliable Agents](docs/assets/img/reliabilitythrough.png)
Individual agents have pushed the boundaries of what machines can learn and accomplish. However, despite their impressive capabilities, these agents face inherent limitations that can hinder their effectiveness in complex, real-world applications. This blog explores the critical constraints of individual agents, such as context window limits, hallucination, single-task threading, and lack of collaboration, and illustrates how multi-agent collaboration can address these limitations. In short,
- Context Window Limits
- Single Task Execution
- Hallucination
- No collaboration
#### Context Window Limits
One of the most significant constraints of individual agents, particularly in the domain of language models, is the context window limit. This limitation refers to the maximum amount of information an agent can consider at any given time. For instance, many language models can only process a fixed number of tokens (words or characters) in a single inference, restricting their ability to understand and generate responses based on longer texts. This limitation can lead to a lack of coherence in longer compositions and an inability to maintain context in extended conversations or documents.
#### Hallucination
Hallucination in AI refers to the phenomenon where an agent generates information that is not grounded in the input data or real-world facts. This can manifest as making up facts, entities, or events that do not exist or are incorrect. Hallucinations pose a significant challenge in ensuring the reliability and trustworthiness of AI-generated content, particularly in critical applications such as news generation, academic research, and legal advice.
#### Single Task Threading
Individual agents are often designed to excel at specific tasks, leveraging their architecture and training data to optimize performance in a narrowly defined domain. However, this specialization can also be a drawback, as it limits the agent's ability to multitask or adapt to tasks that fall outside its primary domain. Single-task threading means an agent may excel in language translation but struggle with image recognition or vice versa, necessitating the deployment of multiple specialized agents for comprehensive AI solutions.
#### Lack of Collaboration
Traditional AI agents operate in isolation, processing inputs and generating outputs independently. This isolation limits their ability to leverage diverse perspectives, share knowledge, or build upon the insights of other agents. In complex problem-solving scenarios, where multiple facets of a problem need to be addressed simultaneously, this lack of collaboration can lead to suboptimal solutions or an inability to tackle multifaceted challenges effectively.
# The Elegant yet Simple Solution
- ## Multi-Agent Collaboration
Recognizing the limitations of individual agents, researchers and practitioners have explored the potential of multi-agent collaboration as a means to transcend these constraints. Multi-agent systems comprise several agents that can interact, communicate, and collaborate to achieve common goals or solve complex problems. This collaborative approach offers several advantages:
#### Overcoming Context Window Limits
By dividing a large task among multiple agents, each focusing on different segments of the problem, multi-agent systems can effectively overcome the context window limits of individual agents. For instance, in processing a long document, different agents could be responsible for understanding and analyzing different sections, pooling their insights to generate a coherent understanding of the entire text.
#### Mitigating Hallucination
Through collaboration, agents can cross-verify facts and information, reducing the likelihood of hallucinations. If one agent generates a piece of information, other agents can provide checks and balances, verifying the accuracy against known data or through consensus mechanisms.
#### Enhancing Multitasking Capabilities
Multi-agent systems can tackle tasks that require a diverse set of skills by leveraging the specialization of individual agents. For example, in a complex project that involves both natural language processing and image analysis, one agent specialized in text can collaborate with another specialized in visual data, enabling a comprehensive approach to the task.
#### Facilitating Collaboration and Knowledge Sharing
Multi-agent collaboration inherently encourages the sharing of knowledge and insights, allowing agents to learn from each other and improve their collective performance. This can be particularly powerful in scenarios where iterative learning and adaptation are crucial, such as dynamic environments or tasks that evolve over time.
### Conclusion
While individual AI agents have made remarkable strides in various domains, their inherent limitations necessitate innovative approaches to unlock the full potential of artificial intelligence. Multi-agent collaboration emerges as a compelling solution, offering a pathway to transcend individual constraints through collective intelligence. By harnessing the power of collaborative AI, we can address more complex, multifaceted problems, paving the way for more versatile, efficient, and effective AI systems in the future.

@ -37,7 +37,6 @@ class AbstractAgent:
def memory(self, memory_store):
"""init memory"""
pass
def reset(self):
"""(Abstract method) Reset the agent."""
@ -82,7 +81,7 @@ agent.reset()
The `run` method allows the agent to perform a specific task.
```python
agent.run('some_task')
agent.run("some_task")
```
#### 3. `chat`
@ -90,7 +89,7 @@ agent.run('some_task')
The `chat` method enables communication with the agent through a series of messages.
```python
messages = [{'id': 1, 'text': 'Hello, agent!'}, {'id': 2, 'text': 'How are you?'}]
messages = [{"id": 1, "text": "Hello, agent!"}, {"id": 2, "text": "How are you?"}]
agent.chat(messages)
```
@ -99,7 +98,7 @@ agent.chat(messages)
The `step` method allows the agent to process a single message.
```python
agent.step('Hello, agent!')
agent.step("Hello, agent!")
```
### Asynchronous Methods

@ -60,10 +60,7 @@ The `Message` class represents a message in the agent system. Upon initializatio
Creating a `Message` object and displaying its string representation.
```python
mes = Message(
sender = "Kye",
content = "Hello! How are you?"
)
mes = Message(sender="Kye", content="Hello! How are you?")
print(mes)
```
@ -80,9 +77,7 @@ Creating a `Message` object with metadata.
```python
metadata = {"priority": "high", "category": "urgent"}
mes_with_metadata = Message(
sender = "Alice",
content = "Important update",
metadata = metadata
sender="Alice", content="Important update", metadata=metadata
)
print(mes_with_metadata)
@ -98,10 +93,7 @@ Output:
Creating a `Message` object without providing metadata.
```python
mes_no_metadata = Message(
sender = "Bob",
content = "Reminder: Meeting at 2PM"
)
mes_no_metadata = Message(sender="Bob", content="Reminder: Meeting at 2PM")
print(mes_no_metadata)
```

@ -39,10 +39,12 @@ For streaming mode, this function yields the response token by token, ensuring a
## Examples & Use Cases
Initialize the `OmniModalAgent` and communicate with it:
```python
import os
from dotenv import load_dotenv
from swarms.agents.omni_modal_agent import OmniModalAgent, OpenAIChat
from swarms.models import OpenAIChat
from dotenv import load_dotenv
import os
# Load the environment variables
load_dotenv()

@ -69,6 +69,7 @@ The `ToolAgent` class takes the following arguments:
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from swarms import ToolAgent
# Creating a model and tokenizer
@ -82,11 +83,8 @@ json_schema = {
"name": {"type": "string"},
"age": {"type": "number"},
"is_student": {"type": "boolean"},
"courses": {
"type": "array",
"items": {"type": "string"}
}
}
"courses": {"type": "array", "items": {"type": "string"}},
},
}
# Defining a task

@ -38,7 +38,7 @@ worker = Worker(
human_in_the_loop=False,
temperature=0.5,
llm=some_language_model,
openai_api_key="my_key"
openai_api_key="my_key",
)
worker.run("What's the weather in Miami?")
```

@ -69,7 +69,9 @@ from basechunker import BaseChunker, ChunkSeparator
chunker = BaseChunker()
# Text to be chunked
input_text = "This is a long text that needs to be split into smaller chunks for processing."
input_text = (
"This is a long text that needs to be split into smaller chunks for processing."
)
# Chunk the text
chunks = chunker.chunk(input_text)

@ -62,8 +62,8 @@ Let's explore how to use the `PdfChunker` class with different scenarios and app
#### Example 1: Basic Chunking
```python
from swarms.chunkers.pdf_chunker import PdfChunker
from swarms.chunkers.chunk_seperator import ChunkSeparator
from swarms.chunkers.pdf_chunker import PdfChunker
# Initialize the PdfChunker
pdf_chunker = PdfChunker()
@ -82,8 +82,8 @@ for idx, chunk in enumerate(chunks, start=1):
#### Example 2: Custom Separators
```python
from swarms.chunkers.pdf_chunker import PdfChunker
from swarms.chunkers.chunk_seperator import ChunkSeparator
from swarms.chunkers.pdf_chunker import PdfChunker
# Define custom separators for PDF chunking
custom_separators = [ChunkSeparator("\n\n"), ChunkSeparator(". ")]

@ -28,7 +28,6 @@ We have a small gallery of examples to run here, [for more check out the docs to
- Enterprise Grade + Production Grade: `Agent` is designed and optimized for automating real-world tasks at scale!
```python
from swarms.models import OpenAIChat
from swarms.structs import Agent
@ -64,9 +63,6 @@ out = agent.run("Generate a 10,000 word blog on health and wellness.")
# out = agent.print_history_and_memory()
# # out = agent.save_state("flow_state.json")
# print(out)
```
------
@ -82,9 +78,7 @@ from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
# Example usage
api_key = (
"" # Your actual API key here
)
api_key = "" # Your actual API key here
# Initialize the language agent
llm = OpenAIChat(
@ -118,7 +112,6 @@ workflow.run()
# Output the results
for task in workflow.tasks:
print(f"Task: {task.description}, Result: {task.result}")
```
---

@ -110,7 +110,9 @@ def setup(
```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")
vector_store = PgVectorVectorStore(
connection_string="your-db-connection-string", table_name="your-table-name"
)
# Set up the database with default settings
vector_store.setup()
@ -120,10 +122,14 @@ vector_store.setup()
```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")
vector_store = PgVectorVectorStore(
connection_string="your-db-connection-string", table_name="your-table-name"
)
# Set up the database with customized settings
vector_store.setup(create_schema=False, install_uuid_extension=True, install_vector_extension=True)
vector_store.setup(
create_schema=False, install_uuid_extension=True, install_vector_extension=True
)
```
### 4.2 Upserting Vectors <a name="upserting-vectors"></a>
@ -137,7 +143,7 @@ def upsert_vector(
vector_id: Optional[str] = None,
namespace: Optional[str] = None,
meta: Optional[dict] = None,
**kwargs
**kwargs,
) -> str:
"""
Inserts or updates a vector in the collection.
@ -158,7 +164,9 @@ def upsert_vector(
```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")
vector_store = PgVectorVectorStore(
connection_string="your-db-connection-string", table_name="your-table-name"
)
# Define a vector and upsert it
vector = [0.1, 0.2, 0.3, 0.4]
@ -167,10 +175,7 @@ namespace = "your-namespace"
meta = {"key1": "value1", "key2": "value2"}
vector_store.upsert_vector(
vector=vector,
vector_id=vector_id,
namespace=namespace,
meta=meta
vector=vector, vector_id=vector_id, namespace=namespace, meta=meta
)
```
@ -222,9 +227,7 @@ else:
The `load_entries` method allows you to load all vector entries from the collection, optionally filtering by namespace.
```python
def load_entries(
self, namespace: Optional[str] = None
) -> list[BaseVectorStore.Entry]:
def load_entries(self, namespace: Optional[str] = None) -> list[BaseVectorStore.Entry]:
"""
Retrieves all vector entries from the collection, optionally filtering to only those that match the provided namespace.
@ -240,7 +243,9 @@ def load_entries(
```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")
vector_store = PgVectorVectorStore(
connection_string="your-db-connection-string", table_name="your-table-name"
)
# Load all vector entries in the specified namespace
entries = vector_store.load_entries(namespace="your-namespace")
@ -266,7 +271,7 @@ def query(
namespace: Optional[str] = None,
include_vectors: bool = False,
distance_metric: str = "cosine_distance",
**kwargs
**kwargs,
) -> list[BaseVectorStore.QueryResult]:
"""
Performs a search on the collection to find vectors similar to the provided input vector,
@ -290,7 +295,9 @@ def query(
```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")
vector_store = PgVectorVectorStore(
connection_string="your-db-connection-string", table_name="your-table-name"
)
# Perform a vector query
query_string = "your-query-string"
@ -304,7 +311,7 @@ results = vector_store.query(
count=count,
namespace=namespace,
include_vectors=include_vectors,
distance_metric=distance_metric
distance_metric=distance_metric,
)
# Process the query results

@ -174,7 +174,7 @@ pv = PineconeVector(
api_key="your-api-key",
index_name="your-index-name",
environment="us-west1-gcp",
project_name="your-project-name"
project_name="your-project-name",
)
```
@ -198,12 +198,7 @@ vector_id = "unique-vector-id"
namespace = "your-namespace"
meta = {"key1": "value1", "key2": "value2"}
pv.upsert_vector(
vector=vector,
vector_id=vector_id,
namespace=namespace,
meta=meta
)
pv.upsert_vector(vector=vector, vector_id=vector_id, namespace=namespace, meta=meta)
```
### 4.4 Querying the Index <a name="querying-the-index"></a>
@ -222,7 +217,7 @@ results = pv.query(
count=count,
namespace=namespace,
include_vectors=include_vectors,
include_metadata=include_metadata
include_metadata=include_metadata,
)
# Process the query results

@ -14,7 +14,15 @@ pip install qdrant-client sentence-transformers httpx
```python
class Qdrant:
def __init__(self, api_key: str, host: str, port: int = 6333, collection_name: str = "qdrant", model_name: str = "BAAI/bge-small-en-v1.5", https: bool = True):
def __init__(
self,
api_key: str,
host: str,
port: int = 6333,
collection_name: str = "qdrant",
model_name: str = "BAAI/bge-small-en-v1.5",
https: bool = True,
):
...
```
@ -60,10 +68,7 @@ qdrant_client = Qdrant(api_key="your_api_key", host="localhost", port=6333)
### Example 2: Adding Vectors to a Collection
```python
documents = [
{"page_content": "Sample text 1"},
{"page_content": "Sample text 2"}
]
documents = [{"page_content": "Sample text 1"}, {"page_content": "Sample text 2"}]
operation_info = qdrant_client.add_vectors(documents)
print(operation_info)

@ -125,7 +125,9 @@ def update_short_term(self, index, role: str, message: str, *args, **kwargs):
##### Example: Updating a Message in Short-Term Memory
```python
memory.update_short_term(index=0, role="Updated Role", message="Updated message content.")
memory.update_short_term(
index=0, role="Updated Role", message="Updated message content."
)
```
#### 7. `clear`

@ -82,7 +82,7 @@ weaviate_client.create_collection(
{"name": "property1", "dataType": ["string"]},
{"name": "property2", "dataType": ["int"]},
],
vectorizer_config=None # Optional vectorizer configuration
vectorizer_config=None, # Optional vectorizer configuration
)
```
@ -99,8 +99,7 @@ The `add` method allows you to add an object to a specified collection in Weavia
```python
weaviate_client.add(
collection_name="my_collection",
properties={"property1": "value1", "property2": 42}
collection_name="my_collection", properties={"property1": "value1", "property2": 42}
)
```
@ -142,7 +141,7 @@ The `update` method allows you to update an object in a specified collection in
weaviate_client.update(
collection_name="my_collection",
object_id="object123",
properties={"property1": "new_value", "property2": 99}
properties={"property1": "new_value", "property2": 99},
)
```
@ -158,10 +157,7 @@ The `delete` method allows you to delete an object from a specified collection i
#### Usage
```python
weaviate_client.delete(
collection_name="my_collection",
object_id="object123"
)
weaviate_client.delete(collection_name="my_collection", object_id="object123")
```
## Examples
@ -175,28 +171,21 @@ weaviate_client.create_collection(
name="people",
properties=[
{"name": "name", "dataType": ["string"]},
{"name": "age", "dataType": ["int"]}
]
{"name": "age", "dataType": ["int"]},
],
)
```
### Example 2: Adding an Object
```python
weaviate_client.add(
collection_name="people",
properties={"name": "John", "age": 30}
)
weaviate_client.add(collection_name="people", properties={"name": "John", "age": 30})
```
### Example 3: Querying Objects
```python
results = weaviate_client.query(
collection_name="people",
query="name:John",
limit=5
)
results = weaviate_client.query(collection_name="people", query="name:John", limit=5)
```
These examples cover the basic operations of creating collections, adding objects, and querying objects using the Weaviate API Client.

@ -72,9 +72,7 @@ class Anthropic:
from swarms.models import Anthropic
# Initialize an instance of the Anthropic class
model = Anthropic(
anthropic_api_key=""
)
model = Anthropic(anthropic_api_key="")
# Using the run method
completion_1 = model.run("What is the capital of France?")

@ -149,7 +149,9 @@ model = BaseMultiModalModel(
)
# Run the model with a text task and an image URL
response = model.run("Generate a summary of this text", "https://www.example.com/image.jpg")
response = model.run(
"Generate a summary of this text", "https://www.example.com/image.jpg"
)
print(response)
```
@ -209,6 +211,7 @@ for response in responses:
```python
from swarms.models import BaseMultiModalModel
class CustomMultiModalModel(BaseMultiModalModel):
def __init__(self, model_name, custom_parameter, *args, **kwargs):
# Call the parent class constructor
@ -226,6 +229,7 @@ class CustomMultiModalModel(BaseMultiModalModel):
# You can use self.custom_parameter and other inherited attributes
pass
# Create an instance of your custom multimodal model
custom_model = CustomMultiModalModel(
model_name="your_custom_model_name",
@ -236,7 +240,9 @@ custom_model = CustomMultiModalModel(
)
# Run your custom model
response = custom_model.run("Generate a summary of this text", "https://www.example.com/image.jpg")
response = custom_model.run(
"Generate a summary of this text", "https://www.example.com/image.jpg"
)
print(response)
# Generate a summary using your custom model

@ -39,7 +39,6 @@ print(response)
```python
from swarms.models.bing_chat import BingChat
edgegpt = BingChat(cookies_path="./path/to/cookies.json")
response = edgegpt("Hello, my name is ChatGPT")
print(response)
@ -48,7 +47,9 @@ print(response)
3. Generate an image based on a text prompt:
```python
image_path = edgegpt.create_img("Sunset over mountains", output_dir="./output", auth_cookie="your_auth_cookie")
image_path = edgegpt.create_img(
"Sunset over mountains", output_dir="./output", auth_cookie="your_auth_cookie"
)
print(f"Generated image saved at {image_path}")
```
@ -59,7 +60,9 @@ from swarms.models.bing_chat import BingChat
edgegpt = BingChat(cookies_path="./path/to/cookies.json")
image_path = edgegpt.create_img("Sunset over mountains", output_dir="./output", auth_cookie="your_auth_cookie")
image_path = edgegpt.create_img(
"Sunset over mountains", output_dir="./output", auth_cookie="your_auth_cookie"
)
print(f"Generated image saved at {image_path}")
```

@ -83,7 +83,6 @@ print(generated_text)
```python
from swarms.models import BioGPT
# Initialize the BioGPT model
biogpt = BioGPT()
@ -99,7 +98,6 @@ print(features)
```python
from swarms.models import BioGPT
# Initialize the BioGPT model
biogpt = BioGPT()

@ -29,7 +29,7 @@ from swarms.models import DistilWhisperModel
model_wrapper = DistilWhisperModel()
# Initialize with a specific model ID
model_wrapper = DistilWhisperModel(model_id='distil-whisper/distil-large-v2')
model_wrapper = DistilWhisperModel(model_id="distil-whisper/distil-large-v2")
```
## Attributes
@ -62,7 +62,7 @@ Transcribes audio input synchronously.
```python
# Synchronous transcription
transcription = model_wrapper.transcribe('path/to/audio.mp3')
transcription = model_wrapper.transcribe("path/to/audio.mp3")
print(transcription)
```
@ -84,7 +84,7 @@ Transcribes audio input asynchronously.
import asyncio
# Asynchronous transcription
transcription = asyncio.run(model_wrapper.async_transcribe('path/to/audio.mp3'))
transcription = asyncio.run(model_wrapper.async_transcribe("path/to/audio.mp3"))
print(transcription)
```
@ -103,7 +103,7 @@ Simulates real-time transcription of an audio file.
```python
# Real-time transcription simulation
model_wrapper.real_time_transcribe('path/to/audio.mp3', chunk_duration=5)
model_wrapper.real_time_transcribe("path/to/audio.mp3", chunk_duration=5)
```
## Error Handling

@ -114,9 +114,11 @@ from swarms.models import HuggingfaceLLM
custom_config = {
"quantize": True,
"quantization_config": {"load_in_4bit": True},
"verbose": True
"verbose": True,
}
inference = HuggingfaceLLM(model_id="NousResearch/Nous-Hermes-2-Vision-Alpha", **custom_config)
inference = HuggingfaceLLM(
model_id="NousResearch/Nous-Hermes-2-Vision-Alpha", **custom_config
)
# Generate text based on a prompt
prompt_text = "Tell me a joke"

@ -36,7 +36,9 @@ model = Idefics()
2. Generate text based on prompts:
```python
prompts = ["User: What is in this image? https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG"]
prompts = [
"User: What is in this image? https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG"
]
response = model(prompts)
print(response)
```
@ -47,7 +49,9 @@ print(response)
from swarms.models import Idefics
model = Idefics()
prompts = ["User: What is in this image? https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG"]
prompts = [
"User: What is in this image? https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG"
]
response = model(prompts)
print(response)
```

@ -42,9 +42,10 @@ OpenAI(api_key: str, system: str = None, console: bool = True, model: str = None
**Usage Example:**
```python
from swarms import OpenAI
import asyncio
from swarms import OpenAI
chat = OpenAI(api_key="YOUR_OPENAI_API_KEY")
response = chat.generate("Hello, how can I assist you?")
@ -126,7 +127,10 @@ GooglePalm(model_name: str = "models/chat-bison-001", google_api_key: str = None
from swarms import GooglePalm
google_palm = GooglePalm()
messages = [{"role": "system", "content": "You are a helpful assistant"}, {"role": "user", "content": "Tell me a joke"}]
messages = [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Tell me a joke"},
]
response = google_palm.generate(messages)
print(response["choices"][0]["text"])

@ -30,7 +30,9 @@ kosmos = Kosmos()
2. Perform Multimodal Grounding:
```python
kosmos.multimodal_grounding("Find the red apple in the image.", "https://example.com/apple.jpg")
kosmos.multimodal_grounding(
"Find the red apple in the image.", "https://example.com/apple.jpg"
)
```
### Example 1 - Multimodal Grounding
@ -40,13 +42,17 @@ from swarms.models.kosmos_two import Kosmos
kosmos = Kosmos()
kosmos.multimodal_grounding("Find the red apple in the image.", "https://example.com/apple.jpg")
kosmos.multimodal_grounding(
"Find the red apple in the image.", "https://example.com/apple.jpg"
)
```
3. Perform Referring Expression Comprehension:
```python
kosmos.referring_expression_comprehension("Show me the green bottle.", "https://example.com/bottle.jpg")
kosmos.referring_expression_comprehension(
"Show me the green bottle.", "https://example.com/bottle.jpg"
)
```
### Example 2 - Referring Expression Comprehension
@ -56,13 +62,17 @@ from swarms.models.kosmos_two import Kosmos
kosmos = Kosmos()
kosmos.referring_expression_comprehension("Show me the green bottle.", "https://example.com/bottle.jpg")
kosmos.referring_expression_comprehension(
"Show me the green bottle.", "https://example.com/bottle.jpg"
)
```
4. Generate Referring Expressions:
```python
kosmos.referring_expression_generation("It is on the table.", "https://example.com/table.jpg")
kosmos.referring_expression_generation(
"It is on the table.", "https://example.com/table.jpg"
)
```
### Example 3 - Referring Expression Generation
@ -72,7 +82,9 @@ from swarms.models.kosmos_two import Kosmos
kosmos = Kosmos()
kosmos.referring_expression_generation("It is on the table.", "https://example.com/table.jpg")
kosmos.referring_expression_generation(
"It is on the table.", "https://example.com/table.jpg"
)
```
5. Perform Grounded Visual Question Answering (VQA):
@ -127,7 +139,10 @@ kosmos.grounded_image_captioning_detailed("https://example.com/beach.jpg")
```python
image = kosmos.get_image("https://example.com/image.jpg")
entities = [("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]), ("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)])]
entities = [
("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]),
("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)]),
]
kosmos.draw_entity_boxes_on_image(image, entities, show=True)
```
@ -139,24 +154,38 @@ from swarms.models.kosmos_two import Kosmos
kosmos = Kosmos()
image = kosmos.get_image("https://example.com/image.jpg")
entities = [("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]), ("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)])]
entities = [
("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]),
("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)]),
]
kosmos.draw_entity_boxes_on_image(image, entities, show=True)
```
9. Generate Boxes for Entities:
```python
entities = [("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]), ("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)])]
image = kosmos.generate_boxes("Find the apple and the banana in the image.", "https://example.com/image.jpg")
entities = [
("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]),
("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)]),
]
image = kosmos.generate_boxes(
"Find the apple and the banana in the image.", "https://example.com/image.jpg"
)
```
### Example 8 - Generating Boxes for Entities
```python
from swarms.models.kosmos_two import Kosmos
kosmos = Kosmos()
entities = [("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]), ("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)])]
image = kosmos.generate_boxes("Find the apple and the banana in the image.", "https://example.com/image.jpg")
entities = [
("apple", (0, 3), [(0.2, 0.3, 0.4, 0.5)]),
("banana", (4, 9), [(0.6, 0.2, 0.8, 0.4)]),
]
image = kosmos.generate_boxes(
"Find the apple and the banana in the image.", "https://example.com/image.jpg"
)
```
## How Kosmos Works

@ -150,7 +150,6 @@ Example:
```python
from swarms.models import Mistral
model = Mistral()
task = "Translate the following English text to French: 'Hello, how are you?'"
result = model.run(task)

@ -52,10 +52,10 @@ class MPT7B:
from swarms.models import MPT7B
# Initialize the MPT7B class
mpt = MPT7B('mosaicml/mpt-7b-storywriter', 'EleutherAI/gpt-neox-20b', max_tokens=150)
mpt = MPT7B("mosaicml/mpt-7b-storywriter", "EleutherAI/gpt-neox-20b", max_tokens=150)
# Generate text
output = mpt.run('generate', 'Once upon a time in a land far, far away...')
output = mpt.run("generate", "Once upon a time in a land far, far away...")
print(output)
```
@ -77,13 +77,16 @@ print(outputs)
```python
import asyncio
from swarms.models import MPT7B
# Initialize the MPT7B class
mpt = MPT7B('mosaicml/mpt-7b-storywriter', 'EleutherAI/gpt-neox-20b', max_tokens=150)
mpt = MPT7B("mosaicml/mpt-7b-storywriter", "EleutherAI/gpt-neox-20b", max_tokens=150)
# Generate text asynchronously
output = asyncio.run(mpt.run_async('generate', 'Once upon a time in a land far, far away...'))
output = asyncio.run(
mpt.run_async("generate", "Once upon a time in a land far, far away...")
)
print(output)
```

@ -168,7 +168,11 @@ prompt = "Translate the following English text to French: 'Hello, how are you?'"
generated_text = openai.generate(prompt, max_tokens=50)
# Generate text from multiple prompts
prompts = ["Translate this: 'Good morning' to Spanish.", "Summarize the following article:", article_text]
prompts = [
"Translate this: 'Good morning' to Spanish.",
"Summarize the following article:",
article_text,
]
generated_texts = openai.generate(prompts, max_tokens=100)
# Generate text asynchronously
@ -188,7 +192,7 @@ custom_options = {
"max_tokens": 100,
"top_p": 0.9,
"frequency_penalty": 0.2,
"presence_penalty": 0.4
"presence_penalty": 0.4,
}
generated_text = openai.generate(prompt, **custom_options)
```

@ -150,7 +150,9 @@ user_message = "User: Tell me another joke."
response = openai_chat.generate([user_message])
# Print the generated response
print(response[0][0].text) # Output: "Assistant: Why don't scientists trust atoms? Because they make up everything!"
print(
response[0][0].text
) # Output: "Assistant: Why don't scientists trust atoms? Because they make up everything!"
```
### Example 3: Asynchronous Generation
@ -158,12 +160,14 @@ print(response[0][0].text) # Output: "Assistant: Why don't scientists trust ato
```python
import asyncio
# Define an asynchronous function for generating responses
async def generate_responses():
user_message = "User: Tell me a fun fact."
async for chunk in openai_chat.stream([user_message]):
print(chunk.text)
# Run the asynchronous generation function
asyncio.run(generate_responses())
```

@ -26,20 +26,26 @@ To use the Vilt model, follow these steps:
```python
from swarms.models import Vilt
model = Vilt()
```
2. Call the model with a text question and an image URL:
```python
output = model("What is this image?", "http://images.cocodataset.org/val2017/000000039769.jpg")
output = model(
"What is this image?", "http://images.cocodataset.org/val2017/000000039769.jpg"
)
```
### Example 1 - Image Questioning
```python
model = Vilt()
output = model("What are the objects in this image?", "http://images.cocodataset.org/val2017/000000039769.jpg")
output = model(
"What are the objects in this image?",
"http://images.cocodataset.org/val2017/000000039769.jpg",
)
print(output)
```
@ -47,7 +53,10 @@ print(output)
```python
model = Vilt()
output = model("Describe the scene in this image.", "http://images.cocodataset.org/val2017/000000039769.jpg")
output = model(
"Describe the scene in this image.",
"http://images.cocodataset.org/val2017/000000039769.jpg",
)
print(output)
```
@ -55,7 +64,10 @@ print(output)
```python
model = Vilt()
output = model("Tell me more about the landmark in this image.", "http://images.cocodataset.org/val2017/000000039769.jpg")
output = model(
"Tell me more about the landmark in this image.",
"http://images.cocodataset.org/val2017/000000039769.jpg",
)
print(output)
```

@ -63,7 +63,7 @@ custom_vllm = vLLM(
trust_remote_code=True,
revision="abc123",
temperature=0.7,
top_p=0.8
top_p=0.8,
)
```
@ -108,7 +108,7 @@ custom_vllm = vLLM(
trust_remote_code=True,
revision="abc123",
temperature=0.7,
top_p=0.8
top_p=0.8,
)
# Generate text with custom configuration
@ -128,7 +128,7 @@ vllm = vLLM()
tasks = [
"Translate the following sentence to French: 'Hello, world!'",
"Write a short story set in a futuristic world.",
"Summarize the main points of a news article about climate change."
"Summarize the main points of a news article about climate change.",
]
for task in tasks:

@ -45,6 +45,7 @@ To use the Zephyr model, follow these steps:
```python
from swarms.models import Zephyr
model = Zephyr(max_new_tokens=300, temperature=0.7, top_k=50, top_p=0.95)
```

@ -47,9 +47,11 @@ The `AbstractSwarm` class is an abstract base class that serves as the foundatio
```python
from abc import ABC, abstractmethod
from typing import Optional, List, Dict, Any
from typing import List
from swarms.swarms.base import AbstractWorker
class AbstractSwarm(ABC):
"""
Abstract class for swarm simulation architectures
@ -58,12 +60,12 @@ class AbstractSwarm(ABC):
---------
...
"""
# The class definition and constructor are provided here.
@abstractmethod
def __init__(self, workers: List["AbstractWorker"]):
"""Initialize the swarm with workers"""
pass
# Other abstract methods are listed here.
```

@ -68,7 +68,9 @@ final_response = agent.run(initial_task)
You can collect feedback during the conversation using the `provide_feedback` method:
```python
agent.provide_feedback("Generate an SOP for new sales employees on the best cold sales practices")
agent.provide_feedback(
"Generate an SOP for new sales employees on the best cold sales practices"
)
```
### Stopping Condition
@ -78,9 +80,11 @@ You can define a custom stopping condition using a function. For example, you ca
```python
from swarms.structs import Agent
def stop_when_repeats(response: str) -> bool:
return "Stop" in response.lower()
agent = Agent(llm=my_language_model, max_loops=5, stopping_condition=stop_when_repeats)
```
@ -107,9 +111,9 @@ Here are three usage examples:
### Example 1: Simple Conversation
```python
from swarms.structs import Agent
# Select any Language model from the models folder
from swarms.models import Mistral, OpenAIChat
from swarms.structs import Agent
llm = Mistral()
# llm = OpenAIChat()
@ -128,9 +132,11 @@ final_response = agent.run(initial_task)
```python
from swarms.structs import Agent
def stop_when_repeats(response: str) -> bool:
return "Stop" in response.lower()
agent = Agent(llm=llm, max_loops=5, stopping_condition=stop_when_repeats)
```

@ -41,9 +41,7 @@ class Artifact(BaseModel):
)
relative_path: Optional[str] = Field(
None,
description=(
"Relative path of the artifact in the agent's workspace"
),
description=("Relative path of the artifact in the agent's workspace"),
example="python/code/",
)
```
@ -64,7 +62,7 @@ from swarms.structs import Artifact
artifact_instance = Artifact(
artifact_id="b225e278-8b4c-4f99-a696-8facf19f0e56",
file_name="main.py",
relative_path="python/code/"
relative_path="python/code/",
)
```
@ -85,8 +83,7 @@ If the `relative_path` attribute is not provided during artifact creation, it wi
```python
artifact_instance_no_path = Artifact(
artifact_id="c280s347-9b7d-3c68-m337-7abvf50j23k",
file_name="script.js"
artifact_id="c280s347-9b7d-3c68-m337-7abvf50j23k", file_name="script.js"
)
print(artifact_instance_no_path.relative_path)

@ -15,9 +15,7 @@ class ArtifactUpload(BaseModel):
file: bytes = Field(..., description="File to upload")
relative_path: Optional[str] = Field(
None,
description=(
"Relative path of the artifact in the agent's workspace"
),
description=("Relative path of the artifact in the agent's workspace"),
example="python/code/",
)
```
@ -32,10 +30,12 @@ The `ArtifactUpload` class is used to create an instance of an artifact upload.
from swarms.structs import ArtifactUpload
# Uploading a file with no relative path
upload_no_path = ArtifactUpload(file=b'example_file_contents')
upload_no_path = ArtifactUpload(file=b"example_file_contents")
# Uploading a file with a relative path
upload_with_path = ArtifactUpload(file=b'example_file_contents', relative_path="python/code/")
upload_with_path = ArtifactUpload(
file=b"example_file_contents", relative_path="python/code/"
)
```
In the above example, `upload_no_path` is an instance of `ArtifactUpload` with no specified `relative_path`, whereas `upload_with_path` is an instance of `ArtifactUpload` with the `relative_path` set to "python/code/".

@ -36,7 +36,9 @@ Initializes the `AutoScaler` with a predefined number of agents and sets up conf
```python
from swarms import AutoScaler
scaler = AutoScaler(initial_agents=5, scale_up_factor=3, idle_threshold=0.1, busy_threshold=0.8)
scaler = AutoScaler(
initial_agents=5, scale_up_factor=3, idle_threshold=0.1, busy_threshold=0.8
)
```
---
@ -140,7 +142,9 @@ scaler.start()
from swarms import AutoScaler
# Initialize the scaler
auto_scaler = AutoScaler(initial_agents=15, scale_up_factor=2, idle_threshold=0.2, busy_threshold=0.7)
auto_scaler = AutoScaler(
initial_agents=15, scale_up_factor=2, idle_threshold=0.2, busy_threshold=0.7
)
# Start the monitoring and task processing
auto_scaler.start()
@ -161,7 +165,6 @@ auto_scaler.start()
for i in range(100): # Adding tasks
auto_scaler.add_task(f"Task {i}")
```

@ -24,7 +24,7 @@ agents = Agent()
manager = GroupChatManager(groupchat, selector)
# Call the group chat manager passing a specific chat task
result = manager('Discuss the agenda for the upcoming meeting')
result = manager("Discuss the agenda for the upcoming meeting")
```
Explanation:
@ -67,9 +67,7 @@ class GroupChatManager:
Returns:
str: The response from the group chat.
"""
self.groupchat.messages.append(
{"role": self.selector.name, "content": task}
)
self.groupchat.messages.append({"role": self.selector.name, "content": task})
for i in range(self.groupchat.max_round):
speaker = self.groupchat.select_speaker(
last_speaker=self.selector, selector=self.selector

@ -0,0 +1,134 @@
# **Documentation for `swarms.structs.JSON` Class**
The `swarms.structs.JSON` class is a helper class that provides a templated framework for creating new classes that deal with JSON objects and need to validate these objects against a JSON Schema. Being an abstract base class (ABC), the `JSON` class allows for the creation of subclasses that implement specific behavior while ensuring that they all adhere to a common interface, particularly the `validate` method.
Given that documenting the entire code provided in full detail would exceed our platform's limitations, below is a generated documentation for the `JSON` class following the steps you provided. This is an outline and would need to be expanded upon to reach the desired word count and thoroughness in a full, professional documentation.
---
## Introduction
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. `swarms.structs.JSON` class aims to provide a basic structure for utilizing JSON and validating it against a pre-defined schema. This is essential for applications where data integrity and structure are crucial, such as configurations for applications, communications over networks, and data storage.
## Class Definition
| Parameter | Type | Description |
|---------------|------------|------------------------------------|
| `schema_path` | `str` | The path to the JSON schema file. |
### `JSON.__init__(self, schema_path)`
Class constructor that initializes a `JSON` object with the specified JSON schema path.
```python
def __init__(self, schema_path):
self.schema_path = schema_path
self.schema = self.load_schema()
```
### `JSON.load_schema(self)`
Private method that loads and returns the JSON schema from the file specified at the `schema_path`.
### `JSON.validate(self, data)`
Abstract method that needs to be implemented by subclasses to validate input `data` against the JSON schema.
---
## Functionality and Usage
### Why use `JSON` Class
The `JSON` class abstracts away the details of loading and validating JSON data, allowing for easy implementation in any subclass that needs to handle JSON input. It sets up a standard for all subclasses to follow, ensuring consistency across different parts of code or different projects.
By enforcing a JSON schema, the `JSON` class helps maintain the integrity of the data, catching errors early in the process of reading and writing JSON.
### Step-by-step Guide
1. Subclass the `JSON` class.
2. Provide an implementation for the `validate` method.
3. Use the provided schema to enforce required fields and types within your JSON data.
---
## Example Usage
### Implementing a Subclass
Suppose we have a JSON Schema in `config_schema.json` for application configuration.
```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"debug": {
"type": "boolean"
},
"window_size": {
"type": "array",
"items": {
"type": "number"
},
"minItems": 2,
"maxItems": 2
}
},
"required": ["debug", "window_size"]
}
```
Now we'll create a subclass `AppConfig` that uses this schema.
```python
from swarms.structs import JSON
class AppConfig(JSON):
def __init__(self, schema_path):
super().__init__(schema_path)
def validate(self, config_data):
# Here we'll use a JSON Schema validation library like jsonschema
from jsonschema import ValidationError, validate
try:
validate(instance=config_data, schema=self.schema)
except ValidationError as e:
print(f"Invalid configuration: {e}")
return False
return True
# Main Example Usage
if __name__ == "__main__":
config = {"debug": True, "window_size": [800, 600]}
app_config = AppConfig("config_schema.json")
if app_config.validate(config):
print("Config is valid!")
else:
print("Config is invalid.")
```
In this example, an `AppConfig` class that inherits from `JSON` is created. The `validate` method is implemented to check whether a configuration dictionary is valid against the provided schema.
### Note
- Validate real JSON data using this class in a production environment.
- Catch and handle any exceptions as necessary to avoid application crashes.
- Extend functionality within subclasses as required for your application.
---
## Additional Information and Tips
- Use detailed JSON Schemas for complex data validation.
- Use the jsonschema library for advanced validation features.
## References and Resources
- Official Python Documentation for ABCs: https://docs.python.org/3/library/abc.html
- JSON Schema: https://json-schema.org/
- jsonschema Python package: https://pypi.org/project/jsonschema/
This generated documentation serves as a template and starting point intended for creating in-depth, practical documentation. Expanding upon each section, in practice, would involve deeper code examples, common patterns and pitfalls, and more thorough explanations of the `JSON` class internals and how to best utilize them in various real-world scenarios.

@ -0,0 +1,111 @@
# `MajorityVoting` Documentation
## Overview
The `swarms.structs` library provides a flexible architecture for creating and managing swarms of agents capable of performing tasks and making decisions based on majority voting. This documentation will guide you through the `MajorityVoting` class, explaining its purpose, architecture, and usage with examples.
## Table of Contents
- [Introduction](#introduction)
- [Installation](#installation)
- [The `MajorityVoting` Class](#the-majorityvoting-class)
- [Class Definition](#class-definition)
- [Parameters](#parameters)
- [Methods](#methods)
- [`__init__`](#__init__)
- [`run`](#run)
- [Usage Examples](#usage-examples)
- [Basic Usage](#basic-usage)
- [Concurrent Execution](#concurrent-execution)
- [Asynchronous Execution](#asynchronous-execution)
- [Advanced Features](#advanced-features)
- [Troubleshooting and FAQ](#troubleshooting-and-faq)
- [Conclusion](#conclusion)
- [References](#references)
## Introduction
The `swarms.structs` library introduces a mode of distributed computation through "agents" that collaborate to determine the outcome of tasks using a majority voting system. It becomes crucial in scenarios where collective decision-making is preferred over individual agent accuracy.
## Installation
To install the `swarms.structs` library, run the following command:
```bash
pip install swarms-structs
```
## The `MajorityVoting` Class
The `MajorityVoting` class is a high-level abstraction used to coordinate a group of agents that perform tasks and return results. These results are then aggregated to form a majority vote, determining the final output.
### Class Definition
### Parameters
| Parameter | Type | Default | Description |
|-----------------|------------|----------|----------------------------------------------------------------------|
| agents | List[Agent]| Required | A list of agent instances to participate in the voting process. |
| concurrent | bool | False | Enables concurrent execution using threading if set to `True`. |
| multithreaded | bool | False | Enables execution using multiple threads if set to `True`. |
| multiprocess | bool | False | Enables execution using multiple processes if set to `True`. |
| asynchronous | bool | False | Enables asynchronous execution if set to `True`. |
| output_parser | callable | None | A function to parse the output from the majority voting function. |
| autosave | bool | False | Enables automatic saving of the process state if set to `True`. (currently not used in source code) |
| verbose | bool | False | Enables verbose logging if set to `True`. |
### Methods
#### `__init__`
The constructor for the `MajorityVoting` class. Initializes a new majority voting system with the given configuration.
*This method doesn't return any value.*
#### `run`
Executes the given task by all participating agents and aggregates the results through majority voting.
| Parameter | Type | Description |
|-----------|-----------|----------------------------------|
| task | str | The task to be performed. |
| *args | list | Additional positional arguments. |
| **kwargs | dict | Additional keyword arguments. |
*Returns:* List[Any] - The result based on the majority vote.
## Usage Examples
### Basic Usage
```python
from swarms.structs.agent import Agent
from swarms.structs.majority_voting import MajorityVoting
def create_agent(name):
return Agent(name)
agents = [create_agent(name) for name in ["GPT-3", "Codex", "Tabnine"]]
majority_voting = MajorityVoting(agents)
result = majority_voting.run("What is the capital of France?")
print(result) # Output: Paris
```
### Concurrent Execution
```python
majority_voting = MajorityVoting(agents, concurrent=True)
result = majority_voting.run("What is the largest continent?")
print(result) # Example Output: Asia
```
### Asynchronous Execution
```python
majority_voting = MajorityVoting(agents, asynchronous=True)
result = majority_voting.run("What is the square root of 16?")
print(result) # Output: 4
```

@ -310,9 +310,7 @@ from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
# Example usage
api_key = (
"" # Your actual API key here
)
api_key = "" # Your actual API key here
# Initialize the language agent
llm = OpenAIChat(
@ -350,9 +348,7 @@ from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
# Example usage
api_key = (
"" # Your actual API key here
)
api_key = "" # Your actual API key here
# Initialize the language agent
llm = OpenAIChat(
@ -393,9 +389,7 @@ from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
# Example usage
api_key = (
"" # Your actual API key here
)
api_key = "" # Your actual API key here
# Initialize the language agent
llm = OpenAIChat(
@ -436,9 +430,7 @@ from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
# Example usage
api_key = (
"" # Your actual API key here
)
api_key = "" # Your actual API key here
# Initialize the language agent
llm = OpenAIChat(

@ -0,0 +1,114 @@
# StackOverflowSwarm Class Documentation
## Overview
The `StackOverflowSwarm` class is part of the `swarms.structs` library. It is designed to simulate a collective intelligence or swarm intelligence scenario where multiple individual agents (referred to as `Agent` objects) come together to solve problems or answer questions typically found on platforms like Stack Overflow. This class is helpful in experiments involving cooperative multi-agent interactions, decision-making, and problem-solving, primarily when applied to question-and-answer scenarios.
Swarm intelligence is modeled after social insects and natural systems where the collective behavior of decentralized, self-organized systems leads to the solving of complex tasks. `StackOverflowSwarm`, as a mini-framework within this library, provides a way to simulate such systems programmatically.
The design of the `StackOverflowSwarm` class is intended to allow easy tracking of multi-agent interactions, the ability to autosave conversations, provide verbose outputs for monitoring purposes, and deal with problem-solving in a structured manner. This document provides a deep dive into the class' mechanisms, its architecture, and comprehensive usage examples for developers and researchers interested in swarm intelligence applications.
## Class Definition
### StackOverflowSwarm Attributes:
| Attribute | Type | Description |
|-----------------|---------------------|-----------------------------------------------------------------------------|
| `agents` | `List[Agent]` | The list of agents in the swarm. |
| `autosave` | `bool` | Flag indicating whether to automatically save the conversation. |
| `verbose` | `bool` | Flag indicating whether to display verbose output. |
| `save_filepath` | `str` | The filepath to save the conversation. |
| `conversation` | `Conversation` | The conversation object for storing the interactions. |
| `eval_agent` | `Agent` or `None` | An optional evaluation agent within the swarm (not used in provided code). |
| `upvotes` | `int` | Counter for the number of upvotes per post (initialized as 0). |
| `downvotes` | `int` | Counter for the number of downvotes per post (initialized as 0). |
| `forum` | `List` | An empty list to represent the forum for the agents to interact. |
### StackOverflowSwarm Method: `__init__`
| Argument | Type | Default | Description |
|------------------|---------------|----------------------------------|---------------------------------------------------|
| `agents` | `List[Agent]` | Required | The list of agents in the swarm. |
| `autosave` | `bool` | `False` | Whether to automatically save the conversation. |
| `verbose` | `bool` | `False` | Whether to display verbose output. |
| `save_filepath` | `str` | `"stack_overflow_swarm.json"` | The filepath to save the conversation. |
| `eval_agent` | `Agent` | `None` | An optional eval agent (not entirely implemented).|
| `*args` | `variable` | | Variable length argument list. |
| `**kwargs` | `variable` | | Arbitrary keyword arguments. |
### StackOverflowSwarm Method: `run`
| Argument | Type | Description |
|-----------|----------|------------------------------------------------------------------------|
| `task` | `str` | The task to be performed by the agents. |
| `*args` | `variable`| Variable length argument list. |
| `**kwargs`| `variable`| Arbitrary keyword arguments. |
#### Return
| Type | Description |
|--------------|---------------------------------------------|
| `List[str]` | The conversation history as a list of strings.|
### API Usage and Examples
**Initializing and Running a StackOverflowSwarm**
```python
from swarms.structs.agent import Agent
from swarms.structs.stack_overflow_swarm import StackOverflowSwarm
# Define custom Agents with some logic (placeholder for actual Agent implementation)
class CustomAgent(Agent):
def run(self, conversation, *args, **kwargs):
return "This is a response from CustomAgent."
# Initialize agents
agent1 = CustomAgent(ai_name="Agent1")
agent2 = CustomAgent(ai_name="Agent2")
# Create a swarm
swarm = StackOverflowSwarm(agents=[agent1, agent2], autosave=True, verbose=True)
# Define a task
task_description = "How can I iterate over a list in Python?"
# Run the swarm with a task
conversation_history = swarm.run(task_description)
# Output the conversation history
print(conversation_history)
```
### How the Swarm Works
The `StackOverflowSwarm` starts by initializing agents, autosave preferences, conversation object, upvote/downvote counters, and a forum list to manage inter-agent communication. When the `run` method is invoked, it adds the given task to the conversation, logging this addition if verbose mode is enabled.
Each agent in the swarm runs its logic, possibly taking the current conversation history into consideration (the exact logic depends on the agent's implementation) and then responds to the task. Each agent's response is added to the conversation and logged.
If autosave is enabled, the conversation is saved to the specified file path. The `run` method ultimately returns the conversation history as a string, which could also be a serialized JSON depending on the implementation of `Agent` and `Conversation`.
### Considerations
- This is a high-level conceptual example and lacks the detailed implementations of `Agent`, `Conversation`, and the actual `run` logic within each `Agent`.
- The `eval_agent` attribute and related logic have not been implemented in the provided code.
### Common Issues
- Since the implementation of `Agent` and `Conversation` is not provided, one must ensure these components are compatible with the `StackOverflowSwarm` class for the interconnectivity and conversation saving/management to function correctly.
- It is essential to handle exceptions and errors within the `run` methods of each `Agent` to ensure that the failure of one agent does not halt the entire swarm.
### Additional Resources
For further exploration into swarm intelligence, collective behavior in natural and artificial systems, and multi-agent problem solving:
1. Bonabeau, E., Dorigo, M., & Theraulaz, G. (1999). Swarm Intelligence: From Natural to Artificial Systems. Oxford University Press.
2. Kennedy, J., Eberhart, R. C., & Shi, Y. (2001). Swarm Intelligence. Morgan Kaufmann.
3. [Multi-Agent Systems Virtual Labs](http://multiagent.fr)
4. [PyTorch Deep Learning and Artificial Intelligence](https://pytorch.org)
### Note
This documentation provides an overview of the `StackOverflowSwarm` class, its attributes, and methods. It should be adapted and expanded upon with actual code implementations for proper functionality and achieving the desired behavior in a swarm-based system.

@ -12,10 +12,7 @@ The `StepInput` class is defined as follows:
class StepInput(BaseModel):
__root__: Any = Field(
...,
description=(
"Input parameters for the task step. Any value is"
" allowed."
),
description=("Input parameters for the task step. Any value is" " allowed."),
example='{\n"file_to_refactor": "models.py"\n}',
)
```
@ -29,10 +26,7 @@ The `StepInput` class is designed to accept any input value, providing flexibili
```python
from swarms.structs import StepInput
input_params = {
"file_to_refactor": "models.py",
"refactor_method": "code"
}
input_params = {"file_to_refactor": "models.py", "refactor_method": "code"}
step_input = StepInput(__root__=input_params)
```
@ -42,10 +36,7 @@ In this example, we import the `StepInput` class from the `swarms.structs` libra
```python
from swarms.structs import StepInput
input_params = {
"input_path": "data.csv",
"output_path": "result.csv"
}
input_params = {"input_path": "data.csv", "output_path": "result.csv"}
step_input = StepInput(__root__=input_params)
```
@ -56,7 +47,7 @@ In this example, we again create an instance of `StepInput` by passing a diction
from swarms.structs import StepInput
file_path = "config.json"
with open(file_path, 'r') as f:
with open(file_path) as f:
input_data = json.load(f)
step_input = StepInput(__root__=input_data)

@ -28,6 +28,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.add_task("task")
@ -41,6 +42,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
await swarm.async_add_task("task")
@ -57,6 +59,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.run_single_agent(agent_id, "task")
@ -72,6 +75,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.run_many_agents("task")
@ -85,6 +89,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.list_agents()
@ -98,6 +103,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork()
swarm.add_agent(agent)
@ -111,6 +117,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.remove_agent(agent_id)
@ -124,6 +131,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
swarm = SwarmNetwork()
swarm.scale_up(num_agents=5)
```
@ -136,6 +144,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
swarm = SwarmNetwork(agents=[agent1, agent2, agent3, agent4, agent5])
swarm.scale_down(num_agents=2)
```
@ -146,6 +155,7 @@ The `SwarmNetwork` class has the following parameters:
```python
from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork
agent = Agent()
swarm = SwarmNetwork(agents=[agent])
swarm.create_apis_for_agents()

@ -8,8 +8,9 @@
```python
# Example 1: Creating and executing a Task
from swarms.structs import Task, Agent
from swarms.models import OpenAIChat
from swarms.structs import Agent, Task
agent = Agent(llm=OpenAIChat(openai_api_key=""), max_loops=1, dashboard=False)
task = Task(agent=agent)
task.execute("What's the weather in miami")

@ -20,11 +20,14 @@ The `TaskInput` class encapsulates the input parameters in a structured format.
#### Usage Example 1: Using TaskInput for Debugging
```python
from pydantic import BaseModel, Field
from swarms.structs import TaskInput
class DebugInput(TaskInput):
debug: bool
# Creating an instance of DebugInput
debug_params = DebugInput(__root__={"debug": True})
@ -35,11 +38,14 @@ print(debug_params.debug) # Output: True
#### Usage Example 2: Using TaskInput for Task Modes
```python
from pydantic import BaseModel, Field
from swarms.structs import TaskInput
class ModeInput(TaskInput):
mode: str
# Creating an instance of ModeInput
mode_params = ModeInput(__root__={"mode": "benchmarks"})
@ -50,12 +56,15 @@ print(mode_params.mode) # Output: benchmarks
#### Usage Example 3: Using TaskInput with Arbitrary Parameters
```python
from pydantic import BaseModel, Field
from swarms.structs import TaskInput
class ArbitraryInput(TaskInput):
message: str
quantity: int
# Creating an instance of ArbitraryInput
arbitrary_params = ArbitraryInput(__root__={"message": "Hello, world!", "quantity": 5})

@ -0,0 +1,130 @@
# `TaskQueueBase`
## Introduction
The `swarms.structs` library is a key component of a multi-agent system's task management infrastructure. It provides the necessary classes and methods to create and manage queues of tasks that can be distributed among a swarm of agents. The purpose of this documentation is to guide users through the proper use of the `TaskQueueBase` class, which serves as an abstract base class for implementing task queues.
## TaskQueueBase Class
```python
import threading
from abc import ABC, abstractmethod
# Include any additional imports that are relevant to decorators and other classes such as Task and Agent if needed
# Definition of the synchronized_queue decorator (if necessary)
class TaskQueueBase(ABC):
def __init__(self):
self.lock = threading.Lock()
@synchronized_queue
@abstractmethod
def add_task(self, task: Task) -> bool:
pass
@synchronized_queue
@abstractmethod
def get_task(self, agent: Agent) -> Task:
pass
@synchronized_queue
@abstractmethod
def complete_task(self, task_id: str):
pass
@synchronized_queue
@abstractmethod
def reset_task(self, task_id: str):
pass
```
### Architecture and Purpose
The `TaskQueueBase` class provides an abstract interface for task queue implementations. This class uses the `threading.Lock` to ensure mutual exclusion, making it suitable for concurrent environments. The `@synchronized_queue` decorator implies that each method should be synchronized to prevent race conditions.
Tasks are generally represented by the `Task` class, and agents by the `Agent` class. Implementations of the `TaskQueueBase` will provide the logic to store tasks, distribute them to agents, and manage their lifecycles.
#### Methods and Their Arguments
Here's an overview of each method and its arguments:
| Method | Arguments | Return Type | Description |
|----------------|----------------|-------------|-----------------------------------------------------------------------------------------------|
| add_task | task (Task) | bool | Adds a task to the queue and returns True if successfully added, False otherwise. |
| get_task | agent (Agent) | Task | Retrieves the next task for the given agent. |
| complete_task | task_id (str) | None | Marks the task identified by task_id as completed. |
| reset_task | task_id (str) | None | Resets the task identified by task_id, typically done if an agent fails to complete the task. |
### Example Usage
Below are three examples of how the `TaskQueueBase` class can be implemented and used.
**Note:** The actual code for decorators, Task, Agent, and concrete implementations of `TaskQueueBase` is not provided and should be created as per specific requirements.
#### Example 1: Basic Implementation
```python
# file: basic_queue.py
# Assume synchronized_queue decorator is defined elsewhere
from decorators import synchronized_queue
from swarms.structs import Agent, Task, TaskQueueBase
class BasicTaskQueue(TaskQueueBase):
def __init__(self):
super().__init__()
self.tasks = []
@synchronized_queue
def add_task(self, task: Task) -> bool:
self.tasks.append(task)
return True
@synchronized_queue
def get_task(self, agent: Agent) -> Task:
return self.tasks.pop(0)
@synchronized_queue
def complete_task(self, task_id: str):
# Logic to mark task as completed
pass
@synchronized_queue
def reset_task(self, task_id: str):
# Logic to reset the task
pass
# Usage
queue = BasicTaskQueue()
# Add task, assuming Task object is created
queue.add_task(someTask)
# Get task for an agent, assuming Agent object is created
task = queue.get_task(someAgent)
```
#### Example 2: Priority Queue Implementation
```python
# file: priority_queue.py
# Similar to example 1, but tasks are managed based on priority within add_task and get_task methods
```
#### Example 3: Persistent Queue Implementation
```python
# file: persistent_queue.py
# An example demonstrating tasks being saved to a database or filesystem. Methods would include logic for persistence.
```
### Additional Information and Common Issues
This section would provide insights on thread safety, error handling, and best practices in working with task queues in a multi-agent system.
### References
Links to further resources and any academic papers or external documentation related to task queues and multi-agent systems would be included here.

@ -1,249 +0,0 @@
# `ModelParallelizer` Documentation
## Table of Contents
1. [Understanding the Purpose](#understanding-the-purpose)
2. [Overview and Introduction](#overview-and-introduction)
3. [Class Definition](#class-definition)
4. [Functionality and Usage](#functionality-and-usage)
5. [Additional Information](#additional-information)
6. [Examples](#examples)
7. [Conclusion](#conclusion)
## 1. Understanding the Purpose <a name="understanding-the-purpose"></a>
To create comprehensive documentation for the `ModelParallelizer` class, let's begin by understanding its purpose and functionality.
### Purpose and Functionality
`ModelParallelizer` is a class designed to facilitate the orchestration of multiple Language Model Models (LLMs) to perform various tasks simultaneously. It serves as a powerful tool for managing, distributing, and collecting responses from these models.
Key features and functionality include:
- **Parallel Task Execution**: `ModelParallelizer` can distribute tasks to multiple LLMs and execute them in parallel, improving efficiency and reducing response time.
- **Structured Response Presentation**: The class presents the responses from LLMs in a structured tabular format, making it easy for users to compare and analyze the results.
- **Task History Tracking**: `ModelParallelizer` keeps a record of tasks that have been submitted, allowing users to review previous tasks and responses.
- **Asynchronous Execution**: The class provides options for asynchronous task execution, which can be particularly useful for handling a large number of tasks.
Now that we have an understanding of its purpose, let's proceed to provide a detailed overview and introduction.
## 2. Overview and Introduction <a name="overview-and-introduction"></a>
### Overview
The `ModelParallelizer` class is a crucial component for managing and utilizing multiple LLMs in various natural language processing (NLP) tasks. Its architecture and functionality are designed to address the need for parallel processing and efficient response handling.
### Importance and Relevance
In the rapidly evolving field of NLP, it has become common to use multiple language models to achieve better results in tasks such as translation, summarization, and question answering. `ModelParallelizer` streamlines this process by allowing users to harness the capabilities of several LLMs simultaneously.
Key points:
- **Parallel Processing**: `ModelParallelizer` leverages multithreading to execute tasks concurrently, significantly reducing the time required for processing.
- **Response Visualization**: The class presents responses in a structured tabular format, enabling users to visualize and analyze the outputs from different LLMs.
- **Task Tracking**: Developers can track the history of tasks submitted to `ModelParallelizer`, making it easier to manage and monitor ongoing work.
### Architecture and How It Works
The architecture and working of `ModelParallelizer` can be summarized in four steps:
1. **Task Reception**: `ModelParallelizer` receives a task from the user.
2. **Task Distribution**: The class distributes the task to all registered LLMs.
3. **Response Collection**: `ModelParallelizer` collects the responses generated by the LLMs.
4. **Response Presentation**: Finally, the class presents the responses from all LLMs in a structured tabular format, making it easy for users to compare and analyze the results.
Now that we have an overview, let's proceed with a detailed class definition.
## 3. Class Definition <a name="class-definition"></a>
### Class Attributes
- `llms`: A list of LLMs (Language Model Models) that `ModelParallelizer` manages.
- `last_responses`: Stores the responses from the most recent task.
- `task_history`: Keeps a record of all tasks submitted to `ModelParallelizer`.
### Methods
The `ModelParallelizer` class defines various methods to facilitate task distribution, execution, and response presentation. Let's examine some of the key methods:
- `run(task)`: Distributes a task to all LLMs, collects responses, and returns them.
- `print_responses(task)`: Prints responses from all LLMs in a structured tabular format.
- `run_all(task)`: Runs the task on all LLMs sequentially and returns responses.
- `arun_all(task)`: Asynchronously runs the task on all LLMs and returns responses.
- `print_arun_all(task)`: Prints responses from all LLMs after asynchronous execution.
- `save_responses_to_file(filename)`: Saves responses to a file for future reference.
- `load_llms_from_file(filename)`: Loads LLMs from a file, making it easy to configure `ModelParallelizer` for different tasks.
- `get_task_history()`: Retrieves the task history, allowing users to review previous tasks.
- `summary()`: Provides a summary of task history and the last responses, aiding in post-processing and analysis.
Now that we have covered the class definition, let's delve into the functionality and usage of `ModelParallelizer`.
## 4. Functionality and Usage <a name="functionality-and-usage"></a>
### Distributing a Task and Collecting Responses
One of the primary use cases of `ModelParallelizer` is to distribute a task to all registered LLMs and collect their responses. This can be achieved using the `run(task)` method. Below is an example:
```python
parallelizer = ModelParallelizer(llms)
responses = parallelizer.run("Translate the following English text to French: 'Hello, how are you?'")
```
### Printing Responses
To present the responses from all LLMs in a structured tabular format, use the `print_responses(task)` method. Example:
```python
parallelizer.print_responses("Summarize the main points of 'War and Peace.'")
```
### Saving Responses to a File
Users can save the responses to a file using the `save_responses_to_file(filename)` method. This is useful for archiving and reviewing responses later. Example:
```python
parallelizer.save_responses_to_file("responses.txt")
```
### Task History
The `ModelParallelizer` class keeps track of the task history. Developers can access the task history using the `get_task_history()` method. Example:
```python
task_history = parallelizer.get_task_history()
for i, task in enumerate(task_history):
print(f"Task {i + 1}: {task}")
```
## 5. Additional Information <a name="additional-information"></a>
### Parallel Execution
`ModelParallelizer` employs multithreading to execute tasks concurrently. This parallel processing capability significantly improves the efficiency of handling multiple tasks simultaneously.
### Response Visualization
The structured tabular format used for presenting responses simplifies the comparison and analysis of outputs from different LLMs.
## 6. Examples <a name="examples"></a>
Let's explore additional usage examples to illustrate the versatility of `ModelParallelizer` in handling various NLP tasks.
### Example 1: Sentiment Analysis
```python
from swarms.models import OpenAIChat
from swarms.swarms import ModelParallelizer
from swarms.workers.worker import Worker
# Create an instance of an LLM for sentiment analysis
llm = OpenAIChat(model_name="gpt-4", openai_api_key="api-key", temperature=0.5)
# Create worker agents
worker1 = Worker(
llm=llm,
ai_name="Bumble Bee",
ai_role="Worker in a swarm",
external_tools=None,
human_in_the_loop=False,
temperature=0.5,
)
worker2 = Worker
(
llm=llm,
ai_name="Optimus Prime",
ai_role="Worker in a swarm",
external_tools=None,
human_in_the_loop=False,
temperature=0.5,
)
worker3 = Worker(
llm=llm,
ai_name="Megatron",
ai_role="Worker in a swarm",
external_tools=None,
human_in_the_loop=False,
temperature=0.5,
)
# Register the worker agents with ModelParallelizer
agents = [worker1, worker2, worker3]
parallelizer = ModelParallelizer(agents)
# Task for sentiment analysis
task = "Please analyze the sentiment of the following sentence: 'This movie is amazing!'"
# Print responses from all agents
parallelizer.print_responses(task)
```
### Example 2: Translation
```python
from swarms.models import OpenAIChat
from swarms.swarms import ModelParallelizer
# Define LLMs for translation tasks
translator1 = OpenAIChat(model_name="translator-en-fr", openai_api_key="api-key", temperature=0.7)
translator2 = OpenAIChat(model_name="translator-en-es", openai_api_key="api-key", temperature=0.7)
translator3 = OpenAIChat(model_name="translator-en-de", openai_api_key="api-key", temperature=0.7)
# Register translation agents with ModelParallelizer
translators = [translator1, translator2, translator3]
parallelizer = ModelParallelizer(translators)
# Task for translation
task = "Translate the following English text to French: 'Hello, how are you?'"
# Print translated responses from all agents
parallelizer.print_responses(task)
```
### Example 3: Summarization
```python
from swarms.models import OpenAIChat
from swarms.swarms import ModelParallelizer
# Define LLMs for summarization tasks
summarizer1 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", temperature=0.6)
summarizer2 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", temperature=0.6)
summarizer3 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", temperature=0.6)
# Register summarization agents with ModelParallelizer
summarizers = [summarizer1, summarizer2, summarizer3]
parallelizer = ModelParallelizer(summarizers)
# Task for summarization
task = "Summarize the main points of the article titled 'Climate Change and Its Impact on the Environment.'"
# Print summarized responses from all agents
parallelizer.print_responses(task)
```
## 7. Conclusion <a name="conclusion"></a>
In conclusion, the `ModelParallelizer` class is a powerful tool for managing and orchestrating multiple Language Model Models in natural language processing tasks. Its ability to distribute tasks, collect responses, and present them in a structured format makes it invaluable for streamlining NLP workflows. By following the provided documentation, users can harness the full potential of `ModelParallelizer` to enhance their natural language processing projects.
For further information on specific LLMs or advanced usage, refer to the documentation of the respective models and their APIs. Additionally, external resources on parallel execution and response visualization can provide deeper insights into these topics.

@ -1,167 +0,0 @@
# Swarms Framework Documentation
---
## Overview
The Swarms framework is a Python library designed to facilitate the creation and management of a simulated group chat environment. This environment can be used for a variety of purposes, such as training conversational agents, role-playing games, or simulating dialogues for machine learning purposes. The core functionality revolves around managing the agent of messages between different agents within the chat, as well as handling the selection and responses of these agents based on the conversation's context.
### Purpose
The purpose of the Swarms framework, and specifically the `GroupChat` and `GroupChatManager` classes, is to simulate a dynamic and interactive conversation between multiple agents. This simulates a real-time chat environment where each participant is represented by an agent with a specific role and behavioral patterns. These agents interact within the rules of the group chat, controlled by the `GroupChatManager`.
### Key Features
- **Agent Interaction**: Allows multiple agents to communicate within a group chat scenario.
- **Message Management**: Handles the storage and agent of messages within the group chat.
- **Role Play**: Enables agents to assume specific roles and interact accordingly.
- **Conversation Context**: Maintains the context of the conversation for appropriate responses by agents.
---
## GroupChat Class
The `GroupChat` class is the backbone of the Swarms framework's chat simulation. It maintains the list of agents participating in the chat, the messages that have been exchanged, and the logic to reset the chat and determine the next speaker.
### Class Definition
#### Parameters
| Parameter | Type | Description | Default Value |
|------------|---------------------|--------------------------------------------------------------|---------------|
| agents | List[Agent] | List of agent flows participating in the group chat. | None |
| messages | List[Dict] | List of message dictionaries exchanged in the group chat. | None |
| max_round | int | Maximum number of rounds/messages allowed in the group chat. | 10 |
| admin_name | str | The name of the admin agent in the group chat. | "Admin" |
#### Class Properties and Methods
- `agent_names`: Returns a list of the names of the agents in the group chat.
- `reset()`: Clears all messages from the group chat.
- `agent_by_name(name: str) -> Agent`: Finds and returns an agent by name.
- `next_agent(agent: Agent) -> Agent`: Returns the next agent in the list.
- `select_speaker_msg() -> str`: Returns the message for selecting the next speaker.
- `select_speaker(last_speaker: Agent, selector: Agent) -> Agent`: Logic to select the next speaker based on the last speaker and the selector agent.
- `_participant_roles() -> str`: Returns a string listing all participant roles.
- `format_history(messages: List[Dict]) -> str`: Formats the history of messages for display or processing.
### Usage Examples
#### Example 1: Initializing a GroupChat
```python
from swarms.structs.agent import Agent
from swarms.groupchat import GroupChat
# Assuming Agent objects (flow1, flow2, flow3) are initialized and configured
agents = [flow1, flow2, flow3]
group_chat = GroupChat(agents=agents, messages=[], max_round=10)
```
#### Example 2: Resetting a GroupChat
```python
group_chat.reset()
```
#### Example 3: Selecting a Speaker
```python
last_speaker = agents[0] # Assuming this is a Agent object representing the last speaker
selector = agents[1] # Assuming this is a Agent object with the selector role
next_speaker = group_chat.select_speaker(last_speaker, selector)
```
---
## GroupChatManager Class
The `GroupChatManager` class acts as a controller for the `GroupChat` instance. It orchestrates the interaction between agents, prompts for tasks, and manages the rounds of conversation.
### Class Definition
#### Constructor Parameters
| Parameter | Type | Description |
|------------|-------------|------------------------------------------------------|
| groupchat | GroupChat | The GroupChat instance that the manager will handle. |
| selector | Agent | The Agent object that selects the next speaker. |
#### Methods
- `__call__(task: str)`: Invokes the GroupChatManager with a given task string to start the conversation.
### Usage Examples
#### Example 1: Initializing GroupChatManager
```python
from swarms.groupchat import GroupChat, GroupChatManager
from swarms.structs.agent import Agent
# Initialize your agents and group chat as shown in previous examples
chat_manager = GroupChatManager(groupchat=group_chat, selector=manager)
```
#### Example 2: Starting a Conversation
```python
# Start the group chat with a task
chat_history = chat_manager("Start a conversation about space exploration.")
```
#### Example 3: Using the Call Method
```python
# The call method is the same as starting a conversation
chat_history = chat_manager.__call__("Discuss recent advances in AI.")
```
---
## Conclusion
In summary, the Swarms framework offers a unique and effective solution for simulating group chat environments. Its `GroupChat` and `GroupChatManager` classes provide the necessary infrastructure to create dynamic conversations between agents, manage messages, and maintain the context of the dialogue. This framework can be instrumental in developing more sophisticated conversational agents, experimenting with social dynamics in chat environments, and providing a rich dataset for machine learning applications.
By leveraging the framework's features, users can create complex interaction scenarios that closely mimic real-world group communication. This can prove to be a valuable asset in the fields of artificial intelligence, computational social science, and beyond.
---
### Frequently Asked Questions (FAQ)
**Q: Can the Swarms framework handle real-time interactions between agents?**
A: The Swarms framework is designed to simulate group chat environments. While it does not handle real-time interactions as they would occur on a network, it can simulate the agent of conversation in a way that mimics real-time communication.
**Q: Is the Swarms framework capable of natural language processing?**
A: The framework itself is focused on the structure and management of group chats. It does not inherently include natural language processing (NLP) capabilities. However, it can be integrated with NLP tools to enhance the simulation with language understanding and generation features.
**Q: Can I customize the roles and behaviors of agents within the framework?**
A: Yes, the framework is designed to be flexible. You can define custom roles and behaviors for agents to fit the specific requirements of your simulation scenario.
**Q: What are the limitations of the Swarms framework?**
A: The framework is constrained by its design to simulate text-based group chats. It is not suitable for voice or video communication simulations. Additionally, its effectiveness depends on the sophistication of the agents decision-making logic, which is outside the framework itself.
**Q: Is it possible to integrate the Swarms framework with other chat services?**
A: The framework is can be integrated with any chat services. However, it could potentially be adapted to work with chat service APIs, where the agents could be used to simulate user behavior within a real chat application.
**Q: How does the `GroupChatManager` select the next speaker?**
A: The `GroupChatManager` uses a selection mechanism, which is typically based on the conversation's context and the roles of the agents, to determine the next speaker. The specifics of this mechanism can be customized to match the desired agent of the conversation.
**Q: Can I contribute to the Swarms framework or suggest features?**
A: As with many open-source projects, contributions and feature suggestions can usually be made through the project's repository on platforms like GitHub. It's best to check with the maintainers of the Swarms framework for their contribution guidelines.
**Q: Are there any tutorials or community support for new users of the Swarms framework?**
A: Documentation and usage examples are provided with the framework. Community support may be available through forums, chat groups, or the platform where the framework is hosted. Tutorials may also be available from third-party educators or in official documentation.
**Q: What programming skills do I need to use the Swarms framework effectively?**
A: You should have a good understanding of Python programming, including experience with classes and methods. Familiarity with the principles of agent-based modeling and conversational AI would also be beneficial.

@ -25,18 +25,19 @@ The `BaseTokenizer` class provides the structure for creating tokenizers. It inc
```python
from swarms.tokenizers import BaseTokenizer
class SimpleTokenizer(BaseTokenizer):
class SimpleTokenizer(BaseTokenizer):
def count_tokens(self, text: Union[str, List[dict]]) -> int:
if isinstance(text, str):
# Split text by spaces as a simple tokenization approach
return len(text.split())
elif isinstance(text, list):
# Assume list of dictionaries with 'token' key
return sum(len(item['token'].split()) for item in text)
return sum(len(item["token"].split()) for item in text)
else:
raise TypeError("Unsupported type for text")
# Usage example
tokenizer = SimpleTokenizer(max_tokens=100)
text = "This is an example sentence to tokenize."

@ -82,13 +82,14 @@ First, the Cohere client must be initialized and passed in to create an instance
```python
from cohere import Client
from swarms.tokenizers import CohereTokenizer
# Initialize Cohere client with your API key
cohere_client = Client('your-api-key')
cohere_client = Client("your-api-key")
# Instantiate the tokenizer
tokenizer = CohereTokenizer(model='your-model-name', client=cohere_client)
tokenizer = CohereTokenizer(model="your-model-name", client=cohere_client)
```
### Count Tokens Example 1

@ -76,7 +76,7 @@ Tokenizes given text when the object is called like a function.
from swarms.tokenizers import HuggingFaceTokenizer
# Initialize the tokenizer with the path to your tokenizer model.
tokenizer = HuggingFaceTokenizer('/path/to/your/model_dir')
tokenizer = HuggingFaceTokenizer("/path/to/your/model_dir")
```
### 2. Encoding Text

@ -50,7 +50,7 @@ Given the extensive nature of this class, several examples are provided for each
```python
from swarms.tokenizers import OpenAITokenizer
tokenizer = OpenAITokenizer(model='gpt-4')
tokenizer = OpenAITokenizer(model="gpt-4")
```
This example creates a new instance of `OpenAITokenizer` set to work with the GPT-4 model.
@ -61,7 +61,7 @@ This example creates a new instance of `OpenAITokenizer` set to work with the GP
text = "Hello, this is an example text to tokenize."
# Initialize the tokenizer
tokenizer = OpenAITokenizer(model='gpt-4')
tokenizer = OpenAITokenizer(model="gpt-4")
# Count tokens
num_tokens = tokenizer.count_tokens(text)
@ -78,7 +78,7 @@ messages = [
{"name": "Bob", "message": "I'm good! Just working on some code."},
]
tokenizer = OpenAITokenizer(model='gpt-3.5-turbo')
tokenizer = OpenAITokenizer(model="gpt-3.5-turbo")
# Count tokens for a list of messages
num_tokens = tokenizer.len(messages, model="gpt-3.5-turbo-0613")

@ -45,7 +45,7 @@ Parameter | Type | Description
```python
from swarms.tokenizers import SentencePieceTokenizer
tokenizer = SentencePieceTokenizer(model_file='your_model.model')
tokenizer = SentencePieceTokenizer(model_file="your_model.model")
```
### Properties: Vocabulary Information

@ -36,8 +36,10 @@ This function does not take any mandatory argument. However, it supports optiona
### Example 1: Basic Usage
```python
import torch
import logging
import torch
from swarms.utils import check_device
# Basic usage
@ -45,7 +47,7 @@ device = check_device(
log_level=logging.INFO,
memory_threshold=0.8,
capability_threshold=3.5,
return_type="list"
return_type="list",
)
```
@ -53,7 +55,7 @@ device = check_device(
```python
import torch
import logging
from swarms.utils import check_device
# When CUDA is not available
@ -65,7 +67,7 @@ print(device) # If CUDA is not available it should return torch.device('cpu')
```python
import torch
import logging
from swarms.utils import check_device
# When multiple GPUs are available

@ -57,14 +57,13 @@ Below are three examples of how you might use this function:
Extracting code blocks from a simple markdown string.
```python
import re
from swarms.utils import extract_code_from_markdown
markdown_string = '''# Example
markdown_string = """# Example
This is an example of a code block:
```python
print("Hello World!")
``` '''
``` """
print(extract_code_from_markdown(markdown_string))
```
@ -75,13 +74,15 @@ Extracting code blocks from a markdown file.
```python
import re
def extract_code_from_markdown(markdown_content: str) -> str:
pattern = r"```(?:\w+\n)?(.*?)```"
matches = re.findall(pattern, markdown_content, re.DOTALL)
return "\n".join(code.strip() for code in matches)
# Assume that 'example.md' contains multiple code blocks
with open('example.md', 'r') as file:
with open("example.md") as file:
markdown_content = file.read()
print(extract_code_from_markdown(markdown_content))
```
@ -93,17 +94,20 @@ Using the function in a pipeline to extract and then analyze code blocks.
```python
import re
def extract_code_from_markdown(markdown_content: str) -> str:
pattern = r"```(?:\w+\n)?(.*?)```"
matches = re.findall(pattern, markdown_content, re.DOTALL)
return "\n".join(code.strip() for code in matches)
def analyze_code_blocks(code: str):
# Add your analysis logic here
pass
# Assume that 'example.md' contains multiple code blocks
with open('example.md', 'r') as file:
with open("example.md") as file:
markdown_content = file.read()
code_blocks = extract_code_from_markdown(markdown_content)
analyze_code_blocks(code_blocks)

@ -40,15 +40,9 @@ The function `find_image_path` performs text parsing and pattern recognition to
```python
def find_image_path(text):
pattern = r"([A-Za-z]:\\[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))|(/[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))"
matches = [
match.group()
for match in re.finditer(pattern, text)
if match.group()
]
matches = [match.group() for match in re.finditer(pattern, text) if match.group()]
matches += [match.replace("\\", "") for match in matches if match]
existing_paths = [
match for match in matches if os.path.exists(match)
]
existing_paths = [match for match in matches if os.path.exists(match)]
return max(existing_paths, key=len) if existing_paths else None
```
@ -75,7 +69,9 @@ Consider the case where the text has multiple image paths.
from swarms.utils import find_image_path
text = "Here is an image path: /home/user/image1.png. Here is another one: C:\\Users\\User\\Documents\\image2.jpeg"
print(find_image_path(text)) # Outputs: the longest image path (depends on your file system and existing files)
print(
find_image_path(text)
) # Outputs: the longest image path (depends on your file system and existing files)
```
**Example 3:**

@ -50,12 +50,14 @@ This function can be used directly inside your code as shown in the following ex
Loading a model without specifying a device results in the function choosing the most optimal available device automatically.
```python
from swarms.utils import load_model_torch
import torch.nn as nn
from swarms.utils import load_model_torch
# Assume `mymodel.pth` is in the current directory
model_path = "./mymodel.pth"
# Define your model architecture if the model file only contains state dict
class MyModel(nn.Module):
def __init__(self):
@ -65,6 +67,7 @@ class MyModel(nn.Module):
def forward(self, x):
return self.linear(x)
model = MyModel()
# Load the model

@ -15,6 +15,8 @@ Let's say you have two functions: `ground_truth` and `generated_func`, that have
@math_eval(ground_truth, generated_func)
def test_func(x):
return x
result1, result2 = test_func(5)
print(f"Result from ground_truth: {result1}")
print(f"Result from generated_func: {result2}")
@ -46,6 +48,7 @@ Here's how to implement the `math_eval` decorator:
import functools
import logging
def math_eval(func1, func2):
"""Math evaluation decorator."""
@ -65,9 +68,7 @@ def math_eval(func1, func2):
result2 = None
if result1 != result2:
logging.warning(
f"Outputs do not match: {result1} != {result2}"
)
logging.warning(f"Outputs do not match: {result1} != {result2}")
return result1, result2

@ -70,6 +70,7 @@ def text_generator(self, text: str):
# language generation implementation goes here
return tokens
# Instantiate the class and call the decorated function
obj = ClassName()
obj.text_generator("Hello, world!")

@ -1,7 +1,7 @@
# pdf_to_text
## Introduction
The function `pdf_to_text` is a Python utility for converting a PDF file into a string of text content. It leverages the `PyPDF2` library, an excellent Python library for processing PDF files. The function takes in a PDF file's path and reads its content, subsequently returning the extracted textual data.
The function `pdf_to_text` is a Python utility for converting a PDF file into a string of text content. It leverages the `pypdf` library, an excellent Python library for processing PDF files. The function takes in a PDF file's path and reads its content, subsequently returning the extracted textual data.
This function can be very useful when you want to extract textual information from PDF files automatically. For instance, when processing a large number of documents, performing textual analysis, or when you're dealing with text data that is only available in PDF format.
@ -34,14 +34,14 @@ def pdf_to_text(pdf_path: str) -> str:
## Function Description
`pdf_to_text` utilises the `PdfReader` function from the `PyPDF2` library to read the PDF file. If the PDF file does not exist at the specified path or there was an error while reading the file, appropriate exceptions will be raised. It then iterates through each page in the PDF and uses the `extract_text` function to extract the text content from each page. These contents are then concatenated into a single variable and returned as the result.
`pdf_to_text` utilises the `PdfReader` function from the `pypdf` library to read the PDF file. If the PDF file does not exist at the specified path or there was an error while reading the file, appropriate exceptions will be raised. It then iterates through each page in the PDF and uses the `extract_text` function to extract the text content from each page. These contents are then concatenated into a single variable and returned as the result.
## Usage Examples
To use this function, you first need to install the `PyPDF2` library. It can be installed via pip:
To use this function, you first need to install the `pypdf` library. It can be installed via pip:
```python
!pip install pypdf2
!pip install pypdf
```
Then, you should import the `pdf_to_text` function:
@ -54,7 +54,7 @@ Here is an example of how to use `pdf_to_text`:
```python
# Define the path to the pdf file
pdf_path = 'sample.pdf'
pdf_path = "sample.pdf"
# Use the function to extract text
text = pdf_to_text(pdf_path)
@ -68,4 +68,4 @@ print(text)
- This function reads the text from the PDF. It does not handle images, graphical elements, or any non-text content.
- If the PDF contains scanned images rather than textual data, the `extract_text` function may not be able to extract any text. In such cases, you would require OCR (Optical Character Recognition) tools to extract the text.
- Be aware of the possibility that the output string might contain special characters or escape sequences because they were part of the PDF's content. You might need to clean the resulting text according to your requirements.
- The function uses the PyPDF2 library to facilitate the PDF reading and text extraction. For any issues related to PDF manipulation, consult the [PyPDF2 library documentation](https://pythonhosted.org/PyPDF2/).
- The function uses the pypdf library to facilitate the PDF reading and text extraction. For any issues related to PDF manipulation, consult the [pypdf library documentation](https://pypdf.readthedocs.io/en/stable/).

@ -56,7 +56,8 @@ Here are some examples of how you can use the `prep_torch_inference` method. Bef
```python
import torch
from swarms.utils import prep_torch_inference, load_model_torch
from swarms.utils import load_model_torch, prep_torch_inference
```
### Example 1: Load a model for inference on CPU

@ -41,39 +41,42 @@ class AbstractWorker:
@property
def name(self):
"""Get the name of the worker."""
pass
def run(self, task: str):
"""Run the worker agent once."""
pass
def send(self, message: Union[Dict, str], recipient, request_reply: Optional[bool] = None):
def send(
self, message: Union[Dict, str], recipient, request_reply: Optional[bool] = None
):
"""Send a message to another worker."""
pass
async def a_send(self, message: Union[Dict, str], recipient, request_reply: Optional[bool] = None):
async def a_send(
self, message: Union[Dict, str], recipient, request_reply: Optional[bool] = None
):
"""Send a message to another worker asynchronously."""
pass
def receive(self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None):
def receive(
self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None
):
"""Receive a message from another worker."""
pass
async def a_receive(self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None):
async def a_receive(
self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None
):
"""Receive a message from another worker asynchronously."""
pass
def reset(self):
"""Reset the worker."""
pass
def generate_reply(self, messages: Optional[List[Dict]] = None, sender=None, **kwargs) -> Union[str, Dict, None]:
def generate_reply(
self, messages: Optional[List[Dict]] = None, sender=None, **kwargs
) -> Union[str, Dict, None]:
"""Generate a reply based on received messages."""
pass
async def a_generate_reply(self, messages: Optional[List[Dict]] = None, sender=None, **kwargs) -> Union[str, Dict, None]:
async def a_generate_reply(
self, messages: Optional[List[Dict]] = None, sender=None, **kwargs
) -> Union[str, Dict, None]:
"""Generate a reply based on received messages asynchronously."""
pass
```
### 2.2 Attributes <a name="attributes"></a>
@ -121,6 +124,7 @@ class MyWorker(AbstractWorker):
def run(self, task: str):
print(f"{self.name} is performing task: {task}")
worker = MyWorker("Worker1")
worker.run("Collect data")
```
@ -155,6 +159,7 @@ The `a_send()` method is an asynchronous version of the `send()` method, allowin
```python
import asyncio
async def main():
worker1 = AbstractWorker("Worker1")
worker2 = AbstractWorker("Worker2")
@ -162,6 +167,7 @@ async def main():
message = "Hello, Worker2!"
await worker1.a_send(message, worker2)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
@ -208,6 +214,7 @@ The `a_receive()` method is an asynchronous version of the `receive()` method, a
```python
import asyncio
async def main():
worker1 = AbstractWorker("Worker1")
worker2 = AbstractWorker("Worker2")
@ -218,6 +225,7 @@ async def main():
await worker1.a_receive(message1, worker2)
await worker1.a_receive(message2, worker2)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
@ -233,6 +241,7 @@ class MyWorker(AbstractWorker):
def reset(self):
print(f"{self.name} has been reset.")
worker = MyWorker("Worker1")
worker.reset()
```
@ -253,13 +262,16 @@ The `generate_reply()` method is a placeholder for generating a reply based on r
```python
class MyWorker(AbstractWorker):
def generate_reply(self, messages: Optional[List[Dict]] = None, sender=None, **kwargs) -> Union[str, Dict, None]:
def generate_reply(
self, messages: Optional[List[Dict]] = None, sender=None, **kwargs
) -> Union[str, Dict, None]:
if messages:
# Generate a reply based on received messages
return f"Received {len(messages)} messages from {sender.name}."
else:
return None
worker1 = MyWorker("Worker1")
worker2 = MyWorker("Worker2")
@ -284,6 +296,7 @@ The `a_generate_reply()` method is an asynchronous version of the `generate_repl
```python
import asyncio
async def main():
worker1 = AbstractWorker("Worker1")
worker2 = AbstractWorker("Worker2")
@ -294,6 +307,7 @@ async def main():
if reply:
print(f"{worker2.name} generated a reply: {reply}")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
@ -313,11 +327,15 @@ class CustomWorker(AbstractWorker):
def run(self, task: str):
print(f"{self.name} is performing task: {task}")
def receive(self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None):
def receive(
self, message: Union[Dict, str], sender, request_reply: Optional[bool] = None
):
if isinstance(message, str):
print(f"{self.name} received a text message from {sender.name}: {message}")
elif isinstance(message, dict):
print(f"{self.name} received a dictionary message from {sender.name}: {message}")
print(
f"{self.name} received a dictionary message from {sender.name}: {message}"
)
```
### Step 2: Create Custom Worker Instances
@ -355,7 +373,9 @@ Customize the `generate_reply()` method to allow your workers to generate replie
```python
class CustomWorker(AbstractWorker):
def generate_reply(self, messages: Optional[List[Dict]] = None, sender=None, **kwargs) -> Union[str, Dict, None]:
def generate_reply(
self, messages: Optional[List[Dict]] = None, sender=None, **kwargs
) -> Union[str, Dict, None]:
if messages:
# Generate a reply based on received messages
return f"Received {len(messages)} messages from {sender.name}."

@ -49,8 +49,8 @@ Makes the Worker class callable. When an instance of the class is called, it wil
### **Example 1**: Basic usage with default parameters:
```python
from swarms.models import OpenAIChat
from swarms import Worker
from swarms.models import OpenAIChat
llm = OpenAIChat(
# enter your api key
@ -195,14 +195,13 @@ response = node.run(task)
# Print the response
print(response)
```
### **Example 3**: Usage with human in the loop:
```python
from swarms.models import OpenAIChat
from swarms import Worker
from swarms.models import OpenAIChat
llm = OpenAIChat(
# enter your api key
@ -223,7 +222,6 @@ node = Worker(
task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."
response = node.run(task)
print(response)
```
## **Mathematical Description**:

@ -0,0 +1,53 @@
# Why Swarms?
The need for multiple agents to work together in artificial intelligence (AI) and particularly in the context of Large Language Models (LLMs) stems from several inherent limitations and challenges in handling complex, dynamic, and multifaceted tasks with single-agent systems. Collaborating with multiple agents offers a pathway to enhance computational efficiency, cognitive diversity, and problem-solving capabilities. This section delves into the rationale behind employing multi-agent systems and strategizes on overcoming the associated expenses, such as API bills and hosting costs.
### Why Multiple Agents Are Necessary
#### 1. **Cognitive Diversity**
Different agents can bring varied perspectives, knowledge bases, and problem-solving approaches to a task. This diversity is crucial in complex problem-solving scenarios where a single approach might not be sufficient. Cognitive diversity enhances creativity, leading to innovative solutions and the ability to tackle a broader range of problems.
#### 2. **Specialization and Expertise**
In many cases, tasks are too complex for a single agent to handle efficiently. By dividing the task among multiple specialized agents, each can focus on a segment where it excels, thereby increasing the overall efficiency and effectiveness of the solution. This approach leverages the expertise of individual agents to achieve superior performance in tasks that require multifaceted knowledge and skills.
#### 3. **Scalability and Flexibility**
Multi-agent systems can more easily scale to handle large-scale or evolving tasks. Adding more agents to the system can increase its capacity or capabilities, allowing it to adapt to larger workloads or new types of tasks. This scalability is essential in dynamic environments where the demand and nature of tasks can change rapidly.
#### 4. **Robustness and Redundancy**
Collaboration among multiple agents enhances the system's robustness by introducing redundancy. If one agent fails or encounters an error, others can compensate, ensuring the system remains operational. This redundancy is critical in mission-critical applications where failure is not an option.
### Overcoming Expenses with API Bills and Hosting
Deploying multiple agents, especially when relying on cloud-based services or APIs, can incur significant costs. Here are strategies to manage and reduce these expenses:
#### 1. **Optimize Agent Efficiency**
Before scaling up the number of agents, ensure each agent operates as efficiently as possible. This can involve refining algorithms, reducing unnecessary API calls, and optimizing data processing to minimize computational requirements and, consequently, the associated costs.
#### 2. **Use Open Source and Self-Hosted Solutions**
Where possible, leverage open-source models and technologies that can be self-hosted. While there is an initial investment in setting up the infrastructure, over time, self-hosting can significantly reduce costs related to API calls and reliance on third-party services.
#### 3. **Implement Intelligent Caching**
Caching results for frequently asked questions or common tasks can drastically reduce the need for repeated computations or API calls. Intelligent caching systems can determine what information to store and for how long, optimizing the balance between fresh data and computational savings.
#### 4. **Dynamic Scaling and Load Balancing**
Use cloud services that offer dynamic scaling and load balancing to adjust the resources allocated based on the current demand. This ensures you're not paying for idle resources during low-usage periods while still being able to handle high demand when necessary.
#### 5. **Collaborative Cost-Sharing Models**
In scenarios where multiple stakeholders benefit from the multi-agent system, consider implementing a cost-sharing model. This approach distributes the financial burden among the users or beneficiaries, making it more sustainable.
#### 6. **Monitor and Analyze Costs**
Regularly monitor and analyze your usage and associated costs to identify potential savings. Many cloud providers offer tools to track and forecast expenses, helping you to adjust your usage patterns and configurations to minimize costs without sacrificing performance.
### Conclusion
The collaboration of multiple agents in AI systems presents a robust solution to the complexity, specialization, scalability, and robustness challenges inherent in single-agent approaches. While the associated costs can be significant, strategic optimization, leveraging open-source technologies, intelligent caching, dynamic resource management, collaborative cost-sharing, and diligent monitoring can mitigate these expenses. By adopting these strategies, organizations can harness the power of multi-agent systems to tackle complex problems more effectively and efficiently, ensuring the sustainable deployment of these advanced technologies.

@ -58,6 +58,7 @@ nav:
- Home:
- Overview: "index.md"
- Contributing: "contributing.md"
- Limitations of Individual Agents: "limits_of_individual_agents.md"
- Swarms:
- Overview: "swarms/index.md"
- swarms.agents:
@ -104,6 +105,7 @@ nav:
- stepinput: "swarms/structs/stepinput.md"
- artifact: "swarms/structs/artifact.md"
- task: "swarms/structs/task.md"
- Task Queue Base: "swarms/structs/taskqueuebase.md"
- Workflows:
- recursiveworkflow: "swarms/structs/recursiveworkflow.md"
- concurrentworkflow: "swarms/structs/concurrentworkflow.md"
@ -116,6 +118,7 @@ nav:
- groupchat: "swarms/structs/groupchat.md"
- swarmnetwork: "swarms/structs/swarmnetwork.md"
- groupchatmanager: "swarms/structs/groupchatmanager.md"
- MajorityVoting: "swarms/structs/majorityvoting.md"
- swarms.tokenizers:
- Language:
- Tokenizer: "swarms/tokenizers/tokenizer.md"

@ -1,8 +1,9 @@
# Description: This is an example of how to use the Agent class to run a multi-modal workflow
import os
from dotenv import load_dotenv
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.structs import Agent
from swarms import Agent, GPT4VisionAPI
# Load the environment variables
load_dotenv()

@ -0,0 +1,49 @@
import timeit
from swarms import Agent, ConcurrentWorkflow, Task
from swarms.agents.multion_agent import MultiOnAgent
# model
model = MultiOnAgent(multion_api_key="api-key")
# out = model.run("search for a recipe")
agent = Agent(
agent_name="MultiOnAgent",
description="A multi-on agent that performs browsing tasks.",
llm=model,
max_loops=1,
system_prompt=None,
)
# logger.info("[Agent][ID][MultiOnAgent][Initialized][Successfully")
# Task
task = Task(
agent=agent,
description="Download https://www.coachcamel.com/",
)
# Swarm
# logger.info(
# f"Running concurrent workflow with task: {task.description}"
# )
# Measure execution time
start_time = timeit.default_timer()
workflow = ConcurrentWorkflow(
max_workers=20,
autosave=True,
print_results=True,
return_results=True,
)
# Add task to workflow
workflow.add(task)
workflow.run()
# Calculate execution time
execution_time = timeit.default_timer() - start_time
# logger.info(f"Execution time: {execution_time} seconds")
print(f"Execution time: {execution_time} seconds")

@ -0,0 +1,72 @@
import os
from dotenv import load_dotenv
import swarms.prompts.security_team as stsp
from swarms.models import GPT4VisionAPI
from swarms.structs import Agent
# Load environment variables and initialize the Vision API
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
llm = GPT4VisionAPI(openai_api_key=api_key)
# Image for analysis
img = "bank_robbery.jpg"
# Initialize agents with respective prompts for security tasks
crowd_analysis_agent = Agent(
llm=llm,
sop=stsp.CROWD_ANALYSIS_AGENT_PROMPT,
max_loops=1,
multi_modal=True,
)
weapon_detection_agent = Agent(
llm=llm,
sop=stsp.WEAPON_DETECTION_AGENT_PROMPT,
max_loops=1,
multi_modal=True,
)
surveillance_monitoring_agent = Agent(
llm=llm,
sop=stsp.SURVEILLANCE_MONITORING_AGENT_PROMPT,
max_loops=1,
multi_modal=True,
)
emergency_response_coordinator = Agent(
llm=llm,
sop=stsp.EMERGENCY_RESPONSE_COORDINATOR_PROMPT,
max_loops=1,
multi_modal=True,
)
# Run agents with respective tasks on the same image
crowd_analysis = crowd_analysis_agent.run(
"Analyze the crowd dynamics in the scene", img
)
weapon_detection_analysis = weapon_detection_agent.run(
"Inspect the scene for any potential threats", img
)
surveillance_monitoring_analysis = surveillance_monitoring_agent.run(
"Monitor the overall scene for unusual activities", img
)
emergency_response_analysis = emergency_response_coordinator.run(
"Develop a response plan based on the scene analysis", img
)
# Process and output results for each task
# Example output (uncomment to use):
print(f"Crowd Analysis: {crowd_analysis}")
print(f"Weapon Detection Analysis: {weapon_detection_analysis}")
print(
"Surveillance Monitoring Analysis:"
f" {surveillance_monitoring_analysis}"
)
print(f"Emergency Response Analysis: {emergency_response_analysis}")

@ -3,12 +3,11 @@ import os
from dotenv import load_dotenv
from swarms import (
OpenAIChat,
Conversation,
OpenAIChat,
detect_markdown,
extract_code_from_markdown,
)
from swarms.tools.code_executor import CodeExecutor
conv = Conversation(

@ -1,5 +1,6 @@
# Import necessary libraries
from transformers import AutoModelForCausalLM, AutoTokenizer
from swarms import ToolAgent
# Load the pre-trained model and tokenizer

@ -1,8 +1,10 @@
# Importing necessary modules
import os
from dotenv import load_dotenv
from swarms.agents.worker_agent import Worker
from swarms import OpenAIChat
from swarms.agents.worker_agent import Worker
# Loading environment variables from .env file
load_dotenv()

@ -1,5 +1,7 @@
import os
from dotenv import load_dotenv
from swarms.models import Anthropic, OpenAIChat
from swarms.prompts.accountant_swarm_prompts import (
DECISION_MAKING_PROMPT,

@ -1,9 +1,11 @@
import random
import os
import random
from dotenv import load_dotenv
from swarms.models import OpenAIChat
from swarms.structs import Agent
from swarms.models.stable_diffusion import StableDiffusion
from swarms.structs import Agent
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")

@ -1,5 +1,5 @@
from swarms.structs import Agent
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.structs import Agent
llm = GPT4VisionAPI()

@ -1,4 +1,5 @@
import re
from swarms.models.openai_models import OpenAIChat

@ -1,7 +1,9 @@
import os
from autotemp import AutoTemp
from termcolor import colored
from swarms.models import OpenAIChat
from autotemp import AutoTemp
from swarms.structs import SequentialWorkflow

@ -14,6 +14,7 @@ Documentation agent -> Tests agent
"""
import os
from dotenv import load_dotenv

@ -1,9 +1,10 @@
import os
from dotenv import load_dotenv
from swarms.models import OpenAIChat
from swarms.models.stable_diffusion import StableDiffusion
from swarms.structs import Agent, SequentialWorkflow
import swarms.prompts.education as edu_prompts
from swarms import Agent, SequentialWorkflow
from swarms.models import OpenAIChat
# Load environment variables
load_dotenv()
@ -15,9 +16,6 @@ llm = OpenAIChat(
openai_api_key=api_key, temperature=0.5, max_tokens=3000
)
# Initialize Stable Diffusion
sd_api = StableDiffusion(api_key=stability_api_key)
# User preferences (can be dynamically set in a real application)
user_preferences = {
"subjects": "Cognitive Architectures",
@ -60,8 +58,6 @@ workflow.add(sample_lesson_agent, "Generate a practice test")
workflow.run()
# Generate an image using Stable Diffusion
image_result = sd_api.run(image_prompt)
# Output results for each task
for task in workflow.tasks:
print(
@ -70,7 +66,4 @@ for task in workflow.tasks:
)
# Output image result
print(
"Image Generation Task: Generate an image for the interactive"
f" lesson\nResult: {image_result}"
)
print("Image Generation Task: Generate an image for the interactive")

@ -1,5 +1,7 @@
import os
from dotenv import load_dotenv
from swarms.models.gemini import Gemini
from swarms.prompts.react import react_prompt

@ -1,5 +1,7 @@
import os
from dotenv import load_dotenv
from swarms.models.gemini import Gemini
from swarms.prompts.react import react_prompt

@ -1,12 +1,12 @@
import os
from dotenv import load_dotenv
from termcolor import colored
from swarms.models import OpenAIChat
from swarms.prompts.code_interpreter import CODE_INTERPRETER
from swarms.prompts.programming import DOCUMENTATION_SOP, TEST_SOP
from swarms.structs import Agent
from swarms.prompts.programming import TEST_SOP, DOCUMENTATION_SOP
from termcolor import colored
load_dotenv()

@ -1,9 +1,8 @@
from swarms.structs import Agent
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)
from swarms.structs import Agent
llm = GPT4VisionAPI()

@ -1,8 +1,10 @@
import os
from dotenv import load_dotenv
from swarms import Agent
from langchain.llms import OpenAIChat
from swarms import Agent
# Loading environment variables from .env file
load_dotenv()

@ -1,16 +1,18 @@
from swarms.structs import Agent
import os
from dotenv import load_dotenv
from swarms.models import GPT4VisionAPI
from swarms.prompts.logistics import (
Efficiency_Agent_Prompt,
Health_Security_Agent_Prompt,
Quality_Control_Agent_Prompt,
Productivity_Agent_Prompt,
Quality_Control_Agent_Prompt,
Safety_Agent_Prompt,
Security_Agent_Prompt,
Sustainability_Agent_Prompt,
Efficiency_Agent_Prompt,
)
from swarms.structs import Agent
# from swarms.utils.banana_wrapper import banana

@ -1,6 +1,5 @@
from swarms.structs import Agent
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.structs import Agent
llm = GPT4VisionAPI()

@ -16,10 +16,12 @@ task: Generate an image of a swarm of bees -> Image generator -> GPT4V evaluates
"""
import os
from dotenv import load_dotenv
from termcolor import colored
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.models.stable_diffusion import StableDiffusion
from termcolor import colored
# Load the environment variables
load_dotenv()

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save