注意
このチュートリアルでは、 セマンティック カーネルPythonライブラリ を使用します。C#ライブラリを使用するチュートリアルについては、セマンティック カーネルC#統合を使い始める を参照してください。
MongoDB ベクトル検索 をMicrosoftセマンティック カーネルと統合して、 AIアプリケーションを構築し、検索拡張生成 (RAG)(RAG) を実装できます。このチュートリアルでは、セマンティック カーネルとともにMongoDB ベクトル検索を使用してデータに対してセマンティック検索を実行し、RAG実装を構築する方法を説明します。具体的には、次のアクションを実行します。
環境を設定します。
カスタム データをMongoDBに保存します。
データにMongoDB ベクトル検索インデックスを作成します。
データに対してセマンティック検索クエリを実行します。
MongoDB ベクトル検索を使用してデータの質問に答え、RAG を実装します。
バックグラウンド
セマンティック カーネルは、アプリケーションでさまざまな AI サービスとプラグインを組み合わせることができるオープンソースの SDK です。 セマンティック カーネルは、 RAGを含むさまざまな AI ユースケースに使用できます。
By integrating MongoDB Vector Search with Semantic Kernel, you can use MongoDB as a vector database and use MongoDB Vector Search to implement RAG by retrieving semantically similar documents from your data. To learn more about RAG, see Retrieval-Augmented Generation (RAG) with MongoDB.
前提条件
Atlas の サンプル データ セット からの映画データを含むコレクションを使用します。
次のいずれかのMongoDBクラスター タイプ
An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later. Ensure that your IP address is included in your Atlas project's access list.
A local Atlas deployment created using Python and Docker. Install
atlas-local-lib-py(pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.A MongoDB Community cluster with Search and Vector Search installed.
OpenAI APIキー。API リクエストに使用できるクレジットがある OpenAI アカウントが必要です。OpenAI のアカウント登録の詳細については、OpenAI API のウェブサイトをご覧ください。
Comb などのインタラクティブ Python ノートを実行するための環境。
環境を設定する
このチュートリアルの環境を設定します。 .ipynb 拡張機能のファイルを保存して、インタラクティブPythonノートを作成します。 このノートはPythonコード スニペットを個別に実行でき、このチュートリアルのコードを実行するために使用します。
ノートク環境を設定するには、次の手順に従います。
依存関係をインストールしてインポートします。
ノート PC で次のコマンドを実行して、環境にセマンティック カーネルをインストールします。
pip install --quiet --upgrade semantic-kernel openai motor 必要なパッケージをインポートするには、次のコードを実行します。
import semantic_kernel as sk from semantic_kernel.connectors.ai.open_ai import (OpenAIChatCompletion, OpenAITextEmbedding) from semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasMemoryStore from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory from semantic_kernel.prompt_template.input_variable import InputVariable from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig from pymongo import MongoClient from 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セマンティック検索クエリを実行するために使用されるインデックス。
It also imports a plugin called TextMemoryPlugin, which provides a group of native functions to help you store and retrieve text in memory.
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 collection client = MongoClient(MONGODB_URI) collection = client["semantic_kernel_db"]["test"] # Create your index model, then create the search index search_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)
The index definition indexes the embedding field as the vector type. The embedding field contains the embeddings created using OpenAI's text-embedding-ada-002 embedding model. The index definition specifies 1536 vector dimensions and measures similarity using cosine.
ベクトル検索クエリの実行
MongoDB がインデックスを構築 したら、データに対してベクトル検索クエリを実行できるようになります。
In your notebook, run the following code to perform a basic semantic search for the string What is my job title?. It prints the most relevant document and a relevance score between 0 and 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 に指示します。
The following code defines a prompt to instruct the LLM to use the retrieved document as context for your query. In this example, you prompt the LLM with the sample query When did I start using MongoDB?. Because you augmented the knowledge base of the LLM with custom data, the chat model is able to generate a more accurate, context-aware response.
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].text answer = await prompt.invoke( kernel=kernel, input=question, context=retrieved_document ) print(answer)
You started using MongoDB two years ago.
次のステップ
MongoDBは、次の開発者リソースも提供しています。