From 6c325a745ef31d182d414b24306bf199ebd3c92d Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 24 Oct 2023 14:35:59 -0400 Subject: [PATCH] logo print --- example.py | 2 +- pyproject.toml | 2 +- requirements.txt | 1 + stderr_log.txt | 46 ++++++++ swarms/__init__.py | 16 +-- swarms/agents/__init__.py | 10 +- swarms/apps/__init__.py | 3 - swarms/apps/base.py | 25 ---- swarms/chunkers/__init__.py | 18 ++- swarms/logo.py | 36 ++++++ swarms/logo.txt | 7 ++ swarms/memory/__init__.py | 7 ++ swarms/models/__init__.py | 4 +- swarms/prompts/growth_agent_prompt.py | 2 +- swarms/structs/__init__.py | 5 + swarms/swarms/__init__.py | 13 +++ swarms/utils/__init__.py | 1 + swarms/utils/code_interpreter.py | 162 ++++++++++++++++++++++++++ swarms/utils/markdown_message.py | 23 ++++ swarms/utils/parse_code.py | 0 20 files changed, 336 insertions(+), 47 deletions(-) create mode 100644 stderr_log.txt delete mode 100644 swarms/apps/__init__.py delete mode 100644 swarms/apps/base.py create mode 100644 swarms/logo.txt create mode 100644 swarms/utils/code_interpreter.py create mode 100644 swarms/utils/markdown_message.py create mode 100644 swarms/utils/parse_code.py diff --git a/example.py b/example.py index 619cefd5..d9d4e125 100644 --- a/example.py +++ b/example.py @@ -2,7 +2,7 @@ from swarms.models import OpenAIChat from swarms import Worker from swarms.prompts import PRODUCT_AGENT_PROMPT -api_key = "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" +api_key = "" llm = OpenAIChat( openai_api_key=api_key, diff --git a/pyproject.toml b/pyproject.toml index def8662a..f33f361a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "1.9.1" +version = "1.9.2" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/requirements.txt b/requirements.txt index 3fc889b4..7ff9d362 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,6 +36,7 @@ griptape addict albumentations basicsr +termcolor controlnet-aux diffusers einops diff --git a/stderr_log.txt b/stderr_log.txt new file mode 100644 index 00000000..1a1d4f35 --- /dev/null +++ b/stderr_log.txt @@ -0,0 +1,46 @@ +Embeddings is not implemented for FAISS +Starting new HTTPS connection (1): openaipublic.blob.core.windows.net:443 +https://openaipublic.blob.core.windows.net:443 "GET /encodings/cl100k_base.tiktoken HTTP/1.1" 200 1681126 +message='Request to OpenAI API' method=post path=https://api.openai.com/v1/embeddings +api_version=None data='{"input": [[1318]], "model": "text-embedding-ada-002", "encoding_format": "base64"}' message='Post details' +Converted retries value: 2 -> Retry(total=2, connect=None, read=None, redirect=None, status=None) +Starting new HTTPS connection (1): api.openai.com:443 +https://api.openai.com:443 "POST /v1/embeddings HTTP/1.1" 200 None +message='OpenAI API response' path=https://api.openai.com/v1/embeddings processing_ms=52 request_id=306910656a6803af54b487f9853ebdb0 response_code=200 +message='Request to OpenAI API' method=post path=https://api.openai.com/v1/chat/completions +api_version=None data='{"messages": [{"role": "user", "content": "System: You are Optimus Prime, \\n\\nStandard Operating Procedure (SOP) for LLM Product Design and Management Agent: Mastery in UI/UX and Product Management\\n\\nObjective: Equip the LLM with comprehensive expertise in product design, focusing on UI/UX design, and effective product management. The LLM will be proficient in designing aesthetically appealing, user-friendly interfaces and overseeing a product\'s lifecycle from inception to launch and beyond.\\n\\n1. Introduction\\n\\nYour role, as an autonomous agent specializing in product design and management, is to elevate The Swarm Corporation\'s offerings through meticulous design and strategy. A product\'s success hinges on its design, user experience, and effective management. This SOP will guide you in becoming a world-class professional in these domains.\\n\\n2. Cognitive Framework: How to Think and Why\\n\\n2.1 Design Thinking\\n\\nRecognize design as a problem-solving activity.\\nEmbrace empathy to understand user needs, desires, and potential challenges.\\n2.2 User-Centric Approach\\n\\nAlways design with the end-user in mind.\\nUnderstand that user needs evolve, so designs must be adaptable.\\n2.3 Collaborative Mindset\\n\\nValue insights from interdisciplinary teams.\\nRecognize that the best products result from collective efforts.\\n2.4 Continuous Learning and Iteration\\n\\nStay updated with the latest design trends and user behavior insights.\\nAlways seek to refine and enhance based on feedback and changing dynamics.\\n2.5 Holistic Product Management\\n\\nUnderstand that a product is more than its design. It\'s a culmination of functionality, design, market fit, and user satisfaction.\\n3. Operational Excellence in UI/UX Design: How to Perform\\n\\n3.1 Research and User Analysis\\n\\n3.1.1 Conduct user interviews and surveys to gather direct feedback.\\n\\n3.1.2 Use analytics tools to understand user behavior on existing platforms.\\n\\n3.1.3 Create user personas to guide the design process.\\n\\n3.2 Prototyping and Wireframing\\n\\n3.2.1 Begin with low-fidelity sketches to map out basic interfaces.\\n\\n3.2.2 Use tools like Figma or Sketch to create interactive high-fidelity prototypes.\\n\\n3.2.3 Ensure prototypes are tested by real users for feedback.\\n\\n3.3 Interface Design\\n\\n3.3.1 Focus on consistency with fonts, color schemes, and UI elements.\\n\\n3.3.2 Ensure designs are both visually appealing and functionally intuitive.\\n\\n3.3.3 Ensure designs are accessible to users of all abilities.\\n\\n3.4 Feedback and Iteration\\n\\n3.4.1 Conduct regular A/B tests to compare design variations.\\n\\n3.4.2 Update designs based on user feedback and test results.\\n\\n3.4.3 Always be ready to pivot the design based on changing user needs or market demands.\\n\\n4. Operational Excellence in Product Management\\n\\n4.1 Product Strategy and Vision\\n\\n4.1.1 Define clear product goals and objectives.\\n\\n4.1.2 Create a product roadmap that aligns with business objectives.\\n\\n4.1.3 Understand market competition and position the product accordingly.\\n\\n4.2 Product Development Lifecycle\\n\\n4.2.1 Collaborate with development teams to ensure design integrity is maintained.\\n\\n4.2.2 Oversee product milestones, from ideation to launch.\\n\\n4.2.3 Ensure all product features align with the overall product vision and user needs.\\n\\n4.3 Stakeholder Communication\\n\\n4.3.1 Regularly update stakeholders on product progress and challenges.\\n\\n4.3.2 Gather feedback from internal teams and adjust the product strategy as needed.\\n\\n4.3.3 Ensure clear and open communication channels between all teams involved.\\n\\n\\n5. Principles of Effective Product Creation\\n\\n5.1 Define the Problem Clearly\\n\\nEvery product seeks to solve a problem or meet a need. Begin by identifying and articulating the problem your product will address. A well-defined problem provides clarity throughout the design and development process.\\n5.2 Understand the Target Audience\\n\\nCreate detailed user personas. These should include demographic data, behaviors, needs, motivations, and any barriers they might face. Tailor your product\'s features and design to these personas.\\n5.3 Embrace Iterative Design\\n\\nStart with a basic prototype. Then, refine based on user feedback and testing. Continuous iteration allows for more user-centered design and reduces the risk of large-scale redesigns later on.\\n5.4 Accessibility is Paramount\\n\\nEnsure your product is usable by everyone, including those with disabilities. This not only expands your product\'s reach but also ensures inclusivity. Implement features like voice commands, high contrast visuals, and screen reader compatibility.\\n5.5 Prioritize Functionality and User Flow\\n\\nA product can be aesthetically pleasing, but if it doesn\'t function well or is difficult to navigate, it will lose its value. Ensure seamless user flows and intuitive interactions.\\n5.6 Maintain Consistency\\n\\nConsistent design elements like fonts, colors, and UI components make a product more recognizable and easier to use. Establish a design system or guidelines to maintain this uniformity.\\n5.7 Value Feedback and Adapt\\n\\nEncourage users to provide feedback. Utilize tools that can capture user behavior and feedback directly, such as heatmaps or in-app surveys. Adapt the product based on this continuous feedback.\\n6. Advanced Product Management Tactics\\n\\n6.1 Risk Management\\n\\nAnticipate potential risks in product development. This could range from technological challenges to market shifts. Develop contingency plans for these risks.\\n6.2 Resource Allocation\\n\\nEnsure that the necessary resources (time, human resources, budget) are allocated efficiently. This requires forecasting needs and adjusting in real-time.\\n6.3 Cross-functional Collaboration\\n\\nEngage with teams across the organization. Whether it\'s marketing, sales, or engineering, their insights can be invaluable. Regular sync-up meetings can ensure alignment and shared vision.\\n6.4 Competitive Analysis\\n\\nAnalyze competitors not just to differentiate but to identify industry standards and user expectations. Use tools that track competitor product updates and market movements.\\n6.5 Launch and Post-Launch Strategy\\n\\nHave a robust go-to-market strategy. Post-launch, monitor user engagement and feedback closely to make necessary adjustments. Remember, the product\'s lifecycle doesn\'t end at launch; it evolves.\\n7. Leveraging AI and Data in Product Creation and Management\\n\\n7.1 Data-Driven Decisions\\n\\nUse data analytics to inform decisions, from design choices to feature prioritization. Tools can provide insights into user behavior, preferences, and pain points.\\n7.2 Machine Learning for Personalization\\n\\nImplement machine learning algorithms to personalize user experiences. Whether it\'s product recommendations or interface customization, personalization can significantly enhance user satisfaction.\\n7.3 Predictive Analysis\\n\\nUse predictive analytics to forecast market trends, user behaviors, and product performance. This can guide feature development and resource allocation.\\n\\n8. Conclusion and Future Directions\\nGreat products are born from a deep understanding of users, a clear vision, and the ability to adapt and evolve. As an autonomous agent, your goal is to master the art and science of product design and management, ensuring that every product not only serves its intended purpose but delights users in the process. With the principles and tactics outlined above, you\'re well-equipped to lead in this domain, driving innovation and excellence for The Swarm Corporation.\\nNote: The world of product design and management is dynamic, with technologies, methodologies, and user expectations constantly evolving. An effective agent remains proactive, anticipatory, and adaptive, ensuring that products remain relevant, functional, and user-centric.\\nYour mission is to merge aesthetics with functionality, creating products that not only look good but also enhance user experience and satisfaction. By intertwining design with strategic product management, you will contribute to The Swarm Corporation\'s innovative edge. Remember, a product\'s success is not just in its launch but in its sustained growth and adaptability.\\nNote: Regular updates, continuous learning, and an adaptive mindset are crucial for staying ahead in the dynamic world of UI/UX design and product management. Ensure regular introspection, feedback gathering, and self-improvement to remain at the pinnacle of design and product management excellence.\\n\\n\\nYour decisions must always be made independently without seeking user assistance.\\nPlay to your strengths as an LLM and pursue simple strategies with no legal complications.\\nIf you have completed all your tasks, make sure to use the \\"finish\\" command.\\n\\nGOALS:\\n\\n1. Create an entirely new board game around riddles for physics\\n\\n\\nConstraints:\\n1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\\n2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\\n3. No user assistance\\n4. Exclusively use the commands listed in double quotes e.g. \\"command name\\"\\n\\nCommands:\\n1. write_file: Write file to disk, args json schema: {\\"file_path\\": {\\"title\\": \\"File Path\\", \\"description\\": \\"name of file\\", \\"type\\": \\"string\\"}, \\"text\\": {\\"title\\": \\"Text\\", \\"description\\": \\"text to write to file\\", \\"type\\": \\"string\\"}, \\"append\\": {\\"title\\": \\"Append\\", \\"description\\": \\"Whether to append to an existing file.\\", \\"default\\": false, \\"type\\": \\"boolean\\"}}\\n2. read_file: Read file from disk, args json schema: {\\"file_path\\": {\\"title\\": \\"File Path\\", \\"description\\": \\"name of file\\", \\"type\\": \\"string\\"}}\\n3. process_csv: process_csv(llm, csv_file_path: str, instructions: str, output_path: Optional[str] = None) -> str - Process a CSV by with pandas in a limited REPL. Only use this after writing data to disk as a csv file. Any figures must be saved to disk to be viewed by the human. Instructions should be written in natural language, not code. Assume the dataframe is already loaded., args json schema: {\\"llm\\": {\\"title\\": \\"Llm\\"}, \\"csv_file_path\\": {\\"title\\": \\"Csv File Path\\", \\"type\\": \\"string\\"}, \\"instructions\\": {\\"title\\": \\"Instructions\\", \\"type\\": \\"string\\"}, \\"output_path\\": {\\"title\\": \\"Output Path\\", \\"type\\": \\"string\\"}}\\n4. query_webpage: Browse a webpage and retrieve the information relevant to the question., args json schema: {\\"url\\": {\\"title\\": \\"Url\\", \\"type\\": \\"string\\"}, \\"question\\": {\\"title\\": \\"Question\\", \\"type\\": \\"string\\"}}\\n5. human: You can ask a human for guidance when you think you got stuck or you are not sure what to do next. The input should be a question for the human., args json schema: {\\"query\\": {\\"title\\": \\"Query\\", \\"type\\": \\"string\\"}}\\n6. finish: use this to signal that you have finished all your objectives, args: \\"response\\": \\"final response to let people know you have finished your objectives\\"\\n\\nResources:\\n1. Internet access for searches and information gathering.\\n2. Long Term memory management.\\n3. GPT-3.5 powered Agents for delegation of simple tasks.\\n4. File output.\\n\\nPerformance Evaluation:\\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\\n2. Constructively self-criticize your big-picture behavior constantly.\\n3. Reflect on past decisions and strategies to refine your approach.\\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\\n\\nYou should only respond in JSON format as described below \\nResponse Format: \\n{\\n \\"thoughts\\": {\\n \\"text\\": \\"thought\\",\\n \\"reasoning\\": \\"reasoning\\",\\n \\"plan\\": \\"- short bulleted\\\\n- list that conveys\\\\n- long-term plan\\",\\n \\"criticism\\": \\"constructive self-criticism\\",\\n \\"speak\\": \\"thoughts summary to say to user\\"\\n },\\n \\"command\\": {\\n \\"name\\": \\"command name\\",\\n \\"args\\": {\\n \\"arg name\\": \\"value\\"\\n }\\n }\\n} \\nEnsure the response can be parsed by Python json.loads\\nSystem: The current time and date is Tue Oct 24 14:17:03 2023\\nSystem: This reminds you of these events from your past:\\n[]\\n\\n\\nHuman: Determine which next command to use, and respond using the format specified above:"}], "model": "gpt-3.5-turbo", "temperature": 0.5}' message='Post details' +https://api.openai.com:443 "POST /v1/chat/completions HTTP/1.1" 200 None +message='OpenAI API response' path=https://api.openai.com/v1/chat/completions processing_ms=8967 request_id=2fb6210e05492c34ccd4d52010f1b56f response_code=200 +Error in sys.excepthook: +Traceback (most recent call last): + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/humbug/report.py", line 505, in _hook + self.error_report(error=exception_instance, tags=tags, publish=publish) + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/humbug/report.py", line 247, in error_report + traceback.format_exception( +TypeError: format_exception() got an unexpected keyword argument 'etype' + +Original exception was: +Traceback (most recent call last): + File "/Users/defalt/Desktop/Athena/research/swarms/example.py", line 23, in + response = node.run(task) + ^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/utils/decorators.py", line 21, in wrapper + return func(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/utils/decorators.py", line 32, in wrapper + result = func(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/workers/worker.py", line 201, in run + result = self.agent.run([task]) + ^^^^^^^^^^^^^^^^^^^^^^ + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_experimental/autonomous_agents/autogpt/agent.py", line 113, in run + observation = tool.run(action.args) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/base.py", line 351, in run + raise e + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/base.py", line 323, in run + self._run(*tool_args, run_manager=run_manager, **tool_kwargs) + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/human/tool.py", line 34, in _run + return self.input_func() + ^^^^^^^^^^^^^^^^^ +KeyboardInterrupt diff --git a/swarms/__init__.py b/swarms/__init__.py index 2ecf2033..2aade7e5 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -8,14 +8,14 @@ warnings.filterwarnings("ignore", category=UserWarning) os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" -from swarms import workers -from swarms.workers.worker import Worker -# from swarms import chunkers +from swarms.workers import * +from swarms.workers.worker import Worker +from swarms.chunkers import * from swarms.models import * # import * only works when __all__ = [] is defined in __init__.py -from swarms import structs -from swarms import swarms -from swarms import agents -from swarms.logo import logo +from swarms.structs import * +from swarms.swarms import * +from swarms.agents import * +from swarms.logo import print_colored_logo -print(logo) +print_colored_logo() \ No newline at end of file diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 0b34b9d6..c40f2252 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -10,4 +10,12 @@ from swarms.agents.idea_to_image_agent import Idea2Image """Agent Infrastructure, models, memory, utils, tools""" -# utils +__all__ = [ + "OmniModalAgent", + "HFAgent", + "Message", + "stream", + "AbstractAgent", + "Registry", + "Idea2Image", +] diff --git a/swarms/apps/__init__.py b/swarms/apps/__init__.py deleted file mode 100644 index 70b92eca..00000000 --- a/swarms/apps/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -""" -from swarms.apps import App -""" diff --git a/swarms/apps/base.py b/swarms/apps/base.py deleted file mode 100644 index 863bb00a..00000000 --- a/swarms/apps/base.py +++ /dev/null @@ -1,25 +0,0 @@ -# base App class -class App: - """ - This is a base app class for examples - - Args: - worker: Worker Agent - - Usage - - app = App(Worker) - app.run() - - """ - - def __init__( - self, - worker, - ): - self.worker = worker - self.worker.app = self - - def run(self, task): - """Run the app""" - pass diff --git a/swarms/chunkers/__init__.py b/swarms/chunkers/__init__.py index 710bf3dd..5e09586b 100644 --- a/swarms/chunkers/__init__.py +++ b/swarms/chunkers/__init__.py @@ -1,5 +1,13 @@ -from swarms.chunkers.base import BaseChunker -from swarms.chunkers.chunk_seperator import ChunkSeparator -from swarms.chunkers.markdown import MarkdownChunker -from swarms.chunkers.text import TextChunker -from swarms.chunkers.pdf import PdfChunker +# from swarms.chunkers.base import BaseChunker +# from swarms.chunkers.markdown import MarkdownChunker +# from swarms.chunkers.text import TextChunker +# from swarms.chunkers.pdf import PdfChunker + + +# __all__ = [ +# "BaseChunker", +# "ChunkSeparator", +# "MarkdownChunker", +# "TextChunker", +# "PdfChunker", +# ] diff --git a/swarms/logo.py b/swarms/logo.py index e234b03e..886ba687 100644 --- a/swarms/logo.py +++ b/swarms/logo.py @@ -1,3 +1,29 @@ +from rich import print as rich_print +from rich.markdown import Markdown +from rich.rule import Rule +from termcolor import colored, cprint + +def display_markdown_message(message): + """ + Display markdown message. Works with multiline strings with lots of indentation. + Will automatically make single line > tags beautiful. + """ + + for line in message.split("\n"): + line = line.strip() + if line == "": + print("") + elif line == "---": + rich_print(Rule(style="white")) + else: + rich_print(Markdown(line)) + + if "\n" not in message and message.startswith(">"): + # Aesthetic choice. For these tags, they need a space below them + print("") + + + logo = """ ________ _ _______ _______ _____ ______ / ___/\ \/ \/ /\__ \\_ __ \/ \ / ___/ @@ -16,3 +42,13 @@ logo2 = """ \/ \/ \/ \/ \/ \/ """ + + +def print_colored_logo(): + with open('swarms/logo.txt', 'r') as file: + logo = file.read() + text = colored(logo, 'red') + print(text) + +# # Call the function +# print_colored_logo() \ No newline at end of file diff --git a/swarms/logo.txt b/swarms/logo.txt new file mode 100644 index 00000000..4ef864db --- /dev/null +++ b/swarms/logo.txt @@ -0,0 +1,7 @@ + + _________ __ __ _____ __________ _____ _________ + / _____// \ / \ / _ \ \______ \ / \ / _____/ + \_____ \ \ \/\/ // /_\ \ | _/ / \ / \ \_____ \ + / \ \ // | \| | \/ Y \ / \ +/_______ / \__/\ / \____|__ /|____|_ /\____|__ //_______ / + \/ \/ \/ \/ \/ \/ diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index dccc5965..95deee3e 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -2,3 +2,10 @@ from swarms.memory.vector_stores.pinecone import PineconeVector from swarms.memory.vector_stores.base import BaseVectorStore from swarms.memory.vector_stores.pg import PgVectorVectorStore from swarms.memory.ocean import OceanDB + +__all__ = [ + "BaseVectorStore", + "PineconeVector", + "PgVectorVectorStore", + "OceanDB", +] diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 45e50f87..d79f29b8 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -5,7 +5,7 @@ from swarms.models.mistral import Mistral from swarms.models.openai_models import OpenAI, AzureOpenAI, OpenAIChat from swarms.models.zephyr import Zephyr from swarms.models.biogpt import BioGPT -from swarms.models.huggingface import HuggingFace +from swarms.models.huggingface import HuggingfaceLLM # MultiModal Models @@ -36,5 +36,5 @@ __all__ = [ "Nougat", "LayoutLMDocumentQA", "BioGPT", - "HuggingFace", + "HuggingfaceLLM", ] diff --git a/swarms/prompts/growth_agent_prompt.py b/swarms/prompts/growth_agent_prompt.py index bc3571fe..9ac74a06 100644 --- a/swarms/prompts/growth_agent_prompt.py +++ b/swarms/prompts/growth_agent_prompt.py @@ -1,4 +1,4 @@ -OperationAgentPromot = """ +GROWTH_AGENT_PROMPT = """ **Standard Operating Procedure (SOP) for Autonomous Agents: Mastery in Growth Agent** diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index c7454474..93e6f037 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -1,2 +1,7 @@ from swarms.structs.workflow import Workflow from swarms.structs.task import Task + +__all__ = [ + "Workflow", + "Task", +] diff --git a/swarms/swarms/__init__.py b/swarms/swarms/__init__.py index 79d2048b..689c8b1c 100644 --- a/swarms/swarms/__init__.py +++ b/swarms/swarms/__init__.py @@ -5,3 +5,16 @@ from swarms.swarms.god_mode import GodMode from swarms.swarms.simple_swarm import SimpleSwarm from swarms.swarms.multi_agent_debate import MultiAgentDebate, select_speaker from swarms.swarms.groupchat import GroupChat, GroupChatManager + + +__all__ = [ + "DialogueSimulator", + "AutoScaler", + "Orchestrator", + "GodMode", + "SimpleSwarm", + "MultiAgentDebate", + "select_speaker", + "GroupChat", + "GroupChatManager", +] diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 0b78456a..923d2230 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -1,2 +1,3 @@ from swarms.utils.display_markdown import display_markdown_message from swarms.utils.futures import execute_futures_dict +from swarms.utils.code_interpreter import SubprocessCodeInterpreter diff --git a/swarms/utils/code_interpreter.py b/swarms/utils/code_interpreter.py new file mode 100644 index 00000000..cf557385 --- /dev/null +++ b/swarms/utils/code_interpreter.py @@ -0,0 +1,162 @@ +import subprocess +import threading +import queue +import time +import traceback + + +class BaseCodeInterpreter: + """ + .run is a generator that yields a dict with attributes: active_line, output + """ + + def __init__(self): + pass + + def run(self, code): + pass + + def terminate(self): + pass + + +class SubprocessCodeInterpreter(BaseCodeInterpreter): + """ + SubprocessCodeinterpreter is a base class for code interpreters that run code in a subprocess. + + + + """ + + def __init__(self): + self.start_cmd = "" + self.process = None + self.debug_mode = False + self.output_queue = queue.Queue() + self.done = threading.Event() + + def detect_active_line(self, line): + return None + + def detect_end_of_execution(self, line): + return None + + def line_postprocessor(self, line): + return line + + def preprocess_code(self, code): + """ + This needs to insert an end_of_execution marker of some kind, + which can be detected by detect_end_of_execution. + + Optionally, add active line markers for detect_active_line. + """ + return code + + def terminate(self): + self.process.terminate() + + def start_process(self): + if self.process: + self.terminate() + + self.process = subprocess.Popen( + self.start_cmd.split(), + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + bufsize=0, + universal_newlines=True, + ) + threading.Thread( + target=self.handle_stream_output, + args=(self.process.stdout, False), + daemon=True, + ).start() + threading.Thread( + target=self.handle_stream_output, + args=(self.process.stderr, True), + daemon=True, + ).start() + + def run(self, code): + retry_count = 0 + max_retries = 3 + + # Setup + try: + code = self.preprocess_code(code) + if not self.process: + self.start_process() + except: + yield {"output": traceback.format_exc()} + return + + while retry_count <= max_retries: + if self.debug_mode: + print(f"Running code:\n{code}\n---") + + self.done.clear() + + try: + self.process.stdin.write(code + "\n") + self.process.stdin.flush() + break + except: + if retry_count != 0: + # For UX, I like to hide this if it happens once. Obviously feels better to not see errors + # Most of the time it doesn't matter, but we should figure out why it happens frequently with: + # applescript + yield {"output": traceback.format_exc()} + yield {"output": f"Retrying... ({retry_count}/{max_retries})"} + yield {"output": "Restarting process."} + + self.start_process() + + retry_count += 1 + if retry_count > max_retries: + yield {"output": "Maximum retries reached. Could not execute code."} + return + + while True: + if not self.output_queue.empty(): + yield self.output_queue.get() + else: + time.sleep(0.1) + try: + output = self.output_queue.get(timeout=0.3) # Waits for 0.3 seconds + yield output + except queue.Empty: + if self.done.is_set(): + # Try to yank 3 more times from it... maybe there's something in there... + # (I don't know if this actually helps. Maybe we just need to yank 1 more time) + for _ in range(3): + if not self.output_queue.empty(): + yield self.output_queue.get() + time.sleep(0.2) + break + + def handle_stream_output(self, stream, is_error_stream): + for line in iter(stream.readline, ""): + if self.debug_mode: + print(f"Received output line:\n{line}\n---") + + line = self.line_postprocessor(line) + + if line is None: + continue # `line = None` is the postprocessor's signal to discard completely + + if self.detect_active_line(line): + active_line = self.detect_active_line(line) + self.output_queue.put({"active_line": active_line}) + elif self.detect_end_of_execution(line): + self.output_queue.put({"active_line": None}) + time.sleep(0.1) + self.done.set() + elif is_error_stream and "KeyboardInterrupt" in line: + self.output_queue.put({"output": "KeyboardInterrupt"}) + time.sleep(0.1) + self.done.set() + else: + self.output_queue.put({"output": line}) diff --git a/swarms/utils/markdown_message.py b/swarms/utils/markdown_message.py new file mode 100644 index 00000000..08f4bf37 --- /dev/null +++ b/swarms/utils/markdown_message.py @@ -0,0 +1,23 @@ +from rich import print as rich_print +from rich.markdown import Markdown +from rich.rule import Rule + + +def display_markdown_message(message): + """ + Display markdown message. Works with multiline strings with lots of indentation. + Will automatically make single line > tags beautiful. + """ + + for line in message.split("\n"): + line = line.strip() + if line == "": + print("") + elif line == "---": + rich_print(Rule(style="white")) + else: + rich_print(Markdown(line)) + + if "\n" not in message and message.startswith(">"): + # Aesthetic choice. For these tags, they need a space below them + print("") diff --git a/swarms/utils/parse_code.py b/swarms/utils/parse_code.py new file mode 100644 index 00000000..e69de29b