RagTool
Description
TheRagTool is designed to answer questions by leveraging the power of Retrieval-Augmented Generation (RAG) through CrewAI’s native RAG system.
It provides a dynamic knowledge base that can be queried to retrieve relevant information from various data sources.
This tool is particularly useful for applications that require access to a vast array of information and need to provide contextually relevant answers.
Example
The following example demonstrates how to initialize the tool and use it with different data sources:Code
Supported Data Sources
TheRagTool can be used with a wide variety of data sources, including:
- 📰 PDF files
- 📊 CSV files
- 📃 JSON files
- 📝 Text
- 📁 Directories/Folders
- 🌐 HTML Web pages
- 📽️ YouTube Channels
- 📺 YouTube Videos
- 📚 Documentation websites
- 📝 MDX files
- 📄 DOCX files
- 🧾 XML files
- 📬 Gmail
- 📝 GitHub repositories
- 🐘 PostgreSQL databases
- 🐬 MySQL databases
- 🤖 Slack conversations
- 💬 Discord messages
- 🗨️ Discourse forums
- 📝 Substack newsletters
- 🐝 Beehiiv content
- 💾 Dropbox files
- 🖼️ Images
- ⚙️ Custom data sources
Parameters
TheRagTool accepts the following parameters:
- summarize: Optional. Whether to summarize the retrieved content. Default is
False. - adapter: Optional. A custom adapter for the knowledge base. If not provided, a CrewAIRagAdapter will be used.
- config: Optional. Configuration for the underlying CrewAI RAG system. Accepts a
RagToolConfigTypedDict with optionalembedding_model(ProviderSpec) andvectordb(VectorDbConfig) keys. All configuration values provided programmatically take precedence over environment variables.
Adding Content
You can add content to the knowledge base using theadd method:
Code
Agent Integration Example
Here’s how to integrate theRagTool with a CrewAI agent:
Code
Advanced Configuration
You can customize the behavior of theRagTool by providing a configuration dictionary:
Code
Embedding Model Configuration
Theembedding_model parameter accepts a crewai.rag.embeddings.types.ProviderSpec dictionary with the structure:
Supported Providers
OpenAI
OpenAI
main.py
api_key(str): OpenAI API keymodel_name(str): Model to use. Default:text-embedding-ada-002. Options:text-embedding-3-small,text-embedding-3-large,text-embedding-ada-002dimensions(int): Number of dimensions for the embeddingorganization_id(str): OpenAI organization IDapi_base(str): Custom API base URLapi_version(str): API versiondefault_headers(dict): Custom headers for API requests
OPENAI_API_KEYorEMBEDDINGS_OPENAI_API_KEY:api_keyOPENAI_ORGANIZATION_IDorEMBEDDINGS_OPENAI_ORGANIZATION_ID:organization_idOPENAI_MODEL_NAMEorEMBEDDINGS_OPENAI_MODEL_NAME:model_nameOPENAI_API_BASEorEMBEDDINGS_OPENAI_API_BASE:api_baseOPENAI_API_VERSIONorEMBEDDINGS_OPENAI_API_VERSION:api_versionOPENAI_DIMENSIONSorEMBEDDINGS_OPENAI_DIMENSIONS:dimensions
Cohere
Cohere
main.py
api_key(str): Cohere API keymodel_name(str): Model to use. Default:large. Options:embed-english-v3.0,embed-multilingual-v3.0,large,small
COHERE_API_KEYorEMBEDDINGS_COHERE_API_KEY:api_keyEMBEDDINGS_COHERE_MODEL_NAME:model_name
VoyageAI
VoyageAI
main.py
api_key(str): VoyageAI API keymodel(str): Model to use. Default:voyage-2. Options:voyage-3,voyage-3-lite,voyage-code-3,voyage-large-2input_type(str): Type of input. Options:document(for storage),query(for search)truncation(bool): Whether to truncate inputs that exceed max length. Default:Trueoutput_dtype(str): Output data typeoutput_dimension(int): Dimension of output embeddingsmax_retries(int): Maximum number of retry attempts. Default:0timeout(float): Request timeout in seconds
VOYAGEAI_API_KEYorEMBEDDINGS_VOYAGEAI_API_KEY:api_keyVOYAGEAI_MODELorEMBEDDINGS_VOYAGEAI_MODEL:modelVOYAGEAI_INPUT_TYPEorEMBEDDINGS_VOYAGEAI_INPUT_TYPE:input_typeVOYAGEAI_TRUNCATIONorEMBEDDINGS_VOYAGEAI_TRUNCATION:truncationVOYAGEAI_OUTPUT_DTYPEorEMBEDDINGS_VOYAGEAI_OUTPUT_DTYPE:output_dtypeVOYAGEAI_OUTPUT_DIMENSIONorEMBEDDINGS_VOYAGEAI_OUTPUT_DIMENSION:output_dimensionVOYAGEAI_MAX_RETRIESorEMBEDDINGS_VOYAGEAI_MAX_RETRIES:max_retriesVOYAGEAI_TIMEOUTorEMBEDDINGS_VOYAGEAI_TIMEOUT:timeout
Ollama
Ollama
main.py
model_name(str): Ollama model name (e.g.,llama2,mistral,nomic-embed-text)url(str): Ollama API endpoint URL. Default:http://localhost:11434/api/embeddings
OLLAMA_MODELorEMBEDDINGS_OLLAMA_MODEL:model_nameOLLAMA_URLorEMBEDDINGS_OLLAMA_URL:url
Amazon Bedrock
Amazon Bedrock
main.py
model_name(str): Bedrock model ID. Default:amazon.titan-embed-text-v1. Options:amazon.titan-embed-text-v1,amazon.titan-embed-text-v2:0,cohere.embed-english-v3,cohere.embed-multilingual-v3session(Any): Boto3 session object for AWS authentication
AWS_ACCESS_KEY_ID: AWS access keyAWS_SECRET_ACCESS_KEY: AWS secret keyAWS_REGION: AWS region (e.g.,us-east-1)
Azure OpenAI
Azure OpenAI
main.py
deployment_id(str): Required - Azure OpenAI deployment IDapi_key(str): Azure OpenAI API keyapi_base(str): Azure OpenAI resource endpointapi_version(str): API version. Example:2024-02-01model_name(str): Model name. Default:text-embedding-ada-002api_type(str): API type. Default:azuredimensions(int): Output dimensionsdefault_headers(dict): Custom headers
AZURE_OPENAI_API_KEYorEMBEDDINGS_AZURE_API_KEY:api_keyAZURE_OPENAI_ENDPOINTorEMBEDDINGS_AZURE_API_BASE:api_baseEMBEDDINGS_AZURE_DEPLOYMENT_ID:deployment_idEMBEDDINGS_AZURE_API_VERSION:api_versionEMBEDDINGS_AZURE_MODEL_NAME:model_nameEMBEDDINGS_AZURE_API_TYPE:api_typeEMBEDDINGS_AZURE_DIMENSIONS:dimensions
Google Generative AI
Google Generative AI
main.py
api_key(str): Google AI API keymodel_name(str): Model name. Default:gemini-embedding-001. Options:gemini-embedding-001,text-embedding-005,text-multilingual-embedding-002task_type(str): Task type for embeddings. Default:RETRIEVAL_DOCUMENT. Options:RETRIEVAL_DOCUMENT,RETRIEVAL_QUERY
GOOGLE_API_KEY,GEMINI_API_KEY, orEMBEDDINGS_GOOGLE_API_KEY:api_keyEMBEDDINGS_GOOGLE_GENERATIVE_AI_MODEL_NAME:model_nameEMBEDDINGS_GOOGLE_GENERATIVE_AI_TASK_TYPE:task_type
Google Vertex AI
Google Vertex AI
main.py
model_name(str): Model name. Default:textembedding-gecko. Options:text-embedding-004,textembedding-gecko,textembedding-gecko-multilingualproject_id(str): Google Cloud project ID. Default:cloud-large-language-modelsregion(str): Google Cloud region. Default:us-central1api_key(str): API key for authentication
GOOGLE_APPLICATION_CREDENTIALS: Path to service account JSON fileGOOGLE_CLOUD_PROJECTorEMBEDDINGS_GOOGLE_VERTEX_PROJECT_ID:project_idEMBEDDINGS_GOOGLE_VERTEX_MODEL_NAME:model_nameEMBEDDINGS_GOOGLE_VERTEX_REGION:regionEMBEDDINGS_GOOGLE_VERTEX_API_KEY:api_key
Jina AI
Jina AI
main.py
api_key(str): Jina AI API keymodel_name(str): Model name. Default:jina-embeddings-v2-base-en. Options:jina-embeddings-v3,jina-embeddings-v2-base-en,jina-embeddings-v2-small-en
JINA_API_KEYorEMBEDDINGS_JINA_API_KEY:api_keyEMBEDDINGS_JINA_MODEL_NAME:model_name
HuggingFace
HuggingFace
main.py
url(str): Full URL to HuggingFace inference API endpoint
HUGGINGFACE_URLorEMBEDDINGS_HUGGINGFACE_URL:url
Instructor
Instructor
main.py
model_name(str): HuggingFace model ID. Default:hkunlp/instructor-base. Options:hkunlp/instructor-xl,hkunlp/instructor-large,hkunlp/instructor-basedevice(str): Device to run on. Default:cpu. Options:cpu,cuda,mpsinstruction(str): Instruction prefix for embeddings
EMBEDDINGS_INSTRUCTOR_MODEL_NAME:model_nameEMBEDDINGS_INSTRUCTOR_DEVICE:deviceEMBEDDINGS_INSTRUCTOR_INSTRUCTION:instruction
Sentence Transformer
Sentence Transformer
main.py
model_name(str): Sentence Transformers model name. Default:all-MiniLM-L6-v2. Options:all-mpnet-base-v2,all-MiniLM-L6-v2,paraphrase-multilingual-MiniLM-L12-v2device(str): Device to run on. Default:cpu. Options:cpu,cuda,mpsnormalize_embeddings(bool): Whether to normalize embeddings. Default:False
EMBEDDINGS_SENTENCE_TRANSFORMER_MODEL_NAME:model_nameEMBEDDINGS_SENTENCE_TRANSFORMER_DEVICE:deviceEMBEDDINGS_SENTENCE_TRANSFORMER_NORMALIZE_EMBEDDINGS:normalize_embeddings
ONNX
ONNX
main.py
preferred_providers(list[str]): List of ONNX execution providers in order of preference
EMBEDDINGS_ONNX_PREFERRED_PROVIDERS:preferred_providers(comma-separated list)
OpenCLIP
OpenCLIP
main.py
model_name(str): OpenCLIP model architecture. Default:ViT-B-32. Options:ViT-B-32,ViT-B-16,ViT-L-14checkpoint(str): Pretrained checkpoint name. Default:laion2b_s34b_b79k. Options:laion2b_s34b_b79k,laion400m_e32,openaidevice(str): Device to run on. Default:cpu. Options:cpu,cuda
EMBEDDINGS_OPENCLIP_MODEL_NAME:model_nameEMBEDDINGS_OPENCLIP_CHECKPOINT:checkpointEMBEDDINGS_OPENCLIP_DEVICE:device
Text2Vec
Text2Vec
main.py
model_name(str): Text2Vec model name from HuggingFace. Default:shibing624/text2vec-base-chinese. Options:shibing624/text2vec-base-multilingual,shibing624/text2vec-base-chinese
EMBEDDINGS_TEXT2VEC_MODEL_NAME:model_name
Roboflow
Roboflow
main.py
api_key(str): Roboflow API key. Default:""(empty string)api_url(str): Roboflow inference API URL. Default:https://infer.roboflow.com
ROBOFLOW_API_KEYorEMBEDDINGS_ROBOFLOW_API_KEY:api_keyROBOFLOW_API_URLorEMBEDDINGS_ROBOFLOW_API_URL:api_url
WatsonX (IBM)
WatsonX (IBM)
main.py
model_id(str): WatsonX model identifierurl(str): WatsonX API endpointapi_key(str): IBM Cloud API keyproject_id(str): WatsonX project IDspace_id(str): WatsonX space ID (alternative to project_id)batch_size(int): Batch size for embeddings. Default:100concurrency_limit(int): Maximum concurrent requests. Default:10persistent_connection(bool): Use persistent connections. Default:True- Plus 20+ additional authentication and configuration options
WATSONX_API_KEYorEMBEDDINGS_WATSONX_API_KEY:api_keyWATSONX_URLorEMBEDDINGS_WATSONX_URL:urlWATSONX_PROJECT_IDorEMBEDDINGS_WATSONX_PROJECT_ID:project_idEMBEDDINGS_WATSONX_MODEL_ID:model_idEMBEDDINGS_WATSONX_SPACE_ID:space_idEMBEDDINGS_WATSONX_BATCH_SIZE:batch_sizeEMBEDDINGS_WATSONX_CONCURRENCY_LIMIT:concurrency_limitEMBEDDINGS_WATSONX_PERSISTENT_CONNECTION:persistent_connection
Custom
Custom
main.py
embedding_callable(type[EmbeddingFunction]): Custom embedding function class
EmbeddingFunction protocol defined in crewai.rag.core.base_embeddings_callable. The __call__ method should accept input data and return embeddings as a list of numpy arrays (or compatible format that will be normalized). The returned embeddings are automatically normalized and validated.Notes
- All config fields are optional unless marked as Required
- API keys can typically be provided via environment variables instead of config
- Default values are shown where applicable
Conclusion
TheRagTool provides a powerful way to create and query knowledge bases from various data sources. By leveraging Retrieval-Augmented Generation, it enables agents to access and retrieve relevant information efficiently, enhancing their ability to provide accurate and contextually appropriate responses.