diff --git a/README.md b/README.md index 4a39b021..af66cf6a 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ We provide vast array of features to save agent states using json, yaml, toml, u | `get_docs_from_doc_folders()` | Fetches all the documents from the doc folders. | | `activate_agentops()` | Activates agent operations. | | `check_end_session_agentops()` | Checks the end of the session for agent operations. | +| `model_dump_markdown()` | Dumps the model output to a markdown file. | @@ -544,7 +545,7 @@ Steps: For example, here's an example on how to create an agent from griptape. -Here’s how you can create a custom **Griptape** agent that integrates with the **Swarms** framework by inheriting from the `Agent` class in **Swarms** and overriding the `run(task: str) -> str` method. +Here's how you can create a custom **Griptape** agent that integrates with the **Swarms** framework by inheriting from the `Agent` class in **Swarms** and overriding the `run(task: str) -> str` method. ```python @@ -1857,7 +1858,7 @@ Documentation is located here at: [docs.swarms.world](https://docs.swarms.world) ----- ## Folder Structure -The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`. +The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`. ```sh ├── __init__.py diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index be02c704..32bf750e 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -2001,8 +2001,97 @@ class Agent: model = YamlModel() return model.dict_to_yaml(self.agent_output.model_dump()) + elif self.output_type == "markdown": + # Convert the responses to markdown format + markdown_output = "# Agent Output\n\n" + markdown_output += f"## Agent: {self.agent_name}\n\n" + markdown_output += "### Responses:\n\n" + for i, response in enumerate(responses, 1): + markdown_output += f"{i}. {response}\n\n" + return markdown_output + elif self.output_type == "dict": return self.agent_output.model_dump() elif self.return_history: return self.short_memory.return_history_as_string() + + def to_markdown(self) -> str: + """Convert agent output to markdown format + + Returns: + str: Markdown formatted string of agent output + """ + markdown = [] + + # Add agent name as title + markdown.append(f"# {self.agent_name}\n") + + # Add metadata section + markdown.append("## Metadata\n") + markdown.append(f"- **Created**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + markdown.append(f"- **Max Loops**: {self.max_loops}") + markdown.append(f"- **Autosave**: {self.autosave}") + markdown.append("") + + # Add system prompt if exists + if self.system_prompt: + markdown.append("## System Prompt\n") + markdown.append(f"{self.system_prompt}\n") + + # Add agent output + if hasattr(self, 'agent_output'): + markdown.append("## Output\n") + if isinstance(self.agent_output, list): + for i, output in enumerate(self.agent_output, 1): + markdown.append(f"{i}. {output}\n") + else: + markdown.append(f"{self.agent_output}\n") + + # Add conversation history if exists + if self.short_memory and self.short_memory.messages: + markdown.append("## Conversation History\n") + for msg in self.short_memory.messages: + markdown.append(f"### {msg.role.title()}") + markdown.append(f"{msg.content}\n") + + # Add tools section if tools exist + if hasattr(self, 'tools') and self.tools: + markdown.append("## Tools Used\n") + for tool in self.tools: + markdown.append(f"- {tool.__class__.__name__}") + markdown.append("") + + return "\n".join(markdown) + + def model_dump_markdown(self) -> str: + """Save the agent output as a markdown file + + Returns: + str: Path to saved markdown file + """ + try: + # Generate filename with timestamp + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + filename = f"{self.agent_name}_{timestamp}.md" + + logger.info( + f"Saving {self.agent_name} model to Markdown in the {self.workspace_dir} directory" + ) + + # Create markdown content + markdown_content = self.to_markdown() + + # Save to file + create_file_in_folder( + self.workspace_dir, + filename, + markdown_content, + ) + + logger.success(f"Successfully saved markdown to {filename}") + return f"Model saved to {self.workspace_dir}/{filename}" + + except Exception as e: + logger.error(f"Error saving markdown: {str(e)}") + raise diff --git a/swarms/tools/base_tool.py b/swarms/tools/base_tool.py index d6c8ef1e..c97096f0 100644 --- a/swarms/tools/base_tool.py +++ b/swarms/tools/base_tool.py @@ -442,6 +442,8 @@ class BaseTool(BaseModel): f"Function {func.__name__} does not have type hints" ) + + # # Example function definitions and mappings # def get_current_weather(location, unit='celsius'):