Knowledge
What is knowledge in CrewAI and how to use it.
Using Knowledge in CrewAI
What is Knowledge?
Knowledge in CrewAI is a powerful system that allows AI agents to access and utilize external information sources during their tasks. Think of it as giving your agents a reference library they can consult while working.
Key benefits of using Knowledge:
- Enhance agents with domain-specific information
- Support decisions with real-world data
- Maintain context across conversations
- Ground responses in factual information
Supported Knowledge Sources
CrewAI supports various types of knowledge sources out of the box:
Text Sources
- Raw strings
- Text files (.txt)
- PDF documents
Structured Data
- CSV files
- Excel spreadsheets
- JSON documents
Quick Start
Here’s an example using string-based knowledge:
Knowledge Configuration
Chunking Configuration
Control how content is split for processing by setting the chunk size and overlap.
Embedder Configuration
You can also configure the embedder for the knowledge store. This is useful if you want to use a different embedder for the knowledge store than the one used for the agents.
Clearing Knowledge
If you need to clear the knowledge stored in CrewAI, you can use the crewai reset-memories
command with the --knowledge
option.
This is useful when you’ve updated your knowledge sources and want to ensure that the agents are using the most recent information.
Custom Knowledge Sources
CrewAI allows you to create custom knowledge sources for any type of data by extending the BaseKnowledgeSource
class. Let’s create a practical example that fetches and processes space news articles.
Space News Knowledge Source Example
Key Components Explained
-
Custom Knowledge Source (
SpaceNewsKnowledgeSource
):- Extends
BaseKnowledgeSource
for integration with CrewAI - Configurable API endpoint and article limit
- Implements three key methods:
load_content()
: Fetches articles from the API_format_articles()
: Structures the articles into readable textadd()
: Processes and stores the content
- Extends
-
Agent Configuration:
- Specialized role as a Space News Analyst
- Uses the knowledge source to access space news
-
Task Setup:
- Takes a user question as input through
{user_question}
- Designed to provide detailed answers based on the knowledge source
- Takes a user question as input through
-
Crew Orchestration:
- Manages the workflow between agent and task
- Handles input/output through the kickoff method
This example demonstrates how to:
- Create a custom knowledge source that fetches real-time data
- Process and format external data for AI consumption
- Use the knowledge source to answer specific user questions
- Integrate everything seamlessly with CrewAI’s agent system
About the Spaceflight News API
The example uses the Spaceflight News API, which:
- Provides free access to space-related news articles
- Requires no authentication
- Returns structured data about space news
- Supports pagination and filtering
You can customize the API query by modifying the endpoint URL: