swarms 7.5.2__tar.gz → 7.5.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. {swarms-7.5.2 → swarms-7.5.5}/PKG-INFO +1 -1
  2. {swarms-7.5.2 → swarms-7.5.5}/pyproject.toml +1 -1
  3. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/__init__.py +7 -0
  4. swarms-7.5.5/swarms/agents/consistency_agent.py +207 -0
  5. swarms-7.5.5/swarms/agents/i_agent.py +302 -0
  6. swarms-7.5.5/swarms/agents/reasoning_agents.py +130 -0
  7. swarms-7.5.5/swarms/agents/reasoning_duo.py +109 -0
  8. swarms-7.5.5/swarms/prompts/reasoning_prompt.py +9 -0
  9. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/__init__.py +3 -0
  10. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent.py +30 -22
  11. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/conversation.py +19 -11
  12. swarms-7.5.5/swarms/structs/de_hallucination_swarm.py +273 -0
  13. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/malt.py +8 -26
  14. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_router.py +5 -3
  15. swarms-7.5.5/swarms/utils/history_output_formatter.py +14 -0
  16. {swarms-7.5.2 → swarms-7.5.5}/LICENSE +0 -0
  17. {swarms-7.5.2 → swarms-7.5.5}/README.md +0 -0
  18. {swarms-7.5.2 → swarms-7.5.5}/swarms/__init__.py +0 -0
  19. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/agent_print.py +0 -0
  20. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/ape_agent.py +0 -0
  21. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/auto_generate_swarm_config.py +0 -0
  22. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/create_agents_from_yaml.py +0 -0
  23. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/openai_assistant.py +1 -1
  24. {swarms-7.5.2 → swarms-7.5.5}/swarms/agents/tool_agent.py +0 -0
  25. {swarms-7.5.2 → swarms-7.5.5}/swarms/artifacts/__init__.py +0 -0
  26. {swarms-7.5.2 → swarms-7.5.5}/swarms/artifacts/main_artifact.py +0 -0
  27. {swarms-7.5.2 → swarms-7.5.5}/swarms/cli/__init__.py +0 -0
  28. {swarms-7.5.2 → swarms-7.5.5}/swarms/cli/create_agent.py +0 -0
  29. {swarms-7.5.2 → swarms-7.5.5}/swarms/cli/main.py +0 -0
  30. {swarms-7.5.2 → swarms-7.5.5}/swarms/cli/onboarding_process.py +0 -0
  31. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/__init__.py +0 -0
  32. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/accountant_swarm_prompts.py +0 -0
  33. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/ag_prompt.py +0 -0
  34. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/aga.py +0 -0
  35. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/agent_prompt.py +0 -0
  36. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/agent_prompts.py +0 -0
  37. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/agent_system_prompts.py +0 -0
  38. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/ai_research_team.py +0 -0
  39. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/aot_prompt.py +0 -0
  40. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/autobloggen.py +0 -0
  41. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/autoswarm.py +0 -0
  42. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/chat_prompt.py +0 -0
  43. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/code_interpreter.py +0 -0
  44. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/code_spawner.py +0 -0
  45. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/debate.py +0 -0
  46. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/documentation.py +0 -0
  47. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/education.py +0 -0
  48. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/finance_agent_prompt.py +0 -0
  49. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/finance_agent_sys_prompt.py +0 -0
  50. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/growth_agent_prompt.py +0 -0
  51. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/idea2img.py +0 -0
  52. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/legal_agent_prompt.py +0 -0
  53. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/logistics.py +0 -0
  54. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/meta_system_prompt.py +0 -0
  55. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/multi_modal_autonomous_instruction_prompt.py +0 -0
  56. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/multi_modal_prompts.py +0 -0
  57. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/multi_modal_visual_prompts.py +0 -0
  58. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/operations_agent_prompt.py +0 -0
  59. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/personal_stylist.py +0 -0
  60. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/product_agent_prompt.py +0 -0
  61. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/programming.py +0 -0
  62. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/project_manager.py +0 -0
  63. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/prompt.py +0 -0
  64. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/prompt_generator.py +0 -0
  65. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/prompt_generator_optimizer.py +0 -0
  66. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/python.py +0 -0
  67. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/react.py +0 -0
  68. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/refiner_agent_prompt.py +0 -0
  69. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/sales.py +0 -0
  70. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/sales_prompts.py +0 -0
  71. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/security_team.py +0 -0
  72. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/self_operating_prompt.py +0 -0
  73. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/sop_generator_agent_prompt.py +0 -0
  74. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/summaries_prompts.py +0 -0
  75. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/support_agent_prompt.py +0 -0
  76. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/swarm_manager_agent.py +0 -0
  77. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/task_assignment_prompt.py +0 -0
  78. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/tests.py +0 -0
  79. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/tools.py +0 -0
  80. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/urban_planning.py +0 -0
  81. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/visual_cot.py +0 -0
  82. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/worker_prompt.py +0 -0
  83. {swarms-7.5.2 → swarms-7.5.5}/swarms/prompts/xray_swarm_prompt.py +0 -0
  84. {swarms-7.5.2 → swarms-7.5.5}/swarms/schemas/__init__.py +0 -0
  85. {swarms-7.5.2 → swarms-7.5.5}/swarms/schemas/agent_input_schema.py +0 -0
  86. {swarms-7.5.2 → swarms-7.5.5}/swarms/schemas/agent_step_schemas.py +0 -0
  87. {swarms-7.5.2 → swarms-7.5.5}/swarms/schemas/base_schemas.py +0 -0
  88. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent_builder.py +0 -0
  89. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent_registry.py +0 -0
  90. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent_roles.py +0 -0
  91. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent_router.py +0 -0
  92. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agent_security.py +0 -0
  93. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/agents_available.py +0 -0
  94. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/airflow_swarm.py +0 -0
  95. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/async_workflow.py +0 -0
  96. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/auto_swarm.py +0 -0
  97. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/auto_swarm_builder.py +0 -0
  98. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/base_structure.py +0 -0
  99. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/base_swarm.py +0 -0
  100. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/base_workflow.py +0 -0
  101. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/concat.py +0 -0
  102. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/concurrent_workflow.py +0 -0
  103. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/csv_to_agent.py +0 -0
  104. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/graph_swarm.py +0 -0
  105. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/graph_workflow.py +0 -0
  106. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/groupchat.py +0 -0
  107. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/hiearchical_swarm.py +0 -0
  108. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/majority_voting.py +0 -0
  109. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/matrix_swarm.py +0 -0
  110. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/meme_agent_persona_generator.py +0 -0
  111. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/mixture_of_agents.py +0 -0
  112. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/model_router.py +0 -0
  113. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/multi_agent_collab.py +0 -0
  114. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/multi_agent_exec.py +0 -0
  115. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/multi_agent_orchestrator.py +0 -0
  116. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/octotools.py +0 -0
  117. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/omni_agent_types.py +0 -0
  118. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/output_type.py +0 -0
  119. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/output_types.py +0 -0
  120. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/pulsar_swarm.py +0 -0
  121. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/queue_swarm.py +0 -0
  122. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/rearrange.py +0 -0
  123. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/round_robin.py +0 -0
  124. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/safe_loading.py +0 -0
  125. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/sequential_workflow.py +0 -0
  126. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/spreadsheet_swarm.py +0 -0
  127. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/stopping_conditions.py +0 -0
  128. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_arange.py +0 -0
  129. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_builder.py +0 -0
  130. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_eval.py +0 -0
  131. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_id_generator.py +0 -0
  132. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_load_balancer.py +0 -0
  133. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_matcher.py +0 -0
  134. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_output_type.py +0 -0
  135. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarm_registry.py +0 -0
  136. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarming_architectures.py +0 -0
  137. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/swarms_api.py +0 -0
  138. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/talk_hier.py +0 -0
  139. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/tree_swarm.py +0 -0
  140. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/utils.py +0 -0
  141. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/various_alt_swarms.py +0 -0
  142. {swarms-7.5.2 → swarms-7.5.5}/swarms/structs/workspace_manager.py +0 -0
  143. {swarms-7.5.2 → swarms-7.5.5}/swarms/telemetry/__init__.py +0 -0
  144. {swarms-7.5.2 → swarms-7.5.5}/swarms/telemetry/bootup.py +0 -0
  145. {swarms-7.5.2 → swarms-7.5.5}/swarms/telemetry/main.py +0 -0
  146. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/__init__.py +0 -0
  147. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/base_tool.py +0 -0
  148. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/cohere_func_call_schema.py +0 -0
  149. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/func_calling_utils.py +0 -0
  150. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/func_to_str.py +0 -0
  151. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/function_util.py +0 -0
  152. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/json_former.py +0 -0
  153. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/json_utils.py +0 -0
  154. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/logits_processor.py +0 -0
  155. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/openai_func_calling_schema_pydantic.py +0 -0
  156. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/openai_tool_creator_decorator.py +0 -0
  157. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/py_func_to_openai_func_str.py +0 -0
  158. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/pydantic_to_json.py +0 -0
  159. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/tool_parse_exec.py +0 -0
  160. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/tool_registry.py +0 -0
  161. {swarms-7.5.2 → swarms-7.5.5}/swarms/tools/tool_utils.py +0 -0
  162. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/__init__.py +0 -0
  163. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/agent_ops_check.py +0 -0
  164. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/any_to_str.py +0 -0
  165. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/auto_download_check_packages.py +0 -0
  166. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/calculate_func_metrics.py +0 -0
  167. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/data_to_text.py +0 -0
  168. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/disable_logging.py +0 -0
  169. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/file_processing.py +0 -0
  170. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/formatter.py +0 -0
  171. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/function_caller_model.py +0 -0
  172. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/litellm_tokenizer.py +0 -0
  173. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/litellm_wrapper.py +0 -0
  174. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/loguru_logger.py +0 -0
  175. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/markdown_message.py +0 -0
  176. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/pandas_utils.py +0 -0
  177. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/parse_code.py +0 -0
  178. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/pdf_to_text.py +0 -0
  179. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/swarm_reliability_checks.py +0 -0
  180. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/try_except_wrapper.py +0 -0
  181. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/visualizer.py +0 -0
  182. {swarms-7.5.2 → swarms-7.5.5}/swarms/utils/wrapper_clusterop.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: swarms
3
- Version: 7.5.2
3
+ Version: 7.5.5
4
4
  Summary: Swarms - TGSC
5
5
  Home-page: https://github.com/kyegomez/swarms
6
6
  License: MIT
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
5
5
 
6
6
  [tool.poetry]
7
7
  name = "swarms"
8
- version = "7.5.2"
8
+ version = "7.5.5"
9
9
  description = "Swarms - TGSC"
10
10
  license = "MIT"
11
11
  authors = ["Kye Gomez <kye@apac.ai>"]
@@ -16,6 +16,10 @@ from swarms.agents.create_agents_from_yaml import (
16
16
  create_agents_from_yaml,
17
17
  )
18
18
 
19
+ from swarms.agents.i_agent import IterativeReflectiveExpansion
20
+ from swarms.agents.consistency_agent import SelfConsistencyAgent
21
+ from swarms.agents.reasoning_duo import ReasoningDuo
22
+
19
23
  __all__ = [
20
24
  # "ToolAgent",
21
25
  "check_done",
@@ -29,4 +33,7 @@ __all__ = [
29
33
  "check_exit",
30
34
  "check_end",
31
35
  "create_agents_from_yaml",
36
+ "IterativeReflectiveExpansion",
37
+ "SelfConsistencyAgent",
38
+ "ReasoningDuo",
32
39
  ]
@@ -0,0 +1,207 @@
1
+ from collections import Counter
2
+ from concurrent.futures import ThreadPoolExecutor, as_completed
3
+ from typing import List
4
+
5
+ from loguru import logger
6
+
7
+ from swarms.structs.agent import Agent
8
+ from swarms.structs.conversation import Conversation
9
+ from swarms.structs.malt import majority_voting_prompt
10
+ from swarms.structs.output_types import OutputType
11
+ from swarms.utils.any_to_str import any_to_str
12
+ from swarms.utils.history_output_formatter import (
13
+ history_output_formatter,
14
+ )
15
+
16
+ CONSISTENCY_SYSTEM_PROMPT = """
17
+ You are a reasoning agent designed for complex problem-solving and decision-making. Your objective is to provide clear and reliable responses through structured reasoning. Begin by thoroughly understanding the problem, rephrasing it for clarity, and identifying key components. Develop a logical plan that breaks the problem into manageable steps, detailing your approach and any assumptions made. Validate your information with reliable sources and assess the accuracy of your calculations. Explore multiple solutions, weighing their pros and cons, and maintain transparency by documenting your reasoning process, uncertainties, and biases. Summarize your findings in a concise final answer that reflects your thorough analysis, ensuring it is well-organized and accessible. Adapt your reasoning to the context of the problem, integrating new information as needed, and implement error-handling strategies to address any issues that arise. Finally, reflect on your reasoning process to identify areas for improvement and ensure consistency across all reasoning paths.
18
+ """
19
+
20
+
21
+ def aggregation_agent(
22
+ responses: List[str],
23
+ prompt: str = majority_voting_prompt,
24
+ model_name: str = "gpt-4o-mini",
25
+ ) -> str:
26
+ """
27
+ Aggregates a list of responses into a single final answer.
28
+ """
29
+ task = any_to_str(responses)
30
+
31
+ agent = Agent(
32
+ agent_name="Aggregation-Agent",
33
+ description="An agent that aggregates a list of responses into a single final answer.",
34
+ model_name=model_name,
35
+ system_prompt=prompt,
36
+ max_loops=1,
37
+ )
38
+
39
+ final_answer = agent.run(task)
40
+
41
+ return final_answer
42
+
43
+
44
+ class SelfConsistencyAgent(Agent):
45
+ def __init__(
46
+ self,
47
+ name: str = "Self-Consistency-Agent",
48
+ description: str = "An agent that uses self consistency to generate a final answer.",
49
+ system_prompt: str = CONSISTENCY_SYSTEM_PROMPT,
50
+ num_samples: int = 5,
51
+ max_loops: int = 1,
52
+ majority_voting_prompt: str = None,
53
+ eval: bool = False,
54
+ output_type: OutputType = "dict",
55
+ **kwargs,
56
+ ):
57
+ """
58
+ Initializes the SelfConsistencyAgent.
59
+
60
+ Args:
61
+ num_samples (int): Number of independent responses to sample.
62
+ **kwargs: Other keyword arguments passed to the base Agent.
63
+ """
64
+ super().__init__(
65
+ name=name,
66
+ description=description,
67
+ **kwargs,
68
+ )
69
+ self.num_samples = num_samples
70
+ self.conversation = Conversation()
71
+ self.max_loops = max_loops
72
+ self.majority_voting_prompt = majority_voting_prompt
73
+ self.eval = eval
74
+ self.output_type = output_type
75
+ self.system_prompt = system_prompt
76
+
77
+ def run(
78
+ self, task: str, answer: str = None, *args, **kwargs
79
+ ) -> str:
80
+ """
81
+ Generates multiple responses for the given prompt and aggregates them concurrently.
82
+
83
+ Args:
84
+ task (str): The input prompt.
85
+
86
+ Returns:
87
+ str: The aggregated final answer.
88
+ """
89
+ responses = []
90
+ logger.info(
91
+ f"Generating {self.num_samples} responses concurrently..."
92
+ )
93
+
94
+ self.conversation.add(role="User", content=task)
95
+
96
+ with ThreadPoolExecutor() as executor:
97
+ futures = {
98
+ executor.submit(super().run, task, *args, **kwargs): i
99
+ for i in range(self.num_samples)
100
+ }
101
+ for future in as_completed(futures):
102
+ response = future.result()
103
+ responses.append(response)
104
+
105
+ self.conversation.add(role=self.agent_name, content=responses)
106
+
107
+ if self.eval:
108
+ if answer is not None:
109
+ correct = self.check_responses_for_answer(
110
+ responses, answer
111
+ )
112
+
113
+ if not correct:
114
+ logger.info(
115
+ "The answer is not correct. Please try again."
116
+ )
117
+ return None
118
+
119
+ # Aggregation agent
120
+ # final_answer = self.aggregation_agent(responses)
121
+
122
+ final_answer = aggregation_agent(responses)
123
+
124
+ self.conversation.add(
125
+ role="Majority Voting Agent", content=final_answer
126
+ )
127
+
128
+ return history_output_formatter(
129
+ self.conversation, self.output_type
130
+ )
131
+
132
+ def aggregate(self, responses: List[str]) -> str:
133
+ """
134
+ Aggregates a list of responses into a single final answer.
135
+
136
+ Here we use a simple majority vote (most common answer) as an example. Depending on
137
+ the task, you might need a more sophisticated aggregation (e.g., weighting, consensus reasoning, etc.).
138
+
139
+ Args:
140
+ responses (list of str): The list of responses.
141
+
142
+ Returns:
143
+ str: The aggregated answer.
144
+ """
145
+ # Count the frequency of each response.
146
+ counts = Counter(responses)
147
+ most_common, freq = counts.most_common(1)[0]
148
+ logger.info(
149
+ f"Aggregation complete. Most common response (appeared {freq} times):"
150
+ )
151
+ return most_common
152
+
153
+ def check_responses_for_answer(
154
+ self, responses: List[str], answer: str
155
+ ) -> bool:
156
+ """
157
+ Checks if the specified answer is present in any of the provided responses.
158
+
159
+ Args:
160
+ responses (List[str]): A list of responses to check.
161
+ answer (str): The answer to look for in the responses.
162
+
163
+ Returns:
164
+ bool: True if the answer is found in any response, False otherwise.
165
+ """
166
+ for response in responses:
167
+ if answer in response:
168
+ return True
169
+
170
+ # If the answer is not found, log the absence for each response
171
+ for response in responses:
172
+ if answer not in response:
173
+ self.conversation.add(
174
+ role="User",
175
+ content=f"The answer '{answer}' is not found in the response: '{response}'",
176
+ )
177
+ logger.info(
178
+ f"The answer '{answer}' is not found in the response: '{response}'"
179
+ )
180
+ return False
181
+
182
+ def batched_run(
183
+ self, tasks: List[str], *args, **kwargs
184
+ ) -> List[str]:
185
+ """
186
+ Runs the agent in a batched manner.
187
+ """
188
+ responses = []
189
+ for task in tasks:
190
+ response = self.run(task, *args, **kwargs)
191
+ responses.append(response)
192
+ return responses
193
+
194
+
195
+ # # Example usage:
196
+ # if __name__ == "__main__":
197
+ # agent = SelfConsistencyAgent(
198
+ # agent_name="Reasoning-Agent",
199
+ # model_name="gpt-4o-mini",
200
+ # max_loops=1,
201
+ # num_samples=5, # Number of samples for self consistency
202
+ # )
203
+
204
+ # prompt = "What is the 40th prime number?"
205
+ # final_answer = agent.run(prompt)
206
+ # print("\nFinal aggregated answer:")
207
+ # print(final_answer)
@@ -0,0 +1,302 @@
1
+ """
2
+ Iterative Reflective Expansion (IRE) Algorithm
3
+
4
+ A sophisticated reasoning framework that employs iterative hypothesis generation, simulation, and refinement to solve complex problems. IRE leverages a multi-step approach where an AI agent generates initial solution paths, evaluates their effectiveness through simulation, reflects on errors, and dynamically revises reasoning strategies. Through continuous cycles of hypothesis testing and meta-cognitive reflection, the algorithm progressively converges on optimal solutions by learning from both successful and unsuccessful reasoning attempts.
5
+
6
+
7
+ - IRE is a multi-step approach where an AI agent generates initial solution paths, evaluates their effectiveness through simulation, reflects on errors, and dynamically revises reasoning strategies.
8
+ - Through continuous cycles of hypothesis testing and meta-cognitive reflection, the algorithm progressively converges on optimal solutions by learning from both successful and unsuccessful reasoning attempts.
9
+
10
+
11
+ Workflow:
12
+ 1. Generate initial hypotheses
13
+ 2. Simulate paths
14
+ 3. Reflect on errors
15
+ 4. Revise paths
16
+ 5. Select promising paths
17
+ 6. Synthesize solution
18
+
19
+ """
20
+
21
+ from typing import List, Tuple
22
+ from loguru import logger
23
+ from swarms.structs.agent import Agent
24
+ from swarms.structs.conversation import Conversation
25
+ from swarms.structs.output_types import OutputType
26
+ from swarms.utils.history_output_formatter import (
27
+ history_output_formatter,
28
+ )
29
+
30
+ # Define a new system prompt for general problem solving
31
+ GENERAL_REASONING_AGENT_SYS_PROMPT = """
32
+ You are a highly capable problem-solving agent with a unique ability to reason through complex challenges via iterative reflection and hypothesis testing.
33
+ Your role is to assist in generating innovative solutions to a wide array of general problems by engaging in trial and error, reflective evaluation, and dynamic hypothesis expansion.
34
+ When presented with a problem statement, generate multiple hypotheses, simulate reasoning paths, reflect on errors, and iteratively refine your approach to produce the best solution.
35
+ Do not include any finance-related content.
36
+
37
+ """
38
+
39
+
40
+ class IterativeReflectiveExpansion:
41
+ """
42
+ A class implementing the Iterative Reflective Expansion (IRE) reasoning algorithm.
43
+
44
+ This algorithm leverages a Swarms agent to iteratively generate, simulate, reflect on, and refine reasoning paths
45
+ in order to solve complex problems through trial and error, reflective evaluation, and dynamic hypothesis expansion.
46
+ """
47
+
48
+ def __init__(
49
+ self,
50
+ agent_name: str = "General-Reasoning-Agent",
51
+ description: str = "A reasoning agent that can answer questions and help with tasks.",
52
+ agent: Agent = None,
53
+ max_iterations: int = 5,
54
+ system_prompt: str = GENERAL_REASONING_AGENT_SYS_PROMPT,
55
+ model_name: str = "gpt-4o-mini",
56
+ output_type: OutputType = "dict",
57
+ ) -> None:
58
+ """
59
+ Initialize the Iterative Reflective Expansion engine.
60
+
61
+ :param agent: The Swarms agent instance used to perform reasoning tasks.
62
+ :param max_iterations: Maximum number of iterations for the reasoning process.
63
+ """
64
+ self.agent_name = agent_name
65
+ self.description = description
66
+ self.agent = agent
67
+ self.max_iterations = max_iterations
68
+ self.output_type = output_type
69
+ self.system_prompt = system_prompt
70
+ self.conversation = Conversation()
71
+
72
+ self.agent = Agent(
73
+ agent_name=self.agent_name,
74
+ system_prompt=self.system_prompt,
75
+ model_name=model_name,
76
+ max_loops=1,
77
+ dynamic_temperature_enabled=True,
78
+ )
79
+
80
+ def generate_initial_hypotheses(self, task: str) -> List[str]:
81
+ """
82
+ Generate an initial set of reasoning hypotheses based on the problem input.
83
+
84
+ :param task: The problem statement.
85
+ :return: A list of candidate reasoning paths/hypotheses.
86
+ """
87
+ logger.info("Generating initial hypotheses for the problem.")
88
+ prompt = (
89
+ f"Given the following problem:\n\n"
90
+ f"'{task}'\n\n"
91
+ "Generate a list of possible approaches and strategies to solve it. "
92
+ "Present each approach on a new line."
93
+ )
94
+ response = self.agent.run(prompt)
95
+ self.conversation.add(
96
+ role=self.agent.agent_name, content=response
97
+ )
98
+ hypotheses = [
99
+ line.strip()
100
+ for line in response.split("\n")
101
+ if line.strip()
102
+ ]
103
+ logger.debug(f"Initial hypotheses: {hypotheses}")
104
+ return hypotheses
105
+
106
+ def simulate_path(self, path: str) -> Tuple[str, float, str]:
107
+ """
108
+ Simulate a given reasoning path and evaluate its effectiveness.
109
+
110
+ :param path: A candidate reasoning path.
111
+ :return: A tuple containing the simulated outcome, a numerical score (0.0 to 1.0), and error information.
112
+ """
113
+ logger.info(f"Simulating path: {path}")
114
+ prompt = (
115
+ f"Simulate the following reasoning path step by step and provide:\n"
116
+ f"1. Outcome: A brief summary of the resulting solution.\n"
117
+ f"2. Score: A numerical effectiveness score between 0.0 and 1.0.\n"
118
+ f"3. Errors: Any potential errors or shortcomings identified during the reasoning.\n\n"
119
+ f"Reasoning Path: {path}"
120
+ )
121
+ response = self.agent.run(prompt)
122
+ self.conversation.add(
123
+ role=self.agent.agent_name, content=response
124
+ )
125
+ outcome = ""
126
+ score = 0.0
127
+ error_info = ""
128
+ try:
129
+ # Expecting a response with lines starting with "Outcome:", "Score:", and "Errors:"
130
+ for line in response.splitlines():
131
+ if line.startswith("Outcome:"):
132
+ outcome = line[len("Outcome:") :].strip()
133
+ elif line.startswith("Score:"):
134
+ score = float(line[len("Score:") :].strip())
135
+ elif line.startswith("Errors:"):
136
+ error_info = line[len("Errors:") :].strip()
137
+ except Exception as e:
138
+ logger.error(f"Error parsing simulation response: {e}")
139
+ logger.debug(
140
+ f"Simulated outcome: {outcome}, Score: {score}, Errors: {error_info}"
141
+ )
142
+ return outcome, score, error_info
143
+
144
+ def meta_reflect(self, error_info: str) -> str:
145
+ """
146
+ Perform meta-cognitive reflection on the provided error information.
147
+
148
+ :param error_info: Information regarding errors in the reasoning path.
149
+ :return: Feedback and suggestions for revising the reasoning path.
150
+ """
151
+ logger.info(
152
+ "Performing meta-reflection on error information."
153
+ )
154
+ prompt = (
155
+ f"Analyze the following error information and suggest modifications to improve the reasoning process:\n"
156
+ f"{error_info}\n"
157
+ "Provide clear and actionable feedback."
158
+ )
159
+ feedback = self.agent.run(prompt)
160
+ self.conversation.add(
161
+ role=self.agent.agent_name, content=feedback
162
+ )
163
+ logger.debug(f"Meta-reflection feedback: {feedback}")
164
+ return feedback
165
+
166
+ def revise_path(self, path: str, feedback: str) -> List[str]:
167
+ """
168
+ Revise the reasoning path based on the provided feedback.
169
+
170
+ :param path: The original reasoning path.
171
+ :param feedback: Feedback from meta-cognitive reflection.
172
+ :return: A list of revised reasoning paths.
173
+ """
174
+ logger.info("Revising reasoning path based on feedback.")
175
+ prompt = (
176
+ f"Given the reasoning path:\n'{path}'\n\n"
177
+ f"and the following feedback:\n'{feedback}'\n\n"
178
+ "Generate revised reasoning paths that address the issues raised. "
179
+ "Present each revised path on a new line."
180
+ )
181
+ response = self.agent.run(prompt)
182
+ self.conversation.add(
183
+ role=self.agent.agent_name, content=response
184
+ )
185
+ revised_paths = [
186
+ line.strip()
187
+ for line in response.split("\n")
188
+ if line.strip()
189
+ ]
190
+ logger.debug(f"Revised paths: {revised_paths}")
191
+ return revised_paths
192
+
193
+ def select_promising_paths(self, paths: List[str]) -> List[str]:
194
+ """
195
+ Select the most promising reasoning paths from a list of candidates.
196
+
197
+ :param paths: A list of candidate reasoning paths.
198
+ :return: A pruned list containing the most promising paths.
199
+ """
200
+ logger.info("Selecting promising reasoning paths.")
201
+ prompt = (
202
+ "Evaluate the following reasoning paths and select the ones that appear most promising for further exploration. "
203
+ "List each selected path on a new line:\n"
204
+ + "\n".join(paths)
205
+ )
206
+ response = self.agent.run(prompt)
207
+ self.conversation.add(
208
+ role=self.agent.agent_name, content=response
209
+ )
210
+ selected_paths = [
211
+ line.strip()
212
+ for line in response.split("\n")
213
+ if line.strip()
214
+ ]
215
+ logger.debug(f"Selected paths: {selected_paths}")
216
+ return selected_paths
217
+
218
+ def synthesize_solution(
219
+ self, paths: List[str], memory_pool: List[str]
220
+ ) -> str:
221
+ """
222
+ Synthesize a final solution from the promising reasoning paths and historical memory.
223
+
224
+ :param paths: The current promising reasoning paths.
225
+ :param memory_pool: A list of all previously generated reasoning paths.
226
+ :return: A coherent final solution.
227
+ """
228
+ logger.info(
229
+ "Synthesizing final solution from promising paths."
230
+ )
231
+ prompt = (
232
+ "Based on the following promising reasoning paths:\n"
233
+ f"{chr(10).join(paths)}\n\n"
234
+ "and the historical reasoning memory:\n"
235
+ f"{chr(10).join(memory_pool)}\n\n"
236
+ "Synthesize a final, coherent solution to the problem."
237
+ )
238
+ solution = self.agent.run(prompt)
239
+ self.conversation.add(
240
+ role=self.agent.agent_name, content=solution
241
+ )
242
+ logger.debug(f"Synthesized solution: {solution}")
243
+ return solution
244
+
245
+ def run(self, task: str) -> str:
246
+ """
247
+ Execute the Iterative Reflective Expansion process on the provided problem.
248
+
249
+ :param task: The problem statement.
250
+ :return: The final solution generated after iterative reasoning.
251
+ """
252
+ logger.info(
253
+ f"Starting iterative reflective expansion for problem: {task}"
254
+ )
255
+ candidate_paths = self.generate_initial_hypotheses(task)
256
+ memory_pool: List[str] = []
257
+
258
+ for iteration in range(self.max_iterations):
259
+ logger.info(
260
+ f"Iteration {iteration + 1}/{self.max_iterations}"
261
+ )
262
+ expanded_paths: List[str] = []
263
+
264
+ for path in candidate_paths:
265
+ outcome, score, error_info = self.simulate_path(path)
266
+ # Use a threshold score of 0.7 (this can be adjusted)
267
+ if score < 0.7:
268
+ feedback = self.meta_reflect(error_info)
269
+ revised_paths = self.revise_path(path, feedback)
270
+ expanded_paths.extend(revised_paths)
271
+ else:
272
+ expanded_paths.append(path)
273
+
274
+ memory_pool.extend(candidate_paths)
275
+ candidate_paths = self.select_promising_paths(
276
+ expanded_paths
277
+ )
278
+ logger.info(
279
+ f"Candidate paths for next iteration: {candidate_paths}"
280
+ )
281
+
282
+ self.synthesize_solution(candidate_paths, memory_pool)
283
+ logger.info("Final solution generated.")
284
+
285
+ return history_output_formatter(
286
+ self.conversation, self.output_type
287
+ )
288
+
289
+
290
+ # def main() -> None:
291
+ # """
292
+ # Main function to execute the Iterative Reflective Expansion algorithm on a sample problem.
293
+ # """
294
+ # problem_statement = "What is the 40th prime number?"
295
+ # reasoning_engine = IterativeReflectiveExpansion(max_iterations=1)
296
+ # final_solution = reasoning_engine.run(problem_statement)
297
+ # print("Final Solution:")
298
+ # print(final_solution)
299
+
300
+
301
+ # if __name__ == "__main__":
302
+ # main()
@@ -0,0 +1,130 @@
1
+ from typing import List, Literal
2
+
3
+ from swarms.agents.consistency_agent import SelfConsistencyAgent
4
+ from swarms.agents.i_agent import (
5
+ IterativeReflectiveExpansion as IREAgent,
6
+ )
7
+ from swarms.agents.reasoning_duo import ReasoningDuo
8
+ from swarms.structs.output_types import OutputType
9
+
10
+ agent_types = Literal[
11
+ "reasoning-duo",
12
+ "self-consistency",
13
+ "ire",
14
+ "reasoning-agent",
15
+ "consistency-agent",
16
+ "ire-agent",
17
+ ]
18
+
19
+
20
+ class ReasoningAgentRouter:
21
+ """
22
+ A Reasoning Agent that can answer questions and assist with various tasks using different reasoning strategies.
23
+
24
+ Attributes:
25
+ agent_name (str): The name of the agent.
26
+ description (str): A brief description of the agent's capabilities.
27
+ model_name (str): The name of the model used for reasoning.
28
+ system_prompt (str): The prompt that guides the agent's reasoning process.
29
+ max_loops (int): The maximum number of loops for the reasoning process.
30
+ swarm_type (agent_types): The type of reasoning swarm to use (e.g., reasoning duo, self-consistency, IRE).
31
+ num_samples (int): The number of samples to generate for self-consistency agents.
32
+ output_type (OutputType): The format of the output (e.g., dict, list).
33
+ """
34
+
35
+ def __init__(
36
+ self,
37
+ agent_name: str = "reasoning_agent",
38
+ description: str = "A reasoning agent that can answer questions and help with tasks.",
39
+ model_name: str = "gpt-4o-mini",
40
+ system_prompt: str = "You are a helpful assistant that can answer questions and help with tasks.",
41
+ max_loops: int = 1,
42
+ swarm_type: agent_types = "reasoning_duo",
43
+ num_samples: int = 1,
44
+ output_type: OutputType = "dict",
45
+ ):
46
+ self.agent_name = agent_name
47
+ self.description = description
48
+ self.model_name = model_name
49
+ self.system_prompt = system_prompt
50
+ self.max_loops = max_loops
51
+ self.swarm_type = swarm_type
52
+ self.num_samples = num_samples
53
+ self.output_type = output_type
54
+
55
+ def select_swarm(self):
56
+ """
57
+ Selects and initializes the appropriate reasoning swarm based on the specified swarm type.
58
+
59
+ Returns:
60
+ An instance of the selected reasoning swarm.
61
+ """
62
+ if (
63
+ self.swarm_type == "reasoning-duo"
64
+ or self.swarm_type == "reasoning-agent"
65
+ ):
66
+ return ReasoningDuo(
67
+ agent_name=self.agent_name,
68
+ agent_description=self.description,
69
+ model_name=[self.model_name, self.model_name],
70
+ system_prompt=self.system_prompt,
71
+ output_type=self.output_type,
72
+ )
73
+
74
+ elif (
75
+ self.swarm_type == "self-consistency"
76
+ or self.swarm_type == "consistency-agent"
77
+ ):
78
+ return SelfConsistencyAgent(
79
+ agent_name=self.agent_name,
80
+ description=self.description,
81
+ model_name=self.model_name,
82
+ system_prompt=self.system_prompt,
83
+ max_loops=self.max_loops,
84
+ num_samples=self.num_samples,
85
+ output_type=self.output_type,
86
+ )
87
+
88
+ elif (
89
+ self.swarm_type == "ire" or self.swarm_type == "ire-agent"
90
+ ):
91
+ return IREAgent(
92
+ agent_name=self.agent_name,
93
+ description=self.description,
94
+ model_name=self.model_name,
95
+ system_prompt=self.system_prompt,
96
+ max_loops=self.max_loops,
97
+ max_iterations=self.num_samples,
98
+ output_type=self.output_type,
99
+ )
100
+
101
+ else:
102
+ raise ValueError(f"Invalid swarm type: {self.swarm_type}")
103
+
104
+ def run(self, task: str, *args, **kwargs):
105
+ """
106
+ Executes the selected swarm's reasoning process on the given task.
107
+
108
+ Args:
109
+ task (str): The task or question to be processed by the reasoning agent.
110
+
111
+ Returns:
112
+ The result of the reasoning process.
113
+ """
114
+ swarm = self.select_swarm()
115
+ return swarm.run(task=task)
116
+
117
+ def batched_run(self, tasks: List[str], *args, **kwargs):
118
+ """
119
+ Executes the reasoning process on a batch of tasks.
120
+
121
+ Args:
122
+ tasks (List[str]): A list of tasks to be processed.
123
+
124
+ Returns:
125
+ List of results from the reasoning process for each task.
126
+ """
127
+ results = []
128
+ for task in tasks:
129
+ results.append(self.run(task, *args, **kwargs))
130
+ return results