From ac2815c781e275a957bc8f5903c819355d31e21f Mon Sep 17 00:00:00 2001 From: "Brandon Hancock (bhancock_ai)" <109994880+bhancockio@users.noreply.github.com> Date: Mon, 15 Jul 2024 20:39:15 -0400 Subject: [PATCH] Add docs for `crewoutput` and `taskoutput` (#943) * Add docs for crewoutput and taskoutput * Add reference to change log --- docs/core-concepts/Crews.md | 100 ++++++++++++++++++++++++-------- docs/core-concepts/Tasks.md | 99 +++++++++++++++++++++++++------ mkdocs.yml | 1 + src/crewai/crews/crew_output.py | 20 ++----- src/crewai/tasks/task_output.py | 16 +---- 5 files changed, 166 insertions(+), 70 deletions(-) diff --git a/docs/core-concepts/Crews.md b/docs/core-concepts/Crews.md index 2d94022588..b33f33d9b1 100644 --- a/docs/core-concepts/Crews.md +++ b/docs/core-concepts/Crews.md @@ -4,36 +4,37 @@ description: Understanding and utilizing crews in the crewAI framework with comp --- ## What is a Crew? + A crew in crewAI represents a collaborative group of agents working together to achieve a set of tasks. Each crew defines the strategy for task execution, agent collaboration, and the overall workflow. ## Crew Attributes -| 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. | +| 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. +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. ## Creating a Crew @@ -89,6 +90,57 @@ my_crew = Crew( ) ``` +## Crew Output + +!!! note "Understanding Crew Outputs" +The output of a crew in the crewAI framework is encapsulated within the `CrewOutput` class. +This class provides a structured way to access results of the crew's execution, including various formats such as raw strings, JSON, and Pydantic models. +The `CrewOutput` includes the results from the final task output, token usage, and individual task outputs. + +### Crew Output Attributes + +| Attribute | Parameters | Type | Description | +| :--------------- | :------------- | :------------------------- | :--------------------------------------------------------------------------------------------------- | +| **Raw** | `raw` | `str` | The raw output of the crew. This is the default format for the output. | +| **Pydantic** | `pydantic` | `Optional[BaseModel]` | A Pydantic model object representing the structured output of the crew. | +| **JSON Dict** | `json_dict` | `Optional[Dict[str, Any]]` | A dictionary representing the JSON output of the crew. | +| **Tasks Output** | `tasks_output` | `List[TaskOutput]` | A list of `TaskOutput` objects, each representing the output of a task in the crew. | +| **Token Usage** | `token_usage` | `Dict[str, Any]` | A summary of token usage, providing insights into the language model's performance during execution. | + +### Crew Output Methods and Properties + +| Method/Property | Description | +| :-------------- | :------------------------------------------------------------------------------------------------ | +| **json** | Returns the JSON string representation of the crew output if the output format is JSON. | +| **to_dict** | Converts the JSON and Pydantic outputs to a dictionary. | +| \***\*str\*\*** | Returns the string representation of the crew output, prioritizing Pydantic, then JSON, then raw. | + +### Accessing Crew Outputs + +Once a crew has been executed, its output can be accessed through the `output` attribute of the `Crew` object. The `CrewOutput` class provides various ways to interact with and present this output. + +#### Example + +```python +# Example crew execution +crew = Crew( + agents=[research_agent, writer_agent], + tasks=[research_task, write_article_task], + verbose=2 +) + +result = crew.kickoff() + +# Accessing the crew output +print(f"Raw Output: {crew_output.raw}") +if crew_output.json_dict: + print(f"JSON Output: {json.dumps(crew_output.json_dict, indent=2)}") +if crew_output.pydantic: + print(f"Pydantic Output: {crew_output.pydantic}") +print(f"Tasks Output: {crew_output.tasks_output}") +print(f"Token Usage: {crew_output.token_usage}") +``` + ## Memory Utilization Crews can utilize memory (short-term, long-term, and entity memory) to enhance their execution and learning over time. This feature allows crews to store and recall execution memories, aiding in decision-making and task execution strategies. diff --git a/docs/core-concepts/Tasks.md b/docs/core-concepts/Tasks.md index b3ea030c47..65a26e752d 100644 --- a/docs/core-concepts/Tasks.md +++ b/docs/core-concepts/Tasks.md @@ -4,27 +4,29 @@ description: Detailed guide on managing and creating tasks within the crewAI fra --- ## Overview of a Task + !!! note "What is a Task?" - In the crewAI framework, tasks are specific assignments completed by agents. They provide all necessary details for execution, such as a description, the agent responsible, required tools, and more, facilitating a wide range of action complexities. +In the crewAI framework, tasks are specific assignments completed by agents. They provide all necessary details for execution, such as a description, the agent responsible, required tools, and more, facilitating a wide range of action complexities. Tasks within crewAI can be collaborative, requiring multiple agents to work together. This is managed through the task properties and orchestrated by the Crew's process, enhancing teamwork and efficiency. ## Task Attributes -| 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. | +| 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. | +| **Output** _(optional)_ | `output` | The output of the task, containing the raw, JSON, and Pydantic output plus additional details. | +| **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 @@ -35,12 +37,75 @@ from crewai import Task task = Task( description='Find and summarize the latest and most relevant news on AI', - agent=sales_agent + agent=sales_agent, + expected_output='A bullet list summary of the top 5 most important AI news', ) ``` !!! note "Task Assignment" - Directly specify an `agent` for assignment or let the `hierarchical` CrewAI's process decide based on roles, availability, etc. +Directly specify an `agent` for assignment or let the `hierarchical` CrewAI's process decide based on roles, availability, etc. + +## Task Output + +!!! note "Understanding Task Outputs" +The output of a task in the crewAI framework is encapsulated within the `TaskOutput` class. This class provides a structured way to access results of a task, including various formats such as raw strings, JSON, and Pydantic models. +By default, the `TaskOutput` will only include the `raw` output. A `TaskOutput` will only include the `pydantic` or `json_dict` output if the original `Task` object was configured with `output_pydantic` or `output_json`, respectively. + +### Task Output Attributes + +| Attribute | Parameters | Type | Description | +| :---------------- | :-------------- | :------------------------- | :------------------------------------------------------------------------------------------------- | +| **Description** | `description` | `str` | A brief description of the task. | +| **Summary** | `summary` | `Optional[str]` | A short summary of the task, auto-generated from the description. | +| **Raw** | `raw` | `str` | The raw output of the task. This is the default format for the output. | +| **Pydantic** | `pydantic` | `Optional[BaseModel]` | A Pydantic model object representing the structured output of the task. | +| **JSON Dict** | `json_dict` | `Optional[Dict[str, Any]]` | A dictionary representing the JSON output of the task. | +| **Agent** | `agent` | `str` | The agent that executed the task. | +| **Output Format** | `output_format` | `OutputFormat` | The format of the task output, with options including RAW, JSON, and Pydantic. The default is RAW. | + +### Task Output Methods and Properties + +| Method/Property | Description | +| :-------------- | :------------------------------------------------------------------------------------------------ | +| **json** | Returns the JSON string representation of the task output if the output format is JSON. | +| **to_dict** | Converts the JSON and Pydantic outputs to a dictionary. | +| \***\*str\*\*** | Returns the string representation of the task output, prioritizing Pydantic, then JSON, then raw. | + +### Accessing Task Outputs + +Once a task has been executed, its output can be accessed through the `output` attribute of the `Task` object. The `TaskOutput` class provides various ways to interact with and present this output. + +#### Example + +```python +# Example task +task = Task( + description='Find and summarize the latest AI news', + expected_output='A bullet list summary of the top 5 most important AI news', + agent=research_agent, + tools=[search_tool] +) + +# Execute the crew +crew = Crew( + agents=[research_agent], + tasks=[task], + verbose=2 +) + +result = crew.kickoff() + +# Accessing the task output +task_output = task.output + +print(f"Task Description: {task_output.description}") +print(f"Task Summary: {task_output.summary}") +print(f"Raw Output: {task_output.raw}") +if task_output.json_dict: + print(f"JSON Output: {json.dumps(task_output.json_dict, indent=2)}") +if task_output.pydantic: + print(f"Pydantic Output: {task_output.pydantic}") +``` ## Integrating Tools with Tasks diff --git a/mkdocs.yml b/mkdocs.yml index e17bc02818..622340b393 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -181,6 +181,7 @@ nav: - Landing Page Generator: https://github.com/joaomdmoura/crewAI-examples/tree/main/landing_page_generator" - Prepare for meetings: https://github.com/joaomdmoura/crewAI-examples/tree/main/prep-for-a-meeting" - Telemetry: 'telemetry/Telemetry.md' + - Change Log: 'https://github.com/crewAIInc/crewAI/releases' extra_css: - stylesheets/output.css diff --git a/src/crewai/crews/crew_output.py b/src/crewai/crews/crew_output.py index 0ffc154f70..e630c1f3a6 100644 --- a/src/crewai/crews/crew_output.py +++ b/src/crewai/crews/crew_output.py @@ -24,16 +24,6 @@ class CrewOutput(BaseModel): description="Processed token summary", default={} ) - # @property - # def pydantic(self) -> Optional[BaseModel]: - # # Check if the final task output included a pydantic model - # if self.tasks_output[-1].output_format != OutputFormat.PYDANTIC: - # raise ValueError( - # "No pydantic model found in the final task. Please make sure to set the output_pydantic property in the final task in your crew." - # ) - - # return self._pydantic - @property def json(self) -> Optional[str]: if self.tasks_output[-1].output_format != OutputFormat.JSON: @@ -44,11 +34,13 @@ def json(self) -> Optional[str]: return json.dumps(self.json_dict) def to_dict(self) -> Dict[str, Any]: + """Convert json_output and pydantic_output to a dictionary.""" + output_dict = {} if self.json_dict: - return self.json_dict - if self.pydantic: - return self.pydantic.model_dump() - raise ValueError("No output to convert to dictionary") + output_dict.update(self.json_dict) + elif self.pydantic: + output_dict.update(self.pydantic.model_dump()) + return output_dict def __str__(self): if self.pydantic: diff --git a/src/crewai/tasks/task_output.py b/src/crewai/tasks/task_output.py index 1d01e98062..604dab6e85 100644 --- a/src/crewai/tasks/task_output.py +++ b/src/crewai/tasks/task_output.py @@ -30,20 +30,6 @@ def set_summary(self): self.summary = f"{excerpt}..." return self - # @property - # def pydantic(self) -> Optional[BaseModel]: - # # Check if the final task output included a pydantic model - # if self.output_format != OutputFormat.PYDANTIC: - # raise ValueError( - # """ - # Invalid output format requested. - # If you would like to access the pydantic model, - # please make sure to set the output_pydantic property for the task. - # """ - # ) - - # return self._pydantic - @property def json(self) -> Optional[str]: if self.output_format != OutputFormat.JSON: @@ -62,7 +48,7 @@ def to_dict(self) -> Dict[str, Any]: output_dict = {} if self.json_dict: output_dict.update(self.json_dict) - if self.pydantic: + elif self.pydantic: output_dict.update(self.pydantic.model_dump()) return output_dict