From 1948eddff34d85be8c85a1950e3047b01a12e410 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sun, 15 Jun 2025 01:37:27 +0530 Subject: [PATCH] Add conversation export example and docs --- README.md | 13 ++++++++++ docs/mkdocs.yml | 2 ++ docs/utils/history_output_formatter.md | 25 +++++++++++++++++++ .../conversation_export/export_to_yaml_xml.py | 15 +++++++++++ 4 files changed, 55 insertions(+) create mode 100644 docs/utils/history_output_formatter.md create mode 100644 examples/conversation_export/export_to_yaml_xml.py diff --git a/README.md b/README.md index f4f85753..82b302e8 100644 --- a/README.md +++ b/README.md @@ -491,6 +491,19 @@ agent.model_dump_json() print(agent.to_toml()) ``` +### Export Conversation History + +```python +from swarms.structs.conversation import Conversation +from swarms.utils.history_output_formatter import history_output_formatter + +conversation = Conversation() +conversation.add("user", "Hello") +conversation.add("assistant", "Hi!") + +yaml_history = history_output_formatter(conversation, type="yaml") +xml_history = history_output_formatter(conversation, type="xml") +``` diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 9a4a8823..eecfc95b 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -281,6 +281,8 @@ nav: - Pinecone: "swarms_memory/pinecone.md" - Faiss: "swarms_memory/faiss.md" + - Utilities: + - History Output Formatter: "utils/history_output_formatter.md" - Deployment Solutions: - Deploy your Swarms on Google Cloud Run: "swarms_cloud/cloud_run.md" - Deploy your Swarms on Phala: "swarms_cloud/phala_deploy.md" diff --git a/docs/utils/history_output_formatter.md b/docs/utils/history_output_formatter.md new file mode 100644 index 00000000..47ff7732 --- /dev/null +++ b/docs/utils/history_output_formatter.md @@ -0,0 +1,25 @@ +# history_output_formatter Utility + +The `history_output_formatter` function converts a `Conversation` object into various formats. +It supports lists, dictionaries, strings, JSON, YAML, and XML. + +## Export to YAML + +```python +from swarms.structs.conversation import Conversation +from swarms.utils.history_output_formatter import history_output_formatter + +conversation = Conversation() +conversation.add("user", "Hello") +conversation.add("assistant", "Hi there!") + +yaml_history = history_output_formatter(conversation, type="yaml") +print(yaml_history) +``` + +## Export to XML + +```python +xml_history = history_output_formatter(conversation, type="xml") +print(xml_history) +``` diff --git a/examples/conversation_export/export_to_yaml_xml.py b/examples/conversation_export/export_to_yaml_xml.py new file mode 100644 index 00000000..2f696d87 --- /dev/null +++ b/examples/conversation_export/export_to_yaml_xml.py @@ -0,0 +1,15 @@ +from swarms.structs.conversation import Conversation +from swarms.utils.history_output_formatter import history_output_formatter + +# Create a simple conversation +conversation = Conversation() +conversation.add("user", "Hello") +conversation.add("assistant", "Hi there!") + +# Export the conversation to YAML +yaml_output = history_output_formatter(conversation, type="yaml") +print("YAML Format:\n", yaml_output) + +# Export the conversation to XML +xml_output = history_output_formatter(conversation, type="xml") +print("\nXML Format:\n", xml_output)