A utility package hosting complex logic for the Swarms ecosystem. This package provides essential tools for JSON formatting, swarm matching, and logits processing in multi-agent systems.
- JSON Formatter: Structured JSON generation with schema validation
- Swarm Matcher: Intelligent task-to-swarm type matching using embeddings
- Logits Processor: Advanced token filtering and stopping criteria for language models
pip install swarms-utilsOr install from source:
git clone https://github.com/The-Swarm-Corporation/swarms-utils.git
cd swarms-utils
pip install -e .Generate structured JSON output using language models with schema validation:
from swarms_utils import Jsonformer
import transformers
# Initialize model and tokenizer
model = transformers.AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = transformers.AutoTokenizer.from_pretrained("gpt2")
# Define JSON schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"},
"is_active": {"type": "boolean"}
}
}
# Create formatter
formatter = Jsonformer(
model=model,
tokenizer=tokenizer,
json_schema=schema,
prompt="Generate a user profile"
)
# Generate structured output
result = formatter()
print(result)
# Output: {"name": "John Doe", "age": 30, "is_active": true}Intelligently match tasks to appropriate swarm types using semantic similarity:
from swarms_utils import SwarmMatcher, SwarmMatcherConfig, SwarmType
# Configure the matcher
config = SwarmMatcherConfig()
matcher = SwarmMatcher(config)
# Add custom swarm types
custom_swarm = SwarmType(
name="DataAnalysisSwarm",
description="Specialized in data analysis, visualization, and statistical modeling"
)
matcher.add_swarm_type(custom_swarm)
# Match a task to the best swarm type
task = "Analyze sales data and create visualizations"
best_match, confidence = matcher.find_best_match(task)
print(f"Best match: {best_match} (confidence: {confidence:.2f})")Control language model output with custom stopping criteria and token filtering:
from swarms_utils import StringStoppingCriteria, NumberStoppingCriteria, OutputNumbersTokens
# String stopping criteria - stops at quotes
string_criteria = StringStoppingCriteria(tokenizer, prompt_length=10)
# Number stopping criteria - stops at appropriate number precision
number_criteria = NumberStoppingCriteria(tokenizer, prompt_length=10, precision=2)
# Output only number tokens
number_warper = OutputNumbersTokens(tokenizer, prompt)The main class for structured JSON generation.
Parameters:
model: Pre-trained language modeltokenizer: Tokenizer for the modeljson_schema: JSON schema definitionprompt: Input prompt for generationdebug: Enable debug mode (default: False)max_array_length: Maximum array length (default: 10)max_number_tokens: Maximum tokens for numbers (default: 6)temperature: Generation temperature (default: 1.0)max_string_token_length: Maximum string token length (default: 10)
Methods:
generate_number(): Generate a number valuegenerate_boolean(): Generate a boolean valuegenerate_string(): Generate a string valuegenerate_object(): Generate an object with propertiesgenerate_array(): Generate an array of items__call__(): Generate complete JSON object
Intelligent task-to-swarm matching using semantic embeddings.
Parameters:
config: SwarmMatcherConfig instance
Methods:
add_swarm_type(swarm_type): Add a new swarm typefind_best_match(task): Find best matching swarm typeauto_select_swarm(task): Automatically select swarm typesave_swarm_types(filename): Save swarm types to JSONload_swarm_types(filename): Load swarm types from JSON
Pydantic model for defining swarm types.
Fields:
name: Swarm type namedescription: Detailed description for matchingembedding: Optional pre-computed embedding
StringStoppingCriteria: Stops generation at string delimiters NumberStoppingCriteria: Stops generation at appropriate number precision OutputNumbersTokens: Filters logits to only allow number tokens
- Multi-Agent Systems: Match tasks to specialized agent swarms
- Structured Output: Generate JSON responses from language models
- Content Generation: Control model output with custom stopping criteria
- Workflow Optimization: Automatically select appropriate processing workflows
The package supports various configuration options:
from swarms_utils import SwarmMatcherConfig
config = SwarmMatcherConfig(
model_name="sentence-transformers/all-MiniLM-L6-v2",
embedding_dim=512
)We welcome contributions! Please see our contributing guidelines for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Open an issue on GitHub
- Join our Discord community
- Check the documentation
Made by the Swarms Team