Skip to content

Commit

Permalink
Merge pull request #33 from jlowin/mock
Browse files Browse the repository at this point in the history
Fix max iterations settings
  • Loading branch information
jlowin authored May 14, 2024
2 parents 6658173 + 17dd4fe commit 2d301a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/controlflow/core/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Controller(BaseModel, ExposeSyncMethodsMixin):
validate_default=True,
)
agents: list[Agent] | None = None
run_dependencies: bool = True
context: dict = {}
graph: Graph = None
model_config: dict = dict(extra="forbid")
Expand Down Expand Up @@ -90,7 +89,7 @@ def end_run():
End your turn if you have no tasks to work on. Only call this tool
if necessary; otherwise you can end your turn normally.
"""
raise EndRun()
return EndRun()

return end_run

Expand Down
27 changes: 17 additions & 10 deletions src/controlflow/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from controlflow.utilities.context import ctx
from controlflow.utilities.logging import get_logger
from controlflow.utilities.prefect import wrap_prefect_tool
from controlflow.utilities.types import AssistantTool, ControlFlowModel
from controlflow.utilities.types import NOTSET, AssistantTool, ControlFlowModel
from controlflow.utilities.user_access import talk_to_human

if TYPE_CHECKING:
Expand All @@ -46,9 +46,6 @@ class TaskStatus(Enum):
SKIPPED = "skipped"


NOTSET = "__notset__"


def visit_task_collection(
val: Any, fn: Callable, recursion_limit: int = 3, _counter: int = 0
) -> list["Task"]:
Expand Down Expand Up @@ -239,24 +236,34 @@ def add_dependency(self, task: "Task"):
if self not in task._downstreams:
task._downstreams.append(self)

def run_once(self, agent: "Agent" = None, run_dependencies: bool = True):
def run_once(self, agent: "Agent" = None):
"""
Runs the task with provided agent. If no agent is provided, one will be selected from the task's agents.
"""
from controlflow.core.controller import Controller

controller = Controller(
tasks=[self], agents=agent, run_dependencies=run_dependencies
)
controller = Controller(tasks=[self], agents=agent)

controller.run_once()

def run(self, run_dependencies: bool = True) -> T:
def run(self, max_iterations: int = NOTSET) -> T:
"""
Runs the task with provided agents until it is complete.
If max_iterations is provided, the task will run at most that many times before raising an error.
"""
if max_iterations == NOTSET:
max_iterations = marvin.settings.max_task_iterations
if max_iterations is None:
max_iterations = float("inf")

counter = 0
while self.is_incomplete():
self.run_once(run_dependencies=run_dependencies)
if counter >= max_iterations:
raise ValueError(
f"{self.friendly_name()} did not complete after {max_iterations} iterations."
)
self.run_once()
if self.is_successful():
return self.result
elif self.is_failed():
Expand Down
2 changes: 1 addition & 1 deletion src/controlflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def apply(self):

class Settings(ControlFlowSettings):
assistant_model: str = "gpt-4o"
max_agent_iterations: int = 10
max_task_iterations: int = None
prefect: PrefectSettings = Field(default_factory=PrefectSettings)
enable_global_flow: bool = Field(
True,
Expand Down
3 changes: 3 additions & 0 deletions src/controlflow/utilities/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from marvin.utilities.asyncio import ExposeSyncMethodsMixin
from pydantic import BaseModel

# flag for unset defaults
NOTSET = "__NOTSET__"


class ControlFlowModel(BaseModel):
model_config = dict(validate_assignment=True, extra="forbid")

0 comments on commit 2d301a0

Please sign in to comment.