Skip to main content

Overview

The ForestSwarm 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.
Parameters:
  • prompt (str): The text to extract keywords from
  • top_n (int): Maximum number of keywords to return
Returns: List of extracted keywords sorted by frequency

cosine_similarity()

Calculates the cosine similarity between two embedding vectors.
Parameters:
  • vec1 (List[float]): First embedding vector
  • vec2 (List[float]): Second embedding vector
Returns: Cosine similarity score between 0 and 1

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

str
Name of the agent
str
Description of the agent
str
A string that defines the agent’s area of expertise and task-handling capability
str
default:"gpt-5.4"
Name of the language model to use
str
The name of the agent
List[float]
litellm-generated embedding of the system prompt for similarity-based task matching
List[str]
Keywords dynamically extracted from the system prompt to assist in task matching
Optional[float]
The computed distance between agents based on embedding similarity
str
default:"text-embedding-ada-002"
Name of the litellm embedding model
bool
default:"False"
Whether to enable verbose logging

TreeAgent Methods

calculate_distance()

Calculates the cosine similarity distance between this agent and another agent.
Parameters:
  • other_agent (TreeAgent): Another agent to compare with
Returns: Cosine similarity distance as a float

run_task()

Executes the task, logs the input/output, and returns the result.
Parameters:
  • task (str): The task to execute
  • img (str, optional): Optional image input
Returns: The result of the task execution

is_relevant_for_task()

Checks if the agent is relevant for the task using keyword matching and litellm embedding similarity.
Parameters:
  • task (str): The task to check relevance for
  • threshold (float): Similarity threshold (default: 0.7)
Returns: Boolean indicating if the agent is relevant for the task

Tree

Tree organizes multiple agents into a hierarchical structure, where agents are sorted based on their relevance to tasks using litellm embeddings.

Tree Attributes

str
The name of the tree (represents a domain of agents, e.g., “Financial Tree”)
List[TreeAgent]
List of agents belonging to this tree, sorted by embedding-based distance
bool
default:"False"
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.
Parameters:
  • task (str): The task to find a relevant agent for
Returns: The most relevant TreeAgent, or None if no relevant agent is found

log_tree_execution()

Logs details of the task execution by the selected agent.
Parameters:
  • task (str): The executed task description
  • selected_agent (TreeAgent): The agent that executed the task
  • result (Any): The result of the execution

ForestSwarm Attributes

str
default:"default-forest-swarm"
Name of the forest swarm
str
default:"Standard forest swarm"
Description of the forest swarm
List[Tree]
default:"[]"
List of trees containing agents organized by domain
Any
default:"None"
Shared memory object for inter-tree communication
str
default:"None"
Rules governing the forest swarm behavior
bool
default:"False"
Whether to enable verbose logging
str
File path for saving conversation logs
Conversation
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.
Parameters:
  • task (str): The task to find a relevant tree for
Returns: The most relevant 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.
Parameters:
  • task (str): The task to execute
  • img (str, optional): Optional image input
Returns: The result of the task execution

batched_run()

Executes multiple tasks by finding the most relevant agent for each task.
Parameters:
  • tasks (List[str]): List of tasks to execute
Returns: List of results, one per task

Usage Examples

Full Working Example

How It Works

  1. Create Agents: Agents are initialized with varying system prompts, representing different areas of expertise (e.g., financial planning, tax filing).
  2. Generate Embeddings: Each agent’s system prompt is converted to litellm embeddings for semantic similarity calculations.
  3. Create Trees: Agents are grouped into trees, with each tree representing a domain (e.g., “Financial Services Tree”, “Investment & Trading Tree”).
  4. Calculate Distances: litellm embeddings are used to calculate semantic distances between agents within each tree.
  5. 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
  6. Task Execution: The selected agent processes the task, and the result is returned and logged.
  7. Batched Processing: Multiple tasks can be processed using the batched_run method 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_run method
  • 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.

AgentLogOutput

Output log model for tracking agent task completion.

TreeLog

Tree execution log model for tracking tree-level operations.

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.

Source Code

View the source code on GitHub