Documentation Index
Fetch the complete documentation index at: https://docs.swarms.world/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheForestSwarm organizes agents into trees, where each agent specializes in processing specific tasks. Trees are collections of agents, each assigned based on their relevance to a task through keyword extraction and litellm-based embedding similarity.
The architecture allows for efficient task assignment by selecting the most relevant agent from a set of trees. Tasks are processed with agents selected based on task relevance, calculated by the similarity of system prompts and task keywords using litellm embeddings and cosine similarity calculations.
Installation
Utility Functions
extract_keywords()
Extracts relevant keywords from a text prompt using basic word splitting and frequency counting.prompt(str): The text to extract keywords fromtop_n(int): Maximum number of keywords to return
cosine_similarity()
Calculates the cosine similarity between two embedding vectors.vec1(List[float]): First embedding vectorvec2(List[float]): Second embedding vector
TreeAgent
TreeAgent represents an individual agent responsible for handling a specific task. Agents are initialized with a system prompt and use litellm embeddings to dynamically determine their relevance to a given task.
TreeAgent Attributes
Name of the agent
Description of the agent
A string that defines the agent’s area of expertise and task-handling capability
Name of the language model to use
The name of the agent
litellm-generated embedding of the system prompt for similarity-based task matching
Keywords dynamically extracted from the system prompt to assist in task matching
The computed distance between agents based on embedding similarity
Name of the litellm embedding model
Whether to enable verbose logging
TreeAgent Methods
calculate_distance()
Calculates the cosine similarity distance between this agent and another agent.other_agent(TreeAgent): Another agent to compare with
run_task()
Executes the task, logs the input/output, and returns the result.task(str): The task to executeimg(str, optional): Optional image input
is_relevant_for_task()
Checks if the agent is relevant for the task using keyword matching and litellm embedding similarity.task(str): The task to check relevance forthreshold(float): Similarity threshold (default: 0.7)
Tree
Tree organizes multiple agents into a hierarchical structure, where agents are sorted based on their relevance to tasks using litellm embeddings.
Tree Attributes
The name of the tree (represents a domain of agents, e.g., “Financial Tree”)
List of agents belonging to this tree, sorted by embedding-based distance
Whether to enable verbose logging
Tree Methods
calculate_agent_distances()
Calculates and assigns distances between agents based on litellm embedding similarity of prompts.find_relevant_agent()
Finds the most relevant agent for a task based on keyword and litellm embedding similarity.task(str): The task to find a relevant agent for
TreeAgent, or None if no relevant agent is found
log_tree_execution()
Logs details of the task execution by the selected agent.task(str): The executed task descriptionselected_agent(TreeAgent): The agent that executed the taskresult(Any): The result of the execution
ForestSwarm Attributes
Name of the forest swarm
Description of the forest swarm
List of trees containing agents organized by domain
Shared memory object for inter-tree communication
Whether to enable verbose logging
File path for saving conversation logs
Conversation object for tracking interactions
ForestSwarm Methods
find_relevant_tree()
Searches across all trees to find the most relevant tree based on litellm embedding similarity.task(str): The task to find a relevant tree for
Tree, or None if no relevant tree is found
run()
Executes the task by finding the most relevant agent from the relevant tree using litellm embeddings.task(str): The task to executeimg(str, optional): Optional image input
batched_run()
Executes multiple tasks by finding the most relevant agent for each task.tasks(List[str]): List of tasks to execute
Usage Examples
Full Working Example
How It Works
- Create Agents: Agents are initialized with varying system prompts, representing different areas of expertise (e.g., financial planning, tax filing).
- Generate Embeddings: Each agent’s system prompt is converted to litellm embeddings for semantic similarity calculations.
- Create Trees: Agents are grouped into trees, with each tree representing a domain (e.g., “Financial Services Tree”, “Investment & Trading Tree”).
- Calculate Distances: litellm embeddings are used to calculate semantic distances between agents within each tree.
- Run Task: When a task is submitted, the system:
- Generates litellm embeddings for the task
- Searches through all trees using cosine similarity
- Finds the most relevant agent based on embedding similarity and keyword matching
- Task Execution: The selected agent processes the task, and the result is returned and logged.
- Batched Processing: Multiple tasks can be processed using the
batched_runmethod for efficient batch processing.
Key Features
litellm Integration
- Embedding Generation: Uses litellm’s
embedding()function for generating high-quality embeddings - Model Flexibility: Supports various embedding models (default: “text-embedding-ada-002”)
- Error Handling: Robust fallback mechanisms for embedding failures
Semantic Similarity
- Cosine Similarity: Implements efficient cosine similarity calculations for vector comparisons
- Threshold-based Selection: Configurable similarity thresholds for agent selection
- Hybrid Matching: Combines keyword matching with semantic similarity for optimal results
Dynamic Agent Organization
- Automatic Distance Calculation: Agents are automatically organized by semantic similarity
- Real-time Relevance: Task relevance is calculated dynamically using current embeddings
- Scalable Architecture: Easy to add/remove agents and trees without manual configuration
Batch Processing
- Batched Execution: Process multiple tasks efficiently using
batched_runmethod - Parallel Processing: Each task is processed independently with the most relevant agent
- Result Aggregation: All results are returned as a list for easy processing
Logging Models
AgentLogInput
Input log model for tracking agent task execution.| Field | Type | Description |
|---|---|---|
log_id | str | Unique identifier for the log entry |
agent_name | str | Name of the agent executing the task |
task | str | Description of the task being executed |
timestamp | datetime | When the task was started |
AgentLogOutput
Output log model for tracking agent task completion.| Field | Type | Description |
|---|---|---|
log_id | str | Unique identifier for the log entry |
agent_name | str | Name of the agent that completed the task |
result | Any | Result/output from the task execution |
timestamp | datetime | When the task was completed |
TreeLog
Tree execution log model for tracking tree-level operations.| Field | Type | Description |
|---|---|---|
log_id | str | Unique identifier for the log entry |
tree_name | str | Name of the tree that executed the task |
task | str | Description of the task that was executed |
selected_agent | str | Name of the agent selected for the task |
timestamp | datetime | When the task was executed |
result | Any | Result/output from the task execution |
Architecture Analysis
The ForestSwarm Architecture leverages a hierarchical structure (forest) composed of individual trees, each containing agents specialized in specific domains. This design allows for:- Modular and Scalable Organization: By separating agents into trees, it is easy to expand or contract the system by adding or removing trees or agents.
- Task Specialization: Each agent is specialized, which ensures that tasks are matched with the most appropriate agent based on litellm embedding similarity and expertise.
- Dynamic Matching: The architecture uses both keyword-based and litellm embedding-based matching to assign tasks, ensuring a high level of accuracy in agent selection.
- Logging and Accountability: Each task execution is logged in detail, providing transparency and an audit trail of which agent handled which task and the results produced.
- Batch Processing: The architecture supports efficient batch processing of multiple tasks simultaneously.