commit
435c4b7b9e
@ -1,3 +0,0 @@
|
||||
This section of the documentation is dedicated to examples highlighting Swarms functionality.
|
||||
|
||||
We try to keep all examples up to date, but if you think there is a bug please [submit a pull request](https://github.com/kyegomez/swarms-docs/tree/main/docs/examples). We are also more than happy to include new examples)
|
@ -0,0 +1,127 @@
|
||||
## Swarms Framework Conceptual Breakdown
|
||||
|
||||
The `swarms` framework is a sophisticated structure designed to orchestrate the collaborative work of multiple agents in a hierarchical manner. This breakdown provides a conceptual and visual representation of the framework, highlighting the interactions between models, tools, memory, agents, and swarms.
|
||||
|
||||
### Hierarchical Structure
|
||||
|
||||
The framework can be visualized as a multi-layered hierarchy:
|
||||
|
||||
1. **Models, Tools, Memory**: These form the foundational components that agents utilize to perform tasks.
|
||||
2. **Agents**: Individual entities that encapsulate specific functionalities, utilizing models, tools, and memory.
|
||||
3. **Swarm**: A collection of multiple agents working together in a coordinated manner.
|
||||
4. **Structs**: High-level structures that organize and manage swarms, enabling complex workflows and interactions.
|
||||
|
||||
### Visual Representation
|
||||
|
||||
Below are visual graphs illustrating the hierarchical and tree structure of the `swarms` framework.
|
||||
|
||||
#### 1. Foundational Components: Models, Tools, Memory
|
||||
|
||||
```mermaid
|
||||
graph TD;
|
||||
Models --> Agents
|
||||
Tools --> Agents
|
||||
Memory --> Agents
|
||||
subgraph Foundational_Components
|
||||
Models
|
||||
Tools
|
||||
Memory
|
||||
end
|
||||
```
|
||||
|
||||
#### 2. Agents and Their Interactions
|
||||
|
||||
```mermaid
|
||||
graph TD;
|
||||
Agents --> Swarm
|
||||
subgraph Agents_Collection
|
||||
Agent1
|
||||
Agent2
|
||||
Agent3
|
||||
end
|
||||
subgraph Individual_Agents
|
||||
Agent1 --> Models
|
||||
Agent1 --> Tools
|
||||
Agent1 --> Memory
|
||||
Agent2 --> Models
|
||||
Agent2 --> Tools
|
||||
Agent2 --> Memory
|
||||
Agent3 --> Models
|
||||
Agent3 --> Tools
|
||||
Agent3 --> Memory
|
||||
end
|
||||
```
|
||||
|
||||
#### 3. Multiple Agents Form a Swarm
|
||||
|
||||
```mermaid
|
||||
graph TD;
|
||||
Swarm1 --> Struct
|
||||
Swarm2 --> Struct
|
||||
Swarm3 --> Struct
|
||||
subgraph Swarms_Collection
|
||||
Swarm1
|
||||
Swarm2
|
||||
Swarm3
|
||||
end
|
||||
subgraph Individual_Swarms
|
||||
Swarm1 --> Agent1
|
||||
Swarm1 --> Agent2
|
||||
Swarm1 --> Agent3
|
||||
Swarm2 --> Agent4
|
||||
Swarm2 --> Agent5
|
||||
Swarm2 --> Agent6
|
||||
Swarm3 --> Agent7
|
||||
Swarm3 --> Agent8
|
||||
Swarm3 --> Agent9
|
||||
end
|
||||
```
|
||||
|
||||
#### 4. Structs Organizing Multiple Swarms
|
||||
|
||||
```mermaid
|
||||
graph TD;
|
||||
Struct --> Swarms_Collection
|
||||
subgraph High_Level_Structs
|
||||
Struct1
|
||||
Struct2
|
||||
Struct3
|
||||
end
|
||||
subgraph Struct1
|
||||
Swarm1
|
||||
Swarm2
|
||||
end
|
||||
subgraph Struct2
|
||||
Swarm3
|
||||
end
|
||||
subgraph Struct3
|
||||
Swarm4
|
||||
Swarm5
|
||||
end
|
||||
```
|
||||
|
||||
### Directory Breakdown
|
||||
|
||||
The directory structure of the `swarms` framework is organized to support its hierarchical architecture:
|
||||
|
||||
```sh
|
||||
swarms/
|
||||
├── agents/
|
||||
├── artifacts/
|
||||
├── marketplace/
|
||||
├── memory/
|
||||
├── models/
|
||||
├── prompts/
|
||||
├── schemas/
|
||||
├── structs/
|
||||
├── telemetry/
|
||||
├── tools/
|
||||
├── utils/
|
||||
└── __init__.py
|
||||
```
|
||||
|
||||
### Summary
|
||||
|
||||
The `swarms` framework is designed to facilitate complex multi-agent interactions through a structured and layered approach. By leveraging foundational components like models, tools, and memory, individual agents are empowered to perform specialized tasks. These agents are then coordinated within swarms to achieve collective goals, and swarms are managed within high-level structs to orchestrate sophisticated workflows.
|
||||
|
||||
This hierarchical design ensures scalability, flexibility, and robustness, making the `swarms` framework a powerful tool for various applications in AI, data analysis, optimization, and beyond.
|
@ -0,0 +1,6 @@
|
||||
# Getting Started with Multi-Agent Collaboration Using the Multi-Agent Github Template
|
||||
|
||||
|
||||
The Multi-Agent Github Template, a radically simple, reliable, and high-performance framework, is designed to empower developers and prompt engineers to harness the full potential of multi-agent collaboration. [LINK](https://medium.com/@kyeg/getting-started-with-multi-agent-collaboration-using-the-multi-agent-github-template-0f0a6cba0dc0)
|
||||
|
||||
[GITHUB](https://github.com/kyegomez/Multi-Agent-Template-App)
|
@ -0,0 +1,363 @@
|
||||
# Enterprise-Grade and Production Ready Agents
|
||||
|
||||
Swarms is an enterprise grade and production ready multi-agent collaboration framework that enables you to orchestrate many agents to work collaboratively at scale to automate real-world activities.
|
||||
|
||||
| **Feature** | **Description** | **Performance Impact** | **Documentation Link** |
|
||||
|------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------|-------------------------------|
|
||||
| Models | Pre-trained models that can be utilized for various tasks within the swarm framework. | ⭐⭐⭐ | [Documentation](https://docs.swarms.world/en/latest/swarms/models/) |
|
||||
| Models APIs | APIs to interact with and utilize the models effectively, providing interfaces for inference, training, and fine-tuning. | ⭐⭐⭐ | [Documentation](https://docs.swarms.world/en/latest/swarms/models/) |
|
||||
| Agents with Tools | Agents equipped with specialized tools to perform specific tasks more efficiently, such as data processing, analysis, or interaction with external systems. | ⭐⭐⭐⭐ | [Documentation](https://medium.com/@kyeg/the-swarms-tool-system-functions-pydantic-basemodels-as-tools-and-radical-customization-c2a2e227b8ca) |
|
||||
| Agents with Memory | Mechanisms for agents to store and recall past interactions, improving learning and adaptability over time. | ⭐⭐⭐⭐ | [Documentation](https://github.com/kyegomez/swarms/blob/master/playground/structs/agent/agent_with_longterm_memory.py) |
|
||||
| Multi-Agent Orchestration | Coordination of multiple agents to work together seamlessly on complex tasks, leveraging their individual strengths to achieve higher overall performance. | ⭐⭐⭐⭐⭐ | [Documentation]() |
|
||||
|
||||
The performance impact is rated on a scale from one to five stars, with multi-agent orchestration being the highest due to its ability to combine the strengths of multiple agents and optimize task execution.
|
||||
|
||||
----
|
||||
|
||||
## Install 💻
|
||||
`$ pip3 install -U swarms`
|
||||
|
||||
---
|
||||
|
||||
# Usage Examples 🤖
|
||||
|
||||
### Google Collab Example
|
||||
Run example in Collab: <a target="_blank" href="https://colab.research.google.com/github/kyegomez/swarms/blob/master/playground/swarms_example.ipynb">
|
||||
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
|
||||
</a>
|
||||
|
||||
---
|
||||
|
||||
## `Agents`
|
||||
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:
|
||||
|
||||
✅ Any LLM / Any framework
|
||||
|
||||
✅ Extremely customize-able with max loops, autosaving, import docs (PDFS, TXT, CSVs, etc), tool usage, etc etc
|
||||
|
||||
✅ Long term memory database with RAG (ChromaDB, Pinecone, Qdrant)
|
||||
|
||||
```python
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Import the OpenAIChat model and the Agent struct
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
# Load the environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Get the API key from the environment
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
## Initialize the workflow
|
||||
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.")
|
||||
```
|
||||
|
||||
|
||||
### `Agent` + Long Term Memory
|
||||
`Agent` equipped with quasi-infinite long term memory. Great for long document understanding, analysis, and retrieval.
|
||||
|
||||
```python
|
||||
from swarms import Agent, OpenAIChat
|
||||
from playground.memory.chromadb_example import ChromaDB # Copy and paste the code and put it in your own local directory.
|
||||
|
||||
# Making an instance of the ChromaDB class
|
||||
memory = ChromaDB(
|
||||
metric="cosine",
|
||||
n_results=3,
|
||||
output_dir="results",
|
||||
docs_folder="docs",
|
||||
)
|
||||
|
||||
# Initializing the agent with the Gemini instance and other parameters
|
||||
agent = Agent(
|
||||
agent_name="Covid-19-Chat",
|
||||
agent_description=(
|
||||
"This agent provides information about COVID-19 symptoms."
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops="auto",
|
||||
autosave=True,
|
||||
verbose=True,
|
||||
long_term_memory=memory,
|
||||
stopping_condition="finish",
|
||||
)
|
||||
|
||||
# Defining the task and image path
|
||||
task = ("What are the symptoms of COVID-19?",)
|
||||
|
||||
# Running the agent with the specified task and image
|
||||
out = agent.run(task)
|
||||
print(out)
|
||||
|
||||
```
|
||||
|
||||
|
||||
### `Agent` ++ Long Term Memory ++ Tools!
|
||||
An LLM equipped with long term memory and tools, a full stack agent capable of automating all and any digital tasks given a good prompt.
|
||||
|
||||
```python
|
||||
from swarms import Agent, ChromaDB, OpenAIChat
|
||||
|
||||
# Making an instance of the ChromaDB class
|
||||
memory = ChromaDB(
|
||||
metric="cosine",
|
||||
n_results=3,
|
||||
output_dir="results",
|
||||
docs_folder="docs",
|
||||
)
|
||||
|
||||
# Initialize a tool
|
||||
def search_api(query: str):
|
||||
# Add your logic here
|
||||
return query
|
||||
|
||||
# Initializing the agent with the Gemini instance and other parameters
|
||||
agent = Agent(
|
||||
agent_name="Covid-19-Chat",
|
||||
agent_description=(
|
||||
"This agent provides information about COVID-19 symptoms."
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops="auto",
|
||||
autosave=True,
|
||||
verbose=True,
|
||||
long_term_memory=memory,
|
||||
stopping_condition="finish",
|
||||
tools=[search_api],
|
||||
)
|
||||
|
||||
# Defining the task and image path
|
||||
task = ("What are the symptoms of COVID-19?",)
|
||||
|
||||
# Running the agent with the specified task and image
|
||||
out = agent.run(task)
|
||||
print(out)
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Devin
|
||||
Implementation of Devin in less than 90 lines of code with several tools:
|
||||
terminal, browser, and edit files.
|
||||
|
||||
```python
|
||||
from swarms import Agent, Anthropic
|
||||
import subprocess
|
||||
|
||||
# Model
|
||||
llm = Anthropic(
|
||||
temperature=0.1,
|
||||
)
|
||||
|
||||
# Tools
|
||||
def terminal(
|
||||
code: str,
|
||||
):
|
||||
"""
|
||||
Run code in the terminal.
|
||||
|
||||
Args:
|
||||
code (str): The code to run in the terminal.
|
||||
|
||||
Returns:
|
||||
str: The output of the code.
|
||||
"""
|
||||
out = subprocess.run(
|
||||
code, shell=True, capture_output=True, text=True
|
||||
).stdout
|
||||
return str(out)
|
||||
|
||||
def browser(query: str):
|
||||
"""
|
||||
Search the query in the browser with the `browser` tool.
|
||||
|
||||
Args:
|
||||
query (str): The query to search in the browser.
|
||||
|
||||
Returns:
|
||||
str: The search results.
|
||||
"""
|
||||
import webbrowser
|
||||
|
||||
url = f"https://www.google.com/search?q={query}"
|
||||
webbrowser.open(url)
|
||||
return f"Searching for {query} in the browser."
|
||||
|
||||
def create_file(file_path: str, content: str):
|
||||
"""
|
||||
Create a file using the file editor tool.
|
||||
|
||||
Args:
|
||||
file_path (str): The path to the file.
|
||||
content (str): The content to write to the file.
|
||||
|
||||
Returns:
|
||||
str: The result of the file creation operation.
|
||||
"""
|
||||
with open(file_path, "w") as file:
|
||||
file.write(content)
|
||||
return f"File {file_path} created successfully."
|
||||
|
||||
def file_editor(file_path: str, mode: str, content: str):
|
||||
"""
|
||||
Edit a file using the file editor tool.
|
||||
|
||||
Args:
|
||||
file_path (str): The path to the file.
|
||||
mode (str): The mode to open the file in.
|
||||
content (str): The content to write to the file.
|
||||
|
||||
Returns:
|
||||
str: The result of the file editing operation.
|
||||
"""
|
||||
with open(file_path, mode) as file:
|
||||
file.write(content)
|
||||
return f"File {file_path} edited successfully."
|
||||
|
||||
|
||||
# Agent
|
||||
agent = Agent(
|
||||
agent_name="Devin",
|
||||
system_prompt=(
|
||||
"Autonomous agent that can interact with humans and other"
|
||||
" agents. Be Helpful and Kind. Use the tools provided to"
|
||||
" assist the user. Return all code in markdown format."
|
||||
),
|
||||
llm=llm,
|
||||
max_loops="auto",
|
||||
autosave=True,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
interactive=True,
|
||||
tools=[terminal, browser, file_editor, create_file],
|
||||
code_interpreter=True,
|
||||
# streaming=True,
|
||||
)
|
||||
|
||||
# Run the agent
|
||||
out = agent("Create a new file for a plan to take over the world.")
|
||||
print(out)
|
||||
```
|
||||
|
||||
|
||||
### `Agent`with Pydantic BaseModel as Output Type
|
||||
The following is an example of an agent that intakes a pydantic basemodel and outputs it at the same time:
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field
|
||||
from swarms import Anthropic, Agent
|
||||
|
||||
|
||||
# Initialize the schema for the person's information
|
||||
class Schema(BaseModel):
|
||||
name: str = Field(..., title="Name of the person")
|
||||
agent: int = Field(..., title="Age of the person")
|
||||
is_student: bool = Field(..., title="Whether the person is a student")
|
||||
courses: list[str] = Field(
|
||||
..., title="List of courses the person is taking"
|
||||
)
|
||||
|
||||
|
||||
# Convert the schema to a JSON string
|
||||
tool_schema = Schema(
|
||||
name="Tool Name",
|
||||
agent=1,
|
||||
is_student=True,
|
||||
courses=["Course1", "Course2"],
|
||||
)
|
||||
|
||||
# Define the task to generate a person's information
|
||||
task = "Generate a person's information based on the following schema:"
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Person Information Generator",
|
||||
system_prompt=(
|
||||
"Generate a person's information based on the following schema:"
|
||||
),
|
||||
# Set the tool schema to the JSON string -- this is the key difference
|
||||
tool_schema=tool_schema,
|
||||
llm=Anthropic(),
|
||||
max_loops=3,
|
||||
autosave=True,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
interactive=True,
|
||||
# Set the output type to the tool schema which is a BaseModel
|
||||
output_type=tool_schema, # or dict, or str
|
||||
metadata_output_type="json",
|
||||
# List of schemas that the agent can handle
|
||||
list_tool_schemas=[tool_schema],
|
||||
function_calling_format_type="OpenAI",
|
||||
function_calling_type="json", # or soon yaml
|
||||
)
|
||||
|
||||
# Run the agent to generate the person's information
|
||||
generated_data = agent.run(task)
|
||||
|
||||
# Print the generated data
|
||||
print(f"Generated data: {generated_data}")
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Multi Modal Autonomous Agent
|
||||
Run the agent with multiple modalities useful for various real-world tasks in manufacturing, logistics, and health.
|
||||
|
||||
```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
|
||||
|
||||
# Load the environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Get the API key from the environment
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
|
||||
# Initialize the language model
|
||||
llm = GPT4VisionAPI(
|
||||
openai_api_key=api_key,
|
||||
max_tokens=500,
|
||||
)
|
||||
|
||||
# Initialize the task
|
||||
task = (
|
||||
"Analyze this image of an assembly line and identify any issues such as"
|
||||
" misaligned parts, defects, or deviations from the standard assembly"
|
||||
" process. IF there is anything unsafe in the image, explain why it is"
|
||||
" unsafe and how it could be improved."
|
||||
)
|
||||
img = "assembly_line.jpg"
|
||||
|
||||
## Initialize the workflow
|
||||
agent = Agent(
|
||||
llm=llm, max_loops="auto", autosave=True, dashboard=True, multi_modal=True
|
||||
)
|
||||
|
||||
# Run the workflow on a task
|
||||
agent.run(task=task, img=img)
|
||||
```
|
||||
----
|
||||
|
@ -0,0 +1,379 @@
|
||||
# MixtureOfAgents Class Documentation
|
||||
|
||||
## Overview
|
||||
|
||||
The `MixtureOfAgents` class represents a mixture of agents operating within a swarm. The workflow of the swarm follows a parallel → sequential → parallel → final output agent process. This implementation is inspired by concepts discussed in the paper: [https://arxiv.org/pdf/2406.04692](https://arxiv.org/pdf/2406.04692).
|
||||
|
||||
The class is designed to manage a collection of agents, orchestrate their execution in layers, and handle the final aggregation of their outputs through a designated final agent. This architecture facilitates complex, multi-step processing where intermediate results are refined through successive layers of agent interactions.
|
||||
|
||||
## Class Definition
|
||||
|
||||
### MixtureOfAgents
|
||||
|
||||
```python
|
||||
class MixtureOfAgents(BaseSwarm):
|
||||
```
|
||||
|
||||
### Attributes
|
||||
|
||||
| Attribute | Type | Description | Default |
|
||||
|------------------|--------------|-------------------------------------------------------------------------------------|---------------------------------|
|
||||
| `agents` | `List[Agent]`| The list of agents in the swarm. | `None` |
|
||||
| `flow` | `str` | The flow of the swarm. | `parallel -> sequential -> parallel -> final output agent` |
|
||||
| `max_loops` | `int` | The maximum number of loops to run. | `1` |
|
||||
| `verbose` | `bool` | Flag indicating whether to print verbose output. | `True` |
|
||||
| `layers` | `int` | The number of layers in the swarm. | `3` |
|
||||
| `rules` | `str` | The rules for the swarm. | `None` |
|
||||
| `final_agent` | `Agent` | The agent to handle the final output processing. | `None` |
|
||||
| `auto_save` | `bool` | Flag indicating whether to auto-save the metadata to a file. | `False` |
|
||||
| `saved_file_name`| `str` | The name of the file where the metadata will be saved. | `"moe_swarm.json"` |
|
||||
|
||||
## Methods
|
||||
|
||||
### `__init__`
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Parameter | Type | Description | Default |
|
||||
|------------------|--------------|-----------------------------------------------------------------------------------------------|----------------------------------------|
|
||||
| `name` | `str` | The name of the swarm. | `"MixtureOfAgents"` |
|
||||
| `description` | `str` | A brief description of the swarm. | `"A swarm of agents that run in parallel and sequentially."` |
|
||||
| `agents` | `List[Agent]`| The list of agents in the swarm. | `None` |
|
||||
| `max_loops` | `int` | The maximum number of loops to run. | `1` |
|
||||
| `verbose` | `bool` | Flag indicating whether to print verbose output. | `True` |
|
||||
| `layers` | `int` | The number of layers in the swarm. | `3` |
|
||||
| `rules` | `str` | The rules for the swarm. | `None` |
|
||||
| `final_agent` | `Agent` | The agent to handle the final output processing. | `None` |
|
||||
| `auto_save` | `bool` | Flag indicating whether to auto-save the metadata to a file. | `False` |
|
||||
| `saved_file_name`| `str` | The name of the file where the metadata will be saved. | `"moe_swarm.json"` |
|
||||
|
||||
### `agent_check`
|
||||
|
||||
```python
|
||||
def agent_check(self):
|
||||
```
|
||||
|
||||
#### Description
|
||||
|
||||
Checks if the provided `agents` attribute is a list of `Agent` instances. Raises a `TypeError` if the validation fails.
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```python
|
||||
moe_swarm = MixtureOfAgents(agents=[agent1, agent2])
|
||||
moe_swarm.agent_check() # Validates the agents
|
||||
```
|
||||
|
||||
### `final_agent_check`
|
||||
|
||||
```python
|
||||
def final_agent_check(self):
|
||||
```
|
||||
|
||||
#### Description
|
||||
|
||||
Checks if the provided `final_agent` attribute is an instance of `Agent`. Raises a `TypeError` if the validation fails.
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```python
|
||||
moe_swarm = MixtureOfAgents(final_agent=final_agent)
|
||||
moe_swarm.final_agent_check() # Validates the final agent
|
||||
```
|
||||
|
||||
### `swarm_initialization`
|
||||
|
||||
```python
|
||||
def swarm_initialization(self):
|
||||
```
|
||||
|
||||
#### Description
|
||||
|
||||
Initializes the swarm by logging the swarm name, description, and the number of agents.
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```python
|
||||
moe_swarm = MixtureOfAgents(agents=[agent1, agent2])
|
||||
moe_swarm.swarm_initialization() # Initializes the swarm
|
||||
```
|
||||
|
||||
### `run`
|
||||
|
||||
```python
|
||||
def run(self, task: str = None, *args, **kwargs):
|
||||
```
|
||||
|
||||
#### Parameters
|
||||
|
||||
| Parameter | Type | Description | Default |
|
||||
|-----------|--------|---------------------------------|---------|
|
||||
| `task` | `str` | The task to be performed by the swarm. | `None` |
|
||||
| `*args` | `tuple`| Additional arguments. | `None` |
|
||||
| `**kwargs`| `dict` | Additional keyword arguments. | `None` |
|
||||
|
||||
#### Returns
|
||||
|
||||
| Type | Description |
|
||||
|-------|---------------------------------------------|
|
||||
| `str` | The conversation history as a string. |
|
||||
|
||||
#### Description
|
||||
|
||||
Runs the swarm with the given task, orchestrates the execution of agents through the specified layers, and returns the conversation history.
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```python
|
||||
moe_swarm = MixtureOfAgents(agents=[agent1, agent2], final_agent=final_agent)
|
||||
history = moe_swarm.run(task="Solve this problem.")
|
||||
print(history)
|
||||
```
|
||||
|
||||
## Detailed Explanation
|
||||
|
||||
### Initialization
|
||||
|
||||
The `__init__` method initializes the swarm with the provided parameters, sets up the conversation rules, and invokes the initialization of the swarm. It also ensures the validity of the `agents` and `final_agent` attributes by calling the `agent_check` and `final_agent_check` methods respectively.
|
||||
|
||||
### Agent Validation
|
||||
|
||||
The `agent_check` method validates whether the `agents` attribute is a list of `Agent` instances, while the `final_agent_check` method validates whether the `final_agent` is an instance of `Agent`. These checks are crucial to ensure that the swarm operates correctly with the appropriate agent types.
|
||||
|
||||
### Swarm Initialization
|
||||
|
||||
The `swarm_initialization` method logs essential information about the swarm, including its name, description, and the number of agents. This provides a clear starting point for the swarm's operations and facilitates debugging and monitoring.
|
||||
|
||||
### Running the Swarm
|
||||
|
||||
The `run` method is the core of the `MixtureOfAgents` class. It orchestrates the execution of agents through multiple layers, collects their outputs, and processes the final output using the `final_agent`. The conversation history is maintained and updated throughout this process, allowing for a seamless flow of information and responses.
|
||||
|
||||
During each layer, the method iterates over the agents, invokes their `run` method with the current conversation history, and logs the outputs. These outputs are then added to the conversation, and the history is updated for the next layer.
|
||||
|
||||
After all layers are completed, the final output agent processes the entire conversation history, and the metadata is created and optionally saved to a file. This metadata includes details about the layers, agent runs, and final output, providing a comprehensive record of the swarm's execution.
|
||||
|
||||
## Additional Information and Tips
|
||||
|
||||
### Common Issues and Solutions
|
||||
|
||||
- **Type Errors**: Ensure that all agents in the `agents` list and the `final_agent` are instances of the `Agent` class. The `agent_check` and `final_agent_check` methods help validate this.
|
||||
- **Verbose Logging**: Use the `verbose` flag to control the verbosity of the output. This can help with debugging or reduce clutter in the logs.
|
||||
- **Auto-Save Feature**: Utilize the `auto_save` flag to automatically save the metadata to a file. This can be useful for keeping records of the swarm's operations without manual intervention.
|
||||
|
||||
### References and Resources
|
||||
|
||||
For further reading and background information on the concepts used in the `MixtureOfAgents` class, refer to the paper: [https://arxiv.org/pdf/2406.04692](https://arxiv.org/pdf/2406.04692).
|
||||
|
||||
### Usage Examples
|
||||
|
||||
#### Example 1: Basic Initialization and Run
|
||||
|
||||
```python
|
||||
from swarms import MixtureOfAgents, Agent, OpenAIOpenAIChat
|
||||
|
||||
# Define agents
|
||||
director = Agent(
|
||||
agent_name="Director",
|
||||
system_prompt="Directs the tasks for the accountants",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="director.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 1
|
||||
accountant1 = Agent(
|
||||
agent_name="Accountant1",
|
||||
system_prompt="Prepares financial statements",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant1.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 2
|
||||
accountant2 = Agent(
|
||||
agent_name="Accountant2",
|
||||
system_prompt="Audits financial records",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant2.json",
|
||||
)
|
||||
|
||||
|
||||
# Initialize the MixtureOfAgents
|
||||
moe_swarm = MixtureOfAgents(agents=[director, accountant1, accountant2], final_agent=director)
|
||||
|
||||
# Run the swarm
|
||||
history = moe_swarm.run(task="Perform task X.")
|
||||
print(history)
|
||||
```
|
||||
|
||||
#### Example 2: Verbose Output and Auto-Save
|
||||
|
||||
```python
|
||||
from swarms import MixtureOfAgents, Agent, OpenAIOpenAIChat
|
||||
|
||||
# Define Agents
|
||||
# Define agents
|
||||
director = Agent(
|
||||
agent_name="Director",
|
||||
system_prompt="Directs the tasks for the accountants",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="director.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 1
|
||||
accountant1 = Agent(
|
||||
agent_name="Accountant1",
|
||||
system_prompt="Prepares financial statements",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant1.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 2
|
||||
accountant2 = Agent(
|
||||
agent_name="Accountant2",
|
||||
system_prompt="Audits financial records",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant2.json",
|
||||
)
|
||||
|
||||
# Initialize the MixtureOfAgents with verbose output and auto-save enabled
|
||||
moe_swarm = MixtureOfAgents(
|
||||
agents=[director, accountant1, accountant2],
|
||||
final_agent=director,
|
||||
verbose=True,
|
||||
auto_save=True
|
||||
)
|
||||
|
||||
# Run the swarm
|
||||
history = moe_swarm.run(task="Analyze data set Y.")
|
||||
print(history)
|
||||
```
|
||||
|
||||
#### Example 3: Custom Rules and Multiple Layers
|
||||
|
||||
```python
|
||||
from swarms import MixtureOfAgents, Agent, OpenAIOpenAIChat
|
||||
|
||||
# Define agents
|
||||
# Initialize the director agent
|
||||
director = Agent(
|
||||
agent_name="Director",
|
||||
system_prompt="Directs the tasks for the accountants",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="director.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 1
|
||||
accountant1 = Agent(
|
||||
agent_name="Accountant1",
|
||||
system_prompt="Prepares financial statements",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant1.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 2
|
||||
accountant2 = Agent(
|
||||
agent_name="Accountant2",
|
||||
system_prompt="Audits financial records",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant2.json",
|
||||
)
|
||||
|
||||
# Initialize the MixtureOfAgents with custom rules and multiple layers
|
||||
moe_swarm = MixtureOfAgents(
|
||||
agents=[director, accountant1, accountant2],
|
||||
final_agent=director,
|
||||
layers=5,
|
||||
rules="Custom rules for the swarm"
|
||||
)
|
||||
|
||||
# Run the swarm
|
||||
history = moe_swarm.run(task="Optimize process Z.")
|
||||
print(history)
|
||||
```
|
||||
|
||||
This comprehensive documentation provides a detailed understanding of the `MixtureOfAgents` class, its attributes, methods, and usage. The examples illustrate how to initialize and run the swarm, demonstrating its flexibility and capability to handle various tasks and configurations.
|
||||
|
||||
|
||||
# Conclusion
|
||||
|
||||
The `MixtureOfAgents` class is a powerful and flexible framework for managing and orchestrating a swarm of agents. By following a structured approach of parallel and sequential processing, it enables the implementation of complex multi-step workflows where intermediate results are refined through multiple layers of agent interactions. This architecture is particularly suitable for tasks that require iterative processing, collaboration among diverse agents, and sophisticated aggregation of outputs.
|
||||
|
||||
### Key Takeaways
|
||||
|
||||
1. **Flexible Initialization**: The class allows for customizable initialization with various parameters, enabling users to tailor the swarm's configuration to their specific needs.
|
||||
2. **Robust Agent Management**: With built-in validation methods, the class ensures that all agents and the final agent are correctly instantiated, preventing runtime errors and facilitating smooth execution.
|
||||
3. **Layered Processing**: The layered approach to processing allows for intermediate results to be iteratively refined, enhancing the overall output quality.
|
||||
4. **Verbose Logging and Auto-Save**: These features aid in debugging, monitoring, and record-keeping, providing transparency and ease of management.
|
||||
5. **Comprehensive Documentation**: The detailed class and method documentation, along with numerous usage examples, provide a clear and thorough understanding of how to leverage the `MixtureOfAgents` class effectively.
|
||||
|
||||
### Practical Applications
|
||||
|
||||
The `MixtureOfAgents` class can be applied in various domains, including but not limited to:
|
||||
|
||||
- **Natural Language Processing (NLP)**: Managing a swarm of NLP models to process, analyze, and synthesize text.
|
||||
- **Data Analysis**: Coordinating multiple data analysis agents to process and interpret complex data sets.
|
||||
- **Optimization Problems**: Running a swarm of optimization algorithms to solve complex problems in fields such as logistics, finance, and engineering.
|
||||
- **AI Research**: Implementing experimental setups that require the collaboration of multiple AI models or agents to explore new methodologies and approaches.
|
||||
|
||||
### Future Extensions
|
||||
|
||||
The `MixtureOfAgents` framework provides a solid foundation for further extensions and customizations, including:
|
||||
|
||||
- **Dynamic Layer Configuration**: Allowing layers to be added or removed dynamically based on the task requirements or intermediate results.
|
||||
- **Advanced Agent Communication**: Enhancing the communication protocols between agents to allow for more sophisticated information exchange.
|
||||
- **Integration with Other Frameworks**: Seamlessly integrating with other machine learning or data processing frameworks to leverage their capabilities within the swarm architecture.
|
||||
|
||||
In conclusion, the `MixtureOfAgents` class represents a versatile and efficient solution for orchestrating multi-agent systems, facilitating complex task execution through its structured and layered approach. By harnessing the power of parallel and sequential processing, it opens up new possibilities for tackling intricate problems across various domains.
|
@ -0,0 +1,15 @@
|
||||
# Multi-Agent Orchestration:
|
||||
Swarms was designed to faciliate the communication between many different and specialized agents from a vast array of other frameworks such as langchain, autogen, crew, and more.
|
||||
|
||||
In traditional swarm theory, there are many types of swarms usually for very specialized use-cases and problem sets. Such as Hiearchical and sequential are great for accounting and sales, because there is usually a boss coordinator agent that distributes a workload to other specialized agents.
|
||||
|
||||
|
||||
| **Name** | **Description** | **Code Link** | **Use Cases** |
|
||||
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|---------------------------------------------------------------------------------------------------|
|
||||
| Hierarchical Swarms | A system where agents are organized in a hierarchy, with higher-level agents coordinating lower-level agents to achieve complex tasks. | [Code Link](#) | Manufacturing process optimization, multi-level sales management, healthcare resource coordination |
|
||||
| Agent Rearrange | A setup where agents rearrange themselves dynamically based on the task requirements and environmental conditions. | [Code Link](https://docs.swarms.world/en/latest/swarms/structs/agent_rearrange/) | Adaptive manufacturing lines, dynamic sales territory realignment, flexible healthcare staffing |
|
||||
| Concurrent Workflows | Agents perform different tasks simultaneously, coordinating to complete a larger goal. | [Code Link](#) | Concurrent production lines, parallel sales operations, simultaneous patient care processes |
|
||||
| Sequential Coordination | Agents perform tasks in a specific sequence, where the completion of one task triggers the start of the next. | [Code Link](https://docs.swarms.world/en/latest/swarms/structs/sequential_workflow/) | Step-by-step assembly lines, sequential sales processes, stepwise patient treatment workflows |
|
||||
| Parallel Processing | Agents work on different parts of a task simultaneously to speed up the overall process. | [Code Link](#) | Parallel data processing in manufacturing, simultaneous sales analytics, concurrent medical tests |
|
||||
|
||||
|
@ -1,88 +0,0 @@
|
||||
# check_device
|
||||
|
||||
# Module/Function Name: check_device
|
||||
|
||||
The `check_device` is a utility function in PyTorch designed to identify and return the appropriate device(s) for CUDA processing. If CUDA is not available, a CPU device is returned. If CUDA is available, the function returns a list of all available GPU devices.
|
||||
|
||||
The function examines the CUDA availability, checks for multiple GPUs, and finds additional properties for each device.
|
||||
|
||||
## Function Signature and Arguments
|
||||
|
||||
**Signature:**
|
||||
```python
|
||||
def check_device(
|
||||
log_level: Any = logging.INFO,
|
||||
memory_threshold: float = 0.8,
|
||||
capability_threshold: float = 3.5,
|
||||
return_type: str = "list",
|
||||
) -> Union[torch.device, List[torch.device]]
|
||||
```
|
||||
|
||||
| Parameter | Data Type | Default Value | Description |
|
||||
| ------------- | ------------- | ------------- | ------------- |
|
||||
| `log_level` | Any | logging.INFO | The log level. |
|
||||
| `memory_threshold` | float | 0.8 | It is used to check the threshold of memory used on the GPU(s). |
|
||||
| `capability_threshold` | float | 3.5 | It is used to consider only those GPU(s) which have higher compute capability compared to the threshold. |
|
||||
| `return_type` | str | "list" | Depending on the `return_type` either a list of devices can be returned or a single device. |
|
||||
|
||||
This function does not take any mandatory argument. However, it supports optional arguments such as `log_level`, `memory_threshold`, `capability_threshold`, and `return_type`.
|
||||
|
||||
**Returns:**
|
||||
|
||||
- A single torch.device if one device or list of torch.devices if multiple CUDA devices are available, else returns the CPU device if CUDA is not available.
|
||||
|
||||
## Usage and Examples
|
||||
|
||||
### Example 1: Basic Usage
|
||||
|
||||
```python
|
||||
import logging
|
||||
|
||||
import torch
|
||||
|
||||
from swarms.utils import check_device
|
||||
|
||||
# Basic usage
|
||||
device = check_device(
|
||||
log_level=logging.INFO,
|
||||
memory_threshold=0.8,
|
||||
capability_threshold=3.5,
|
||||
return_type="list",
|
||||
)
|
||||
```
|
||||
|
||||
### Example 2: Using CPU when CUDA is not available
|
||||
|
||||
```python
|
||||
import torch
|
||||
|
||||
from swarms.utils import check_device
|
||||
|
||||
# When CUDA is not available
|
||||
device = check_device()
|
||||
print(device) # If CUDA is not available it should return torch.device('cpu')
|
||||
```
|
||||
|
||||
### Example 3: Multiple GPU Available
|
||||
|
||||
```python
|
||||
import torch
|
||||
|
||||
from swarms.utils import check_device
|
||||
|
||||
# When multiple GPUs are available
|
||||
device = check_device()
|
||||
print(device) # Should return a list of available GPU devices
|
||||
```
|
||||
|
||||
## Tips and Additional Information
|
||||
|
||||
- This function is useful when a user wants to exploit CUDA capabilities for faster computation but unsure of the available devices. This function abstracts all the necessary checks and provides a list of CUDA devices to the user.
|
||||
- The `memory_threshold` and `capability_threshold` are utilized to filter the GPU devices. The GPUs which have memory usage above the `memory_threshold` and compute capability below the `capability_threshold` are not considered.
|
||||
- As of now, CPU does not have memory or capability values, therefore, in the respective cases, it will be returned as default without any comparison.
|
||||
|
||||
## Relevant Resources
|
||||
|
||||
- For more details about the CUDA properties functions used (`torch.cuda.get_device_capability, torch.cuda.get_device_properties`), please refer to the official PyTorch [CUDA semantics documentation](https://pytorch.org/docs/stable/notes/cuda.html).
|
||||
- For more information about Torch device objects, you can refer to the official PyTorch [device documentation](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
|
||||
- For a better understanding of how the `logging` module works in Python, see the official Python [logging documentation](https://docs.python.org/3/library/logging.html).
|
@ -1,86 +0,0 @@
|
||||
# display_markdown_message
|
||||
|
||||
# Module Name: `display_markdown_message`
|
||||
|
||||
## Introduction
|
||||
|
||||
`display_markdown_message` is a useful utility function for creating visually-pleasing markdown messages within Python scripts. This function automatically manages multiline strings with lots of indentation and makes single-line messages with ">" tags easy to read, providing users with convenient and elegant logging or messaging capacity.
|
||||
|
||||
## Function Definition and Arguments
|
||||
|
||||
Function Definition:
|
||||
```python
|
||||
def display_markdown_message(message: str, color: str = "cyan"):
|
||||
```
|
||||
This function accepts two parameters:
|
||||
|
||||
|Parameter |Type |Default Value |Description |
|
||||
|--- |--- |--- |--- |
|
||||
|message |str |None |This is the message that is to be displayed. This should be a string. It can contain markdown syntax.|
|
||||
|color |str |"cyan" |This allows you to choose the color of the message. Default is "cyan". Accepts any valid color name.|
|
||||
|
||||
## Functionality and Usage
|
||||
|
||||
This utility function is used to display a markdown formatted message on the console. It accepts a message as a string and an optional color for the message. The function is ideal for generating stylized print outputs such as headers, status updates or pretty notifications.
|
||||
|
||||
By default, any text within the string which is enclosed within `>` tags or `---` is treated specially:
|
||||
|
||||
- Lines encased in `>` tags are rendered as a blockquote in markdown.
|
||||
- Lines consisting of `---` are rendered as horizontal rules.
|
||||
|
||||
The function automatically strips off leading and trailing whitespaces from any line within the message, maintaining aesthetic consistency in your console output.
|
||||
|
||||
### Usage Examples
|
||||
|
||||
#### Basic Example
|
||||
|
||||
```python
|
||||
display_markdown_message("> This is an important message", color="red")
|
||||
```
|
||||
|
||||
Output:
|
||||
```md
|
||||
> **This is an important message**
|
||||
```
|
||||
|
||||
This example will print out the string "This is an important message" in red color, enclosed in a blockquote tag.
|
||||
|
||||
#### Multiline Example
|
||||
|
||||
```python
|
||||
message = """
|
||||
> Header
|
||||
|
||||
My normal message here.
|
||||
|
||||
---
|
||||
|
||||
Another important information
|
||||
"""
|
||||
display_markdown_message(message, color="green")
|
||||
```
|
||||
|
||||
Output:
|
||||
```md
|
||||
> **Header**
|
||||
|
||||
My normal message here.
|
||||
_____
|
||||
|
||||
Another important information
|
||||
```
|
||||
The output is a green colored markdown styled text with the "Header" enclosed in a blockquote, followed by the phrase "My normal message here", a horizontal rule, and finally another phrase, "Another important information".
|
||||
|
||||
## Additional Information
|
||||
|
||||
Use newline characters `\n` to separate the lines of the message. Remember, each line of the message is stripped of leading and trailing whitespaces. If you have special markdown requirements, you may need to revise the input message string accordingly.
|
||||
|
||||
Also, keep in mind the console or terminal's ability to display the chosen color. If a particular console does not support the chosen color, the output may fallback to the default console color.
|
||||
|
||||
For a full list of color names supported by the `Console` module, refer to the official [Console documentation](http://console.readthedocs.io/).
|
||||
|
||||
## References and Resources
|
||||
|
||||
- Python Strings: https://docs.python.org/3/tutorial/introduction.html#strings
|
||||
- Python Markdown: https://pypi.org/project/markdown/
|
||||
- Console module: https://console.readthedocs.io/
|
@ -1,118 +0,0 @@
|
||||
# extract_code_from_markdown
|
||||
|
||||
# swarms.utils Module
|
||||
|
||||
The `swarms.utils` module provides utility functions designed to facilitate specific tasks within the main Swarm codebase. The function `extract_code_from_markdown` is a critical function within this module that we will document in this example.
|
||||
|
||||
## Overview and Introduction
|
||||
|
||||
Many software projects use Markdown extensively for writing documentation, tutorials, and other text documents that can be easily rendered and viewed in different formats, including HTML.
|
||||
|
||||
The `extract_code_from_markdown` function plays a crucial role within the swarms.utils library. As developers write large volumes of Markdown, they often need to isolate code snippets from the whole Markdown file body. These isolated snippets can be used to generate test cases, transform into other languages, or analyze for metrics.
|
||||
|
||||
## Function Definition: `extract_code_from_markdown`
|
||||
|
||||
```python
|
||||
def extract_code_from_markdown(markdown_content: str) -> str:
|
||||
"""
|
||||
Extracts code blocks from a Markdown string and returns them as a single string.
|
||||
|
||||
Args:
|
||||
- markdown_content (str): The Markdown content as a string.
|
||||
|
||||
Returns:
|
||||
- str: A single string containing all the code blocks separated by newlines.
|
||||
"""
|
||||
# Regular expression for fenced code blocks
|
||||
pattern = r"```(?:\w+\n)?(.*?)```"
|
||||
matches = re.findall(pattern, markdown_content, re.DOTALL)
|
||||
|
||||
# Concatenate all code blocks separated by newlines
|
||||
return "\n".join(code.strip() for code in matches)
|
||||
```
|
||||
|
||||
### Arguments
|
||||
|
||||
The function `extract_code_from_markdown` takes one argument:
|
||||
|
||||
| Argument | Description | Type | Default Value |
|
||||
|-----------------------|----------------------------------------|-------------|-------------------|
|
||||
| markdown_content | The input markdown content as a string | str | N/A |
|
||||
|
||||
|
||||
## Function Explanation and Usage
|
||||
|
||||
This function uses a regular expression to find all fenced code blocks in a Markdown string. The pattern `r"```(?:\w+\n)?(.*?)```"` matches strings that start and end with three backticks, optionally followed by a newline and then any number of any characters (the `.*?` part) until the first occurrence of another triple backtick set.
|
||||
|
||||
Once we have the matches, we join all the code blocks into a single string, each block separated by a newline.
|
||||
|
||||
The method's functionality is particularly useful when we need to extract code blocks from markdown content for secondary processing, such as syntax highlighting or execution in a different environment.
|
||||
|
||||
### Usage Examples
|
||||
|
||||
Below are three examples of how you might use this function:
|
||||
|
||||
#### Example 1:
|
||||
|
||||
Extracting code blocks from a simple markdown string.
|
||||
|
||||
```python
|
||||
from swarms.utils import extract_code_from_markdown
|
||||
|
||||
markdown_string = """# Example
|
||||
This is an example of a code block:
|
||||
```python
|
||||
print("Hello World!")
|
||||
``` """
|
||||
print(extract_code_from_markdown(markdown_string))
|
||||
```
|
||||
|
||||
#### Example 2:
|
||||
|
||||
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") as file:
|
||||
markdown_content = file.read()
|
||||
print(extract_code_from_markdown(markdown_content))
|
||||
```
|
||||
|
||||
#### Example 3:
|
||||
|
||||
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") as file:
|
||||
markdown_content = file.read()
|
||||
code_blocks = extract_code_from_markdown(markdown_content)
|
||||
analyze_code_blocks(code_blocks)
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
This concludes the detailed documentation of the `extract_code_from_markdown` function from the swarms.utils module. With this documentation, you should be able to understand the function's purpose, how it works, its parameters, and see examples of how to use it effectively.
|
@ -1,90 +0,0 @@
|
||||
# find_image_path
|
||||
|
||||
Firstly, we will divide this documentation into multiple sections.
|
||||
|
||||
# Overview
|
||||
The module **swarms.utils** has the main goal of providing necessary utility functions that are crucial during the creation of the swarm intelligence frameworks. These utility functions can include common operations such as handling input-output operations for files, handling text parsing, and handling basic mathematical computations necessary during the creation of swarm intelligence models.
|
||||
|
||||
The current function `find_image_path` in the module is aimed at extracting an image path from a given text document.
|
||||
|
||||
# Function Detailed Explanation
|
||||
|
||||
## Definition
|
||||
The function `find_image_path` takes a singular argument as an input:
|
||||
|
||||
```python
|
||||
def find_image_path(text):
|
||||
# function body
|
||||
```
|
||||
|
||||
## Parameter
|
||||
The parameter `text` in the function is a string that represents the document or text from which the function is trying to extract all paths to the images present. The function scans the given text, looking for <em>absolute</em> or <em>relative</em> paths to image files (.png, .jpg, .jpeg) on the disk.
|
||||
|
||||
| Parameter Name | Data Type | Default Value | Description |
|
||||
|:--------------:|:---------:|:-------------:|:--------:|
|
||||
| `text` | `str` | - | The text content to scan for image paths |
|
||||
|
||||
## Return Value
|
||||
|
||||
The return value of the function `find_image_path` is a string that represents the longest existing image path extracted from the input text. If no image paths exist within the text, the function returns `None`.
|
||||
|
||||
|
||||
| Return Value | Data Type | Description |
|
||||
|:------------:|:-----------:|:-----------:|
|
||||
| Path | `str` | Longest image path found in the text or `None` if no path found |
|
||||
|
||||
# Function's Code
|
||||
|
||||
The function `find_image_path` performs text parsing and pattern recognition to find image paths within the provided text. The function uses `regular expressions (re)` module to detect all potential paths.
|
||||
|
||||
```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.replace("\\", "") for match in matches if match]
|
||||
existing_paths = [match for match in matches if os.path.exists(match)]
|
||||
return max(existing_paths, key=len) if existing_paths else None
|
||||
```
|
||||
|
||||
# Usage Examples
|
||||
|
||||
Let's consider examples of how the function `find_image_path` can be used in different scenarios.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Consider the case where a text without any image path is provided.
|
||||
|
||||
```python
|
||||
from swarms.utils import find_image_path
|
||||
|
||||
text = "There are no image paths in this text"
|
||||
print(find_image_path(text)) # Outputs: None
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Consider the case where the text has multiple image paths.
|
||||
|
||||
```python
|
||||
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)
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
In the final example, we consider a case where the text has an image path, but the file does not exist.
|
||||
|
||||
```python
|
||||
from swarms.utils import find_image_path
|
||||
|
||||
text = "Here is an image path: /home/user/non_existant.png"
|
||||
print(find_image_path(text)) # Outputs: None
|
||||
```
|
||||
|
||||
# Closing Notes
|
||||
|
||||
In conclusion, the `find_image_path` function is crucial in the `swarms.utils` module as it supports a key operation of identifying image paths within given input text. This allows users to automate the extraction of such data from larger documents/text. However, it's important to note the function returns only existing paths in your file system and only the longest if multiple exist.
|
@ -1,82 +0,0 @@
|
||||
# limit_tokens_from_string
|
||||
|
||||
## Introduction
|
||||
The `Swarms.utils` library contains utility functions used across codes that handle machine learning and other operations. The `Swarms.utils` library includes a notable function named `limit_tokens_from_string()`. This function particularly limits the number of tokens in a given string.
|
||||
|
||||
# Function: limit_tokens_from_string()
|
||||
Within the `Swarms.utils` library, there is a method `limit_tokens_from_string(string: str, model: str = "gpt-4", limit: int = 500) -> str:`
|
||||
|
||||
## Description
|
||||
The function `limit_tokens_from_string()` limits the number of tokens in a given string based on the specified threshold. It is primarily useful when you are handling large text data and need to chunk or limit your text to a certain length. Limiting token length could be useful in various scenarios such as when working with data with limited computational resources, or when dealing with models that accept a specific maximum limit of text.
|
||||
|
||||
## Parameters
|
||||
|
||||
| Parameter | Type | Default Value | Description
|
||||
| :-----------| :----------- | :------------ | :------------|
|
||||
| `string` | `str` | `None` | The input string from which the tokens need to be limited. |
|
||||
| `model` | `str` | `"gpt-4"` | The model used to encode and decode the token. The function defaults to `gpt-4` but you can specify any model supported by `tiktoken`. If a model is not found, it falls back to use `gpt2` |
|
||||
| `limit` | `int` | `500` | The limit up to which the tokens have to be sliced. Default limit is 500.|
|
||||
|
||||
## Returns
|
||||
|
||||
| Return | Type | Description
|
||||
| :-----------| :----------- | :------------
|
||||
| `out` | `str` | A string that is constructed back from the encoded tokens that have been limited to a count of `limit` |
|
||||
|
||||
## Method Detail and Usage Examples
|
||||
|
||||
The method `limit_tokens_from_string()` takes in three parameters - `string`, `model`, and `limit`.
|
||||
|
||||
|
||||
First, it tries to get the encoding for the model specified in the `model` argument using `tiktoken.encoding_for_model(model)`. In case the specified model is not found, the function uses `gpt2` model encoding as a fallback.
|
||||
|
||||
Next, the input `string` is tokenized using the `encode` method on the `encoding` tensor. This results in the `encoded` tensor.
|
||||
|
||||
Then, the function slices the `encoded` tensor to get the first `limit` number of tokens.
|
||||
|
||||
Finally, the function converts back the tokens into the string using the `decode` method of the `encoding` tensor. The resulting string `out` is returned.
|
||||
|
||||
### Example 1:
|
||||
|
||||
```python
|
||||
from swarms.utils import limit_tokens_from_string
|
||||
|
||||
# longer input string
|
||||
string = "This is a very long string that needs to be tokenized. This string might exceed the maximum token limit, so it will need to be truncated."
|
||||
|
||||
# lower token limit
|
||||
limit = 10
|
||||
|
||||
output = limit_tokens_from_string(string, limit=limit)
|
||||
```
|
||||
|
||||
### Example 2:
|
||||
|
||||
```python
|
||||
from swarms.utils import limit_tokens_from_string
|
||||
|
||||
# longer input string with different model
|
||||
string = "This string will be tokenized using gpt2 model. If the string is too long, it will be truncated."
|
||||
|
||||
# model
|
||||
model = "gpt2"
|
||||
|
||||
output = limit_tokens_from_string(string, model=model)
|
||||
```
|
||||
|
||||
### Example 3:
|
||||
|
||||
```python
|
||||
from swarms.utils import limit_tokens_from_string
|
||||
|
||||
# try with a random model string
|
||||
string = "In case the method does not find the specified model, it will fall back to gpt2 model."
|
||||
|
||||
# model
|
||||
model = "gpt-4"
|
||||
|
||||
output = limit_tokens_from_string(string, model=model)
|
||||
```
|
||||
|
||||
**Note:** If specifying a model not supported by `tiktoken` intentionally, it will fall back to `gpt2` model for encoding.
|
||||
|
@ -1,105 +0,0 @@
|
||||
# load_model_torch
|
||||
|
||||
# load_model_torch: Utility Function Documentation
|
||||
|
||||
## Introduction:
|
||||
|
||||
`load_model_torch` is a utility function in the `swarms.utils` library that is designed to load a saved PyTorch model and move it to the designated device. It provides flexibility allowing the user to specify the model file location, the device where the loaded model should be moved to, whether to strictly enforce the keys in the state dictionary to match the keys returned by the model's `state_dict()`, and many more.
|
||||
|
||||
Moreover, if the saved model file only contains the state dictionary, but not the model architecture, you can pass the model architecture as an argument.
|
||||
|
||||
## Function Definition and Parameters:
|
||||
|
||||
```python
|
||||
def load_model_torch(
|
||||
model_path: str = None,
|
||||
device: torch.device = None,
|
||||
model: nn.Module = None,
|
||||
strict: bool = True,
|
||||
map_location=None,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> nn.Module:
|
||||
```
|
||||
|
||||
The following table describes the parameters in detail:
|
||||
|
||||
| Name | Type | Default Value | Description |
|
||||
| ------ | ------ | ------------- | ------------|
|
||||
| model_path | str | None | A string specifying the path to the saved model file on disk. _Required_ |
|
||||
| device | torch.device | None | A `torch.device` object that specifies the target device for the loaded model. If not provided, the function checks for the availability of a GPU and uses it if available. If not, it defaults to CPU. |
|
||||
| model | nn.Module | None | An instance of `torch.nn.Module` representing the model's architecture. This parameter is required if the model file only contains the model's state dictionary and not the model architecture. |
|
||||
| strict | bool | True | A boolean that determines whether to strictly enforce that the keys in the state dictionary match the keys returned by the model's `state_dict()` function. If set to `True`, the function will raise a KeyError when the state dictionary and `state_dict()` keys do not match. |
|
||||
| map_location | callable | None | A function to remap the storage locations of the loaded model's parameters. Useful for loading models saved on a device type that is different from the current one. |
|
||||
| *args, **kwargs | - | - | Additional arguments and keyword arguments to be passed to `torch.load`.
|
||||
|
||||
Returns:
|
||||
|
||||
- `torch.nn.Module` - The loaded model after moving it to the desired device.
|
||||
|
||||
Raises:
|
||||
|
||||
- `FileNotFoundError` - If the saved model file is not found at the specified path.
|
||||
- `RuntimeError` - If there was an error while loading the model.
|
||||
|
||||
## Example of Usage:
|
||||
|
||||
This function can be used directly inside your code as shown in the following examples:
|
||||
|
||||
### Example 1:
|
||||
Loading a model without specifying a device results in the function choosing the most optimal available device automatically.
|
||||
|
||||
```python
|
||||
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):
|
||||
super().__init__()
|
||||
self.linear = nn.Linear(10, 2)
|
||||
|
||||
def forward(self, x):
|
||||
return self.linear(x)
|
||||
|
||||
|
||||
model = MyModel()
|
||||
|
||||
# Load the model
|
||||
loaded_model = load_model_torch(model_path, model=model)
|
||||
|
||||
# Now you can use the loaded model for prediction or further training
|
||||
```
|
||||
### Example 2:
|
||||
Explicitly specifying a device.
|
||||
|
||||
```python
|
||||
# Assume `mymodel.pth` is in the current directory
|
||||
model_path = "./mymodel.pth"
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
# Load the model
|
||||
loaded_model = load_model_torch(model_path, device=device)
|
||||
```
|
||||
|
||||
### Example 3:
|
||||
Using a model file that contains only the state dictionary, not the model architecture.
|
||||
|
||||
```python
|
||||
# Assume `mymodel_state_dict.pth` is in the current directory
|
||||
model_path = "./mymodel_state_dict.pth"
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
# Define your model architecture
|
||||
model = MyModel()
|
||||
|
||||
# Load the model
|
||||
loaded_model = load_model_torch(model_path, device=device, model=model)
|
||||
```
|
||||
|
||||
This gives you an insight on how to use `load_model_torch` utility function from `swarms.utils` library efficiently. Always remember to pass the model path argument while the other arguments can be optional based on your requirements. Furthermore, handle exceptions properly for smooth functioning of your PyTorch related projects.
|
@ -1,79 +0,0 @@
|
||||
# math_eval
|
||||
|
||||
|
||||
The `math_eval` function is a python decorator that wraps around a function to run two functions on the same inputs and compare their results. The decorator can be used for testing functions that are expected to have equivalent functionality, or in situations where two different methods are used to calculate or retrieve a value, and the results need to be compared.
|
||||
|
||||
The `math_eval` function in this case accepts two functions as parameters: `func1` and `func2`, and returns a decorator. This returned decorator, when applied to a function, enhances that function to execute both `func1` and `func2`, and compare the results.
|
||||
|
||||
This can be particularly useful in situations when you are implementing a new function and wants to compare its behavior and results with that of an existing one under the same set of input parameters. It also logs the results if they do not match which could be quite useful during the debug process.
|
||||
|
||||
## Usage Example
|
||||
|
||||
Let's say you have two functions: `ground_truth` and `generated_func`, that have similar functionalities or serve the same purpose. You are writing a new function called `test_func`, and you'd like to compare the results of `ground_truth` and `generated_func` when `test_func` is run. Here is how you would use the `math_eval` decorator:
|
||||
|
||||
```python
|
||||
@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}")
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
| Parameter | Data Type | Description |
|
||||
| ---- | ---- | ---- |
|
||||
| func1 | Callable | The first function whose result you want to compare. |
|
||||
| func2 | Callable | The second function whose result you want to compare. |
|
||||
|
||||
The data types for `func1` and `func2` cannot be specified as they can be any python function (or callable object). The decorator verifies that they are callable and exceptions are handled within the decorator function.
|
||||
|
||||
## Return Values
|
||||
|
||||
The `math_eval` function does not return a direct value, since it is a decorator. When applied to a function, it alters the behavior of the wrapped function to return two values:
|
||||
|
||||
1. `result1`: The result of running `func1` with the given input parameters.
|
||||
2. `result2`: The result of running `func2` with the given input parameters.
|
||||
|
||||
These two return values are provided in that order as a tuple.
|
||||
|
||||
## Source Code
|
||||
|
||||
Here's how to implement the `math_eval` decorator:
|
||||
|
||||
```python
|
||||
import functools
|
||||
import logging
|
||||
|
||||
|
||||
def math_eval(func1, func2):
|
||||
"""Math evaluation decorator."""
|
||||
|
||||
def decorator(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
result1 = func1(*args, **kwargs)
|
||||
except Exception as e:
|
||||
logging.error(f"Error in func1: {e}")
|
||||
result1 = None
|
||||
|
||||
try:
|
||||
result2 = func2(*args, **kwargs)
|
||||
except Exception as e:
|
||||
logging.error(f"Error in func2: {e}")
|
||||
result2 = None
|
||||
|
||||
if result1 != result2:
|
||||
logging.warning(f"Outputs do not match: {result1} != {result2}")
|
||||
|
||||
return result1, result2
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
```
|
||||
Please note that the code is logging exceptions to facilitate debugging, but the actual processing and handling of the exception would depend on how you want your application to respond to exceptions. Therefore, you may want to customize the error handling depending upon your application's requirements.
|
@ -1,87 +0,0 @@
|
||||
# metrics_decorator
|
||||
|
||||
This documentation explains the use and functionality of the `metrics_decorator` function in the LLM (Large Language Models).
|
||||
|
||||
The `metrics_decorator` function is a standard Python decorator that augments a specific function by wrapping extra functionality around it. It is commonly used for things like timing, logging or memoization.
|
||||
--
|
||||
The `metrics_decorator` in LLM is specially designed to measure and calculate three key performance metrics when generating language models:
|
||||
|
||||
1. `Time to First Token`: Measures the elapsed time from the start of function execution until the generation of the first token.
|
||||
2. `Generation Latency`: It measures the total time taken for a complete run.
|
||||
3. `Throughput`: Calculates the rate of production of tokens per unit of time.
|
||||
|
||||
```python
|
||||
def metrics_decorator(func: Callable):
|
||||
"""
|
||||
|
||||
Metrics decorator for LLM
|
||||
|
||||
Args:
|
||||
func (Callable): The function to be decorated.
|
||||
|
||||
"""
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
"""
|
||||
An inner function that wraps the decorated function. It calculates 'Time to First Token',
|
||||
'Generation Latency' and 'Throughput' metrics.
|
||||
|
||||
Args:
|
||||
self : The object instance.
|
||||
*args : Variable length argument list of the decorated function.
|
||||
**kwargs : Arbitrary keyword arguments of the decorated function.
|
||||
"""
|
||||
|
||||
# Measure Time to First Token
|
||||
start_time = time.time()
|
||||
result = func(self, *args, **kwargs)
|
||||
first_token_time = time.time()
|
||||
|
||||
# Measure Generation Latency
|
||||
end_time = time.time()
|
||||
|
||||
# Calculate Throughput (assuming the function returns a list of tokens)
|
||||
throughput = len(result) / (end_time - start_time)
|
||||
|
||||
return f"""
|
||||
Time to First Token: {first_token_time - start_time}
|
||||
Generation Latency: {end_time - start_time}
|
||||
Throughput: {throughput}
|
||||
"""
|
||||
|
||||
return wrapper
|
||||
```
|
||||
## Example Usage
|
||||
Now let's discuss the usage of the `metrics_decorator` function with an example.
|
||||
|
||||
Assuming that we have a language generation function called `text_generator()` that generates a list of tokens.
|
||||
|
||||
```python
|
||||
@metrics_decorator
|
||||
def text_generator(self, text: str):
|
||||
"""
|
||||
Args:
|
||||
text (str): The input text.
|
||||
|
||||
Returns:
|
||||
A list of tokens generated from the input text.
|
||||
"""
|
||||
# language generation implementation goes here
|
||||
return tokens
|
||||
|
||||
|
||||
# Instantiate the class and call the decorated function
|
||||
obj = ClassName()
|
||||
obj.text_generator("Hello, world!")
|
||||
```
|
||||
|
||||
When the decorated `text_generator()` function is called, it will measure and return:
|
||||
|
||||
- Time elapsed until the first token is generated.
|
||||
- The total execution time of the function.
|
||||
- The rate of tokens generation per unit time.
|
||||
|
||||
This example provides a basic overview of how a function can be decorated with the `metrics_decorator`. The provided `func` argument could be any method from any class, as long as it complies with the structure defined in `metrics_decorator`. It is worth noting that the decorated function must return a list of tokens for the `Throughput` metric to work correctly.
|
||||
|
||||
Remember, applying the `metrics_decorator` does not affect the original functionality of the decorated function, it just adds additional measurement and logging capabilities to it. It's a great utility for tracking and optimizing the performance of your language models.
|
@ -1,71 +0,0 @@
|
||||
# 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 `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.
|
||||
|
||||
## Class / Function Definition
|
||||
|
||||
`pdf_to_text` is a standalone function defined as follows:
|
||||
|
||||
```python
|
||||
def pdf_to_text(pdf_path: str) -> str:
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
| Parameter | Type | Description |
|
||||
|:-:|---|---|
|
||||
| pdf_path | str | The path to the PDF file to be converted |
|
||||
|
||||
## Returns
|
||||
|
||||
| Return Value | Type | Description |
|
||||
|:-:|---|---|
|
||||
| text | str | The text extracted from the PDF file. |
|
||||
|
||||
## Raises
|
||||
|
||||
| Exception | Description |
|
||||
|---|---|
|
||||
| FileNotFoundError | If the PDF file is not found at the specified path. |
|
||||
| Exception | If there is an error in reading the PDF file. |
|
||||
|
||||
## Function Description
|
||||
|
||||
`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 `pypdf` library. It can be installed via pip:
|
||||
|
||||
```python
|
||||
!pip install pypdf
|
||||
```
|
||||
|
||||
Then, you should import the `pdf_to_text` function:
|
||||
|
||||
```python
|
||||
from swarms.utils import pdf_to_text
|
||||
```
|
||||
|
||||
Here is an example of how to use `pdf_to_text`:
|
||||
|
||||
```python
|
||||
# Define the path to the pdf file
|
||||
pdf_path = "sample.pdf"
|
||||
|
||||
# Use the function to extract text
|
||||
text = pdf_to_text(pdf_path)
|
||||
|
||||
# Print the extracted text
|
||||
print(text)
|
||||
```
|
||||
|
||||
## Tips and Additional Information
|
||||
- Ensure that the PDF file path is valid and that the file exists at the specified location. If the file does not exist, a `FileNotFoundError` will be raised.
|
||||
- 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 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/).
|
@ -1,103 +0,0 @@
|
||||
# prep_torch_inference
|
||||
|
||||
```python
|
||||
def prep_torch_inference(
|
||||
model_path: str = None,
|
||||
device: torch.device = None,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
Prepare a Torch model for inference.
|
||||
|
||||
Args:
|
||||
model_path (str): Path to the model file.
|
||||
device (torch.device): Device to run the model on.
|
||||
*args: Additional positional arguments.
|
||||
**kwargs: Additional keyword arguments.
|
||||
|
||||
Returns:
|
||||
torch.nn.Module: The prepared model.
|
||||
"""
|
||||
try:
|
||||
model = load_model_torch(model_path, device)
|
||||
model.eval()
|
||||
return model
|
||||
except Exception as e:
|
||||
# Add error handling code here
|
||||
print(f"Error occurred while preparing Torch model: {e}")
|
||||
return None
|
||||
```
|
||||
This method is part of the 'swarms.utils' module. It accepts a model file path and a torch device as input and returns a model that is ready for inference.
|
||||
|
||||
## Detailed Functionality
|
||||
|
||||
The method loads a PyTorch model from the file specified by `model_path`. This model is then moved to the specified `device` if it is provided. Subsequently, the method sets the model to evaluation mode by calling `model.eval()`. This is a crucial step when preparing a model for inference, as certain layers like dropout or batch normalization behave differently during training vs during evaluation.
|
||||
In the case of any exception (e.g., the model file not found or the device unavailable), it prints an error message and returns `None`.
|
||||
|
||||
## Parameters
|
||||
|
||||
| Parameter | Type | Description | Default |
|
||||
|-----------|------|-------------|---------|
|
||||
| model_path | str | Path to the model file. | None |
|
||||
| device | torch.device | Device to run the model on. | None |
|
||||
| args | tuple | Additional positional arguments. | None |
|
||||
| kwargs | dict | Additional keyword arguments. | None |
|
||||
|
||||
## Returns
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| torch.nn.Module | The prepared model ready for inference. Returns `None` if any exception occurs. |
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Here are some examples of how you can use the `prep_torch_inference` method. Before that, you need to import the necessary modules as follows:
|
||||
|
||||
```python
|
||||
import torch
|
||||
|
||||
from swarms.utils import load_model_torch, prep_torch_inference
|
||||
```
|
||||
|
||||
### Example 1: Load a model for inference on CPU
|
||||
|
||||
```python
|
||||
model_path = "saved_model.pth"
|
||||
model = prep_torch_inference(model_path)
|
||||
|
||||
if model is not None:
|
||||
print("Model loaded successfully and is ready for inference.")
|
||||
else:
|
||||
print("Failed to load the model.")
|
||||
```
|
||||
|
||||
### Example 2: Load a model for inference on CUDA device
|
||||
|
||||
```python
|
||||
model_path = "saved_model.pth"
|
||||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
model = prep_torch_inference(model_path, device)
|
||||
|
||||
if model is not None:
|
||||
print(f"Model loaded successfully on device {device} and is ready for inference.")
|
||||
else:
|
||||
print("Failed to load the model.")
|
||||
```
|
||||
|
||||
### Example 3: Load a model with additional arguments for `load_model_torch`
|
||||
|
||||
```python
|
||||
model_path = "saved_model.pth"
|
||||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
# Suppose load_model_torch accepts an additional argument, map_location
|
||||
model = prep_torch_inference(model_path, device, map_location=device)
|
||||
|
||||
if model is not None:
|
||||
print(f"Model loaded successfully on device {device} and is ready for inference.")
|
||||
else:
|
||||
print("Failed to load the model.")
|
||||
```
|
||||
|
||||
Please note, you need to ensure the given model path does exist and the device is available on your machine, else `prep_torch_inference` method will return `None`. Depending on the complexity and size of your models, loading them onto a specific device might take a while. So it's important that you take this into consideration when designing your machine learning workflows.
|
@ -1,110 +0,0 @@
|
||||
# print_class_parameters
|
||||
|
||||
# Module Function Name: print_class_parameters
|
||||
|
||||
The `print_class_parameters` function is a utility function developed to help developers and users alike in retrieving and printing the parameters of a class constructor in Python, either in standard output or returned as a dictionary if the `api_format` is set to `True`.
|
||||
|
||||
This utility function utilizes the `inspect` module to fetch the signature of the class constructor and fetches the parameters from the obtained signature. The parameter values and their respective types are then outputted.
|
||||
|
||||
This function allows developers to easily inspect and understand the class' constructor parameters without the need to individually go through the class structure. This eases the testing and debugging process for developers and users alike, aiding in generating more efficient and readable code.
|
||||
|
||||
__Function Definition:__
|
||||
|
||||
```python
|
||||
def print_class_parameters(cls, api_format: bool = False):
|
||||
```
|
||||
__Parameters:__
|
||||
|
||||
| Parameter | Type | Description | Default value |
|
||||
|---|---|---|---|
|
||||
| cls | type | The Python class to inspect. | None |
|
||||
| api_format | bool | Flag to determine if the output should be returned in dictionary format (if set to True) or printed out (if set to False) | False |
|
||||
|
||||
__Functionality and Usage:__
|
||||
|
||||
Inside the `print_class_parameters` function, it starts by getting the signature of the constructor of the inputted class by invoking `inspect.signature(cls.__init__)`. It then extracts the parameters from the signature and stores it in the `params` variable.
|
||||
|
||||
If the `api_format` argument is set to `True`, instead of printing the parameters and their types, it stores them inside a dictionary where each key-value pair is a parameter name and its type. It then returns this dictionary.
|
||||
|
||||
If `api_format` is set to `False` or not set at all (defaulting to False), the function iterates over the parameters and prints the parameter name and its type. "self" parameters are excluded from the output as they are inherent to all class methods in Python.
|
||||
|
||||
A possible exception that may occur during the execution of this function is during the invocation of the `inspect.signature()` function call. If the inputted class does not have an `__init__` method or any error occurs during the retrieval of the class constructor's signature, an exception will be triggered. In that case, an error message that includes the error details is printed out.
|
||||
|
||||
__Usage and Examples:__
|
||||
|
||||
Assuming the existence of a class:
|
||||
|
||||
```python
|
||||
class Agent:
|
||||
def __init__(self, x: int, y: int):
|
||||
self.x = x
|
||||
self.y = y
|
||||
```
|
||||
|
||||
One could use `print_class_parameters` in its typical usage:
|
||||
|
||||
```python
|
||||
print_class_parameters(Agent)
|
||||
```
|
||||
|
||||
Results in:
|
||||
|
||||
```
|
||||
Parameter: x, Type: <class 'int'>
|
||||
Parameter: y, Type: <class 'int'>
|
||||
```
|
||||
|
||||
Or, with `api_format` set to `True`
|
||||
|
||||
```python
|
||||
output = print_class_parameters(Agent, api_format=True)
|
||||
print(output)
|
||||
```
|
||||
|
||||
Results in:
|
||||
|
||||
```
|
||||
{'x': "<class 'int'>", 'y': "<class 'int'>"}
|
||||
```
|
||||
|
||||
__Note:__
|
||||
|
||||
The function `print_class_parameters` is not limited to custom classes. It can inspect built-in Python classes such as `list`, `dict`, and others. However, it is most useful when inspecting custom-defined classes that aren't inherently documented in Python or third-party libraries.
|
||||
|
||||
__Source Code__
|
||||
|
||||
```python
|
||||
def print_class_parameters(cls, api_format: bool = False):
|
||||
"""
|
||||
Print the parameters of a class constructor.
|
||||
|
||||
Parameters:
|
||||
cls (type): The class to inspect.
|
||||
|
||||
Example:
|
||||
>>> print_class_parameters(Agent)
|
||||
Parameter: x, Type: <class 'int'>
|
||||
Parameter: y, Type: <class 'int'>
|
||||
"""
|
||||
try:
|
||||
# Get the parameters of the class constructor
|
||||
sig = inspect.signature(cls.__init__)
|
||||
params = sig.parameters
|
||||
|
||||
if api_format:
|
||||
param_dict = {}
|
||||
for name, param in params.items():
|
||||
if name == "self":
|
||||
continue
|
||||
param_dict[name] = str(param.annotation)
|
||||
return param_dict
|
||||
|
||||
# Print the parameters
|
||||
for name, param in params.items():
|
||||
if name == "self":
|
||||
continue
|
||||
print(f"Parameter: {name}, Type: {param.annotation}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred while inspecting the class: {e}")
|
||||
```
|
@ -0,0 +1,61 @@
|
||||
from swarms import Agent, OpenAIChat
|
||||
from swarms.structs.mixture_of_agents import MixtureOfAgents
|
||||
|
||||
# Initialize the director agent
|
||||
director = Agent(
|
||||
agent_name="Director",
|
||||
system_prompt="Directs the tasks for the accountants",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="director.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 1
|
||||
accountant1 = Agent(
|
||||
agent_name="Accountant1",
|
||||
system_prompt="Prepares financial statements",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant1.json",
|
||||
)
|
||||
|
||||
# Initialize accountant 2
|
||||
accountant2 = Agent(
|
||||
agent_name="Accountant2",
|
||||
system_prompt="Audits financial records",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
state_save_file_type="json",
|
||||
saved_state_path="accountant2.json",
|
||||
)
|
||||
|
||||
# Create a list of agents
|
||||
agents = [director, accountant1, accountant2]
|
||||
|
||||
|
||||
# Swarm
|
||||
swarm = MixtureOfAgents(
|
||||
name="Mixture of Accountants",
|
||||
agents=agents,
|
||||
layers=3,
|
||||
final_agent=director,
|
||||
)
|
||||
|
||||
|
||||
# Run the swarm
|
||||
out = swarm.run("Prepare financial statements and audit financial records")
|
||||
print(out)
|
@ -0,0 +1,24 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
def concat_strings(string_list: List[str]) -> str:
|
||||
"""
|
||||
Concatenates a list of strings into a single string.
|
||||
|
||||
Args:
|
||||
string_list (List[str]): A list of strings to be concatenated.
|
||||
|
||||
Returns:
|
||||
str: The concatenated string.
|
||||
|
||||
Raises:
|
||||
TypeError: If the input is not a list of strings.
|
||||
|
||||
"""
|
||||
if not isinstance(string_list, list):
|
||||
raise TypeError("Input must be a list of strings.")
|
||||
|
||||
try:
|
||||
return "".join(string_list)
|
||||
except TypeError:
|
||||
raise TypeError("All elements in the list must be strings.")
|
@ -0,0 +1,179 @@
|
||||
from swarms.structs.agent import Agent
|
||||
from swarms.structs.base_swarm import BaseSwarm
|
||||
from typing import List, Any
|
||||
|
||||
from swarms.structs.conversation import Conversation
|
||||
from pydantic import BaseModel
|
||||
from swarms.utils.loguru_logger import logger
|
||||
|
||||
|
||||
class AgentRun(BaseModel):
|
||||
agent_name: str
|
||||
output: Any
|
||||
|
||||
|
||||
class Metadata(BaseModel):
|
||||
layers: int
|
||||
agent_runs: List[AgentRun]
|
||||
final_output: Any
|
||||
|
||||
|
||||
class MixtureOfAgents(BaseSwarm):
|
||||
"""
|
||||
Represents a mixture of agents in a swarm.
|
||||
The process is parallel -> sequential -> parallel -> final output agent.
|
||||
From the paper: https://arxiv.org/pdf/2406.04692
|
||||
|
||||
Attributes:
|
||||
agents (List[Agent]): The list of agents in the swarm.
|
||||
flow (str): The flow of the swarm.
|
||||
max_loops (int): The maximum number of loops to run.
|
||||
verbose (bool): Flag indicating whether to print verbose output.
|
||||
layers (int, optional): The number of layers in the swarm. Defaults to None.
|
||||
rules (str, optional): The rules for the swarm. Defaults to None.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str = "MixtureOfAgents",
|
||||
description: str = "A swarm of agents that run in parallel and sequentially.",
|
||||
agents: List[Agent] = None,
|
||||
max_loops: int = 1,
|
||||
verbose: bool = True,
|
||||
layers: int = 3,
|
||||
rules: str = None,
|
||||
final_agent: Agent = None,
|
||||
auto_save: bool = False,
|
||||
saved_file_name: str = "moe_swarm.json",
|
||||
):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.agents = agents
|
||||
self.max_loops = max_loops
|
||||
self.verbose = verbose
|
||||
self.layers = layers
|
||||
self.rules = rules
|
||||
self.final_agent = final_agent
|
||||
self.auto_save = auto_save
|
||||
self.saved_file_name = saved_file_name
|
||||
|
||||
# Check the agents
|
||||
self.agent_check()
|
||||
self.final_agent_check()
|
||||
|
||||
# Conversation
|
||||
self.conversation = Conversation(
|
||||
time_enabled=True,
|
||||
rules=rules,
|
||||
)
|
||||
|
||||
# Initialize the swarm
|
||||
self.swarm_initialization()
|
||||
|
||||
def agent_check(self):
|
||||
try:
|
||||
if not isinstance(self.agents, list):
|
||||
raise TypeError("Input must be a list of agents.")
|
||||
for agent in self.agents:
|
||||
if not isinstance(agent, Agent):
|
||||
raise TypeError(
|
||||
"Input must be a list of agents."
|
||||
"Each agent must be an instance of Agent."
|
||||
)
|
||||
except TypeError as e:
|
||||
logger.error(f"Error checking agents: {e}")
|
||||
|
||||
def final_agent_check(self):
|
||||
try:
|
||||
if not isinstance(self.final_agent, Agent):
|
||||
raise TypeError(
|
||||
"Final agent must be an instance of Agent."
|
||||
)
|
||||
except TypeError as e:
|
||||
logger.error(f"Error checking final agent: {e}")
|
||||
|
||||
def swarm_initialization(self):
|
||||
"""
|
||||
Initializes the swarm by logging the swarm name, description, and the number of agents.
|
||||
"""
|
||||
# Name, description, and logger
|
||||
logger.info(f"Initializing swarm {self.name}.")
|
||||
logger.info(f"Description: {self.description}")
|
||||
logger.info(f"Initializing swarm with {len(self.agents)} agents.")
|
||||
|
||||
def run(self, task: str = None, *args, **kwargs):
|
||||
"""
|
||||
Runs the swarm with the given task and returns the conversation history.
|
||||
|
||||
Args:
|
||||
task (str): The task to be performed by the swarm.
|
||||
|
||||
Returns:
|
||||
str: The conversation history as a string.
|
||||
"""
|
||||
try:
|
||||
# Running the swarm
|
||||
logger.info(f"Running swarm {self.name}.")
|
||||
|
||||
self.conversation.add("user", task)
|
||||
|
||||
# Conversation history
|
||||
history = self.conversation.return_history_as_string()
|
||||
|
||||
agent_runs = []
|
||||
layer = 0
|
||||
while layer < self.layers:
|
||||
logger.info(f"Running layer {layer} of the swarm.")
|
||||
# Different Layers
|
||||
# Run the agents for all agents on the input
|
||||
responses = []
|
||||
for agent in self.agents:
|
||||
out = agent.run(history, *args, **kwargs)
|
||||
responses.append((agent.agent_name, out))
|
||||
agent_runs.append(
|
||||
AgentRun(agent_name=agent.agent_name, output=out)
|
||||
)
|
||||
|
||||
# Log the agent run
|
||||
logger.info(f"Agent {agent.agent_name} output: {out}")
|
||||
|
||||
# Add all the responses to the conversation
|
||||
logger.info("Adding responses to the conversation.")
|
||||
for agent_name, response in responses:
|
||||
self.conversation.add(agent_name, response)
|
||||
|
||||
# Update the history
|
||||
history = self.conversation.return_history_as_string()
|
||||
|
||||
layer += 1
|
||||
|
||||
logger.info(f"Completed layer {layer} of the swarm.")
|
||||
|
||||
# Run the final output agent on the entire conversation history
|
||||
logger.info(
|
||||
"Running the final output agent on the conversation history."
|
||||
)
|
||||
final_output = self.final_agent.run(history, *args, **kwargs)
|
||||
self.conversation.add(
|
||||
self.final_agent.agent_name, final_output
|
||||
)
|
||||
|
||||
# Create metadata
|
||||
logger.info("Creating metadata for the swarm.")
|
||||
metadata = Metadata(
|
||||
layers=self.layers,
|
||||
agent_runs=agent_runs,
|
||||
final_output=final_output,
|
||||
)
|
||||
|
||||
# Save metadata to JSON file
|
||||
logger.info("Saving metadata to JSON file.")
|
||||
with open(self.saved_file_name, "w") as f:
|
||||
f.write(metadata.json())
|
||||
|
||||
return self.conversation.return_history_as_string()
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error running swarm: {e} try optimizing the swarm inputs or re-iterate on the task."
|
||||
)
|
||||
return None
|
Loading…
Reference in new issue