From 931c967fc55debf3960e6b72d4bc2661b1eb0048 Mon Sep 17 00:00:00 2001 From: Kye Date: Thu, 28 Dec 2023 21:24:17 -0500 Subject: [PATCH] [FEAT][README.md][Task] --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++ swarms/structs/task.py | 6 ++++ task.py | 47 ++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 task.py diff --git a/README.md b/README.md index e8dd3220..732e4320 100644 --- a/README.md +++ b/README.md @@ -612,6 +612,71 @@ out = swarmnet.run_many_agents( ) print(out) +``` + + +### `Task` +Task Execution: The Task structure allows for the execution of tasks by an assigned agent. The run method is used to execute the task. It's like a Zapier for LLMs + +- Task Description: Each Task can have a description, providing a human-readable explanation of what the task is intended to do. +- Task Scheduling: Tasks can be scheduled for execution at a specific time using the schedule_time attribute. +- Task Triggers: The set_trigger method allows for the setting of a trigger function that is executed before the task. +- Task Actions: The set_action method allows for the setting of an action function that is executed after the task. +- Task Conditions: The set_condition method allows for the setting of a condition function. The task will only be executed if this function returns True. +- Task Dependencies: The add_dependency method allows for the addition of dependencies to the task. The task will only be executed if all its dependencies have been completed. +- Task Priority: The set_priority method allows for the setting of the task's priority. Tasks with higher priority will be executed before tasks with lower priority. +- Task History: The history attribute is a list that keeps track of all the results of the task execution. This can be useful for debugging and for tasks that need to be executed multiple times. + +```python +from swarms.structs import Task, Agent +from swarms.models import OpenAIChat +from dotenv import load_dotenv +import os + + +# Load the environment variables +load_dotenv() + + +# Define a function to be used as the action +def my_action(): + print("Action executed") + + +# Define a function to be used as the condition +def my_condition(): + print("Condition checked") + return True + + +# Create an agent +agent = Agent( + llm=OpenAIChat(openai_api_key=os.environ["OPENAI_API_KEY"]), + max_loops=1, + dashboard=False, +) + +# Create a task +task = Task(description="What's the weather in miami", agent=agent) + +# Set the action and condition +task.set_action(my_action) +task.set_condition(my_condition) + +# Execute the task +print("Executing task...") +task.run() + +# Check if the task is completed +if task.is_completed(): + print("Task completed") +else: + print("Task not completed") + +# Output the result of the task +print(f"Task result: {task.result}") + + ``` --- diff --git a/swarms/structs/task.py b/swarms/structs/task.py index b96390b8..9c0f8dac 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -103,6 +103,12 @@ class Task: except Exception as error: print(f"[ERROR][Task] {error}") + def run(self): + self.execute() + + def __call__(self): + self.execute() + def handle_scheduled_task(self): """ Handles the execution of a scheduled task. diff --git a/task.py b/task.py new file mode 100644 index 00000000..089cb263 --- /dev/null +++ b/task.py @@ -0,0 +1,47 @@ +from swarms.structs import Task, Agent +from swarms.models import OpenAIChat +from dotenv import load_dotenv +import os + + +# Load the environment variables +load_dotenv() + + +# Define a function to be used as the action +def my_action(): + print("Action executed") + + +# Define a function to be used as the condition +def my_condition(): + print("Condition checked") + return True + + +# Create an agent +agent = Agent( + llm=OpenAIChat(openai_api_key=os.environ["OPENAI_API_KEY"]), + max_loops=1, + dashboard=False, +) + +# Create a task +task = Task(description="What's the weather in miami", agent=agent) + +# Set the action and condition +task.set_action(my_action) +task.set_condition(my_condition) + +# Execute the task +print("Executing task...") +task.run() + +# Check if the task is completed +if task.is_completed(): + print("Task completed") +else: + print("Task not completed") + +# Output the result of the task +print(f"Task result: {task.result}")