注意
このチュートリアルでは、セマンティック カーネル Pythonライブラリ を使用します。 C#ライブラリを使用するチュートリアルについては、「 セマンティック カーネルC#統合を使い始める 」を参照してください。
Atlas ベクトル検索 をMicrosoftセマンティック カーネルと統合して、 AIアプリケーションを構築し、検索拡張生成 (RAG)(RAG) を実装できます。このチュートリアルでは、セマンティック カーネルと Atlas ベクトル検索 の使用を開始し、データに対してセマンティック検索を実行し、 RAG実装を構築する方法を説明します。具体的には、次のアクションを実行します。
環境を設定します。
カスタム データをMongoDBに保存します。
データに Atlas Vector Search インデックスを作成します。
データに対してセマンティック検索クエリを実行します。
Atlas Vector Search を使用してデータの質問に答え、RAG を実装します。
バックグラウンド
セマンティック カーネルは、アプリケーションでさまざまな AI サービスとプラグインを組み合わせることができるオープンソースの SDK です。 セマンティック カーネルは、 RAGを含むさまざまな AI ユースケースに使用できます。
Atlas ベクトル検索 をセマンティック カーネルと統合することで、 MongoDB をベクトルデータベースとして使用し、Atlas ベクトル検索 を使用してセマンティックで類似したドキュメントを検索して RG を実装することができます。 RG の詳細については、 「 MongoDBでの検索拡張生成(RAG) 」を参照してください。
前提条件
Atlas の サンプル データ セット からの映画データを含むコレクションを使用します。
次のいずれか 1 つ。
MongoDBバージョン6.0.11 、7.0.2 、またはそれ以降を実行中Atlas クラスター。 IPアドレスが Atlas プロジェクトのアクセス リストに含まれていることを確認します。
Atlas CLI を使用して作成されたローカル Atlas 配置。詳細については、「 Atlas クラスターのローカル配置 」を参照してください。
OpenAI APIキー。 APIリクエストに使用できるクレジットを持つ OpenAI アカウントが必要です。 OpenAI アカウントの登録の詳細については、 OpenAI APIウェブサイト を参照してください。
環境を設定する
このチュートリアルの環境を設定します。 .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
セマンティック検索クエリを実行するために使用されるインデックス。
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の 名前空間に移動することでベクトル埋め込みを検証できます。
Atlas Vector Search インデックスの作成
ベクトルストアでベクトル検索クエリを有効にするには、端末上で次のコードを実行して、semantic_kernel_db.test
コレクションに Atlas Vector Search インデックスを作成してください。
# 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)
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
データに関する質問に答えます
このセクションでは、Atlas ベクトル検索とセマンティック カーネルを使用した RAM の実装例を示します。Atlas ベクトル検索 を使用してセマンティックに類似したドキュメントを検索したので、次のコード例を実行して、それらのドキュメントに基づいて質問に答えるように LM に指示します。
次のコードは、検索されたドキュメントをクエリのコンテキストとして使用するように LM に指示するプロンプトを定義します。この例では、サンプルクエリ を使用して LMWhen did I start using MongoDB?
をプロンプトします。 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].text answer = await prompt.invoke( kernel=kernel, input=question, context=retrieved_document ) print(answer)
You started using MongoDB two years ago.
次のステップ
MongoDBは、次の開発者リソースも提供しています。