eaglelandsonce commited on
Commit
10f0064
1 Parent(s): d20aca4

Update crewai/task.py

Browse files
Files changed (1) hide show
  1. crewai/task.py +8 -52
crewai/task.py CHANGED
@@ -1,4 +1,3 @@
1
- import threading
2
  import uuid
3
  from typing import Any, List, Optional
4
 
@@ -13,31 +12,19 @@ from crewai.utilities import I18N
13
  class Task(BaseModel):
14
  """Class that represent a task to be executed."""
15
 
16
- class Config:
17
- arbitrary_types_allowed = True
18
-
19
  __hash__ = object.__hash__ # type: ignore
20
  i18n: I18N = I18N()
21
- thread: threading.Thread = None
22
  description: str = Field(description="Description of the actual task.")
23
  callback: Optional[Any] = Field(
24
  description="Callback to be executed after the task is completed.", default=None
25
  )
26
  agent: Optional[Agent] = Field(
27
- description="Agent responsible for execution the task.", default=None
28
  )
29
  expected_output: Optional[str] = Field(
30
  description="Clear definition of expected output for the task.",
31
  default=None,
32
  )
33
- context: Optional[List["Task"]] = Field(
34
- description="Other tasks that will have their output used as context for this task.",
35
- default=None,
36
- )
37
- async_execution: Optional[bool] = Field(
38
- description="Whether the task should be executed asynchronously or not.",
39
- default=False,
40
- )
41
  output: Optional[TaskOutput] = Field(
42
  description="Task output, it's final result after being executed", default=None
43
  )
@@ -66,57 +53,26 @@ class Task(BaseModel):
66
  self.tools.extend(self.agent.tools)
67
  return self
68
 
69
- def execute(
70
- self,
71
- agent: Agent | None = None,
72
- context: Optional[str] = None,
73
- tools: Optional[List[Any]] = None,
74
- ) -> str:
75
  """Execute the task.
76
-
77
  Returns:
78
  Output of the task.
79
  """
80
-
81
- agent = agent or self.agent
82
- if not agent:
83
  raise Exception(
84
- f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, like hierarchical."
85
  )
86
 
87
- if self.context:
88
- context = []
89
- for task in self.context:
90
- if task.async_execution:
91
- task.thread.join()
92
- context.append(task.output.result)
93
- context = "\n".join(context)
94
 
95
- tools = tools or self.tools
96
-
97
- if self.async_execution:
98
- self.thread = threading.Thread(
99
- target=self._execute, args=(agent, self._prompt(), context, tools)
100
- )
101
- self.thread.start()
102
- else:
103
- result = self._execute(
104
- agent=agent,
105
- task_prompt=self._prompt(),
106
- context=context,
107
- tools=tools,
108
- )
109
- return result
110
-
111
- def _execute(self, agent, task_prompt, context, tools):
112
- result = agent.execute_task(task=task_prompt, context=context, tools=tools)
113
  self.output = TaskOutput(description=self.description, result=result)
114
  self.callback(self.output) if self.callback else None
115
  return result
116
 
117
  def _prompt(self) -> str:
118
  """Prompt the task.
119
-
120
  Returns:
121
  Prompt of the task.
122
  """
@@ -127,4 +83,4 @@ class Task(BaseModel):
127
  expected_output=self.expected_output
128
  )
129
  tasks_slices = [self.description, output]
130
- return "\n".join(tasks_slices)
 
 
1
  import uuid
2
  from typing import Any, List, Optional
3
 
 
12
  class Task(BaseModel):
13
  """Class that represent a task to be executed."""
14
 
 
 
 
15
  __hash__ = object.__hash__ # type: ignore
16
  i18n: I18N = I18N()
 
17
  description: str = Field(description="Description of the actual task.")
18
  callback: Optional[Any] = Field(
19
  description="Callback to be executed after the task is completed.", default=None
20
  )
21
  agent: Optional[Agent] = Field(
22
+ description="Agent responsible for executiong the task.", default=None
23
  )
24
  expected_output: Optional[str] = Field(
25
  description="Clear definition of expected output for the task.",
26
  default=None,
27
  )
 
 
 
 
 
 
 
 
28
  output: Optional[TaskOutput] = Field(
29
  description="Task output, it's final result after being executed", default=None
30
  )
 
53
  self.tools.extend(self.agent.tools)
54
  return self
55
 
56
+ def execute(self, context: Optional[str] = None) -> str:
 
 
 
 
 
57
  """Execute the task.
 
58
  Returns:
59
  Output of the task.
60
  """
61
+ if not self.agent:
 
 
62
  raise Exception(
63
+ f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, either consensual or hierarchical."
64
  )
65
 
66
+ result = self.agent.execute_task(
67
+ task=self._prompt(), context=context, tools=self.tools
68
+ )
 
 
 
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  self.output = TaskOutput(description=self.description, result=result)
71
  self.callback(self.output) if self.callback else None
72
  return result
73
 
74
  def _prompt(self) -> str:
75
  """Prompt the task.
 
76
  Returns:
77
  Prompt of the task.
78
  """
 
83
  expected_output=self.expected_output
84
  )
85
  tasks_slices = [self.description, output]
86
+ return "\n".join(tasks_slices)