From 8cf1cd5a62ee68d57445a1eacd3407fbd9808106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Mon, 25 Nov 2024 10:05:15 -0300 Subject: [PATCH] preparing new version --- docs/concepts/knowledge.mdx | 18 +++--- .../how-to/before-and-after-kickoff-hooks.mdx | 59 +++++++++++++++++++ docs/quickstart.mdx | 48 +++++++++++---- pyproject.toml | 4 +- src/crewai/__init__.py | 2 +- src/crewai/cli/templates/crew/crew.py | 14 ++++- src/crewai/cli/templates/crew/pyproject.toml | 2 +- src/crewai/cli/templates/flow/pyproject.toml | 2 +- .../cli/templates/pipeline/pyproject.toml | 2 +- .../templates/pipeline_router/pyproject.toml | 2 +- src/crewai/cli/templates/tool/pyproject.toml | 2 +- uv.lock | 13 ++-- 12 files changed, 137 insertions(+), 31 deletions(-) create mode 100644 docs/how-to/before-and-after-kickoff-hooks.mdx diff --git a/docs/concepts/knowledge.mdx b/docs/concepts/knowledge.mdx index 2afb1b568..ce0203e13 100644 --- a/docs/concepts/knowledge.mdx +++ b/docs/concepts/knowledge.mdx @@ -1,6 +1,6 @@ --- title: Knowledge -description: What is knowledge in CrewAI and how to use it. +description: Understand what knowledge is in CrewAI and how to effectively use it. icon: book --- @@ -8,7 +8,14 @@ icon: book ## Introduction +Knowledge in CrewAI serves as a foundational component for enriching AI agents with contextual and relevant information. It enables agents to access and utilize structured data sources during their execution processes, making them more intelligent and responsive. + The Knowledge class in CrewAI provides a powerful way to manage and query knowledge sources for your AI agents. This guide will show you how to implement knowledge management in your CrewAI projects. + +## What is Knowledge? + +The `Knowledge` class in CrewAI manages various sources that store information, which can be queried and retrieved by AI agents. This modular approach allows you to integrate diverse data formats such as text, PDFs, spreadsheets, and more into your AI workflows. + Additionally, we have specific tools for generate knowledge sources for strings, text files, PDF's, and Spreadsheets. You can expand on any source type by extending the `KnowledgeSource` class. ## Basic Implementation @@ -25,17 +32,14 @@ string_source = StringKnowledgeSource( content=content, metadata={"preference": "personal"} ) - -llm = LLM(model="gpt-4o-mini", temperature=0) - # Create an agent with the knowledge store +# Create an agent with the knowledge store agent = Agent( role="About User", goal="You know everything about the user.", backstory="""You are a master at understanding people and their preferences.""", - verbose=True, - allow_delegation=False, - llm=llm, + verbose=True ) + task = Task( description="Answer the following questions about the user: {question}", expected_output="An answer to the question.", diff --git a/docs/how-to/before-and-after-kickoff-hooks.mdx b/docs/how-to/before-and-after-kickoff-hooks.mdx new file mode 100644 index 000000000..83058ce3c --- /dev/null +++ b/docs/how-to/before-and-after-kickoff-hooks.mdx @@ -0,0 +1,59 @@ +--- +title: Before and After Kickoff Hooks +description: Learn how to use before and after kickoff hooks in CrewAI +--- + +CrewAI provides hooks that allow you to execute code before and after a crew's kickoff. These hooks are useful for preprocessing inputs or post-processing results. + +## Before Kickoff Hook + +The before kickoff hook is executed before the crew starts its tasks. It receives the input dictionary and can modify it before passing it to the crew. You can use this hook to set up your environment, load necessary data, or preprocess your inputs. This is useful in scenarios where the input data might need enrichment or validation before being processed by the crew. + +Here's an example of defining a before kickoff function in your `crew.py`: + +```python +from crewai import CrewBase, before_kickoff + +@CrewBase +class MyCrew: + @before_kickoff + def prepare_data(self, inputs): + # Preprocess or modify inputs + inputs['processed'] = True + return inputs + +#... +``` + +In this example, the prepare_data function modifies the inputs by adding a new key-value pair indicating that the inputs have been processed. + +## After Kickoff Hook + +The after kickoff hook is executed after the crew has completed its tasks. It receives the result object, which contains the outputs of the crew's execution. This hook is ideal for post-processing results, such as logging, data transformation, or further analysis. + +Here's how you can define an after kickoff function in your `crew.py`: + +```python +from crewai import CrewBase, after_kickoff + +@CrewBase +class MyCrew: + @after_kickoff + def log_results(self, result): + # Log or modify the results + print("Crew execution completed with result:", result) + return result + +# ... +``` + + +In the `log_results` function, the results of the crew execution are simply printed out. You can extend this to perform more complex operations such as sending notifications or integrating with other services. + +## Utilizing Both Hooks + +Both hooks can be used together to provide a comprehensive setup and teardown process for your crew's execution. They are particularly useful in maintaining clean code architecture by separating concerns and enhancing the modularity of your CrewAI implementations. + +## Conclusion + +Before and after kickoff hooks in CrewAI offer powerful ways to interact with the lifecycle of a crew's execution. By understanding and utilizing these hooks, you can greatly enhance the robustness and flexibility of your AI agents. diff --git a/docs/quickstart.mdx b/docs/quickstart.mdx index ef149bfcc..246bbdf84 100644 --- a/docs/quickstart.mdx +++ b/docs/quickstart.mdx @@ -8,7 +8,7 @@ icon: rocket Let's create a simple crew that will help us `research` and `report` on the `latest AI developments` for a given topic or subject. -Before we proceed, make sure you have `crewai` and `crewai-tools` installed. +Before we proceed, make sure you have `crewai` and `crewai-tools` installed. If you haven't installed them yet, you can do so by following the [installation guide](/installation). Follow the steps below to get crewing! 🚣‍♂️ @@ -23,7 +23,7 @@ Follow the steps below to get crewing! 🚣‍♂️ ``` - + You can also modify the agents as needed to fit your use case or copy and paste as is to your project. Any variable interpolated in your `agents.yaml` and `tasks.yaml` files like `{topic}` will be replaced by the value of the variable in the `main.py` file. @@ -39,7 +39,7 @@ Follow the steps below to get crewing! 🚣‍♂️ You're a seasoned researcher with a knack for uncovering the latest developments in {topic}. Known for your ability to find the most relevant information and present it in a clear and concise manner. - + reporting_analyst: role: > {topic} Reporting Analyst @@ -51,7 +51,7 @@ Follow the steps below to get crewing! 🚣‍♂️ it easy for others to understand and act on the information you provide. ``` - + ```yaml tasks.yaml # src/latest_ai_development/config/tasks.yaml research_task: @@ -73,8 +73,8 @@ Follow the steps below to get crewing! 🚣‍♂️ agent: reporting_analyst output_file: report.md ``` - - + + ```python crew.py # src/latest_ai_development/crew.py from crewai import Agent, Crew, Process, Task @@ -121,10 +121,34 @@ Follow the steps below to get crewing! 🚣‍♂️ tasks=self.tasks, # Automatically created by the @task decorator process=Process.sequential, verbose=True, - ) + ) ``` - + + ```python crew.py + # src/latest_ai_development/crew.py + from crewai import Agent, Crew, Process, Task + from crewai.project import CrewBase, agent, crew, task, before_kickoff, after_kickoff + from crewai_tools import SerperDevTool + + @CrewBase + class LatestAiDevelopmentCrew(): + """LatestAiDevelopment crew""" + + @before_kickoff + def before_kickoff_function(self, inputs): + print(f"Before kickoff function with inputs: {inputs}") + return inputs # You can return the inputs or modify them as needed + + @after_kickoff + def after_kickoff_function(self, result): + print(f"After kickoff function with result: {result}") + return result # You can return the result or modify it as needed + + # ... remaining code + ``` + + For example, you can pass the `topic` input to your crew to customize the research and reporting. ```python main.py #!/usr/bin/env python @@ -237,14 +261,14 @@ Follow the steps below to get crewing! 🚣‍♂️ ### Note on Consistency in Naming The names you use in your YAML files (`agents.yaml` and `tasks.yaml`) should match the method names in your Python code. -For example, you can reference the agent for specific tasks from `tasks.yaml` file. +For example, you can reference the agent for specific tasks from `tasks.yaml` file. This naming consistency allows CrewAI to automatically link your configurations with your code; otherwise, your task won't recognize the reference properly. #### Example References Note how we use the same name for the agent in the `agents.yaml` (`email_summarizer`) file as the method name in the `crew.py` (`email_summarizer`) file. - + ```yaml agents.yaml email_summarizer: @@ -281,6 +305,8 @@ Use the annotations to properly reference the agent and task in the `crew.py` fi * `@task` * `@crew` * `@tool` +* `@before_kickoff` +* `@after_kickoff` * `@callback` * `@output_json` * `@output_pydantic` @@ -304,7 +330,7 @@ def email_summarizer_task(self) -> Task: In addition to the [sequential process](../how-to/sequential-process), you can use the [hierarchical process](../how-to/hierarchical-process), -which automatically assigns a manager to the defined crew to properly coordinate the planning and execution of tasks through delegation and validation of results. +which automatically assigns a manager to the defined crew to properly coordinate the planning and execution of tasks through delegation and validation of results. You can learn more about the core concepts [here](/concepts). diff --git a/pyproject.toml b/pyproject.toml index 2dbc00e24..4bd416ab5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "crewai" -version = "0.80.0" +version = "0.83.0" description = "Cutting-edge framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks." readme = "README.md" requires-python = ">=3.10,<=3.13" @@ -29,6 +29,8 @@ dependencies = [ "tomli-w>=1.1.0", "tomli>=2.0.2", "chromadb>=0.5.18", + "pdfplumber>=0.11.4", + "openpyxl>=3.1.5", ] [project.urls] diff --git a/src/crewai/__init__.py b/src/crewai/__init__.py index 6cfa381de..34d7f17c9 100644 --- a/src/crewai/__init__.py +++ b/src/crewai/__init__.py @@ -16,7 +16,7 @@ warnings.filterwarnings( category=UserWarning, module="pydantic.main", ) -__version__ = "0.80.0" +__version__ = "0.83.0" __all__ = [ "Agent", "Crew", diff --git a/src/crewai/cli/templates/crew/crew.py b/src/crewai/cli/templates/crew/crew.py index c47315415..6f8e66c4a 100644 --- a/src/crewai/cli/templates/crew/crew.py +++ b/src/crewai/cli/templates/crew/crew.py @@ -1,5 +1,5 @@ from crewai import Agent, Crew, Process, Task -from crewai.project import CrewBase, agent, crew, task +from crewai.project import CrewBase, agent, crew, task, before_kickoff, after_kickoff # Uncomment the following line to use an example of a custom tool # from {{folder_name}}.tools.custom_tool import MyCustomTool @@ -14,6 +14,18 @@ class {{crew_name}}(): agents_config = 'config/agents.yaml' tasks_config = 'config/tasks.yaml' + @before_kickoff # Optional hook to be executed before the crew starts + def pull_data_example(self, inputs): + # Example of pulling data from an external API, dynamically changing the inputs + inputs['extra_data'] = "This is extra data" + return inputs + + @after_kickoff # Optional hook to be executed after the crew has finished + def log_results(self, output): + # Example of logging results, dynamically changing the output + print(f"Results: {output}") + return output + @agent def researcher(self) -> Agent: return Agent( diff --git a/src/crewai/cli/templates/crew/pyproject.toml b/src/crewai/cli/templates/crew/pyproject.toml index a5ab36877..1e456c725 100644 --- a/src/crewai/cli/templates/crew/pyproject.toml +++ b/src/crewai/cli/templates/crew/pyproject.toml @@ -5,7 +5,7 @@ description = "{{name}} using crewAI" authors = [{ name = "Your Name", email = "you@example.com" }] requires-python = ">=3.10,<=3.13" dependencies = [ - "crewai[tools]>=0.80.0,<1.0.0" + "crewai[tools]>=0.83.0,<1.0.0" ] [project.scripts] diff --git a/src/crewai/cli/templates/flow/pyproject.toml b/src/crewai/cli/templates/flow/pyproject.toml index e863f20b7..575aaf086 100644 --- a/src/crewai/cli/templates/flow/pyproject.toml +++ b/src/crewai/cli/templates/flow/pyproject.toml @@ -5,7 +5,7 @@ description = "{{name}} using crewAI" authors = [{ name = "Your Name", email = "you@example.com" }] requires-python = ">=3.10,<=3.13" dependencies = [ - "crewai[tools]>=0.80.0,<1.0.0", + "crewai[tools]>=0.83.0,<1.0.0", ] [project.scripts] diff --git a/src/crewai/cli/templates/pipeline/pyproject.toml b/src/crewai/cli/templates/pipeline/pyproject.toml index 60294740d..d12dccf11 100644 --- a/src/crewai/cli/templates/pipeline/pyproject.toml +++ b/src/crewai/cli/templates/pipeline/pyproject.toml @@ -6,7 +6,7 @@ authors = ["Your Name "] [tool.poetry.dependencies] python = ">=3.10,<=3.13" -crewai = { extras = ["tools"], version = ">=0.80.0,<1.0.0" } +crewai = { extras = ["tools"], version = ">=0.83.0,<1.0.0" } asyncio = "*" [tool.poetry.scripts] diff --git a/src/crewai/cli/templates/pipeline_router/pyproject.toml b/src/crewai/cli/templates/pipeline_router/pyproject.toml index 7c022b1f7..06487bcfa 100644 --- a/src/crewai/cli/templates/pipeline_router/pyproject.toml +++ b/src/crewai/cli/templates/pipeline_router/pyproject.toml @@ -5,7 +5,7 @@ description = "{{name}} using crewAI" authors = ["Your Name "] requires-python = ">=3.10,<=3.13" dependencies = [ - "crewai[tools]>=0.80.0,<1.0.0" + "crewai[tools]>=0.83.0,<1.0.0" ] [project.scripts] diff --git a/src/crewai/cli/templates/tool/pyproject.toml b/src/crewai/cli/templates/tool/pyproject.toml index 4142c325d..7c1afddfa 100644 --- a/src/crewai/cli/templates/tool/pyproject.toml +++ b/src/crewai/cli/templates/tool/pyproject.toml @@ -5,6 +5,6 @@ description = "Power up your crews with {{folder_name}}" readme = "README.md" requires-python = ">=3.10,<=3.13" dependencies = [ - "crewai[tools]>=0.80.0" + "crewai[tools]>=0.83.0" ] diff --git a/uv.lock b/uv.lock index a4b545d07..baff09ac8 100644 --- a/uv.lock +++ b/uv.lock @@ -608,7 +608,7 @@ wheels = [ [[package]] name = "crewai" -version = "0.80.0" +version = "0.83.0" source = { editable = "." } dependencies = [ { name = "appdirs" }, @@ -622,9 +622,11 @@ dependencies = [ { name = "langchain" }, { name = "litellm" }, { name = "openai" }, + { name = "openpyxl" }, { name = "opentelemetry-api" }, { name = "opentelemetry-exporter-otlp-proto-http" }, { name = "opentelemetry-sdk" }, + { name = "pdfplumber" }, { name = "pydantic" }, { name = "python-dotenv" }, { name = "pyvis" }, @@ -641,6 +643,9 @@ agentops = [ fastembed = [ { name = "fastembed" }, ] +mem0 = [ + { name = "mem0ai" }, +] openpyxl = [ { name = "openpyxl" }, ] @@ -650,9 +655,6 @@ pandas = [ pdfplumber = [ { name = "pdfplumber" }, ] -mem0 = [ - { name = "mem0ai" }, -] tools = [ { name = "crewai-tools" }, ] @@ -694,11 +696,13 @@ requires-dist = [ { name = "litellm", specifier = ">=1.44.22" }, { name = "mem0ai", marker = "extra == 'mem0'", specifier = ">=0.1.29" }, { name = "openai", specifier = ">=1.13.3" }, + { name = "openpyxl", specifier = ">=3.1.5" }, { name = "openpyxl", marker = "extra == 'openpyxl'", specifier = ">=3.1.5" }, { name = "opentelemetry-api", specifier = ">=1.22.0" }, { name = "opentelemetry-exporter-otlp-proto-http", specifier = ">=1.22.0" }, { name = "opentelemetry-sdk", specifier = ">=1.22.0" }, { name = "pandas", marker = "extra == 'pandas'", specifier = ">=2.2.3" }, + { name = "pdfplumber", specifier = ">=0.11.4" }, { name = "pdfplumber", marker = "extra == 'pdfplumber'", specifier = ">=0.11.4" }, { name = "pydantic", specifier = ">=2.4.2" }, { name = "python-dotenv", specifier = ">=1.0.0" }, @@ -952,7 +956,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c1/8b/5fe2cc11fee489817272089c4203e679c63b570a5aaeb18d852ae3cbba6a/et_xmlfile-2.0.0-py3-none-any.whl", hash = "sha256:7a91720bc756843502c3b7504c77b8fe44217c85c537d85037f0f536151b2caa", size = 18059 }, ] - [[package]] name = "exceptiongroup" version = "1.2.2"