From c18208ca594c18e5f67f4ec8979dafa20da93c47 Mon Sep 17 00:00:00 2001 From: Lorenze Jay <63378463+lorenzejay@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:16:26 -0700 Subject: [PATCH 1/4] fixed mixin (#831) * fixed mixin * WIP: fixing types * type fixes on mixin --- src/crewai/agents/agent_builder/base_agent.py | 3 +- .../base_agent_executor_mixin.py | 118 ++++++++++++------ src/crewai/agents/executor.py | 16 ++- 3 files changed, 96 insertions(+), 41 deletions(-) diff --git a/src/crewai/agents/agent_builder/base_agent.py b/src/crewai/agents/agent_builder/base_agent.py index 81375cdb8..65c9b661c 100644 --- a/src/crewai/agents/agent_builder/base_agent.py +++ b/src/crewai/agents/agent_builder/base_agent.py @@ -15,7 +15,8 @@ from pydantic import ( from pydantic_core import PydanticCustomError from crewai.utilities import I18N, RPMController, Logger -from crewai.agents import CacheHandler, ToolsHandler +from crewai.agents.cache.cache_handler import CacheHandler +from crewai.agents.tools_handler import ToolsHandler from crewai.utilities.token_counter_callback import TokenProcess diff --git a/src/crewai/agents/agent_builder/base_agent_executor_mixin.py b/src/crewai/agents/agent_builder/base_agent_executor_mixin.py index 7d31defaf..e3c0aa211 100644 --- a/src/crewai/agents/agent_builder/base_agent_executor_mixin.py +++ b/src/crewai/agents/agent_builder/base_agent_executor_mixin.py @@ -1,65 +1,109 @@ import time +from typing import TYPE_CHECKING, Optional from crewai.memory.entity.entity_memory_item import EntityMemoryItem from crewai.memory.long_term.long_term_memory_item import LongTermMemoryItem from crewai.memory.short_term.short_term_memory_item import ShortTermMemoryItem from crewai.utilities.converter import ConverterError from crewai.utilities.evaluators.task_evaluator import TaskEvaluator +from crewai.utilities import I18N + + +if TYPE_CHECKING: + from crewai.crew import Crew + from crewai.task import Task + from crewai.agents.agent_builder.base_agent import BaseAgent class CrewAgentExecutorMixin: + crew: Optional["Crew"] + crew_agent: Optional["BaseAgent"] + task: Optional["Task"] + iterations: int + force_answer_max_iterations: int + have_forced_answer: bool + _i18n: I18N + def _should_force_answer(self) -> bool: + """Determine if a forced answer is required based on iteration count.""" return ( self.iterations == self.force_answer_max_iterations ) and not self.have_forced_answer def _create_short_term_memory(self, output) -> None: + """Create and save a short-term memory item if conditions are met.""" + if ( + self.crew + and self.crew_agent + and self.task + and "Action: Delegate work to coworker" not in output.log + ): + try: + memory = ShortTermMemoryItem( + data=output.log, + agent=self.crew_agent.role, + metadata={ + "observation": self.task.description, + }, + ) + if ( + hasattr(self.crew, "_short_term_memory") + and self.crew._short_term_memory + ): + self.crew._short_term_memory.save(memory) + except Exception as e: + print(f"Failed to add to short term memory: {e}") + pass + + def _create_long_term_memory(self, output) -> None: + """Create and save long-term and entity memory items based on evaluation.""" if ( self.crew and self.crew.memory - and "Action: Delegate work to coworker" not in output.log + and self.crew._long_term_memory + and self.crew._entity_memory + and self.task + and self.crew_agent ): - memory = ShortTermMemoryItem( - data=output.log, - agent=self.crew_agent.role, - metadata={ - "observation": self.task.description, - }, - ) - self.crew._short_term_memory.save(memory) + try: + ltm_agent = TaskEvaluator(self.crew_agent) + evaluation = ltm_agent.evaluate(self.task, output.log) - def _create_long_term_memory(self, output) -> None: - if self.crew and self.crew.memory: - ltm_agent = TaskEvaluator(self.crew_agent) - evaluation = ltm_agent.evaluate(self.task, output.log) + if isinstance(evaluation, ConverterError): + return - if isinstance(evaluation, ConverterError): - return - - long_term_memory = LongTermMemoryItem( - task=self.task.description, - agent=self.crew_agent.role, - quality=evaluation.quality, - datetime=str(time.time()), - expected_output=self.task.expected_output, - metadata={ - "suggestions": evaluation.suggestions, - "quality": evaluation.quality, - }, - ) - self.crew._long_term_memory.save(long_term_memory) - - for entity in evaluation.entities: - entity_memory = EntityMemoryItem( - name=entity.name, - type=entity.type, - description=entity.description, - relationships="\n".join([f"- {r}" for r in entity.relationships]), + long_term_memory = LongTermMemoryItem( + task=self.task.description, + agent=self.crew_agent.role, + quality=evaluation.quality, + datetime=str(time.time()), + expected_output=self.task.expected_output, + metadata={ + "suggestions": evaluation.suggestions, + "quality": evaluation.quality, + }, ) - self.crew._entity_memory.save(entity_memory) + self.crew._long_term_memory.save(long_term_memory) + + for entity in evaluation.entities: + entity_memory = EntityMemoryItem( + name=entity.name, + type=entity.type, + description=entity.description, + relationships="\n".join( + [f"- {r}" for r in entity.relationships] + ), + ) + self.crew._entity_memory.save(entity_memory) + except AttributeError as e: + print(f"Missing attributes for long term memory: {e}") + pass + except Exception as e: + print(f"Failed to add to long term memory: {e}") + pass def _ask_human_input(self, final_answer: dict) -> str: - """Get human input.""" + """Prompt human input for final decision making.""" return input( self._i18n.slice("getting_input").format(final_answer=final_answer) ) diff --git a/src/crewai/agents/executor.py b/src/crewai/agents/executor.py index 365d04f78..192dc190e 100644 --- a/src/crewai/agents/executor.py +++ b/src/crewai/agents/executor.py @@ -1,6 +1,14 @@ import threading import time -from typing import Any, Dict, Iterator, List, Optional, Tuple, Union +from typing import ( + Any, + Dict, + Iterator, + List, + Optional, + Tuple, + Union, +) from langchain.agents import AgentExecutor from langchain.agents.agent import ExceptionTool @@ -11,13 +19,15 @@ from langchain_core.exceptions import OutputParserException from langchain_core.tools import BaseTool from langchain_core.utils.input import get_color_mapping from pydantic import InstanceOf -from crewai.agents.agent_builder.base_agent_executor_mixin import CrewAgentExecutorMixin +from crewai.agents.agent_builder.base_agent_executor_mixin import ( + CrewAgentExecutorMixin, +) from crewai.agents.tools_handler import ToolsHandler from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException -from crewai.utilities import I18N from crewai.utilities.constants import TRAINING_DATA_FILE from crewai.utilities.training_handler import CrewTrainingHandler +from crewai.utilities import I18N class CrewAgentExecutor(AgentExecutor, CrewAgentExecutorMixin): From bd053a98c797b2f63bbb6d1e0fe6496373b786b3 Mon Sep 17 00:00:00 2001 From: Taleb <76521427+rumble773@users.noreply.github.com> Date: Wed, 3 Jul 2024 03:17:04 +0300 Subject: [PATCH 2/4] Enhanced documentation for readability and clarity (#855) - Added a "Parameters" column to attribute tables. Improved overall document formatting for enhanced readability and ease of use. Thank you to the author for the great project and the excellent foundation provided! --- docs/core-concepts/Agents.md | 36 ++++++++++++++-------------- docs/core-concepts/Crews.md | 46 ++++++++++++++++++------------------ docs/core-concepts/Tasks.md | 28 +++++++++++----------- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/docs/core-concepts/Agents.md b/docs/core-concepts/Agents.md index 862c8e061..bb054f8b9 100644 --- a/docs/core-concepts/Agents.md +++ b/docs/core-concepts/Agents.md @@ -16,24 +16,24 @@ description: What are crewAI Agents and how to use them. ## Agent Attributes -| Attribute | Description | -| :------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **Role** | Defines the agent's function within the crew. It determines the kind of tasks the agent is best suited for. | -| **Goal** | The individual objective that the agent aims to achieve. It guides the agent's decision-making process. | -| **Backstory** | Provides context to the agent's role and goal, enriching the interaction and collaboration dynamics. | -| **LLM** *(optional)* | Represents the language model that will run the agent. It dynamically fetches the model name from the `OPENAI_MODEL_NAME` environment variable, defaulting to "gpt-4" if not specified. | -| **Tools** *(optional)* | Set of capabilities or functions that the agent can use to perform tasks. Expected to be instances of custom classes compatible with the agent's execution environment. Tools are initialized with a default value of an empty list. | -| **Function Calling LLM** *(optional)* | Specifies the language model that will handle the tool calling for this agent, overriding the crew function calling LLM if passed. Default is `None`. | -| **Max Iter** *(optional)* | `max_iter` is the maximum number of iterations the agent can perform before being forced to give its best answer. Default is `25`. | -| **Max RPM** *(optional)* | `max_rpm` is Tte maximum number of requests per minute the agent can perform to avoid rate limits. It's optional and can be left unspecified, with a default value of `None`. | -| **Max Execution Time** *(optional)* | `max_execution_time` is the Maximum execution time for an agent to execute a task. It's optional and can be left unspecified, with a default value of `None`, meaning no max execution time. | -| **Verbose** *(optional)* | Setting this to `True` configures the internal logger to provide detailed execution logs, aiding in debugging and monitoring. Default is `False`. | -| **Allow Delegation** *(optional)* | Agents can delegate tasks or questions to one another, ensuring that each task is handled by the most suitable agent. Default is `True`. | -| **Step Callback** *(optional)* | A function that is called after each step of the agent. This can be used to log the agent's actions or to perform other operations. It will overwrite the crew `step_callback`. | -| **Cache** *(optional)* | Indicates if the agent should use a cache for tool usage. Default is `True`. | -| **System Template** *(optional)* | Specifies the system format for the agent. Default is `None`. | -| **Prompt Template** *(optional)* | Specifies the prompt format for the agent. Default is `None`. | -| **Response Template** *(optional)* | Specifies the response format for the agent. Default is `None`. | +| Attribute | Parameter | Description | +| :------------------------- | :---- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **Role** | `role` | Defines the agent's function within the crew. It determines the kind of tasks the agent is best suited for. | +| **Goal** | `goal` | The individual objective that the agent aims to achieve. It guides the agent's decision-making process. | +| **Backstory** | `backstory` | Provides context to the agent's role and goal, enriching the interaction and collaboration dynamics. | +| **LLM** *(optional)* | `llm` | Represents the language model that will run the agent. It dynamically fetches the model name from the `OPENAI_MODEL_NAME` environment variable, defaulting to "gpt-4" if not specified. | +| **Tools** *(optional)* | `tools` | Set of capabilities or functions that the agent can use to perform tasks. Expected to be instances of custom classes compatible with the agent's execution environment. Tools are initialized with a default value of an empty list. | +| **Function Calling LLM** *(optional)* | `function_calling_llm` | Specifies the language model that will handle the tool calling for this agent, overriding the crew function calling LLM if passed. Default is `None`. | +| **Max Iter** *(optional)* | `max_iter` | Max Iter is the maximum number of iterations the agent can perform before being forced to give its best answer. Default is `25`. | +| **Max RPM** *(optional)* | `max_rpm` | Max RPM is the maximum number of requests per minute the agent can perform to avoid rate limits. It's optional and can be left unspecified, with a default value of `None`. | +| **Max Execution Time** *(optional)* | `max_execution_time` | Max Execution Time is the Maximum execution time for an agent to execute a task. It's optional and can be left unspecified, with a default value of `None`, meaning no max execution time. | +| **Verbose** *(optional)* | `verbose` | Setting this to `True` configures the internal logger to provide detailed execution logs, aiding in debugging and monitoring. Default is `False`. | +| **Allow Delegation** *(optional)* | `allow_delegation` | Agents can delegate tasks or questions to one another, ensuring that each task is handled by the most suitable agent. Default is `True`. | +| **Step Callback** *(optional)* | `step_callback` | A function that is called after each step of the agent. This can be used to log the agent's actions or to perform other operations. It will overwrite the crew `step_callback`. | +| **Cache** *(optional)* | `cache` | Indicates if the agent should use a cache for tool usage. Default is `True`. | +| **System Template** *(optional)* | `system_template` | Specifies the system format for the agent. Default is `None`. | +| **Prompt Template** *(optional)* | `prompt_template` | Specifies the prompt format for the agent. Default is `None`. | +| **Response Template** *(optional)* | `response_template` | Specifies the response format for the agent. Default is `None`. | ## Creating an Agent diff --git a/docs/core-concepts/Crews.md b/docs/core-concepts/Crews.md index 3342024b8..dec6adfc8 100644 --- a/docs/core-concepts/Crews.md +++ b/docs/core-concepts/Crews.md @@ -8,29 +8,29 @@ A crew in crewAI represents a collaborative group of agents working together to ## Crew Attributes -| Attribute | Description | -| :-------------------------- | :----------------------------------------------------------- | -| **Tasks** | A list of tasks assigned to the crew. | -| **Agents** | A list of agents that are part of the crew. | -| **Process** *(optional)* | The process flow (e.g., sequential, hierarchical) the crew follows. | -| **Verbose** *(optional)* | The verbosity level for logging during execution. | -| **Manager LLM** *(optional)*| The language model used by the manager agent in a hierarchical process. **Required when using a hierarchical process.** | -| **Function Calling LLM** *(optional)* | If passed, the crew will use this LLM to do function calling for tools for all agents in the crew. Each agent can have its own LLM, which overrides the crew's LLM for function calling. | -| **Config** *(optional)* | Optional configuration settings for the crew, in `Json` or `Dict[str, Any]` format. | -| **Max RPM** *(optional)* | Maximum requests per minute the crew adheres to during execution. | -| **Language** *(optional)* | Language used for the crew, defaults to English. | -| **Language File** *(optional)* | Path to the language file to be used for the crew. | -| **Memory** *(optional)* | Utilized for storing execution memories (short-term, long-term, entity memory). | -| **Cache** *(optional)* | Specifies whether to use a cache for storing the results of tools' execution. | -| **Embedder** *(optional)* | Configuration for the embedder to be used by the crew. Mostly used by memory for now. | -| **Full Output** *(optional)*| Whether the crew should return the full output with all tasks outputs or just the final output. | -| **Step Callback** *(optional)* | A function that is called after each step of every agent. This can be used to log the agent's actions or to perform other operations; it won't override the agent-specific `step_callback`. | -| **Task Callback** *(optional)* | A function that is called after the completion of each task. Useful for monitoring or additional operations post-task execution. | -| **Share Crew** *(optional)* | Whether you want to share the complete crew information and execution with the crewAI team to make the library better, and allow us to train models. | -| **Output Log File** *(optional)* | Whether you want to have a file with the complete crew output and execution. You can set it using True and it will default to the folder you are currently in and it will be called logs.txt or passing a string with the full path and name of the file. | -| **Manager Agent** *(optional)* | `manager` sets a custom agent that will be used as a manager. | -| **Manager Callbacks** *(optional)* | `manager_callbacks` takes a list of callback handlers to be executed by the manager agent when a hierarchical process is used. | -| **Prompt File** *(optional)* | Path to the prompt JSON file to be used for the crew. | +| Attribute | Parameters | Description | +| :-------------------------- | :------------------ | :------------------------------------------------------------------------------------------------------- | +| **Tasks** | `tasks` | A list of tasks assigned to the crew. | +| **Agents** | `agents` | A list of agents that are part of the crew. | +| **Process** *(optional)* | `process` | The process flow (e.g., sequential, hierarchical) the crew follows. | +| **Verbose** *(optional)* | `verbose` | The verbosity level for logging during execution. | +| **Manager LLM** *(optional)*| `manager_llm` | The language model used by the manager agent in a hierarchical process. **Required when using a hierarchical process.** | +| **Function Calling LLM** *(optional)* | `function_calling_llm` | If passed, the crew will use this LLM to do function calling for tools for all agents in the crew. Each agent can have its own LLM, which overrides the crew's LLM for function calling. | +| **Config** *(optional)* | `config` | Optional configuration settings for the crew, in `Json` or `Dict[str, Any]` format. | +| **Max RPM** *(optional)* | `max_rpm` | Maximum requests per minute the crew adheres to during execution. | +| **Language** *(optional)* | `language` | Language used for the crew, defaults to English. | +| **Language File** *(optional)* | `language_file` | Path to the language file to be used for the crew. | +| **Memory** *(optional)* | `memory` | Utilized for storing execution memories (short-term, long-term, entity memory). | +| **Cache** *(optional)* | `cache` | Specifies whether to use a cache for storing the results of tools' execution. | +| **Embedder** *(optional)* | `embedder` | Configuration for the embedder to be used by the crew. Mostly used by memory for now. | +| **Full Output** *(optional)*| `full_output` | Whether the crew should return the full output with all tasks outputs or just the final output. | +| **Step Callback** *(optional)* | `step_callback` | A function that is called after each step of every agent. This can be used to log the agent's actions or to perform other operations; it won't override the agent-specific `step_callback`. | +| **Task Callback** *(optional)* | `task_callback` | A function that is called after the completion of each task. Useful for monitoring or additional operations post-task execution. | +| **Share Crew** *(optional)* | `share_crew` | Whether you want to share the complete crew information and execution with the crewAI team to make the library better, and allow us to train models. | +| **Output Log File** *(optional)* | `output_log_file` | Whether you want to have a file with the complete crew output and execution. You can set it using True and it will default to the folder you are currently in and it will be called logs.txt or passing a string with the full path and name of the file. | +| **Manager Agent** *(optional)* | `manager_agent` | `manager` sets a custom agent that will be used as a manager. | +| **Manager Callbacks** *(optional)* | `manager_callbacks` | `manager_callbacks` takes a list of callback handlers to be executed by the manager agent when a hierarchical process is used. | +| **Prompt File** *(optional)* | `prompt_file` | Path to the prompt JSON file to be used for the crew. | !!! note "Crew Max RPM" The `max_rpm` attribute sets the maximum number of requests per minute the crew can perform to avoid rate limits and will override individual agents' `max_rpm` settings if you set it. diff --git a/docs/core-concepts/Tasks.md b/docs/core-concepts/Tasks.md index bd6460f94..b3ea030c4 100644 --- a/docs/core-concepts/Tasks.md +++ b/docs/core-concepts/Tasks.md @@ -11,20 +11,20 @@ Tasks within crewAI can be collaborative, requiring multiple agents to work toge ## Task Attributes -| Attribute | Description | -| :----------------------| :-------------------------------------------------------------------------------------------- | -| **Description** | A clear, concise statement of what the task entails. | -| **Agent** | The agent responsible for the task, assigned either directly or by the crew's process. | -| **Expected Output** | A detailed description of what the task's completion looks like. | -| **Tools** *(optional)* | The functions or capabilities the agent can utilize to perform the task. | -| **Async Execution** *(optional)* | If set, the task executes asynchronously, allowing progression without waiting for completion.| -| **Context** *(optional)* | Specifies tasks whose outputs are used as context for this task. | -| **Config** *(optional)* | Additional configuration details for the agent executing the task, allowing further customization. | -| **Output JSON** *(optional)* | Outputs a JSON object, requiring an OpenAI client. Only one output format can be set. | -| **Output Pydantic** *(optional)* | Outputs a Pydantic model object, requiring an OpenAI client. Only one output format can be set. | -| **Output File** *(optional)* | Saves the task output to a file. If used with `Output JSON` or `Output Pydantic`, specifies how the output is saved. | -| **Callback** *(optional)* | A Python callable that is executed with the task's output upon completion. | -| **Human Input** *(optional)* | Indicates if the task requires human feedback at the end, useful for tasks needing human oversight. | +| Attribute | Parameters | Description | +| :----------------------| :------------------- | :-------------------------------------------------------------------------------------------- | +| **Description** | `description` | A clear, concise statement of what the task entails. | +| **Agent** | `agent` | The agent responsible for the task, assigned either directly or by the crew's process. | +| **Expected Output** | `expected_output` | A detailed description of what the task's completion looks like. | +| **Tools** *(optional)* | `tools` | The functions or capabilities the agent can utilize to perform the task. | +| **Async Execution** *(optional)* | `async_execution` | If set, the task executes asynchronously, allowing progression without waiting for completion.| +| **Context** *(optional)* | `context` | Specifies tasks whose outputs are used as context for this task. | +| **Config** *(optional)* | `config` | Additional configuration details for the agent executing the task, allowing further customization. | +| **Output JSON** *(optional)* | `output_json` | Outputs a JSON object, requiring an OpenAI client. Only one output format can be set. | +| **Output Pydantic** *(optional)* | `output_pydantic` | Outputs a Pydantic model object, requiring an OpenAI client. Only one output format can be set. | +| **Output File** *(optional)* | `output_file` | Saves the task output to a file. If used with `Output JSON` or `Output Pydantic`, specifies how the output is saved. | +| **Callback** *(optional)* | `callback` | A Python callable that is executed with the task's output upon completion. | +| **Human Input** *(optional)* | `human_input` | Indicates if the task requires human feedback at the end, useful for tasks needing human oversight. | ## Creating a Task From d72b00af3ceb747a3454b7f3f496554d5e61fae3 Mon Sep 17 00:00:00 2001 From: Salman Faroz Date: Wed, 3 Jul 2024 05:47:53 +0530 Subject: [PATCH 3/4] Update Sequential.md (#849) To Resolve : pydantic_core._pydantic_core.ValidationError: 1 validation error for Task expected_output Field required [type=missing, input_value=, input_type=dict] For further information visit https://errors.pydantic.dev/2.6/v/missing "Expected Output" is mandatory now as it forces people to be specific about the expected result and get better result refer : https://github.com/joaomdmoura/crewAI/issues/308 --- docs/how-to/Sequential.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/how-to/Sequential.md b/docs/how-to/Sequential.md index ff9efcc94..ae351197b 100644 --- a/docs/how-to/Sequential.md +++ b/docs/how-to/Sequential.md @@ -37,10 +37,9 @@ writer = Agent( backstory='A skilled writer with a talent for crafting compelling narratives' ) -# Define the tasks in sequence -research_task = Task(description='Gather relevant data...', agent=researcher) -analysis_task = Task(description='Analyze the data...', agent=analyst) -writing_task = Task(description='Compose the report...', agent=writer) +research_task = Task(description='Gather relevant data...', agent=researcher, expected_output='Raw Data') +analysis_task = Task(description='Analyze the data...', agent=analyst, expected_output='Data Insights') +writing_task = Task(description='Compose the report...', agent=writer, expected_output='Final Report') # Form the crew with a sequential process report_crew = Crew( @@ -83,4 +82,4 @@ CrewAI tracks token usage across all tasks and agents. You can access these metr 1. **Order Matters**: Arrange tasks in a logical sequence where each task builds upon the previous one. 2. **Clear Task Descriptions**: Provide detailed descriptions for each task to guide the agents effectively. 3. **Appropriate Agent Selection**: Match agents' skills and roles to the requirements of each task. -4. **Use Context**: Leverage the context from previous tasks to inform subsequent ones \ No newline at end of file +4. **Use Context**: Leverage the context from previous tasks to inform subsequent ones From f47904134b6bfba750f32151dd0a499229816851 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Tue, 2 Jul 2024 17:52:15 -0700 Subject: [PATCH 4/4] Add back AgentOps as Optional Dependency (#543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * implements agentops with a langchain handler, agent tracking and tool call recording * track tool usage * end session after completion * track tool usage time * better tool and llm tracking * code cleanup * make agentops optional * optional dependency usage * remove telemetry code * optional agentops * agentops version bump * remove org key * true dependency * add crew org key to agentops * cleanup * Update pyproject.toml * Revert "true dependency" This reverts commit e52e8e9568c2c00f29c1f800ea00d472413b6067. * Revert "cleanup" This reverts commit 7f5635fb9ef9aaafc4283fdf1a892d8b5c425220. * optional parent key * agentops 0.1.5 * Revert "Revert "cleanup"" This reverts commit cea33d9a5d37289b9e257046c1354057b27fbe7f. * Revert "Revert "true dependency"" This reverts commit 4d1b460b * cleanup * Forcing version 0.1.5 * Update pyproject.toml * agentops update * noop * add crew tag * black formatting * use langchain callback handler to support all LLMs * agentops version bump * track task evaluator * merge upstream * Fix typo in instruction en.json (#676) * Enable search in docs (#663) * Clarify text in docstring (#662) * Update agent.py (#655) Changed default model value from gpt-4 to gpt-4o. Reasoning. gpt-4 costs 30$ per million tokens while gpt-4o costs 5$. This is more cost friendly for default option. * Update README.md (#652) Rework example so that if you use a custom LLM it doesn't throw code errors by uncommenting. * Update BrowserbaseLoadTool.md (#647) * Update crew.py (#644) Fixed Type on line 53 * fixes #665 (#666) * Added timestamp to logger (#646) * Added timestamp to logger Updated the logger.py file to include timestamps when logging output. For example: [2024-05-20 15:32:48][DEBUG]: == Working Agent: Researcher [2024-05-20 15:32:48][INFO]: == Starting Task: Research the topic [2024-05-20 15:33:22][DEBUG]: == [Researcher] Task output: * Update tool_usage.py * Revert "Update tool_usage.py" This reverts commit 95d18d5b6f9bc4b918ce693d2780f6e464d99547. incorrect bramch for this commit * support skip auto end session * conditional protect agentops use * fix crew logger bug * fix crew logger bug * Update crew.py * Update tool_usage.py --------- Co-authored-by: João Moura Co-authored-by: Howard Gil Co-authored-by: Olivier Roberdet Co-authored-by: Paul Sanders Co-authored-by: Anudeep Kolluri <50168940+Anudeep-Kolluri@users.noreply.github.com> Co-authored-by: Mike Heavers Co-authored-by: Mish Ushakov <10400064+mishushakov@users.noreply.github.com> Co-authored-by: theCyberTech - Rip&Tear <84775494+theCyberTech@users.noreply.github.com> Co-authored-by: Saif Mahmud <60409889+vmsaif@users.noreply.github.com> --- pyproject.toml | 4 ++- src/crewai/agent.py | 22 +++++++++++++ src/crewai/crew.py | 9 +++++ src/crewai/tools/tool_usage.py | 33 +++++++++++++------ .../utilities/evaluators/task_evaluator.py | 12 +++++++ src/crewai/utilities/logger.py | 2 +- 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ce2bbede7..17c6f526e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,10 +26,12 @@ click = "^8.1.7" python-dotenv = "^1.0.0" appdirs = "^1.4.4" jsonref = "^1.1.0" +agentops = { version = "^0.1.9", optional = true } embedchain = "^0.1.113" [tool.poetry.extras] tools = ["crewai-tools"] +agentops = ["agentops"] [tool.poetry.group.dev.dependencies] isort = "^5.13.2" @@ -60,4 +62,4 @@ exclude = ["cli/templates/main.py", "cli/templates/crew.py"] [build-system] requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" \ No newline at end of file +build-backend = "poetry.core.masonry.api" diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 67e81f4d0..501d538be 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -19,7 +19,20 @@ from crewai.utilities.token_counter_callback import TokenCalcHandler from crewai.agents.agent_builder.base_agent import BaseAgent from crewai.utilities.training_handler import CrewTrainingHandler +agentops = None +try: + import agentops + from agentops import track_agent +except ImportError: + def track_agent(): + def noop(f): + return f + + return noop + + +@track_agent() class Agent(BaseAgent): """Represents an agent in a system. @@ -48,6 +61,8 @@ class Agent(BaseAgent): default=None, description="Maximum execution time for an agent to execute a task", ) + agent_ops_agent_name: str = None + agent_ops_agent_id: str = None cache_handler: InstanceOf[CacheHandler] = Field( default=None, description="An instance of the CacheHandler class." ) @@ -84,6 +99,7 @@ class Agent(BaseAgent): def __init__(__pydantic_self__, **data): config = data.pop("config", {}) super().__init__(**config, **data) + __pydantic_self__.agent_ops_agent_name = __pydantic_self__.role @model_validator(mode="after") def set_agent_executor(self) -> "Agent": @@ -101,6 +117,12 @@ class Agent(BaseAgent): ): self.llm.callbacks.append(token_handler) + if agentops and not any( + isinstance(handler, agentops.LangchainCallbackHandler) for handler in self.llm.callbacks + ): + agentops.stop_instrumenting() + self.llm.callbacks.append(agentops.LangchainCallbackHandler()) + if not self.agent_executor: if not self.cache_handler: self.cache_handler = CacheHandler() diff --git a/src/crewai/crew.py b/src/crewai/crew.py index e2f927616..636ead48d 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -31,6 +31,11 @@ from crewai.utilities import I18N, FileHandler, Logger, RPMController from crewai.utilities.evaluators.task_evaluator import TaskEvaluator from crewai.utilities.training_handler import CrewTrainingHandler +try: + import agentops +except ImportError: + agentops = None + class Crew(BaseModel): """ @@ -536,6 +541,10 @@ class Crew(BaseModel): def _finish_execution(self, output) -> None: if self.max_rpm: self._rpm_controller.stop_rpm_counter() + if agentops: + agentops.end_session( + end_state="Success", end_state_reason="Finished Execution", is_auto_end=True + ) self._telemetry.end_crew(self, output) def __repr__(self): diff --git a/src/crewai/tools/tool_usage.py b/src/crewai/tools/tool_usage.py index cb7757d84..9bcd1ce91 100644 --- a/src/crewai/tools/tool_usage.py +++ b/src/crewai/tools/tool_usage.py @@ -11,6 +11,12 @@ from crewai.telemetry import Telemetry from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling from crewai.utilities import I18N, Converter, ConverterError, Printer +agentops = None +try: + import agentops +except ImportError: + pass + OPENAI_BIGGER_MODELS = ["gpt-4"] @@ -91,15 +97,16 @@ class ToolUsage: self.task.increment_tools_errors() self._printer.print(content=f"\n\n{error}\n", color="red") return error - return f"{self._use(tool_string=tool_string, tool=tool, calling=calling)}" # type: ignore # BUG?: "_use" of "ToolUsage" does not return a value (it only ever returns None) + return f"{self._use(tool_string=tool_string, tool=tool, calling=calling)}" # type: ignore # BUG?: "_use" of "ToolUsage" does not return a value (it only ever returns None) def _use( self, tool_string: str, tool: BaseTool, calling: Union[ToolCalling, InstructorToolCalling], - ) -> str: # TODO: Fix this return type --> finecwg : I updated return type to str - 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) + ) -> str: # TODO: Fix this return type + tool_event = agentops.ToolEvent(name=calling.tool_name) if agentops else None + 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( tool_names=self.tools_names @@ -110,13 +117,13 @@ class ToolUsage: tool_name=tool.name, attempts=self._run_attempts, ) - result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) + result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) return result # type: ignore # Fix the reutrn type of this function except Exception: self.task.increment_tools_errors() - result = None # type: ignore # Incompatible types in assignment (expression has type "None", variable has type "str") + result = None # type: ignore # Incompatible types in assignment (expression has type "None", variable has type "str") if self.tools_handler.cache: result = self.tools_handler.cache.read( # type: ignore # Incompatible types in assignment (expression has type "str | None", variable has type "str") @@ -133,7 +140,7 @@ class ToolUsage: if calling.arguments: try: - acceptable_args = tool.args_schema.schema()["properties"].keys() # type: ignore # Item "None" of "type[BaseModel] | None" has no attribute "schema" + acceptable_args = tool.args_schema.schema()["properties"].keys() # type: ignore # Item "None" of "type[BaseModel] | None" has no attribute "schema" arguments = { k: v for k, v in calling.arguments.items() @@ -145,7 +152,7 @@ class ToolUsage: arguments = calling.arguments result = tool._run(**arguments) else: - arguments = calling.arguments.values() # type: ignore # Incompatible types in assignment (expression has type "dict_values[str, Any]", variable has type "dict[str, Any]") + arguments = calling.arguments.values() # type: ignore # Incompatible types in assignment (expression has type "dict_values[str, Any]", variable has type "dict[str, Any]") result = tool._run(*arguments) else: result = tool._run() @@ -164,6 +171,10 @@ class ToolUsage: return error # type: ignore # No return value expected self.task.increment_tools_errors() + if agentops: + agentops.record( + agentops.ErrorEvent(exception=e, trigger_event=tool_event) + ) return self.use(calling=calling, tool_string=tool_string) # type: ignore # No return value expected if self.tools_handler: @@ -184,18 +195,20 @@ class ToolUsage: ) self._printer.print(content=f"\n\n{result}\n", color="purple") + if agentops: + agentops.record(tool_event) self._telemetry.tool_usage( llm=self.function_calling_llm, tool_name=tool.name, attempts=self._run_attempts, - ) - result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) + ) + result = self._format_result(result=result) # type: ignore # "_format_result" of "ToolUsage" does not return a value (it only ever returns None) return result # type: ignore # No return value expected def _format_result(self, result: Any) -> None: self.task.used_tools += 1 if self._should_remember_format(): # type: ignore # "_should_remember_format" of "ToolUsage" does not return a value (it only ever returns None) - result = self._remember_format(result=result) # type: ignore # "_remember_format" of "ToolUsage" does not return a value (it only ever returns None) + result = self._remember_format(result=result) # type: ignore # "_remember_format" of "ToolUsage" does not return a value (it only ever returns None) return result def _should_remember_format(self) -> None: diff --git a/src/crewai/utilities/evaluators/task_evaluator.py b/src/crewai/utilities/evaluators/task_evaluator.py index 299590ba5..252541b66 100644 --- a/src/crewai/utilities/evaluators/task_evaluator.py +++ b/src/crewai/utilities/evaluators/task_evaluator.py @@ -5,6 +5,17 @@ from pydantic import BaseModel, Field from crewai.utilities import Converter from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser +agentops = None +try: + import agentops + from agentops import track_agent +except ImportError: + + def track_agent(name): + def noop(f): + return f + + return noop class Entity(BaseModel): @@ -38,6 +49,7 @@ class TrainingTaskEvaluation(BaseModel): ) +@track_agent(name="Task Evaluator") class TaskEvaluator: def __init__(self, original_agent): self.llm = original_agent.llm diff --git a/src/crewai/utilities/logger.py b/src/crewai/utilities/logger.py index e0c74d5ce..b54595c9c 100644 --- a/src/crewai/utilities/logger.py +++ b/src/crewai/utilities/logger.py @@ -1,7 +1,7 @@ from datetime import datetime from crewai.utilities.printer import Printer - +from datetime import datetime class Logger: _printer = Printer()