Advertisement
incomestreamsurfer

crew ai docs

May 30th, 2024
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.82 KB | None | 0 0
  1. What is an Agent?¶
  2. What is an Agent?
  3.  
  4. An agent is an autonomous unit programmed to:
  5.  
  6. Perform tasks
  7. Make decisions
  8. Communicate with other agents
  9.  
  10. Think of an agent as a member of a team, with specific skills and a particular job to do. Agents can have different roles like 'Researcher', 'Writer', or 'Customer Support', each contributing to the overall goal of the crew.
  11. Agent Attributes¶
  12. Attribute Description
  13. Role Defines the agent's function within the crew. It determines the kind of tasks the agent is best suited for.
  14. Goal The individual objective that the agent aims to achieve. It guides the agent's decision-making process.
  15. Backstory Provides context to the agent's role and goal, enriching the interaction and collaboration dynamics.
  16. 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.
  17. 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.
  18. 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.
  19. Max Iter (optional) The maximum number of iterations the agent can perform before being forced to give its best answer. Default is 25.
  20. Max RPM (optional) 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.
  21. max_execution_time (optional) Maximum execution time for an agent to execute a task It's optional and can be left unspecified, with a default value of None, menaning no max execution time
  22. Verbose (optional) Setting this to True configures the internal logger to provide detailed execution logs, aiding in debugging and monitoring. Default is False.
  23. 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.
  24. 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.
  25. Cache (optional) Indicates if the agent should use a cache for tool usage. Default is True.
  26. Creating an Agent¶
  27. Agent Interaction
  28.  
  29. Agents can interact with each other using crewAI's built-in delegation and communication mechanisms. This allows for dynamic task management and problem-solving within the crew.
  30.  
  31. To create an agent, you would typically initialize an instance of the Agent class with the desired properties. Here's a conceptual example including all attributes:
  32.  
  33.  
  34. # Example: Creating an agent with all attributes
  35. from crewai import Agent
  36.  
  37. agent = Agent(
  38. role='Data Analyst',
  39. goal='Extract actionable insights',
  40. backstory="""You're a data analyst at a large company.
  41. You're responsible for analyzing data and providing insights
  42. to the business.
  43. You're currently working on a project to analyze the
  44. performance of our marketing campaigns.""",
  45. tools=[my_tool1, my_tool2], # Optional, defaults to an empty list
  46. llm=my_llm, # Optional
  47. function_calling_llm=my_llm, # Optional
  48. max_iter=15, # Optional
  49. max_rpm=None, # Optional
  50. verbose=True, # Optional
  51. allow_delegation=True, # Optional
  52. step_callback=my_intermediate_step_callback, # Optional
  53. cache=True # Optional
  54. )
  55. Conclusion¶
  56. Agents are the building blocks of the CrewAI framework. By understanding how to define and interact with agents, you can create sophisticated AI systems that leverage the power of collaborative intelligence.
  57.  
  58. Overview of a Task¶
  59. What is a Task?
  60.  
  61. 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.
  62.  
  63. 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.
  64.  
  65. Task Attributes¶
  66. Attribute Description
  67. Description A clear, concise statement of what the task entails.
  68. Agent The agent responsible for the task, assigned either directly or by the crew's process.
  69. Expected Output A detailed description of what the task's completion looks like.
  70. Tools (optional) The functions or capabilities the agent can utilize to perform the task.
  71. Async Execution (optional) If set, the task executes asynchronously, allowing progression without waiting for completion.
  72. Context (optional) Specifies tasks whose outputs are used as context for this task.
  73. Config (optional) Additional configuration details for the agent executing the task, allowing further customization.
  74. Output JSON (optional) Outputs a JSON object, requiring an OpenAI client. Only one output format can be set.
  75. Output Pydantic (optional) Outputs a Pydantic model object, requiring an OpenAI client. Only one output format can be set.
  76. Output File (optional) Saves the task output to a file. If used with Output JSON or Output Pydantic, specifies how the output is saved.
  77. Callback (optional) A Python callable that is executed with the task's output upon completion.
  78. Human Input (optional) Indicates if the task requires human feedback at the end, useful for tasks needing human oversight.
  79. Creating a Task¶
  80. Creating a task involves defining its scope, responsible agent, and any additional attributes for flexibility:
  81.  
  82.  
  83. from crewai import Task
  84.  
  85. task = Task(
  86. description='Find and summarize the latest and most relevant news on AI',
  87. agent=sales_agent
  88. )
  89. Task Assignment
  90.  
  91. Directly specify an agent for assignment or let the hierarchical CrewAI's process decide based on roles, availability, etc.
  92.  
  93. Integrating Tools with Tasks¶
  94. Leverage tools from the crewAI Toolkit and LangChain Tools for enhanced task performance and agent interaction.
  95.  
  96. Creating a Task with Tools¶
  97.  
  98. import os
  99. os.environ["OPENAI_API_KEY"] = "Your Key"
  100. os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key
  101.  
  102. from crewai import Agent, Task, Crew
  103. from crewai_tools import SerperDevTool
  104.  
  105. research_agent = Agent(
  106. role='Researcher',
  107. goal='Find and summarize the latest AI news',
  108. backstory="""You're a researcher at a large company.
  109. You're responsible for analyzing data and providing insights
  110. to the business.""",
  111. verbose=True
  112. )
  113.  
  114. search_tool = SerperDevTool()
  115.  
  116. task = Task(
  117. description='Find and summarize the latest AI news',
  118. expected_output='A bullet list summary of the top 5 most important AI news',
  119. agent=research_agent,
  120. tools=[search_tool]
  121. )
  122.  
  123. crew = Crew(
  124. agents=[research_agent],
  125. tasks=[task],
  126. verbose=2
  127. )
  128.  
  129. result = crew.kickoff()
  130. print(result)
  131. This demonstrates how tasks with specific tools can override an agent's default set for tailored task execution.
  132.  
  133. Referring to Other Tasks¶
  134. In crewAI, the output of one task is automatically relayed into the next one, but you can specifically define what tasks' output, including multiple should be used as context for another task.
  135.  
  136. This is useful when you have a task that depends on the output of another task that is not performed immediately after it. This is done through the context attribute of the task:
  137.  
  138.  
  139. # ...
  140.  
  141. research_ai_task = Task(
  142. description='Find and summarize the latest AI news',
  143. expected_output='A bullet list summary of the top 5 most important AI news',
  144. async_execution=True,
  145. agent=research_agent,
  146. tools=[search_tool]
  147. )
  148.  
  149. research_ops_task = Task(
  150. description='Find and summarize the latest AI Ops news',
  151. expected_output='A bullet list summary of the top 5 most important AI Ops news',
  152. async_execution=True,
  153. agent=research_agent,
  154. tools=[search_tool]
  155. )
  156.  
  157. write_blog_task = Task(
  158. description="Write a full blog post about the importance of AI and its latest news",
  159. expected_output='Full blog post that is 4 paragraphs long',
  160. agent=writer_agent,
  161. context=[research_ai_task, research_ops_task]
  162. )
  163.  
  164. #...
  165. Asynchronous Execution¶
  166. You can define a task to be executed asynchronously. This means that the crew will not wait for it to be completed to continue with the next task. This is useful for tasks that take a long time to be completed, or that are not crucial for the next tasks to be performed.
  167.  
  168. You can then use the context attribute to define in a future task that it should wait for the output of the asynchronous task to be completed.
  169.  
  170.  
  171. #...
  172.  
  173. list_ideas = Task(
  174. description="List of 5 interesting ideas to explore for an article about AI.",
  175. expected_output="Bullet point list of 5 ideas for an article.",
  176. agent=researcher,
  177. async_execution=True # Will be executed asynchronously
  178. )
  179.  
  180. list_important_history = Task(
  181. description="Research the history of AI and give me the 5 most important events.",
  182. expected_output="Bullet point list of 5 important events.",
  183. agent=researcher,
  184. async_execution=True # Will be executed asynchronously
  185. )
  186.  
  187. write_article = Task(
  188. description="Write an article about AI, its history, and interesting ideas.",
  189. expected_output="A 4 paragraph article about AI.",
  190. agent=writer,
  191. context=[list_ideas, list_important_history] # Will wait for the output of the two tasks to be completed
  192. )
  193.  
  194. #...
  195. Callback Mechanism¶
  196. The callback function is executed after the task is completed, allowing for actions or notifications to be triggered based on the task's outcome.
  197.  
  198.  
  199. # ...
  200.  
  201. def callback_function(output: TaskOutput):
  202. # Do something after the task is completed
  203. # Example: Send an email to the manager
  204. print(f"""
  205. Task completed!
  206. Task: {output.description}
  207. Output: {output.raw_output}
  208. """)
  209.  
  210. research_task = Task(
  211. description='Find and summarize the latest AI news',
  212. expected_output='A bullet list summary of the top 5 most important AI news',
  213. agent=research_agent,
  214. tools=[search_tool],
  215. callback=callback_function
  216. )
  217.  
  218. #...
  219. Accessing a Specific Task Output¶
  220. Once a crew finishes running, you can access the output of a specific task by using the output attribute of the task object:
  221.  
  222.  
  223. # ...
  224. task1 = Task(
  225. description='Find and summarize the latest AI news',
  226. expected_output='A bullet list summary of the top 5 most important AI news',
  227. agent=research_agent,
  228. tools=[search_tool]
  229. )
  230.  
  231. #...
  232.  
  233. crew = Crew(
  234. agents=[research_agent],
  235. tasks=[task1, task2, task3],
  236. verbose=2
  237. )
  238.  
  239. result = crew.kickoff()
  240.  
  241. # Returns a TaskOutput object with the description and results of the task
  242. print(f"""
  243. Task completed!
  244. Task: {task1.output.description}
  245. Output: {task1.output.raw_output}
  246. """)
  247. Tool Override Mechanism¶
  248. Specifying tools in a task allows for dynamic adaptation of agent capabilities, emphasizing CrewAI's flexibility.
  249.  
  250. Error Handling and Validation Mechanisms¶
  251. While creating and executing tasks, certain validation mechanisms are in place to ensure the robustness and reliability of task attributes. These include but are not limited to:
  252.  
  253. Ensuring only one output type is set per task to maintain clear output expectations.
  254. Preventing the manual assignment of the id attribute to uphold the integrity of the unique identifier system.
  255. These validations help in maintaining the consistency and reliability of task executions within the crewAI framework.
  256.  
  257. Conclusion¶
  258. Tasks are the driving force behind the actions of agents in crewAI. By properly defining tasks and their outcomes, you set the stage for your AI agents to work effectively, either independently or as a collaborative unit. Equipping tasks with appropriate tools, understanding the execution process, and following robust validation practices are crucial for maximizing CrewAI's potential, ensuring agents are effectively prepared for their assignments and that tasks are executed as intended.
  259.  
  260. Tools
  261. Introduction¶
  262. CrewAI tools empower agents with capabilities ranging from web searching and data analysis to collaboration and delegating tasks among coworkers. This documentation outlines how to create, integrate, and leverage these tools within the CrewAI framework, including a new focus on collaboration tools.
  263.  
  264. What is a Tool?¶
  265. Definition
  266.  
  267. A tool in CrewAI is a skill or function that agents can utilize to perform various actions. This includes tools from the crewAI Toolkit and LangChain Tools, enabling everything from simple searches to complex interactions and effective teamwork among agents.
  268.  
  269. Key Characteristics of Tools¶
  270. Utility: Crafted for tasks such as web searching, data analysis, content generation, and agent collaboration.
  271. Integration: Boosts agent capabilities by seamlessly integrating tools into their workflow.
  272. Customizability: Provides the flexibility to develop custom tools or utilize existing ones, catering to the specific needs of agents.
  273. Error Handling: Incorporates robust error handling mechanisms to ensure smooth operation.
  274. Caching Mechanism: Features intelligent caching to optimize performance and reduce redundant operations.
  275. Using crewAI Tools¶
  276. To enhance your agents' capabilities with crewAI tools, begin by installing our extra tools package:
  277.  
  278.  
  279. pip install 'crewai[tools]'
  280. Here's an example demonstrating their use:
  281.  
  282.  
  283. import os
  284. from crewai import Agent, Task, Crew
  285. # Importing crewAI tools
  286. from crewai_tools import (
  287. DirectoryReadTool,
  288. FileReadTool,
  289. SerperDevTool,
  290. WebsiteSearchTool
  291. )
  292.  
  293. # Set up API keys
  294. os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key
  295. os.environ["OPENAI_API_KEY"] = "Your Key"
  296.  
  297. # Instantiate tools
  298. docs_tool = DirectoryReadTool(directory='./blog-posts')
  299. file_tool = FileReadTool()
  300. search_tool = SerperDevTool()
  301. web_rag_tool = WebsiteSearchTool()
  302.  
  303. # Create agents
  304. researcher = Agent(
  305. role='Market Research Analyst',
  306. goal='Provide up-to-date market analysis of the AI industry',
  307. backstory='An expert analyst with a keen eye for market trends.',
  308. tools=[search_tool, web_rag_tool],
  309. verbose=True
  310. )
  311.  
  312. writer = Agent(
  313. role='Content Writer',
  314. goal='Craft engaging blog posts about the AI industry',
  315. backstory='A skilled writer with a passion for technology.',
  316. tools=[docs_tool, file_tool],
  317. verbose=True
  318. )
  319.  
  320. # Define tasks
  321. research = Task(
  322. description='Research the latest trends in the AI industry and provide a summary.',
  323. expected_output='A summary of the top 3 trending developments in the AI industry with a unique perspective on their significance.',
  324. agent=researcher
  325. )
  326.  
  327. write = Task(
  328. description='Write an engaging blog post about the AI industry, based on the research analyst’s summary. Draw inspiration from the latest blog posts in the directory.',
  329. expected_output='A 4-paragraph blog post formatted in markdown with engaging, informative, and accessible content, avoiding complex jargon.',
  330. agent=writer,
  331. output_file='blog-posts/new_post.md' # The final blog post will be saved here
  332. )
  333.  
  334. # Assemble a crew
  335. crew = Crew(
  336. agents=[researcher, writer],
  337. tasks=[research, write],
  338. verbose=2
  339. )
  340.  
  341. # Execute tasks
  342. crew.kickoff()
  343. Available crewAI Tools¶
  344. Error Handling: All tools are built with error handling capabilities, allowing agents to gracefully manage exceptions and continue their tasks.
  345. Caching Mechanism: All tools support caching, enabling agents to efficiently reuse previously obtained results, reducing the load on external resources and speeding up the execution time, you can also define finner control over the caching mechanism, using cache_function attribute on the tool.
  346. Here is a list of the available tools and their descriptions:
  347.  
  348. Tool Description
  349. CodeDocsSearchTool A RAG tool optimized for searching through code documentation and related technical documents.
  350. CSVSearchTool A RAG tool designed for searching within CSV files, tailored to handle structured data.
  351. DirectorySearchTool A RAG tool for searching within directories, useful for navigating through file systems.
  352. DOCXSearchTool A RAG tool aimed at searching within DOCX documents, ideal for processing Word files.
  353. DirectoryReadTool Facilitates reading and processing of directory structures and their contents.
  354. FileReadTool Enables reading and extracting data from files, supporting various file formats.
  355. GithubSearchTool A RAG tool for searching within GitHub repositories, useful for code and documentation search.
  356. SerperDevTool A specialized tool for development purposes, with specific functionalities under development.
  357. TXTSearchTool A RAG tool focused on searching within text (.txt) files, suitable for unstructured data.
  358. JSONSearchTool A RAG tool designed for searching within JSON files, catering to structured data handling.
  359. MDXSearchTool A RAG tool tailored for searching within Markdown (MDX) files, useful for documentation.
  360. PDFSearchTool A RAG tool aimed at searching within PDF documents, ideal for processing scanned documents.
  361. PGSearchTool A RAG tool optimized for searching within PostgreSQL databases, suitable for database queries.
  362. RagTool A general-purpose RAG tool capable of handling various data sources and types.
  363. ScrapeElementFromWebsiteTool Enables scraping specific elements from websites, useful for targeted data extraction.
  364. ScrapeWebsiteTool Facilitates scraping entire websites, ideal for comprehensive data collection.
  365. WebsiteSearchTool A RAG tool for searching website content, optimized for web data extraction.
  366. XMLSearchTool A RAG tool designed for searching within XML files, suitable for structured data formats.
  367. YoutubeChannelSearchTool A RAG tool for searching within YouTube channels, useful for video content analysis.
  368. YoutubeVideoSearchTool A RAG tool aimed at searching within YouTube videos, ideal for video data extraction.
  369. Creating your own Tools¶
  370. Custom Tool Creation
  371.  
  372. Developers can craft custom tools tailored for their agent’s needs or utilize pre-built options:
  373.  
  374. To create your own crewAI tools you will need to install our extra tools package:
  375.  
  376.  
  377. pip install 'crewai[tools]'
  378. Once you do that there are two main ways for one to create a crewAI tool:
  379.  
  380. Subclassing BaseTool¶
  381.  
  382. from crewai_tools import BaseTool
  383.  
  384. class MyCustomTool(BaseTool):
  385. name: str = "Name of my tool"
  386. description: str = "Clear description for what this tool is useful for, you agent will need this information to use it."
  387.  
  388. def _run(self, argument: str) -> str:
  389. # Implementation goes here
  390. return "Result from custom tool"
  391. Utilizing the tool Decorator¶
  392.  
  393. from crewai_tools import tool
  394. @tool("Name of my tool")
  395. def my_tool(question: str) -> str:
  396. """Clear description for what this tool is useful for, you agent will need this information to use it."""
  397. # Function logic here
  398. return "Result from your custom tool"
  399. Custom Caching Mechanism¶
  400. Caching
  401.  
  402. Tools can optionally implement a cache_function to fine-tune caching behavior. This function determines when to cache results based on specific conditions, offering granular control over caching logic.
  403.  
  404.  
  405. from crewai_tools import tool
  406.  
  407. @tool
  408. def multiplication_tool(first_number: int, second_number: int) -> str:
  409. """Useful for when you need to multiply two numbers together."""
  410. return first_number * second_number
  411.  
  412. def cache_func(args, result):
  413. # In this case, we only cache the result if it's a multiple of 2
  414. cache = result % 2 == 0
  415. return cache
  416.  
  417. multiplication_tool.cache_function = cache_func
  418.  
  419. writer1 = Agent(
  420. role="Writer",
  421. goal="You write lesssons of math for kids.",
  422. backstory="You're an expert in writting and you love to teach kids but you know nothing of math.",
  423. tools=[multiplcation_tool],
  424. allow_delegation=False,
  425. )
  426. #...
  427. Using LangChain Tools¶
  428. LangChain Integration
  429.  
  430. CrewAI seamlessly integrates with LangChain’s comprehensive toolkit for search-based queries and more, here are the available built-in tools that are offered by Langchain LangChain Toolkit
  431.  
  432. :
  433.  
  434.  
  435. from crewai import Agent
  436. from langchain.agents import Tool
  437. from langchain.utilities import GoogleSerperAPIWrapper
  438.  
  439. # Setup API keys
  440. os.environ["SERPER_API_KEY"] = "Your Key"
  441.  
  442. search = GoogleSerperAPIWrapper()
  443.  
  444. # Create and assign the search tool to an agent
  445. serper_tool = Tool(
  446. name="Intermediate Answer",
  447. func=search.run,
  448. description="Useful for search-based queries",
  449. )
  450.  
  451. agent = Agent(
  452. role='Research Analyst',
  453. goal='Provide up-to-date market analysis',
  454. backstory='An expert analyst with a keen eye for market trends.',
  455. tools=[serper_tool]
  456. )
  457.  
  458. # rest of the code ...
  459. Conclusion¶
  460. Tools are pivotal in extending the capabilities of CrewAI agents, enabling them to undertake a broad spectrum of tasks and collaborate effectively. When building solutions with CrewAI, leverage both custom and existing tools to empower your agents and enhance the AI ecosystem. Consider utilizing error handling, caching mechanisms, and the flexibility of tool arguments to optimize your agents' performance and capabilities.
  461.  
  462. Processes
  463. Understanding Processes¶
  464. Core Concept
  465.  
  466. In CrewAI, processes orchestrate the execution of tasks by agents, akin to project management in human teams. These processes ensure tasks are distributed and executed efficiently, in alignment with a predefined strategy.
  467.  
  468. Process Implementations¶
  469. Sequential: Executes tasks sequentially, ensuring tasks are completed in an orderly progression.
  470. Hierarchical: Organizes tasks in a managerial hierarchy, where tasks are delegated and executed based on a structured chain of command. A manager language model (manager_llm) must be specified in the crew to enable the hierarchical process, facilitating the creation and management of tasks by the manager.
  471. Consensual Process (Planned): Aiming for collaborative decision-making among agents on task execution, this process type introduces a democratic approach to task management within CrewAI. It is planned for future development and is not currently implemented in the codebase.
  472. The Role of Processes in Teamwork¶
  473. Processes enable individual agents to operate as a cohesive unit, streamlining their efforts to achieve common objectives with efficiency and coherence.
  474.  
  475. Assigning Processes to a Crew¶
  476. To assign a process to a crew, specify the process type upon crew creation to set the execution strategy. For a hierarchical process, ensure to define manager_llm for the manager agent.
  477.  
  478.  
  479. from crewai import Crew
  480. from crewai.process import Process
  481. from langchain_openai import ChatOpenAI
  482.  
  483. # Example: Creating a crew with a sequential process
  484. crew = Crew(
  485. agents=my_agents,
  486. tasks=my_tasks,
  487. process=Process.sequential
  488. )
  489.  
  490. # Example: Creating a crew with a hierarchical process
  491. # Ensure to provide a manager_llm
  492. crew = Crew(
  493. agents=my_agents,
  494. tasks=my_tasks,
  495. process=Process.hierarchical,
  496. manager_llm=ChatOpenAI(model="gpt-4")
  497. )
  498. Note: Ensure my_agents and my_tasks are defined prior to creating a Crew object, and for the hierarchical process, manager_llm is also required.
  499. Sequential Process¶
  500. This method mirrors dynamic team workflows, progressing through tasks in a thoughtful and systematic manner. Task execution follows the predefined order in the task list, with the output of one task serving as context for the next.
  501.  
  502. To customize task context, utilize the context parameter in the Task class to specify outputs that should be used as context for subsequent tasks.
  503.  
  504. Hierarchical Process¶
  505. Emulates a corporate hierarchy, CrewAI automatically creates a manager for you, requiring the specification of a manager language model (manager_llm) for the manager agent. This agent oversees task execution, including planning, delegation, and validation. Tasks are not pre-assigned; the manager allocates tasks to agents based on their capabilities, reviews outputs, and assesses task completion.
  506.  
  507. Process Class: Detailed Overview¶
  508. The Process class is implemented as an enumeration (Enum), ensuring type safety and restricting process values to the defined types (sequential, hierarchical). The consensual process is planned for future inclusion, emphasizing our commitment to continuous development and innovation.
  509.  
  510. Additional Task Features¶
  511. Asynchronous Execution: Tasks can now be executed asynchronously, allowing for parallel processing and efficiency improvements. This feature is designed to enable tasks to be carried out concurrently, enhancing the overall productivity of the crew.
  512. Human Input Review: An optional feature that enables the review of task outputs by humans to ensure quality and accuracy before finalization. This additional step introduces a layer of oversight, providing an opportunity for human intervention and validation.
  513. Output Customization: Tasks support various output formats, including JSON (output_json), Pydantic models (output_pydantic), and file outputs (output_file), providing flexibility in how task results are captured and utilized. This allows for a wide range of output possibilities, catering to different needs and requirements.
  514. Conclusion¶
  515. The structured collaboration facilitated by processes within CrewAI is crucial for enabling systematic teamwork among agents. This documentation has been updated to reflect the latest features, enhancements, and the planned integration of the Consensual Process, ensuring users have access to the most current and comprehensive information.
  516.  
  517. Previous
  518. Tools
  519. Next
  520.  
  521. Memory
  522. Introduction to Memory Systems in crewAI¶
  523. Enhancing Agent Intelligence
  524.  
  525. The crewAI framework introduces a sophisticated memory system designed to significantly enhance the capabilities of AI agents. This system comprises short-term memory, long-term memory, entity memory, and newly identified contextual memory, each serving a unique purpose in aiding agents to remember, reason, and learn from past interactions.
  526.  
  527. Memory System Components¶
  528. Component Description
  529. Short-Term Memory Temporarily stores recent interactions and outcomes, enabling agents to recall and utilize information relevant to their current context.
  530. Long-Term Memory Preserves valuable insights and learnings from past executions, allowing agents to build and refine their knowledge over time.
  531. Entity Memory Captures and organizes information about entities (people, places, concepts) encountered during tasks, facilitating deeper understanding and relationship mapping.
  532. Contextual Memory Maintains the context of interactions, aiding in the coherence and relevance of agent responses over a sequence of tasks or a conversation.
  533. How Memory Systems Empower Agents¶
  534. Contextual Awareness: With short-term and contextual memory, agents gain the ability to maintain context over a conversation or task sequence, leading to more coherent and relevant responses.
  535.  
  536. Experience Accumulation: Long-term memory allows agents to accumulate experiences, learning from past actions to improve future decision-making and problem-solving.
  537.  
  538. Entity Understanding: By maintaining entity memory, agents can recognize and remember key entities, enhancing their ability to process and interact with complex information.
  539.  
  540. Implementing Memory in Your Crew¶
  541. When configuring a crew, you can enable and customize each memory component to suit the crew's objectives and the nature of tasks it will perform. By default, the memory system is disabled, and you can ensure it is active by setting memory=True in the crew configuration. The memory will use OpenAI Embeddings by default, but you can change it by setting embedder to a different model.
  542.  
  543. Example: Configuring Memory for a Crew¶
  544.  
  545. from crewai import Crew, Agent, Task, Process
  546.  
  547. # Assemble your crew with memory capabilities
  548. my_crew = Crew(
  549. agents=[...],
  550. tasks=[...],
  551. process=Process.sequential,
  552. memory=True,
  553. verbose=True
  554. )
  555. Additional Embedding Providers¶
  556. Using OpenAI embeddings (already default)¶
  557.  
  558. from crewai import Crew, Agent, Task, Process
  559.  
  560. my_crew = Crew(
  561. agents=[...],
  562. tasks=[...],
  563. process=Process.sequential,
  564. memory=True,
  565. verbose=True,
  566. embedder={
  567. "provider": "openai",
  568. "config":{
  569. "model": 'text-embedding-3-small'
  570. }
  571. }
  572. )
  573. Using Google AI embeddings¶
  574.  
  575. from crewai import Crew, Agent, Task, Process
  576.  
  577. my_crew = Crew(
  578. agents=[...],
  579. tasks=[...],
  580. process=Process.sequential,
  581. memory=True,
  582. verbose=True,
  583. embedder={
  584. "provider": "google",
  585. "config":{
  586. "model": 'models/embedding-001',
  587. "task_type": "retrieval_document",
  588. "title": "Embeddings for Embedchain"
  589. }
  590. }
  591. )
  592. Using Azure OpenAI embeddings¶
  593.  
  594. from crewai import Crew, Agent, Task, Process
  595.  
  596. my_crew = Crew(
  597. agents=[...],
  598. tasks=[...],
  599. process=Process.sequential,
  600. memory=True,
  601. verbose=True,
  602. embedder={
  603. "provider": "azure_openai",
  604. "config":{
  605. "model": 'text-embedding-ada-002',
  606. "deployment_name": "you_embedding_model_deployment_name"
  607. }
  608. }
  609. )
  610. Using GPT4ALL embeddings¶
  611.  
  612. from crewai import Crew, Agent, Task, Process
  613.  
  614. my_crew = Crew(
  615. agents=[...],
  616. tasks=[...],
  617. process=Process.sequential,
  618. memory=True,
  619. verbose=True,
  620. embedder={
  621. "provider": "gpt4all"
  622. }
  623. )
  624. Using Vertex AI embeddings¶
  625.  
  626. from crewai import Crew, Agent, Task, Process
  627.  
  628. my_crew = Crew(
  629. agents=[...],
  630. tasks=[...],
  631. process=Process.sequential,
  632. memory=True,
  633. verbose=True,
  634. embedder={
  635. "provider": "vertexai",
  636. "config":{
  637. "model": 'textembedding-gecko'
  638. }
  639. }
  640. )
  641. Using Cohere embeddings¶
  642.  
  643. from crewai import Crew, Agent, Task, Process
  644.  
  645. my_crew = Crew(
  646. agents=[...],
  647. tasks=[...],
  648. process=Process.sequential,
  649. memory=True,
  650. verbose=True,
  651. embedder={
  652. "provider": "cohere",
  653. "config":{
  654. "model": "embed-english-v3.0"
  655. "vector_dimension": 1024
  656. }
  657. }
  658. )
  659. Benefits of Using crewAI's Memory System¶
  660. Adaptive Learning: Crews become more efficient over time, adapting to new information and refining their approach to tasks.
  661. Enhanced Personalization: Memory enables agents to remember user preferences and historical interactions, leading to personalized experiences.
  662. Improved Problem Solving: Access to a rich memory store aids agents in making more informed decisions, drawing on past learnings and contextual insights.
  663. Getting Started¶
  664. Integrating crewAI's memory system into your projects is straightforward. By leveraging the provided memory components and configurations, you can quickly empower your agents with the ability to remember, reason, and learn from their interactions, unlocking new levels of intelligence and capability.
  665.  
  666. Customizing Agents
  667. Customizable Attributes¶
  668. Crafting an efficient CrewAI team hinges on the ability to dynamically tailor your AI agents to meet the unique requirements of any project. This section covers the foundational attributes you can customize.
  669.  
  670. Key Attributes for Customization¶
  671. Role: Specifies the agent's job within the crew, such as 'Analyst' or 'Customer Service Rep'.
  672. Goal: Defines what the agent aims to achieve, in alignment with its role and the overarching objectives of the crew.
  673. Backstory: Provides depth to the agent's persona, enriching its motivations and engagements within the crew.
  674. Tools: Represents the capabilities or methods the agent uses to perform tasks, from simple functions to intricate integrations.
  675. Advanced Customization Options¶
  676. Beyond the basic attributes, CrewAI allows for deeper customization to enhance an agent's behavior and capabilities significantly.
  677.  
  678. Language Model Customization¶
  679. Agents can be customized with specific language models (llm) and function-calling language models (function_calling_llm), offering advanced control over their processing and decision-making abilities. It's important to note that setting the function_calling_llm allows for overriding the default crew function-calling language model, providing a greater degree of customization.
  680.  
  681. Performance and Debugging Settings¶
  682. Adjusting an agent's performance and monitoring its operations are crucial for efficient task execution.
  683.  
  684. Verbose Mode and RPM Limit¶
  685. Verbose Mode: Enables detailed logging of an agent's actions, useful for debugging and optimization. Specifically, it provides insights into agent execution processes, aiding in the optimization of performance.
  686. RPM Limit: Sets the maximum number of requests per minute (max_rpm). This attribute is optional and can be set to None for no limit, allowing for unlimited queries to external services if needed.
  687. Maximum Iterations for Task Execution¶
  688. The max_iter attribute allows users to define the maximum number of iterations an agent can perform for a single task, preventing infinite loops or excessively long executions. The default value is set to 15, providing a balance between thoroughness and efficiency. Once the agent approaches this number, it will try its best to give a good answer.
  689.  
  690. Customizing Agents and Tools¶
  691. Agents are customized by defining their attributes and tools during initialization. Tools are critical for an agent's functionality, enabling them to perform specialized tasks. The tools attribute should be an array of tools the agent can utilize, and it's initialized as an empty list by default. Tools can be added or modified post-agent initialization to adapt to new requirements.
  692.  
  693.  
  694. pip install 'crewai[tools]'
  695. Example: Assigning Tools to an Agent¶
  696.  
  697. import os
  698. from crewai import Agent
  699. from crewai_tools import SerperDevTool
  700.  
  701. # Set API keys for tool initialization
  702. os.environ["OPENAI_API_KEY"] = "Your Key"
  703. os.environ["SERPER_API_KEY"] = "Your Key"
  704.  
  705. # Initialize a search tool
  706. search_tool = SerperDevTool()
  707.  
  708. # Initialize the agent with advanced options
  709. agent = Agent(
  710. role='Research Analyst',
  711. goal='Provide up-to-date market analysis',
  712. backstory='An expert analyst with a keen eye for market trends.',
  713. tools=[search_tool],
  714. memory=True, # Enable memory
  715. verbose=True,
  716. max_rpm=None, # No limit on requests per minute
  717. max_iter=15, # Default value for maximum iterations
  718. allow_delegation=False
  719. )
  720. Delegation and Autonomy¶
  721. Controlling an agent's ability to delegate tasks or ask questions is vital for tailoring its autonomy and collaborative dynamics within the CrewAI framework. By default, the allow_delegation attribute is set to True, enabling agents to seek assistance or delegate tasks as needed. This default behavior promotes collaborative problem-solving and efficiency within the CrewAI ecosystem. If needed, delegation can be disabled to suit specific operational requirements.
  722.  
  723. Example: Disabling Delegation for an Agent¶
  724.  
  725. agent = Agent(
  726. role='Content Writer',
  727. goal='Write engaging content on market trends',
  728. backstory='A seasoned writer with expertise in market analysis.',
  729. allow_delegation=False # Disabling delegation
  730. )
  731. Conclusion¶
  732. Customizing agents in CrewAI by setting their roles, goals, backstories, and tools, alongside advanced options like language model customization, memory, performance settings, and delegation preferences, equips a nuanced and capable AI team ready for complex challenges.
  733.  
  734. Getting Started
  735. Introduction¶
  736. Embark on your CrewAI journey by setting up your environment and initiating your AI crew with the latest features. This guide ensures a smooth start, incorporating all recent updates for an enhanced experience.
  737.  
  738. Step 0: Installation¶
  739. Install CrewAI and any necessary packages for your project. CrewAI is compatible with Python >=3.10,<=3.13.
  740.  
  741.  
  742. pip install crewai
  743. pip install 'crewai[tools]'
  744. Step 1: Assemble Your Agents¶
  745. Define your agents with distinct roles, backstories, and enhanced capabilities like verbose mode and memory usage. These elements add depth and guide their task execution and interaction within the crew.
  746.  
  747.  
  748. import os
  749. os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key
  750. os.environ["OPENAI_API_KEY"] = "Your Key"
  751.  
  752. from crewai import Agent
  753. from crewai_tools import SerperDevTool
  754. search_tool = SerperDevTool()
  755.  
  756. # Creating a senior researcher agent with memory and verbose mode
  757. researcher = Agent(
  758. role='Senior Researcher',
  759. goal='Uncover groundbreaking technologies in {topic}',
  760. verbose=True,
  761. memory=True,
  762. backstory=(
  763. "Driven by curiosity, you're at the forefront of"
  764. "innovation, eager to explore and share knowledge that could change"
  765. "the world."
  766. ),
  767. tools=[search_tool],
  768. allow_delegation=True
  769. )
  770.  
  771. # Creating a writer agent with custom tools and delegation capability
  772. writer = Agent(
  773. role='Writer',
  774. goal='Narrate compelling tech stories about {topic}',
  775. verbose=True,
  776. memory=True,
  777. backstory=(
  778. "With a flair for simplifying complex topics, you craft"
  779. "engaging narratives that captivate and educate, bringing new"
  780. "discoveries to light in an accessible manner."
  781. ),
  782. tools=[search_tool],
  783. allow_delegation=False
  784. )
  785. Step 2: Define the Tasks¶
  786. Detail the specific objectives for your agents, including new features for asynchronous execution and output customization. These tasks ensure a targeted approach to their roles.
  787.  
  788.  
  789. from crewai import Task
  790.  
  791. # Research task
  792. research_task = Task(
  793. description=(
  794. "Identify the next big trend in {topic}."
  795. "Focus on identifying pros and cons and the overall narrative."
  796. "Your final report should clearly articulate the key points,"
  797. "its market opportunities, and potential risks."
  798. ),
  799. expected_output='A comprehensive 3 paragraphs long report on the latest AI trends.',
  800. tools=[search_tool],
  801. agent=researcher,
  802. )
  803.  
  804. # Writing task with language model configuration
  805. write_task = Task(
  806. description=(
  807. "Compose an insightful article on {topic}."
  808. "Focus on the latest trends and how it's impacting the industry."
  809. "This article should be easy to understand, engaging, and positive."
  810. ),
  811. expected_output='A 4 paragraph article on {topic} advancements formatted as markdown.',
  812. tools=[search_tool],
  813. agent=writer,
  814. async_execution=False,
  815. output_file='new-blog-post.md' # Example of output customization
  816. )
  817. Step 3: Form the Crew¶
  818. Combine your agents into a crew, setting the workflow process they'll follow to accomplish the tasks. Now with options to configure language models for enhanced interaction and additional configurations for optimizing performance.
  819.  
  820.  
  821. from crewai import Crew, Process
  822.  
  823. # Forming the tech-focused crew with some enhanced configurations
  824. crew = Crew(
  825. agents=[researcher, writer],
  826. tasks=[research_task, write_task],
  827. process=Process.sequential, # Optional: Sequential task execution is default
  828. memory=True,
  829. cache=True,
  830. max_rpm=100,
  831. share_crew=True
  832. )
  833. Step 4: Kick It Off¶
  834. Initiate the process with your enhanced crew ready. Observe as your agents collaborate, leveraging their new capabilities for a successful project outcome. Input variables will be interpolated into the agents and tasks for a personalized approach.
  835.  
  836.  
  837. # Starting the task execution process with enhanced feedback
  838. result = crew.kickoff(inputs={'topic': 'AI in healthcare'})
  839. print(result)
  840. Conclusion¶
  841. Building and activating a crew in CrewAI has evolved with new functionalities. By incorporating verbose mode, memory capabilities, asynchronous task execution, output customization, language model configuration, and enhanced crew configurations, your AI team is more equipped than ever to tackle challenges efficiently. The depth of agent backstories and the precision of their objectives enrich collaboration, leading to successful project outcomes. This guide aims to provide you with a clear and detailed understanding of setting up and utilizing the CrewAI framework to its full potential.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement