From ead6bf55eadbbbed151bfd91496d958dfd603b8c Mon Sep 17 00:00:00 2001 From: ascender1729 Date: Tue, 13 May 2025 19:34:49 +0530 Subject: [PATCH 1/4] Revert "model docs" This reverts commit 48e7fd8a79c6600571b6dea24dc2011016b515cb. --- cort_agent.py | 206 ------------------------- docs/mkdocs.yml | 1 - docs/swarms/models/agent_and_models.md | 196 ----------------------- pyproject.toml | 2 +- swarms/agents/__init__.py | 1 + 5 files changed, 2 insertions(+), 404 deletions(-) delete mode 100644 cort_agent.py delete mode 100644 docs/swarms/models/agent_and_models.md diff --git a/cort_agent.py b/cort_agent.py deleted file mode 100644 index 3d720e7c..00000000 --- a/cort_agent.py +++ /dev/null @@ -1,206 +0,0 @@ -# AI generate initial response -# AI decides how many "thinking rounds" it needs -# For each round: -# Generates 3 alternative responses -# Evaluates all responses -# Picks the best one -# Final response is the survivor of this AI battle royale -from swarms import Agent - - -# OpenAI function schema for determining thinking rounds -thinking_rounds_schema = { - "name": "determine_thinking_rounds", - "description": "Determines the optimal number of thinking rounds needed for a task", - "parameters": { - "type": "object", - "properties": { - "num_rounds": { - "type": "integer", - "description": "The number of thinking rounds needed (1-5)", - "minimum": 1, - "maximum": 5, - } - }, - "required": ["num_rounds"], - }, -} - -# System prompt for determining thinking rounds -THINKING_ROUNDS_PROMPT = """You are an expert at determining the optimal number of thinking rounds needed for complex tasks. Your role is to analyze the task and determine how many rounds of thinking and evaluation would be most beneficial. - -Consider the following factors when determining the number of rounds: -1. Task Complexity: More complex tasks may require more rounds -2. Potential for Multiple Valid Approaches: Tasks with multiple valid solutions need more rounds -3. Risk of Error: Higher-stakes tasks may benefit from more rounds -4. Time Sensitivity: Balance thoroughness with efficiency - -Guidelines for number of rounds: -- 1 round: Simple, straightforward tasks with clear solutions -- 2-3 rounds: Moderately complex tasks with some ambiguity -- 4-5 rounds: Highly complex tasks with multiple valid approaches or high-stakes decisions - -Your response should be a single number between 1 and 5, representing the optimal number of thinking rounds needed.""" - -# Schema for generating alternative responses -alternative_responses_schema = { - "name": "generate_alternatives", - "description": "Generates multiple alternative responses to a task", - "parameters": { - "type": "object", - "properties": { - "alternatives": { - "type": "array", - "description": "List of alternative responses", - "items": { - "type": "object", - "properties": { - "response": { - "type": "string", - "description": "The alternative response", - }, - "reasoning": { - "type": "string", - "description": "Explanation of why this approach was chosen", - }, - }, - "required": ["response", "reasoning"], - }, - "minItems": 3, - "maxItems": 3, - } - }, - "required": ["alternatives"], - }, -} - -# Schema for evaluating responses -evaluation_schema = { - "name": "evaluate_responses", - "description": "Evaluates and ranks alternative responses", - "parameters": { - "type": "object", - "properties": { - "evaluation": { - "type": "object", - "properties": { - "best_response": { - "type": "string", - "description": "The selected best response", - }, - "ranking": { - "type": "array", - "description": "Ranked list of responses from best to worst", - "items": { - "type": "object", - "properties": { - "response": { - "type": "string", - "description": "The response", - }, - "score": { - "type": "number", - "description": "Score from 0-100", - }, - "reasoning": { - "type": "string", - "description": "Explanation of the score", - }, - }, - "required": [ - "response", - "score", - "reasoning", - ], - }, - }, - }, - "required": ["best_response", "ranking"], - } - }, - "required": ["evaluation"], - }, -} - -# System prompt for generating alternatives -ALTERNATIVES_PROMPT = """You are an expert at generating diverse and creative alternative responses to tasks. Your role is to generate 3 distinct approaches to solving the given task. - -For each alternative: -1. Consider a different perspective or approach -2. Provide clear reasoning for why this approach might be effective -3. Ensure alternatives are meaningfully different from each other -4. Maintain high quality and relevance to the task - -Your response should include 3 alternatives, each with its own reasoning.""" - -# System prompt for evaluation -EVALUATION_PROMPT = """You are an expert at evaluating and comparing different responses to tasks. Your role is to critically analyze each response and determine which is the most effective. - -Consider the following criteria when evaluating: -1. Relevance to the task -2. Completeness of the solution -3. Creativity and innovation -4. Practicality and feasibility -5. Clarity and coherence - -Your response should include: -1. The best response selected -2. A ranked list of all responses with scores and reasoning""" - - -class CortAgent: - def __init__( - self, - alternative_responses: int = 3, - ): - self.thinking_rounds = Agent( - agent_name="CortAgent", - agent_description="CortAgent is a multi-step agent that uses a battle royale approach to determine the best response to a task.", - model_name="gpt-4o-mini", - max_loops=1, - dynamic_temperature_enabled=True, - tools_list_dictionary=thinking_rounds_schema, - system_prompt=THINKING_ROUNDS_PROMPT, - ) - - self.alternatives_agent = Agent( - agent_name="CortAgentAlternatives", - agent_description="Generates multiple alternative responses to a task", - model_name="gpt-4o-mini", - max_loops=1, - dynamic_temperature_enabled=True, - tools_list_dictionary=alternative_responses_schema, - system_prompt=ALTERNATIVES_PROMPT, - ) - - self.evaluation_agent = Agent( - agent_name="CortAgentEvaluation", - agent_description="Evaluates and ranks alternative responses", - model_name="gpt-4o-mini", - max_loops=1, - dynamic_temperature_enabled=True, - tools_list_dictionary=evaluation_schema, - system_prompt=EVALUATION_PROMPT, - ) - - def run(self, task: str): - # First determine number of thinking rounds - num_rounds = self.thinking_rounds.run(task) - - # Initialize with the task - current_task = task - best_response = None - - # Run the battle royale for the determined number of rounds - for round_num in range(num_rounds): - # Generate alternatives - alternatives = self.alternatives_agent.run(current_task) - - # Evaluate alternatives - evaluation = self.evaluation_agent.run(alternatives) - - # Update best response and current task for next round - best_response = evaluation["evaluation"]["best_response"] - current_task = f"Previous best response: {best_response}\nOriginal task: {task}" - - return best_response diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 5c379fc2..eca13089 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -193,7 +193,6 @@ nav: - Documentation: - Agent Class Documentation: "swarms/structs/agent.md" - Create and Run Agents from YAML: "swarms/agents/create_agents_yaml.md" - - Integrating Various Models into Your Agents: "swarms/models/agent_and_models.md" - Tools: - Structured Outputs: "swarms/agents/structured_outputs.md" - Overview: "swarms/tools/main.md" diff --git a/docs/swarms/models/agent_and_models.md b/docs/swarms/models/agent_and_models.md deleted file mode 100644 index 2b13b34f..00000000 --- a/docs/swarms/models/agent_and_models.md +++ /dev/null @@ -1,196 +0,0 @@ -# Model Integration in Agents - -!!! info "About Model Integration" - Agents supports multiple model providers through LiteLLM integration, allowing you to easily switch between different language models. This document outlines the available providers and how to use them with agents. - -## Important Note on Model Names - -!!! warning "Required Format" - When specifying a model in Swarms, you must use the format `provider/model_name`. For example: - ```python - "openai/gpt-4" - "anthropic/claude-3-opus-latest" - "cohere/command-r-plus" - ``` - This format ensures Swarms knows which provider to use for the specified model. - -## Available Model Providers - -### OpenAI - -??? info "OpenAI Models" - - **Provider name**: `openai` - - **Available Models**: - - `gpt-4` - - `gpt-3.5-turbo` - - `gpt-4-turbo-preview` - -### Anthropic -??? info "Anthropic Models" - - **Provider name**: `anthropic` - - **Available Models**: - - **Claude 3 Opus**: - - `claude-3-opus-latest` - - `claude-3-opus-20240229` - - **Claude 3 Sonnet**: - - `claude-3-sonnet-20240229` - - `claude-3-5-sonnet-latest` - - `claude-3-5-sonnet-20240620` - - `claude-3-7-sonnet-latest` - - `claude-3-7-sonnet-20250219` - - `claude-3-5-sonnet-20241022` - - **Claude 3 Haiku**: - - `claude-3-haiku-20240307` - - `claude-3-5-haiku-20241022` - - `claude-3-5-haiku-latest` - - **Legacy Models**: - - `claude-2` - - `claude-2.1` - - `claude-instant-1` - - `claude-instant-1.2` - -### Cohere -??? info "Cohere Models" - - **Provider name**: `cohere` - - **Available Models**: - - **Command**: - - `command` - - `command-r` - - `command-r-08-2024` - - `command-r7b-12-2024` - - **Command Light**: - - `command-light` - - **Command R Plus**: - - `command-r-plus` - - `command-r-plus-08-2024` - -### Google -??? info "Google Models" - - **Provider name**: `google` - - **Available Models**: - - `gemini-pro` - - `gemini-pro-vision` - -### Mistral -??? info "Mistral Models" - - **Provider name**: `mistral` - - **Available Models**: - - `mistral-tiny` - - `mistral-small` - - `mistral-medium` - -## Using Different Models with Swarms - -To use a different model with your Swarms agent, specify the model name in the `model_name` parameter when initializing the Agent, using the provider/model_name format: - -```python -from swarms.structs.agent import Agent - -# Using OpenAI's GPT-4 -agent = Agent( - agent_name="Research-Agent", - model_name="openai/gpt-4o", # Note the provider/model_name format - # ... other parameters -) - -# Using Anthropic's Claude -agent = Agent( - agent_name="Analysis-Agent", - model_name="anthropic/claude-3-sonnet-20240229", # Note the provider/model_name format - # ... other parameters -) - -# Using Cohere's Command -agent = Agent( - agent_name="Text-Agent", - model_name="cohere/command-r-plus", # Note the provider/model_name format - # ... other parameters -) -``` - -## Model Configuration - -When using different models, you can configure various parameters: - -```python -agent = Agent( - agent_name="Custom-Agent", - model_name="openai/gpt-4", - temperature=0.7, # Controls randomness (0.0 to 1.0) - max_tokens=2000, # Maximum tokens in response - top_p=0.9, # Nucleus sampling parameter - frequency_penalty=0.0, # Reduces repetition - presence_penalty=0.0, # Encourages new topics - # ... other parameters -) -``` - -## Best Practices - -### Model Selection -!!! tip "Choosing the Right Model" - - Choose models based on your specific use case - - Consider cost, performance, and feature requirements - - Test different models for your specific task - -### Error Handling -!!! warning "Error Management" - - Implement proper error handling for model-specific errors - - Handle rate limits and API quotas appropriately - -### Cost Management -!!! note "Cost Considerations" - - Monitor token usage and costs - - Use appropriate model sizes for your needs - -## Example Use Cases - -### 1. Complex Analysis (GPT-4) - -```python -agent = Agent( - agent_name="Analysis-Agent", - model_name="openai/gpt-4", # Note the provider/model_name format - temperature=0.3, # Lower temperature for more focused responses - max_tokens=4000 -) -``` - -### 2. Creative Tasks (Claude) - -```python -agent = Agent( - agent_name="Creative-Agent", - model_name="anthropic/claude-3-sonnet-20240229", # Note the provider/model_name format - temperature=0.8, # Higher temperature for more creative responses - max_tokens=2000 -) -``` - -### 3. Vision Tasks (Gemini) - -```python -agent = Agent( - agent_name="Vision-Agent", - model_name="google/gemini-pro-vision", # Note the provider/model_name format - temperature=0.4, - max_tokens=1000 -) -``` - -## Troubleshooting - -!!! warning "Common Issues" - If you encounter issues with specific models: - - 1. Verify your API keys are correctly set - 2. Check model availability in your region - 3. Ensure you have sufficient quota/credits - 4. Verify the model name is correct and supported - -## Additional Resources - -- [LiteLLM Documentation](https://docs.litellm.ai/){target=_blank} -- [OpenAI API Documentation](https://platform.openai.com/docs/api-reference){target=_blank} -- [Anthropic API Documentation](https://docs.anthropic.com/claude/reference/getting-started-with-the-api){target=_blank} -- [Google AI Documentation](https://ai.google.dev/docs){target=_blank} diff --git a/pyproject.toml b/pyproject.toml index f778cee0..e9628e6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "7.7.6" +version = "7.7.5" description = "Swarms - TGSC" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 6617abb8..169009ca 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -25,6 +25,7 @@ from swarms.structs.stopping_conditions import ( ) __all__ = [ + # "ToolAgent", "check_done", "check_finished", "check_complete", From 16b5896f07bdc15d76c2bc6b0d70c4d2e20e097c Mon Sep 17 00:00:00 2001 From: ascender1729 Date: Tue, 13 May 2025 19:36:01 +0530 Subject: [PATCH 2/4] Reapply "model docs" This reverts commit ead6bf55eadbbbed151bfd91496d958dfd603b8c. --- cort_agent.py | 206 +++++++++++++++++++++++++ docs/mkdocs.yml | 1 + docs/swarms/models/agent_and_models.md | 196 +++++++++++++++++++++++ pyproject.toml | 2 +- swarms/agents/__init__.py | 1 - 5 files changed, 404 insertions(+), 2 deletions(-) create mode 100644 cort_agent.py create mode 100644 docs/swarms/models/agent_and_models.md diff --git a/cort_agent.py b/cort_agent.py new file mode 100644 index 00000000..3d720e7c --- /dev/null +++ b/cort_agent.py @@ -0,0 +1,206 @@ +# AI generate initial response +# AI decides how many "thinking rounds" it needs +# For each round: +# Generates 3 alternative responses +# Evaluates all responses +# Picks the best one +# Final response is the survivor of this AI battle royale +from swarms import Agent + + +# OpenAI function schema for determining thinking rounds +thinking_rounds_schema = { + "name": "determine_thinking_rounds", + "description": "Determines the optimal number of thinking rounds needed for a task", + "parameters": { + "type": "object", + "properties": { + "num_rounds": { + "type": "integer", + "description": "The number of thinking rounds needed (1-5)", + "minimum": 1, + "maximum": 5, + } + }, + "required": ["num_rounds"], + }, +} + +# System prompt for determining thinking rounds +THINKING_ROUNDS_PROMPT = """You are an expert at determining the optimal number of thinking rounds needed for complex tasks. Your role is to analyze the task and determine how many rounds of thinking and evaluation would be most beneficial. + +Consider the following factors when determining the number of rounds: +1. Task Complexity: More complex tasks may require more rounds +2. Potential for Multiple Valid Approaches: Tasks with multiple valid solutions need more rounds +3. Risk of Error: Higher-stakes tasks may benefit from more rounds +4. Time Sensitivity: Balance thoroughness with efficiency + +Guidelines for number of rounds: +- 1 round: Simple, straightforward tasks with clear solutions +- 2-3 rounds: Moderately complex tasks with some ambiguity +- 4-5 rounds: Highly complex tasks with multiple valid approaches or high-stakes decisions + +Your response should be a single number between 1 and 5, representing the optimal number of thinking rounds needed.""" + +# Schema for generating alternative responses +alternative_responses_schema = { + "name": "generate_alternatives", + "description": "Generates multiple alternative responses to a task", + "parameters": { + "type": "object", + "properties": { + "alternatives": { + "type": "array", + "description": "List of alternative responses", + "items": { + "type": "object", + "properties": { + "response": { + "type": "string", + "description": "The alternative response", + }, + "reasoning": { + "type": "string", + "description": "Explanation of why this approach was chosen", + }, + }, + "required": ["response", "reasoning"], + }, + "minItems": 3, + "maxItems": 3, + } + }, + "required": ["alternatives"], + }, +} + +# Schema for evaluating responses +evaluation_schema = { + "name": "evaluate_responses", + "description": "Evaluates and ranks alternative responses", + "parameters": { + "type": "object", + "properties": { + "evaluation": { + "type": "object", + "properties": { + "best_response": { + "type": "string", + "description": "The selected best response", + }, + "ranking": { + "type": "array", + "description": "Ranked list of responses from best to worst", + "items": { + "type": "object", + "properties": { + "response": { + "type": "string", + "description": "The response", + }, + "score": { + "type": "number", + "description": "Score from 0-100", + }, + "reasoning": { + "type": "string", + "description": "Explanation of the score", + }, + }, + "required": [ + "response", + "score", + "reasoning", + ], + }, + }, + }, + "required": ["best_response", "ranking"], + } + }, + "required": ["evaluation"], + }, +} + +# System prompt for generating alternatives +ALTERNATIVES_PROMPT = """You are an expert at generating diverse and creative alternative responses to tasks. Your role is to generate 3 distinct approaches to solving the given task. + +For each alternative: +1. Consider a different perspective or approach +2. Provide clear reasoning for why this approach might be effective +3. Ensure alternatives are meaningfully different from each other +4. Maintain high quality and relevance to the task + +Your response should include 3 alternatives, each with its own reasoning.""" + +# System prompt for evaluation +EVALUATION_PROMPT = """You are an expert at evaluating and comparing different responses to tasks. Your role is to critically analyze each response and determine which is the most effective. + +Consider the following criteria when evaluating: +1. Relevance to the task +2. Completeness of the solution +3. Creativity and innovation +4. Practicality and feasibility +5. Clarity and coherence + +Your response should include: +1. The best response selected +2. A ranked list of all responses with scores and reasoning""" + + +class CortAgent: + def __init__( + self, + alternative_responses: int = 3, + ): + self.thinking_rounds = Agent( + agent_name="CortAgent", + agent_description="CortAgent is a multi-step agent that uses a battle royale approach to determine the best response to a task.", + model_name="gpt-4o-mini", + max_loops=1, + dynamic_temperature_enabled=True, + tools_list_dictionary=thinking_rounds_schema, + system_prompt=THINKING_ROUNDS_PROMPT, + ) + + self.alternatives_agent = Agent( + agent_name="CortAgentAlternatives", + agent_description="Generates multiple alternative responses to a task", + model_name="gpt-4o-mini", + max_loops=1, + dynamic_temperature_enabled=True, + tools_list_dictionary=alternative_responses_schema, + system_prompt=ALTERNATIVES_PROMPT, + ) + + self.evaluation_agent = Agent( + agent_name="CortAgentEvaluation", + agent_description="Evaluates and ranks alternative responses", + model_name="gpt-4o-mini", + max_loops=1, + dynamic_temperature_enabled=True, + tools_list_dictionary=evaluation_schema, + system_prompt=EVALUATION_PROMPT, + ) + + def run(self, task: str): + # First determine number of thinking rounds + num_rounds = self.thinking_rounds.run(task) + + # Initialize with the task + current_task = task + best_response = None + + # Run the battle royale for the determined number of rounds + for round_num in range(num_rounds): + # Generate alternatives + alternatives = self.alternatives_agent.run(current_task) + + # Evaluate alternatives + evaluation = self.evaluation_agent.run(alternatives) + + # Update best response and current task for next round + best_response = evaluation["evaluation"]["best_response"] + current_task = f"Previous best response: {best_response}\nOriginal task: {task}" + + return best_response diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index eca13089..5c379fc2 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -193,6 +193,7 @@ nav: - Documentation: - Agent Class Documentation: "swarms/structs/agent.md" - Create and Run Agents from YAML: "swarms/agents/create_agents_yaml.md" + - Integrating Various Models into Your Agents: "swarms/models/agent_and_models.md" - Tools: - Structured Outputs: "swarms/agents/structured_outputs.md" - Overview: "swarms/tools/main.md" diff --git a/docs/swarms/models/agent_and_models.md b/docs/swarms/models/agent_and_models.md new file mode 100644 index 00000000..2b13b34f --- /dev/null +++ b/docs/swarms/models/agent_and_models.md @@ -0,0 +1,196 @@ +# Model Integration in Agents + +!!! info "About Model Integration" + Agents supports multiple model providers through LiteLLM integration, allowing you to easily switch between different language models. This document outlines the available providers and how to use them with agents. + +## Important Note on Model Names + +!!! warning "Required Format" + When specifying a model in Swarms, you must use the format `provider/model_name`. For example: + ```python + "openai/gpt-4" + "anthropic/claude-3-opus-latest" + "cohere/command-r-plus" + ``` + This format ensures Swarms knows which provider to use for the specified model. + +## Available Model Providers + +### OpenAI + +??? info "OpenAI Models" + - **Provider name**: `openai` + - **Available Models**: + - `gpt-4` + - `gpt-3.5-turbo` + - `gpt-4-turbo-preview` + +### Anthropic +??? info "Anthropic Models" + - **Provider name**: `anthropic` + - **Available Models**: + - **Claude 3 Opus**: + - `claude-3-opus-latest` + - `claude-3-opus-20240229` + - **Claude 3 Sonnet**: + - `claude-3-sonnet-20240229` + - `claude-3-5-sonnet-latest` + - `claude-3-5-sonnet-20240620` + - `claude-3-7-sonnet-latest` + - `claude-3-7-sonnet-20250219` + - `claude-3-5-sonnet-20241022` + - **Claude 3 Haiku**: + - `claude-3-haiku-20240307` + - `claude-3-5-haiku-20241022` + - `claude-3-5-haiku-latest` + - **Legacy Models**: + - `claude-2` + - `claude-2.1` + - `claude-instant-1` + - `claude-instant-1.2` + +### Cohere +??? info "Cohere Models" + - **Provider name**: `cohere` + - **Available Models**: + - **Command**: + - `command` + - `command-r` + - `command-r-08-2024` + - `command-r7b-12-2024` + - **Command Light**: + - `command-light` + - **Command R Plus**: + - `command-r-plus` + - `command-r-plus-08-2024` + +### Google +??? info "Google Models" + - **Provider name**: `google` + - **Available Models**: + - `gemini-pro` + - `gemini-pro-vision` + +### Mistral +??? info "Mistral Models" + - **Provider name**: `mistral` + - **Available Models**: + - `mistral-tiny` + - `mistral-small` + - `mistral-medium` + +## Using Different Models with Swarms + +To use a different model with your Swarms agent, specify the model name in the `model_name` parameter when initializing the Agent, using the provider/model_name format: + +```python +from swarms.structs.agent import Agent + +# Using OpenAI's GPT-4 +agent = Agent( + agent_name="Research-Agent", + model_name="openai/gpt-4o", # Note the provider/model_name format + # ... other parameters +) + +# Using Anthropic's Claude +agent = Agent( + agent_name="Analysis-Agent", + model_name="anthropic/claude-3-sonnet-20240229", # Note the provider/model_name format + # ... other parameters +) + +# Using Cohere's Command +agent = Agent( + agent_name="Text-Agent", + model_name="cohere/command-r-plus", # Note the provider/model_name format + # ... other parameters +) +``` + +## Model Configuration + +When using different models, you can configure various parameters: + +```python +agent = Agent( + agent_name="Custom-Agent", + model_name="openai/gpt-4", + temperature=0.7, # Controls randomness (0.0 to 1.0) + max_tokens=2000, # Maximum tokens in response + top_p=0.9, # Nucleus sampling parameter + frequency_penalty=0.0, # Reduces repetition + presence_penalty=0.0, # Encourages new topics + # ... other parameters +) +``` + +## Best Practices + +### Model Selection +!!! tip "Choosing the Right Model" + - Choose models based on your specific use case + - Consider cost, performance, and feature requirements + - Test different models for your specific task + +### Error Handling +!!! warning "Error Management" + - Implement proper error handling for model-specific errors + - Handle rate limits and API quotas appropriately + +### Cost Management +!!! note "Cost Considerations" + - Monitor token usage and costs + - Use appropriate model sizes for your needs + +## Example Use Cases + +### 1. Complex Analysis (GPT-4) + +```python +agent = Agent( + agent_name="Analysis-Agent", + model_name="openai/gpt-4", # Note the provider/model_name format + temperature=0.3, # Lower temperature for more focused responses + max_tokens=4000 +) +``` + +### 2. Creative Tasks (Claude) + +```python +agent = Agent( + agent_name="Creative-Agent", + model_name="anthropic/claude-3-sonnet-20240229", # Note the provider/model_name format + temperature=0.8, # Higher temperature for more creative responses + max_tokens=2000 +) +``` + +### 3. Vision Tasks (Gemini) + +```python +agent = Agent( + agent_name="Vision-Agent", + model_name="google/gemini-pro-vision", # Note the provider/model_name format + temperature=0.4, + max_tokens=1000 +) +``` + +## Troubleshooting + +!!! warning "Common Issues" + If you encounter issues with specific models: + + 1. Verify your API keys are correctly set + 2. Check model availability in your region + 3. Ensure you have sufficient quota/credits + 4. Verify the model name is correct and supported + +## Additional Resources + +- [LiteLLM Documentation](https://docs.litellm.ai/){target=_blank} +- [OpenAI API Documentation](https://platform.openai.com/docs/api-reference){target=_blank} +- [Anthropic API Documentation](https://docs.anthropic.com/claude/reference/getting-started-with-the-api){target=_blank} +- [Google AI Documentation](https://ai.google.dev/docs){target=_blank} diff --git a/pyproject.toml b/pyproject.toml index e9628e6b..f778cee0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "7.7.5" +version = "7.7.6" description = "Swarms - TGSC" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 169009ca..6617abb8 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -25,7 +25,6 @@ from swarms.structs.stopping_conditions import ( ) __all__ = [ - # "ToolAgent", "check_done", "check_finished", "check_complete", From a2d0e50feb38ee9ca3779ff9ab8638e6bdfb0c53 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sat, 17 May 2025 22:13:40 +0000 Subject: [PATCH 3/4] feat: add robust XML output support throughout the agentic framework (#841) --- examples/xml_output_example.py | 43 ++++++++++++++++++++++++ swarms/structs/agent.py | 5 +-- swarms/utils/history_output_formatter.py | 11 +++--- swarms/utils/xml_utils.py | 40 ++++++++++++++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 examples/xml_output_example.py create mode 100644 swarms/utils/xml_utils.py diff --git a/examples/xml_output_example.py b/examples/xml_output_example.py new file mode 100644 index 00000000..cdadb17e --- /dev/null +++ b/examples/xml_output_example.py @@ -0,0 +1,43 @@ +from swarms.structs.agent import Agent +import xml.etree.ElementTree as ET + +""" +User-Facing Example: How to get XML output from a Swarms agent +------------------------------------------------------------- +This example demonstrates how to use the Swarms agent framework to generate output in XML format. +You can use this pattern in your own projects to get structured, machine-readable results from any agent. +""" + +if __name__ == "__main__": + # Step 1: Create your agent and specify output_type="xml" + agent = Agent( + agent_name="XML-Output-Agent", + agent_description="Agent that demonstrates XML output support", + max_loops=2, + model_name="gpt-4o-mini", + dynamic_temperature_enabled=True, + interactive=False, + output_type="xml", # Request XML output + ) + + # Step 2: Ask your question or give a task + task = "Summarize the latest trends in AI research." + xml_out = agent.run(task) + + # Step 3: Print the XML output for inspection or downstream use + print("\n===== XML Output from Agent =====\n") + print(xml_out) + + # Step 4: (Optional) Parse the XML output for programmatic use + try: + root = ET.fromstring(xml_out) + print("\nParsed XML Root Tag:", root.tag) + print("Number of top-level children:", len(root)) + # Print the first child tag and text for demonstration + if len(root): + print("First child tag:", root[0].tag) + print("First child text:", root[0].text) + except ET.ParseError as e: + print(f"Failed to parse XML: {e}") + +# You can copy and adapt this example for your own agent workflows! diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index a38691b2..2dece63b 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -201,7 +201,7 @@ class Agent: limit_tokens_from_string (Callable): The function to limit tokens from a string custom_tools_prompt (Callable): The custom tools prompt tool_schema (ToolUsageType): The tool schema - output_type (agent_output_type): The output type + output_type (agent_output_type): The output type. Supported: 'str', 'string', 'list', 'json', 'dict', 'yaml', 'xml'. function_calling_type (str): The function calling type output_cleaner (Callable): The output cleaner function function_calling_format_type (str): The function calling format type @@ -998,7 +998,7 @@ class Agent: Returns: Any: The output of the agent. - (string, list, json, dict, yaml) + (string, list, json, dict, yaml, xml) Examples: agent(task="What is the capital of France?") @@ -1220,6 +1220,7 @@ class Agent: if self.autosave: self.save() + # Output formatting based on output_type return history_output_formatter( self.short_memory, type=self.output_type ) diff --git a/swarms/utils/history_output_formatter.py b/swarms/utils/history_output_formatter.py index 784437ed..2ba42d33 100644 --- a/swarms/utils/history_output_formatter.py +++ b/swarms/utils/history_output_formatter.py @@ -1,7 +1,7 @@ import yaml from swarms.structs.conversation import Conversation - from typing import Literal, Union, List, Dict, Any +from swarms.utils.xml_utils import to_xml_string HistoryOutputType = Literal[ "list", @@ -14,14 +14,12 @@ HistoryOutputType = Literal[ "json", "all", "yaml", + "xml", # "dict-final", "dict-all-except-first", "str-all-except-first", ] -output_type: HistoryOutputType - - def history_output_formatter( conversation: Conversation, type: HistoryOutputType = "list" ) -> Union[List[Dict[str, Any]], Dict[str, Any], str]: @@ -39,11 +37,12 @@ def history_output_formatter( return conversation.get_str() elif type == "yaml": return yaml.safe_dump(conversation.to_dict(), sort_keys=False) - # elif type == "dict-final": - # return conversation.to_dict() elif type == "dict-all-except-first": return conversation.return_all_except_first() elif type == "str-all-except-first": return conversation.return_all_except_first_string() + elif type == "xml": + data = conversation.to_dict() + return to_xml_string(data, root_tag="conversation") else: raise ValueError(f"Invalid type: {type}") diff --git a/swarms/utils/xml_utils.py b/swarms/utils/xml_utils.py new file mode 100644 index 00000000..1e310f51 --- /dev/null +++ b/swarms/utils/xml_utils.py @@ -0,0 +1,40 @@ +import xml.etree.ElementTree as ET +from typing import Any + +def dict_to_xml(tag: str, d: dict) -> ET.Element: + """Convert a dictionary to an XML Element.""" + elem = ET.Element(tag) + for key, val in d.items(): + child = ET.Element(str(key)) + if isinstance(val, dict): + child.append(dict_to_xml(str(key), val)) + elif isinstance(val, list): + for item in val: + if isinstance(item, dict): + child.append(dict_to_xml(str(key), item)) + else: + item_elem = ET.Element("item") + item_elem.text = str(item) + child.append(item_elem) + else: + child.text = str(val) + elem.append(child) + return elem + +def to_xml_string(data: Any, root_tag: str = "root") -> str: + """Convert a dict or list to an XML string.""" + if isinstance(data, dict): + elem = dict_to_xml(root_tag, data) + elif isinstance(data, list): + elem = ET.Element(root_tag) + for item in data: + if isinstance(item, dict): + elem.append(dict_to_xml("item", item)) + else: + item_elem = ET.Element("item") + item_elem.text = str(item) + elem.append(item_elem) + else: + elem = ET.Element(root_tag) + elem.text = str(data) + return ET.tostring(elem, encoding="unicode") From fc06897b4e40fd03631fe4865818d44b729e6a35 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sat, 17 May 2025 22:16:48 +0000 Subject: [PATCH 4/4] feat: add robust XML output support throughout the agentic framework (#841) --- swarms/structs/output_types.py | 2 +- swarms/structs/swarm_router.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/swarms/structs/output_types.py b/swarms/structs/output_types.py index 5deab2cd..ca1376a6 100644 --- a/swarms/structs/output_types.py +++ b/swarms/structs/output_types.py @@ -3,4 +3,4 @@ from swarms.utils.history_output_formatter import ( ) # Use the OutputType for type annotations -output_type: OutputType +output_type: OutputType # OutputType now includes 'xml' diff --git a/swarms/structs/swarm_router.py b/swarms/structs/swarm_router.py index 6371775f..f73cf7a8 100644 --- a/swarms/structs/swarm_router.py +++ b/swarms/structs/swarm_router.py @@ -121,7 +121,7 @@ class SwarmRouter: shared_memory_system (Any, optional): Shared memory system for agents. Defaults to None. rules (str, optional): Rules to inject into every agent. Defaults to None. documents (List[str], optional): List of document file paths to use. Defaults to empty list. - output_type (str, optional): Output format type. Defaults to "string". + output_type (str, optional): Output format type. Defaults to "string". Supported: 'str', 'string', 'list', 'json', 'dict', 'yaml', 'xml'. Attributes: name (str): Name identifier for the SwarmRouter instance @@ -136,7 +136,7 @@ class SwarmRouter: shared_memory_system (Any): Shared memory system for agents rules (str): Rules injected into every agent documents (List[str]): List of document file paths - output_type (str): Output format type + output_type (str): Output format type. Supported: 'str', 'string', 'list', 'json', 'dict', 'yaml', 'xml'. logs (List[SwarmLog]): List of execution logs swarm: The instantiated swarm object