MongoDBを brewAI と統合すると、専用のロール、ツール、タスクを使用して独立したAIエージェントとマルチエージェント アプリケーションを構築できます。具体的には、 MongoDB ベクトル検索 Tools を使用して、チーム内のAIエージェントがデータから関連情報を取得し、タスクを完了できるようにします。
はじめる
[] と[MongoDB]を使用してチュートリアルを完了するには、CreatorAI とMongoDBを使用してエージェント RAG アプリケーションを構築する を参照してください。
インストール
CRUDAI 用のMongoDBベクトル検索ツールをインストールするには、 Pythonパッケージマネージャーに応じて次のいずれかのコマンドを実行します。
pip install 'crewai-tools[mongodb]'
uv add crewai-tools --extra mongodb
注意
Pythonバージョンの互換性は、CbrewAI の公式ドキュメントとは異なる場合があります。書き込み時に、crewai-tools
パッケージはembedchain
に依存しています。これには、3.9 から 3.13.2 までのPythonバージョンが必要です。(この値を含む)。
使用法
MongoDB ベクトル検索ツールを使用するには、それを初期化し、それをエージェントに渡します。
ツールを初期化するには、以下を指定する必要があります。
from crewai_tools import MongoDBVectorSearchTool tool = MongoDBVectorSearchTool( connection_string="<connection-string>", database_name="<database-name>", collection_name="<collection-name>", # Other optional parameters... ) # To test the tool print(tool.run(query="<test-query>")) # To use the tool in an agent rag_agent = Agent( name="rag_agent", role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.", goal="...", backstory="...", tools=[tool], )
オプションで、ツールのコンストラクターに MongoDBVectorSearchConfig
のインスタンスを指定することで、ツールのベクトル検索クエリーをカスタマイズできます。
ベクトル検索クエリの詳細については、「 ベクトル検索クエリの実行 」を参照してください。
from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool # Custom query configuration query_config = MongoDBVectorSearchConfig( limit = 10, oversampling_factor = 2, ) tool = MongoDBVectorSearchTool( database_name="example_database", collection_name="example_collection", connection_string="<connection_string>", query_config=query_config, # Other optional parameters... ) # To test the tool print(tool.run(query="<test-query>")) # To use the tool in an agent rag_agent = Agent( name="rag_agent", role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.", goal="...", backstory="...", tools=[tool], )
Parameter | 必要性 | 説明 | |
---|---|---|---|
| 必須 | MongoDBインスタンスの接続文字列。 接続文字列 の検索方法について詳しくは、ドライバーを使用してクラスターに接続する を参照してください。 注意ローカル配置の場合、接続文字列は次の形式を使用する必要があります。
接続文字列の詳細については、接続文字列を参照してください。 | |
| 必須 | MongoDB database の名前。 | |
| 必須 | MongoDBコレクションの名前。 | |
| 任意 | ベクトル検索クエリーをカスタマイズする | |
| 任意 | ベクトル埋め込みの生成に使用される OpenAI 埋め込みモデル。デフォルトは | |
| 任意 | MongoDB ベクトル検索インデックスの名前。デフォルトは | |
| 任意 | テキスト コンテンツを含むドキュメントフィールド。デフォルトは | |
| 任意 | ベクトル埋め込みが保存されるドキュメントフィールド。デフォルトは | |
| 任意 | ベクトル埋め込みの次元数。デフォルトは |
Parameter | 必要性 | 説明 |
---|---|---|
| 任意 | 返されるドキュメントの最大数。デフォルトは |
| 任意 | ベクトル検索の前にドキュメントをフィルタリングするMongoDB |
| 任意 | ベクトル検索の後に適用するMongoDB集計ステージのリスト。 |
| 任意 | 検索中に考慮された候補( |
| ブール | true の場合、各結果のベクトル埋め込みが出力に含まれます。デフォルトは |
メソッド
MongoDBVectorSearchTool
クラスは次のメソッドを提供します。
add_texts()
: 指定されたMongoDBコレクションにテキスト ドキュメントを追加します。create_vector_search_index()
:コレクションにベクトル検索インデックスを作成します。run()
: データに対してベクトル検索クエリーを実行します。
import os from crewai_tools import MongoDBVectorSearchTool tool = MongoDBVectorSearchTool( connection_string="<connection-string>", database_name="<database-name>", collection_name="<collection-name>" ) # Example of loading text content from a local folder texts = [] for fname in os.listdir("knowledge"): path = os.path.join("knowledge", fname) if os.path.isfile(path): with open(path, "r", encoding="utf-8") as f: texts.append(f.read()) # Method to add documents to the vector store tool.add_texts(texts) # Method to create the vector search index tool.create_vector_search_index(dimensions=<number-of-dimensions>) # Method to test the tool by running a vector search query tool.run(query="<search-query>")
Parameter | 必要性 | 説明 |
---|---|---|
| 必須 | 埋め込みベクトルの次元数。 |
| 任意 | 類似性メトリクス。 |
| 任意 | インデックスが準備完了するまで待機する時間(秒単位)。デフォルトは |
ベクトル検索インデックスの詳細については、「ベクトル検索のフィールドにインデックスを作成する方法」を参照してください。