Docs Menu
Docs Home
/
Atlas
/ / /

セマンティック カーネル Python 統合を始める

注意

このチュートリアルでは、セマンティック カーネル Pythonライブラリ を使用します。C#ライブラリを使用するチュートリアルについては、セマンティック カーネルC#統合を使い始める を参照してください。

MongoDB ベクトル検索 をMicrosoftセマンティック カーネルと統合して、 AIアプリケーションを構築し、検索拡張生成 (RAG)(RAG) を実装できます。このチュートリアルでは、セマンティック カーネルでMongoDB ベクトル検索を使用してデータのセマンティック検索を実行し、RAG実装を構築する方法を説明します。具体的には、次のアクションを実行します。

  1. 環境を設定します。

  2. カスタム データをMongoDBに保存します。

  3. データにMongoDB ベクトル検索インデックスを作成します。

  4. データに対してセマンティック検索クエリを実行します。

  5. MongoDB ベクトル検索を使用してデータの質問に答え、RAG を実装します。

このチュートリアルの実行可能なバージョンをPythonノートとして操作します。

セマンティック カーネルは、アプリケーションでさまざまな AI サービスとプラグインを組み合わせることができるオープンソースの SDK です。 セマンティック カーネルは、 RAGを含むさまざまな AI ユースケースに使用できます。

MongoDB ベクトル検索 をセマンティック カーネルと統合することで、 MongoDB をベクトルデータベースとして使用し、 MongoDB ベクトル検索を使用してセマンティックで類似したドキュメントを検索して RG を実装することができます。RG の詳細については、 MongoDBによる検索拡張生成(RAG) を参照してください。

Atlas の サンプル データ セット からの映画データを含むコレクションを使用します。

  • 次のいずれかのMongoDBクラスター タイプ

    • MongoDB バージョン 6.0.11、7.0.2、またはそれ以降を実行している Atlas クラスター。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認する。

    • Atlas CLI を使用して作成されたローカル Atlas 配置。詳細については、「Atlas 配置のローカル配置の作成」を参照してください。

    • Search とベクトル検索がインストールされたMongoDB Community または Enterprise クラスター。

  • OpenAI APIキー。APIリクエストに使用できるクレジットを持つ OpenAI アカウントが必要です。OpenAI アカウントの登録の詳細については、OpenAI APIウェブサイト を参照してください。

  • Colab などのインタラクティブPythonノートを実行するための環境。

このチュートリアルの環境を設定します。 .ipynb 拡張子を持つファイルを保存して、インタラクティブPythonノートを作成します。 このノートはPythonコード スニペットを個別に実行でき、このチュートリアルのコードを実行するために使用します。

ノートク環境を設定するには、次の手順に従います。

1
  1. ノート PC で次のコマンドを実行して、環境にセマンティック カーネルをインストールします。

    pip install --quiet --upgrade semantic-kernel openai motor
  2. 必要なパッケージをインポートするには、次のコードを実行します。

    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
2

次のコードを実行し、プレースホルダーを次の値に置き換えます。

  • 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

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

このセクションでは、アプリケーションのサービスとプラグインを管理するために使用されるメインのインターフェースであるカーネルを初期化します。カーネルを介して、 AIサービスを構成し、 MongoDB をベクトルデータベース(メモリストアとも)としてインスタンス化し、カスタム データをMongoDBクラスターにロードします。

MongoDBにカスタム データを保存するには、次のコード スニペットをノートに貼り付けて実行します。

1

以下のコードを実行して、カーネルを初期化します。

kernel = sk.Kernel()
2

次のコードを実行して、このチュートリアルで使用される 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)
3

次のコードを実行して、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")
4

このコードでは、関数を定義して実行し、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

ベクトルストアでベクトル検索クエリを有効にするには、ノート型で次のコードを実行して、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)

embedding フィールドを ベクトルタイプとしてインデックスするインデックス定義です。embedding フィールドには、OpenAI の text-embedding-ada-002 埋め込みモデルを使用して作成された埋め込みが含まれています。インデックス定義では、1536 ベクトル次元を指定し、 cosine を使用して類似性を測定します。

MongoDB がインデックスを構築 したら、データに対してベクトル検索クエリを実行できるようになります。

ノートで次のコードを実行して、 string What is my job title? の基本的なセマンティック検索を実行します。 最も関連性の高いドキュメントと、 01の間の関連性スコアが出力されます。

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].text
answer = await prompt.invoke(
kernel=kernel, input=question, context=retrieved_document
)
print(answer)
You started using MongoDB two years ago.

MongoDBは、次の開発者リソースも提供しています。

戻る

LlamaIndex

項目一覧