AIエージェントの場合: ドキュメントインデックスはhttps://www.mongodb.com/ja-jp/docs/llms.txt で利用可能です。任意のURLパスに .md を追加することで、すべてのページのマークダウン バージョンが利用できます。
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

MongoDBと [CbrewAI] との統合

MongoDB (具体的には、 MongoDB ベクトル検索 Tools を使用して、チーム内のAIエージェントがデータから関連情報を取得し、タスクを完了できるようにします。

CredeAI とMongoDBを使用してチュートリアルを完了するには、CRUI と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
必要性
説明

connection_string

必須

MongoDBインスタンスの接続文字列。接続文字列の検索方法の詳細については、クライアント ライブラリ経由でクラスターに接続する を参照してください。

ローカル配置の場合、接続文字列は次の形式を使用する必要があります。

mongodb://localhost:<port-number>/?directConnection=true

接続文字列の詳細については、接続文字列を参照してください。

database_name

必須

MongoDB database の名前。

collection_name

必須

MongoDBコレクションの名前。

query_config

任意

ベクトル検索クエリーをカスタマイズする MongoDBVectorSearchConfig のインスタンス。

embedding_model

任意

ベクトル埋め込みの生成に使用される OpenAI 埋め込みモデル。デフォルトは text-embedding-3-large です。

vector_index_name

任意

MongoDB ベクトル検索インデックスの名前。デフォルトは vector_index です。

text_key

任意

テキスト コンテンツを含むドキュメントフィールド。デフォルトは text です。

embedding_key

任意

ベクトル埋め込みが保存されるドキュメントフィールド。デフォルトは embedding です。

dimensions

任意

ベクトル埋め込みの次元数。デフォルトは 1536 です。

これらのパラメーターを使用して、ベクトル検索クエリーをカスタマイズします。

Parameter
必要性
説明

limit

任意

返されるドキュメントの最大数。デフォルトは 4 です。

pre_filter

任意

ベクトル検索の前にドキュメントをフィルタリングするMongoDB $match式。

post_filter_pipeline

任意

ベクトル検索の後に適用するMongoDB集計ステージのリスト。

oversampling_factor

任意

検索中に考慮された候補(numCandidates)の数を決定するための limit の乗数。デフォルトは 10 です。

include_embeddings

ブール

true の場合、各結果のベクトル埋め込みが出力に含まれます。デフォルトは False です。

詳細については、CrewAI MongoDB ベクトル検索 Docs を参照してください。

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
必要性
説明

dimensions

必須

埋め込みベクトルの次元数。

relevance_score_fn

任意

類似性メトリクス。euclideancosine、または dotProduct のいずれか。デフォルトは cosine です。

auto_index_timeout

任意

インデックスが準備完了するまで待機する時間(秒単位)。デフォルトは 15 です。

ベクトル検索インデックスの詳細については、 ベクトル検索のフィールドにインデックスを作成する方法 を参照してください。

これらのパラメーターを使用して、 MongoDB がドキュメントを取り込む方法を設定します。

Parameter
必要性
説明

texts

必須

追加する反復可能なテキスト ドキュメントの配列。

metadatas

任意

メタデータドキュメントのリスト(各 テキストドキュメントに 1 つ)。

ids

任意

各ドキュメントの一意の ID のリスト。

batch_size

任意

単一のバッチするで処理および挿入するドキュメントの数。デフォルトは 100 です。