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 のインスタンスを指定することで、ツールのベクトル検索クエリーをカスタマイズできます。
To learn more about vector search queries, see Run Vector Search ANN and ENN Queries.
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], )
メソッド
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>")