注意
このチュートリアルでは、 セマンティック カーネルPythonライブラリ を使用します。C#ライブラリを使用するチュートリアルについては、セマンティック カーネルC#統合を使い始める を参照してください。
MongoDB ベクトル検索 をMicrosoftセマンティック カーネルと統合して、 AIアプリケーションを構築し、検索拡張生成 (RAG)(RAG) を実装できます。このチュートリアルでは、セマンティック カーネルとともにMongoDB ベクトル検索を使用してデータに対してセマンティック検索を実行し、RAG実装を構築する方法を説明します。具体的には、次のアクションを実行します。
環境を設定します。
カスタム データをMongoDBに保存します。
データにMongoDB ベクトル検索インデックスを作成します。
データに対してセマンティック検索クエリを実行します。
MongoDB ベクトル検索を使用してデータの質問に答え、RAG を実装します。
バックグラウンド
セマンティック カーネルは、アプリケーションでさまざまな AI サービスとプラグインを組み合わせることができるオープンソースの SDK です。 セマンティック カーネルは、 RAGを含むさまざまな AI ユースケースに使用できます。
MongoDB ベクトル検索 と Semantic Kernel を統合することで、MongoDB をベクトルデータベースとして利用し、MongoDB ベクトル検索 を使用して、データから意味的に類似したドキュメントを検索し、RAG を実装することができます。RAG の詳細については、「MongoDB による検索拡張生成(RAG)」を参照してください。
前提条件
Atlas の サンプル データ セット からの映画データを含むコレクションを使用します。
次のいずれかのMongoDBクラスター タイプ
MongoDB バージョン 6.0.11 を実行している Atlas クラスター、7.0.2以降。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認します。
Python と Docker を使用して作成されたローカル Atlas 配置。
atlas-local-lib-py(pip install atlas-local-lib-py) をインストールして、ローカル配置をプログラムで作成および管理します。詳細については、atlas-local-lib-py リポジトリを参照してください。検索とベクトル検索がインストールされた MongoDB Community クラスター。
OpenAI APIキー。APIリクエストに使用できるクレジットを持つ OpenAI アカウントが必要です。OpenAI アカウントの登録の詳細については、OpenAI APIウェブサイト を参照してください。
Colab などのインタラクティブPythonノートを実行するための環境。
環境を設定する
このチュートリアルの環境を設定します。 .ipynb 拡張機能のファイルを保存して、インタラクティブPythonノートを作成します。 このノートはPythonコード スニペットを個別に実行でき、このチュートリアルのコードを実行するために使用します。
ノートク環境を設定するには、次の手順に従います。
依存関係をインストールしてインポートします。
ノート PC で次のコマンドを実行して、環境にセマンティック カーネルをインストールします。
pip install --quiet --upgrade semantic-kernel openai motor必要なパッケージをインポートするには、次のコードを実行します。
import semantic_kernel as skfrom semantic_kernel.connectors.ai.open_ai import (OpenAIChatCompletion, OpenAITextEmbedding)from semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasMemoryStorefrom semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPluginfrom semantic_kernel.memory.semantic_text_memory import SemanticTextMemoryfrom semantic_kernel.prompt_template.input_variable import InputVariablefrom semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfigfrom pymongo import MongoClientfrom pymongo.operations import SearchIndexModel
環境変数を定義してください。
次のコードを実行し、プレースホルダーを次の値に置き換えます。
OpenAI API キー。
MongoDBクラスターの接続文字列。
注意
<connection-string> を Atlas クラスターまたはローカル Atlas 配置の接続文字列に置き換えます。
接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net重要:接続文字列にデータベースユーザーの認証情報が含まれていることを確認します。接続文字列の検索方法の詳細については、クライアント ライブラリ経由でクラスターに接続する を参照してください。
接続stringには、次の形式を使用する必要があります。
mongodb://localhost:<port-number>/?directConnection=true詳細については、「接続文字列 」を参照してください。
MongoDBでカスタム データを保存する
このセクションでは、アプリケーションのサービスとプラグインを管理するために使用されるメインのインターフェースであるカーネルを初期化します。カーネルを介して、 AIサービスを構成し、 MongoDB をベクトルデータベース(メモリストアとも)としてインスタンス化し、カスタム データをMongoDBクラスターにロードします。
MongoDBにカスタム データを保存するには、次のコード スニペットをノートに貼り付けて実行します。
AI サービスをカーネルに追加します。
次のコードを実行して、このチュートリアルで使用される OpenAI 埋め込みモデルとチャットモデルを構成し、これらのサービスをカーネルに追加します。 このコードでは、次の項目を指定します。
テキストをベクトル埋め込みに変換するために使用される埋め込みモデルとしての OpenAI の
text-embedding-ada-002。応答の生成に使用されるチャット モデルとしての OpenAI の
gpt-3.5-turbo。
chat_service = OpenAIChatCompletion( service_id="chat", ai_model_id="gpt-3.5-turbo", api_key=OPENAI_API_KEY)embedding_service = OpenAITextEmbedding( ai_model_id="text-embedding-ada-002", api_key=OPENAI_API_KEY)kernel.add_service(chat_service)kernel.add_service(embedding_service)Atlas をメモリ ストアとしてインスタンス化します。
次のコードを実行して、Atlas をメモリ ストアとしてインスタンス化し、カーネルに追加します。 このコードは、Atlas クラスターへの接続を確立し、次の項目を指定します。
semantic_kernel_dbは、ドキュメントを保存するために使用される Atlas データベースです。vector_indexセマンティック検索クエリを実行するために使用されるインデックス。
TextMemoryPluginまた、メモリにテキストを保存および取得するためのネイティブ関数のグループを提供する というプラグインもインポートされます。
mongodb_atlas_memory_store = MongoDBAtlasMemoryStore( connection_string=MONGODB_URI, database_name="semantic_kernel_db", index_name="vector_index")
memory = SemanticTextMemory( storage=mongodb_atlas_memory_store, embeddings_generator=embedding_service)kernel.add_plugin(TextMemoryPlugin(memory), "TextMemoryPlugin")Atlas クラスターに サンプル データ をロードします。
このコードでは、関数を定義して実行し、semantic_kernel_db.testコレクションにサンプルドキュメントを入力します。これらのドキュメントには、LM が最初はアクセスできなかったパーソナライズされたデータが含まれています。
async def populate_memory(kernel: sk.Kernel) -> None: await memory.save_information( collection="test", id="1", text="I am a developer" ) await memory.save_information( collection="test", id="2", text="I started using MongoDB two years ago" ) await memory.save_information( collection="test", id="3", text="I'm using MongoDB Vector Search with Semantic Kernel to implement RAG" ) await memory.save_information( collection="test", id="4", text="I like coffee" )
print("Populating memory...") await populate_memory(kernel) print(kernel)Populating memory...plugins=KernelPluginCollection(plugins={'TextMemoryPlugin': KernelPlugin(name='TextMemoryPlugin', description=None, functions={'recall': KernelFunctionFromMethod(metadata=KernelFunctionMetadata(name='recall', plugin_name='TextMemoryPlugin', description='Recall a fact from the long term memory', parameters=[KernelParameterMetadata(name='ask', description='The information to retrieve', default_value=None, type_='str', is_required=True, type_object=<class 'str'>), KernelParameterMetadata(name='collection', description='The collection to search for information.', default_value='generic', type_='str', is_required=False, type_object=<class 'str'>), KernelParameterMetadata(name='relevance', description='The relevance score, from 0.0 to 1.0; 1.0 means perfect match', default_value=0.75, type_='float', is_required=False, type_object=<class 'float'>), KernelParameterMetadata(name='limit', description='The maximum number of relevant memories to recall.', default_value=1, type_='int', is_required=False, type_object=<class 'int'>)], is_prompt=False, is_asynchronous=True, return_parameter=KernelParameterMetadata(name='return', description='', default_value=None, type_='str', is_required=True, type_object=None)), method=<bound method TextMemoryPlugin.recall of TextMemoryPlugin(memory=SemanticTextMemory())>, stream_method=None), 'save': KernelFunctionFromMethod(metadata=KernelFunctionMetadata(name='save', plugin_name='TextMemoryPlugin', description='Save information to semantic memory', parameters=[KernelParameterMetadata(name='text', description='The information to save.', default_value=None, type_='str', is_required=True, type_object=<class 'str'>), KernelParameterMetadata(name='key', description='The unique key to associate with the information.', default_value=None, type_='str', is_required=True, type_object=<class 'str'>), KernelParameterMetadata(name='collection', description='The collection to save the information.', default_value='generic', type_='str', is_required=False, type_object=<class 'str'>)], is_prompt=False, is_asynchronous=True, return_parameter=KernelParameterMetadata(name='return', description='', default_value=None, type_='', is_required=True, type_object=None)), method=<bound method TextMemoryPlugin.save of TextMemoryPlugin(memory=SemanticTextMemory())>, stream_method=None)})}) services={'chat': OpenAIChatCompletion(ai_model_id='gpt-3.5-turbo', service_id='chat', client=<openai.AsyncOpenAI object at 0x7999971c8fa0>, ai_model_type=<OpenAIModelTypes.CHAT: 'chat'>, prompt_tokens=0, completion_tokens=0, total_tokens=0), 'text-embedding-ada-002': OpenAITextEmbedding(ai_model_id='text-embedding-ada-002', service_id='text-embedding-ada-002', client=<openai.AsyncOpenAI object at 0x7999971c8fd0>, ai_model_type=<OpenAIModelTypes.EMBEDDING: 'embedding'>, prompt_tokens=32, completion_tokens=0, total_tokens=32)} ai_service_selector=<semantic_kernel.services.ai_service_selector.AIServiceSelector object at 0x7999971cad70> retry_mechanism=PassThroughWithoutRetry() function_invoking_handlers={} function_invoked_handlers={}Tip
Atlassemantic_kernel_db.test を使用している場合は、サンプルコードの実行中後、Atlas UIの 名前空間に移動することでベクトル埋め込みを検証できます。
MongoDB ベクトル検索インデックスの作成
ベクトルストアでベクトル検索クエリを有効にするには、ノート型で次のコードを実行して、semantic_kernel_db.testコレクションにMongoDB ベクトル検索インデックスを作成します。
# Connect to your MongoDB cluster and specify the collectionclient = MongoClient(MONGODB_URI)collection = client["semantic_kernel_db"]["test"]
# Create your index model, then create the search indexsearch_index_model = SearchIndexModel( definition={ "fields": [ { "type": "vector", "path": "embedding", "numDimensions": 1536, "similarity": "cosine" } ] }, name="vector_index", type="vectorSearch")
collection.create_search_index(model=search_index_model)インデックス定義は、embeddingフィールドをベクトル型としてインデックスします。embeddingフィールドには、OpenAI の text-embedding-ada-002 埋め込みモデルを使用して作成された埋め込みが含まれます。インデックス定義では、1536ベクトル次元を指定し、cosine を使用して類似性を測定します。
ベクトル検索クエリの実行
MongoDB がインデックスを構築 したら、データに対してベクトル検索クエリを実行できるようになります。
ノートで次のコードを実行して、string What is my job title?の基本的なセマンティック検索を実行します。最も関連性の高いドキュメントと、 0と1の間の関連性スコアが出力されます。
result = await memory.search("test", "What is my job title?")print(f"Retrieved document: {result[0].text}, {result[0].relevance}")Retrieved document: I am a developer, 0.8991971015930176データに関する質問に答えます
このセクションでは、 MongoDB ベクトル検索とセマンティック カーネルが実装RAM の例を示します。MongoDB ベクトル検索 を使用してセマンティックに類似したドキュメントを検索したので、次のコード例を実行して、それらのドキュメントに基づいて質問に答えるように LM に指示します。
次のコードは、検索されたドキュメントをクエリのコンテキストとして使用するように LM に指示するプロンプトを定義します。この例では、サンプルクエリ When did I start using MongoDB? を使用して LM をプロンプトします。 LM の知識ベースをカスタム データで拡張したため、チャットモデルはより正確でコンテキストに対応した応答を生成できます。
service_id = "chat"settings = kernel.get_service(service_id).instantiate_prompt_execution_settings( service_id=service_id)
prompt_template = """ Answer the following question based on the given context.
Question: {{$input}} Context: {{$context}}"""
chat_prompt_template_config = PromptTemplateConfig( execution_settings=settings, input_variables=[ InputVariable(name="input"), InputVariable(name="context") ], template=prompt_template)
prompt = kernel.add_function( function_name="RAG", plugin_name="TextMemoryPlugin", prompt_template_config=chat_prompt_template_config,)
question = "When did I start using MongoDB?"results = await memory.search("test", question)retrieved_document = results[0].textanswer = await prompt.invoke( kernel=kernel, input=question, context=retrieved_document)print(answer)You started using MongoDB two years ago.次のステップ
MongoDBは、次の開発者リソースも提供しています。