Skip to content

Commit

Permalink
fix: Fix planning_llm issue (#1189)
Browse files Browse the repository at this point in the history
* fix: Fix planning_llm issue

* fix: add poetry.lock updated version

* fix: type checking issues

* fix: tests
  • Loading branch information
pythonbyte authored Aug 14, 2024
1 parent 7306414 commit 9f9b52d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 40 deletions.
61 changes: 36 additions & 25 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/crewai/crew.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
import json
import os
import uuid
from concurrent.futures import Future
from hashlib import md5
import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

from langchain_core.callbacks import BaseCallbackHandler
Expand Down Expand Up @@ -48,11 +48,10 @@
from crewai.utilities.task_output_storage_handler import TaskOutputStorageHandler
from crewai.utilities.training_handler import CrewTrainingHandler


agentops = None
if os.environ.get("AGENTOPS_API_KEY"):
try:
import agentops
import agentops # type: ignore
except ImportError:
pass

Expand Down Expand Up @@ -541,7 +540,7 @@ def _handle_crew_planning(self):
)._handle_crew_planning()

for task, step_plan in zip(self.tasks, result.list_of_plans_per_task):
task.description += step_plan
task.description += step_plan.plan

def _store_execution_log(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/crewai/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def tool_usage_error(self, llm: Any):
pass

def individual_test_result_span(
self, crew: Crew, quality: int, exec_time: int, model_name: str
self, crew: Crew, quality: float, exec_time: int, model_name: str
):
if self.ready:
try:
Expand Down
10 changes: 5 additions & 5 deletions src/crewai/tools/tool_usage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ast
from difflib import SequenceMatcher
import os
from difflib import SequenceMatcher
from textwrap import dedent
from typing import Any, List, Union

Expand All @@ -15,7 +15,7 @@
agentops = None
if os.environ.get("AGENTOPS_API_KEY"):
try:
import agentops
import agentops # type: ignore
except ImportError:
pass

Expand Down Expand Up @@ -71,14 +71,14 @@ def __init__(
self.task = task
self.action = action
self.function_calling_llm = function_calling_llm

# Handling bug (see https://github.com/langchain-ai/langchain/pull/16395): raise an error if tools_names have space for ChatOpenAI
if isinstance(self.function_calling_llm, ChatOpenAI):
if " " in self.tools_names:
raise Exception(
"Tools names should not have spaces for ChatOpenAI models."
)

# Set the maximum parsing attempts for bigger models
if (isinstance(self.function_calling_llm, ChatOpenAI)) and (
self.function_calling_llm.openai_api_base is None
Expand Down Expand Up @@ -118,7 +118,7 @@ def _use(
tool: BaseTool,
calling: Union[ToolCalling, InstructorToolCalling],
) -> str: # TODO: Fix this return type
tool_event = agentops.ToolEvent(name=calling.tool_name) if agentops else None
tool_event = agentops.ToolEvent(name=calling.tool_name) if agentops else None # type: ignore
if self._check_tool_repeated_usage(calling=calling): # type: ignore # _check_tool_repeated_usage of "ToolUsage" does not return a value (it only ever returns None)
try:
result = self._i18n.errors("task_repeated_usage").format(
Expand Down
15 changes: 13 additions & 2 deletions src/crewai/utilities/planning_handler.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
from typing import Any, List, Optional

from langchain_openai import ChatOpenAI
from pydantic import BaseModel
from pydantic import BaseModel, Field

from crewai.agent import Agent
from crewai.task import Task


class PlanPerTask(BaseModel):
task: str = Field(..., description="The task for which the plan is created")
plan: str = Field(
...,
description="The step by step plan on how the agents can execute their tasks using the available tools with mastery",
)


class PlannerTaskPydanticOutput(BaseModel):
list_of_plans_per_task: List[str]
list_of_plans_per_task: List[PlanPerTask] = Field(
...,
description="Step by step plan on how the agents can execute their tasks using the available tools with mastery",
)


class CrewPlanner:
Expand Down
17 changes: 14 additions & 3 deletions tests/utilities/test_planning_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from crewai.agent import Agent
from crewai.task import Task
from crewai.tasks.task_output import TaskOutput
from crewai.utilities.planning_handler import CrewPlanner, PlannerTaskPydanticOutput
from crewai.utilities.planning_handler import (
CrewPlanner,
PlannerTaskPydanticOutput,
PlanPerTask,
)


class TestCrewPlanner:
Expand Down Expand Up @@ -44,12 +48,17 @@ def crew_planner_different_llm(self):
return CrewPlanner(tasks, planning_agent_llm)

def test_handle_crew_planning(self, crew_planner):
list_of_plans_per_task = [
PlanPerTask(task="Task1", plan="Plan 1"),
PlanPerTask(task="Task2", plan="Plan 2"),
PlanPerTask(task="Task3", plan="Plan 3"),
]
with patch.object(Task, "execute_sync") as execute:
execute.return_value = TaskOutput(
description="Description",
agent="agent",
pydantic=PlannerTaskPydanticOutput(
list_of_plans_per_task=["Plan 1", "Plan 2", "Plan 3"]
list_of_plans_per_task=list_of_plans_per_task
),
)
result = crew_planner._handle_crew_planning()
Expand Down Expand Up @@ -91,7 +100,9 @@ def test_handle_crew_planning_different_llm(self, crew_planner_different_llm):
execute.return_value = TaskOutput(
description="Description",
agent="agent",
pydantic=PlannerTaskPydanticOutput(list_of_plans_per_task=["Plan 1"]),
pydantic=PlannerTaskPydanticOutput(
list_of_plans_per_task=[PlanPerTask(task="Task1", plan="Plan 1")]
),
)
result = crew_planner_different_llm._handle_crew_planning()

Expand Down

0 comments on commit 9f9b52d

Please sign in to comment.