From ca9deaebb726011b6868d07bc1ba39b74763eb3c Mon Sep 17 00:00:00 2001 From: Taleb <76521427+rumble773@users.noreply.github.com> Date: Sun, 28 Jul 2024 21:39:54 +0300 Subject: [PATCH] Performed spell check across the rest of code base, and enahnced the yaml paraser code a little (#895) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Performed spell check across the entire documentation Thank you once again! * Performed spell check across the most of code base Folders been checked: - agents - cli - memory - project - tasks - telemetry - tools - translations * Trying to add a max_token for the agents, so they limited by number of tokens. * Performed spell check across the rest of code base, and enahnced the yaml paraser code a little * Small change in the main agent doc * Improve _save_file method to handle both dict and str inputs - Add check for dict type input - Use json.dump for dict serialization - Convert non-dict inputs to string - Remove type ignore comments --------- Co-authored-by: João Moura --- docs/core-concepts/Agents.md | 4 +- src/crewai/agent.py | 20 +------ src/crewai/agents/agent_builder/base_agent.py | 4 ++ src/crewai/task.py | 2 +- .../utilities/evaluators/task_evaluator.py | 4 +- src/crewai/utilities/parser.py | 21 +++++-- tests/agent_test.py | 6 +- tests/crew_test.py | 55 ++++++++++++++++--- tests/task_test.py | 2 +- 9 files changed, 78 insertions(+), 40 deletions(-) diff --git a/docs/core-concepts/Agents.md b/docs/core-concepts/Agents.md index bb054f8b9..7b93fdde7 100644 --- a/docs/core-concepts/Agents.md +++ b/docs/core-concepts/Agents.md @@ -114,7 +114,7 @@ from langchain.agents import load_tools langchain_tools = load_tools(["google-serper"], llm=llm) agent1 = CustomAgent( - role="backstory agent", + role="agent role", goal="who is {input}?", backstory="agent backstory", verbose=True, @@ -127,7 +127,7 @@ task1 = Task( ) agent2 = Agent( - role="bio agent", + role="agent role", goal="summarize the short bio for {input} and if needed do more research", backstory="agent backstory", verbose=True, diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 693506017..e2c6b88f1 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -55,11 +55,6 @@ class Agent(BaseAgent): tools: Tools at agents disposal step_callback: Callback to be executed after each step of the agent execution. callbacks: A list of callback functions from the langchain library that are triggered during the agent's execution process - allow_code_execution: Enable code execution for the agent. - max_retry_limit: Maximum number of retries for an agent to execute a task when an error occurs. - """ - - _times_executed: int = PrivateAttr(default=0) max_execution_time: Optional[int] = Field( default=None, description="Maximum execution time for an agent to execute a task", @@ -191,20 +186,6 @@ class Agent(BaseAgent): else: task_prompt = self._use_trained_data(task_prompt=task_prompt) - try: - result = self.agent_executor.invoke( - { - "input": task_prompt, - "tool_names": self.agent_executor.tools_names, - "tools": self.agent_executor.tools_description, - } - )["output"] - except Exception as e: - self._times_executed += 1 - if self._times_executed > self.max_retry_limit: - raise e - result = self.execute_task(task, context, tools) - if self.max_rpm: self._rpm_controller.stop_rpm_counter() @@ -262,6 +243,7 @@ class Agent(BaseAgent): "tools_handler": self.tools_handler, "function_calling_llm": self.function_calling_llm, "callbacks": self.callbacks, + "max_tokens": self.max_tokens, } if self._rpm_controller: diff --git a/src/crewai/agents/agent_builder/base_agent.py b/src/crewai/agents/agent_builder/base_agent.py index 90f886fd5..038753647 100644 --- a/src/crewai/agents/agent_builder/base_agent.py +++ b/src/crewai/agents/agent_builder/base_agent.py @@ -45,6 +45,7 @@ class BaseAgent(ABC, BaseModel): i18n (I18N): Internationalization settings. cache_handler (InstanceOf[CacheHandler]): An instance of the CacheHandler class. tools_handler (InstanceOf[ToolsHandler]): An instance of the ToolsHandler class. + max_tokens: Maximum number of tokens for the agent to generate in a response. Methods: @@ -118,6 +119,9 @@ class BaseAgent(ABC, BaseModel): tools_handler: InstanceOf[ToolsHandler] = Field( default=None, description="An instance of the ToolsHandler class." ) + max_tokens: Optional[int] = Field( + default=None, description="Maximum number of tokens for the agent's execution." + ) _original_role: str | None = None _original_goal: str | None = None diff --git a/src/crewai/task.py b/src/crewai/task.py index 807e373fb..598a0ab2f 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -122,7 +122,7 @@ class Task(BaseModel): @field_validator("output_file") @classmethod - def output_file_validattion(cls, value: str) -> str: + def output_file_validation(cls, value: str) -> str: """Validate the output file path by removing the / from the beginning of the path.""" if value.startswith("/"): return value[1:] diff --git a/src/crewai/utilities/evaluators/task_evaluator.py b/src/crewai/utilities/evaluators/task_evaluator.py index fdb42e125..46cbef2d8 100644 --- a/src/crewai/utilities/evaluators/task_evaluator.py +++ b/src/crewai/utilities/evaluators/task_evaluator.py @@ -54,12 +54,12 @@ class TaskEvaluator: def __init__(self, original_agent): self.llm = original_agent.llm - def evaluate(self, task, ouput) -> TaskEvaluation: + def evaluate(self, task, output) -> TaskEvaluation: evaluation_query = ( f"Assess the quality of the task completed based on the description, expected output, and actual results.\n\n" f"Task Description:\n{task.description}\n\n" f"Expected Output:\n{task.expected_output}\n\n" - f"Actual Output:\n{ouput}\n\n" + f"Actual Output:\n{output}\n\n" "Please provide:\n" "- Bullet points suggestions to improve future similar tasks\n" "- A score from 0 to 10 evaluating on completion, quality, and overall performance" diff --git a/src/crewai/utilities/parser.py b/src/crewai/utilities/parser.py index 1e100c77f..8d286170e 100644 --- a/src/crewai/utilities/parser.py +++ b/src/crewai/utilities/parser.py @@ -1,17 +1,28 @@ import re - class YamlParser: + @staticmethod def parse(file): + """ + Parses a YAML file, modifies specific patterns, and checks for unsupported 'context' usage. + Args: + file (file object): The YAML file to parse. + Returns: + str: The modified content of the YAML file. + Raises: + ValueError: If 'context:' is used incorrectly. + """ content = file.read() + # Replace single { and } with doubled ones, while leaving already doubled ones intact and the other special characters {# and {% modified_content = re.sub(r"(?