mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 07:38:29 +00:00
Merge pull request #168 from MinuraPunchihewa/feature/ai_minds_tool
Added the MindsDB AIMind Tool
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from .tools import (
|
||||
AIMindTool,
|
||||
BraveSearchTool,
|
||||
BrowserbaseLoadTool,
|
||||
CodeDocsSearchTool,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from .ai_mind_tool.ai_mind_tool import AIMindTool
|
||||
from .brave_search_tool.brave_search_tool import BraveSearchTool
|
||||
from .browserbase_load_tool.browserbase_load_tool import BrowserbaseLoadTool
|
||||
from .code_docs_search_tool.code_docs_search_tool import CodeDocsSearchTool
|
||||
|
||||
79
src/crewai_tools/tools/ai_mind_tool/README.md
Normal file
79
src/crewai_tools/tools/ai_mind_tool/README.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# AIMind Tool
|
||||
|
||||
## Description
|
||||
|
||||
[Minds](https://mindsdb.com/minds) are AI systems provided by [MindsDB](https://mindsdb.com/) that work similarly to large language models (LLMs) but go beyond by answering any question from any data.
|
||||
|
||||
This is accomplished by selecting the most relevant data for an answer using parametric search, understanding the meaning and providing responses within the correct context through semantic search, and finally, delivering precise answers by analyzing data and using machine learning (ML) models.
|
||||
|
||||
The `AIMindTool` can be used to query data sources in natural language by simply configuring their connection parameters.
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install the `crewai[tools]` package:
|
||||
|
||||
```shell
|
||||
pip install 'crewai[tools]'
|
||||
```
|
||||
|
||||
2. Install the Minds SDK:
|
||||
|
||||
```shell
|
||||
pip install minds-sdk
|
||||
```
|
||||
|
||||
3. Sign for a Minds account [here](https://mdb.ai/register), and obtain an API key.
|
||||
|
||||
4. Set the Minds API key in an environment variable named `MINDS_API_KEY`.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
from crewai_tools import AIMindTool
|
||||
|
||||
|
||||
# Initialize the AIMindTool.
|
||||
aimind_tool = AIMindTool(
|
||||
datasources=[
|
||||
{
|
||||
"description": "house sales data",
|
||||
"engine": "postgres",
|
||||
"connection_data": {
|
||||
"user": "demo_user",
|
||||
"password": "demo_password",
|
||||
"host": "samples.mindsdb.com",
|
||||
"port": 5432,
|
||||
"database": "demo",
|
||||
"schema": "demo_data"
|
||||
},
|
||||
"tables": ["house_sales"]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
aimind_tool.run("How many 3 bedroom houses were sold in 2008?")
|
||||
```
|
||||
|
||||
The `datasources` parameter is a list of dictionaries, each containing the following keys:
|
||||
|
||||
- `description`: A description of the data contained in the datasource.
|
||||
- `engine`: The engine (or type) of the datasource. Find a list of supported engines in the link below.
|
||||
- `connection_data`: A dictionary containing the connection parameters for the datasource. Find a list of connection parameters for each engine in the link below.
|
||||
- `tables`: A list of tables that the data source will use. This is optional and can be omitted if all tables in the data source are to be used.
|
||||
|
||||
A list of supported data sources and their connection parameters can be found [here](https://docs.mdb.ai/docs/data_sources).
|
||||
|
||||
```python
|
||||
from crewai import Agent
|
||||
from crewai.project import agent
|
||||
|
||||
|
||||
# Define an agent with the AIMindTool.
|
||||
@agent
|
||||
def researcher(self) -> Agent:
|
||||
return Agent(
|
||||
config=self.agents_config["researcher"],
|
||||
allow_delegation=False,
|
||||
tools=[aimind_tool]
|
||||
)
|
||||
```
|
||||
0
src/crewai_tools/tools/ai_mind_tool/__init__.py
Normal file
0
src/crewai_tools/tools/ai_mind_tool/__init__.py
Normal file
87
src/crewai_tools/tools/ai_mind_tool/ai_mind_tool.py
Normal file
87
src/crewai_tools/tools/ai_mind_tool/ai_mind_tool.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import os
|
||||
import secrets
|
||||
from typing import Any, Dict, List, Optional, Text, Type
|
||||
|
||||
from crewai.tools import BaseTool
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class AIMindToolConstants:
|
||||
MINDS_API_BASE_URL = "https://mdb.ai/"
|
||||
MIND_NAME_PREFIX = "crwai_mind_"
|
||||
DATASOURCE_NAME_PREFIX = "crwai_ds_"
|
||||
|
||||
|
||||
class AIMindToolInputSchema(BaseModel):
|
||||
"""Input for AIMind Tool."""
|
||||
|
||||
query: str = "Question in natural language to ask the AI-Mind"
|
||||
|
||||
|
||||
class AIMindTool(BaseTool):
|
||||
name: str = "AIMind Tool"
|
||||
description: str = (
|
||||
"A wrapper around [AI-Minds](https://mindsdb.com/minds). "
|
||||
"Useful for when you need answers to questions from your data, stored in "
|
||||
"data sources including PostgreSQL, MySQL, MariaDB, ClickHouse, Snowflake "
|
||||
"and Google BigQuery. "
|
||||
"Input should be a question in natural language."
|
||||
)
|
||||
args_schema: Type[BaseModel] = AIMindToolInputSchema
|
||||
api_key: Optional[str] = None
|
||||
datasources: Optional[List[Dict[str, Any]]] = None
|
||||
mind_name: Optional[Text] = None
|
||||
|
||||
def __init__(self, api_key: Optional[Text] = None, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.api_key = api_key or os.getenv("MINDS_API_KEY")
|
||||
if not self.api_key:
|
||||
raise ValueError("API key must be provided either through constructor or MINDS_API_KEY environment variable")
|
||||
|
||||
try:
|
||||
from minds.client import Client # type: ignore
|
||||
from minds.datasources import DatabaseConfig # type: ignore
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"`minds_sdk` package not found, please run `pip install minds-sdk`"
|
||||
)
|
||||
|
||||
minds_client = Client(api_key=self.api_key)
|
||||
|
||||
# Convert the datasources to DatabaseConfig objects.
|
||||
datasources = []
|
||||
for datasource in self.datasources:
|
||||
config = DatabaseConfig(
|
||||
name=f"{AIMindToolConstants.DATASOURCE_NAME_PREFIX}_{secrets.token_hex(5)}",
|
||||
engine=datasource["engine"],
|
||||
description=datasource["description"],
|
||||
connection_data=datasource["connection_data"],
|
||||
tables=datasource["tables"],
|
||||
)
|
||||
datasources.append(config)
|
||||
|
||||
# Generate a random name for the Mind.
|
||||
name = f"{AIMindToolConstants.MIND_NAME_PREFIX}_{secrets.token_hex(5)}"
|
||||
|
||||
mind = minds_client.minds.create(
|
||||
name=name, datasources=datasources, replace=True
|
||||
)
|
||||
|
||||
self.mind_name = mind.name
|
||||
|
||||
def _run(
|
||||
self,
|
||||
query: Text
|
||||
):
|
||||
# Run the query on the AI-Mind.
|
||||
# The Minds API is OpenAI compatible and therefore, the OpenAI client can be used.
|
||||
openai_client = OpenAI(base_url=AIMindToolConstants.MINDS_API_BASE_URL, api_key=self.api_key)
|
||||
|
||||
completion = openai_client.chat.completions.create(
|
||||
model=self.mind_name,
|
||||
messages=[{"role": "user", "content": query}],
|
||||
stream=False,
|
||||
)
|
||||
|
||||
return completion.choices[0].message.content
|
||||
Reference in New Issue
Block a user