データ ソース - Azure Cosmos DB for MongoDB 仮想コア

Azure OpenAI On Your Data を使うときに Azure Cosmos DB for MongoDB 仮想コアで構成できるオプション。 このデータ ソースは API バージョン 2024-02-01 でサポートされています。

名前 タイプ Required 説明
parameters パラメーター True Azure Cosmos DB for MongoDB 仮想コアを構成するときに使用するパラメーター。
type string True azure_cosmos_dbである必要があります。

パラメーター

件名 タイプ Required Description
database_name string True Azure Cosmos DB で使用する MongoDB 仮想コア データベースの名前。
container_name string True Azure Cosmos DB リソース コンテナーの名前。
index_name string True Azure Cosmos DB で使用する MongoDB 仮想コア インデックスの名前。
fields_mapping FieldsMappingOptions True 検索インデックスを操作するときに使用するカスタマイズされたフィールド マッピング動作。
authentication ConnectionStringAuthenticationOptions True 定義されたデータ ソースにアクセスするときに使用する認証方法。
embedding_dependency DeploymentNameVectorizationSourceEndpointVectorizationSource のいずれか True ベクトル検索の埋め込み依存関係。
in_scope boolean False クエリをインデックス付きデータの使用に制限するかどうか。 既定値は True です。
role_information string False ふるまいに関する指示と、応答の生成時に参照する必要があるコンテキストをモデルに与えます。 アシスタントのパーソナリティを説明し、応答の書式設定方法を伝えることができます。
strictness integer False 検索の関連性フィルター処理の構成された厳密度。 厳密度が高いほど、精度は高くなりますが、回答の再現率は低くなります。 既定値は 3 です。
top_n_documents integer False 構成されたクエリの特徴量に対して構成されている上位のドキュメントの数。 既定値は 5 です。

接続文字列の認証オプション

接続文字列を使用する場合の Azure OpenAI On Your Data の認証オプション。

名前 タイプ Required Description
connection_string string True 認証に使用する接続文字列。
type string True connection_stringである必要があります。

デプロイ名のベクター化ソース

ベクトル検索を適用するときに Azure OpenAI On Your Data で使用されるベクター化ソースの詳細。 このベクター化ソースは、同じ Azure OpenAI リソース内の内部埋め込みモデル デプロイ名に基づきます。 このベクター化ソースにより、Azure OpenAI API キーを使用せずに、Azure OpenAI パブリック ネットワーク アクセスなしでベクトル検索を使用できます。

名前 タイプ Required Description
deployment_name string True 同じ Azure OpenAI リソース内の埋め込みモデル デプロイ名。
type string True deployment_nameである必要があります。

エンドポイント ベクター化ソース

ベクトル検索を適用するときに Azure OpenAI On Your Data で使用されるベクター化ソースの詳細。 このベクター化ソースは、Azure OpenAI 埋め込み API エンドポイントに基づきます。

名前 タイプ Required Description
endpoint string True 埋め込みの取得元となるリソース エンドポイント URL を指定します。 https://{YOUR_RESOURCE_NAME}.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/embeddings という形式にする必要があります。 api-version クエリ パラメーターは許可されていません。
authentication ApiKeyAuthenticationOptions True 指定したエンドポイントから埋め込みを取得するときに使用する認証オプションを指定します。
type string True endpointである必要があります。

API キー認証オプション

API キーを使用する場合の Azure OpenAI On Your Data の認証オプション。

名前 タイプ Required Description
key string True 認証に使用する API キー。
type string True api_keyである必要があります。

フィールド マッピング オプション

フィールドの処理方法を制御する設定。

名前 タイプ Required 説明
content_fields string[] True コンテンツとして扱う必要があるインデックス フィールドの名前。
vector_fields string[] True ベクトル データを表すフィールドの名前。
content_fields_separator string False コンテンツ フィールドで使用する区切り記号パターン。 既定値は \n です。
filepath_field string False ファイルパスとして使用するインデックス フィールドの名前。
title_field string False タイトルとして使用するインデックス フィールドの名前。
url_field string False URL として使用するインデックス フィールドの名前。

前提条件:

  • ユーザーから Azure OpenAI リソースへのロールの割り当てを構成します。 必要なロール: Cognitive Services OpenAI User
  • Az CLI をインストールし、az login を実行します。
  • 次の環境変数を定義します: AzureOpenAIEndpointChatCompletionsDeploymentNameConnectionStringDatabaseContainerIndexEmbeddingDeploymentName
export AzureOpenAIEndpoint=https://example.openai.azure.com/
export ChatCompletionsDeploymentName=turbo
export ConnectionString='mongodb+srv://username:***@example.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000'
export Database=testdb
export Container=testcontainer
export Index=testindex
export EmbeddingDeploymentName=ada

最新の pip パッケージ openaiazure-identity をインストールします。


import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

endpoint = os.environ.get("AzureOpenAIEndpoint")
deployment = os.environ.get("ChatCompletionsDeploymentName")
connection_string = os.environ.get("ConnectionString")
database = os.environ.get("Database")
container = os.environ.get("Container")
index = os.environ.get("Index")
embedding_deployment_name = os.environ.get("EmbeddingDeploymentName")

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")

client = AzureOpenAI(
    azure_endpoint=endpoint,
    azure_ad_token_provider=token_provider,
    api_version="2024-02-01",
)

completion = client.chat.completions.create(
    model=deployment,
    messages=[
        {
            "role": "user",
            "content": "Who is DRI?",
        },
    ],
    extra_body={
        "data_sources": [
            {
                "type": "azure_cosmos_db",
                "parameters": {
                    "authentication": {
                        "type": "connection_string",
                        "connection_string": connection_string
                    },
                    "database_name": database,
                    "container_name": container,
                    "index_name": index,
                    "fields_mapping": {
                        "content_fields": [
                            "content"
                        ],
                        "vector_fields": [
                            "contentvector"
                        ]
                    },
                    "embedding_dependency": {
                        "type": "deployment_name",
                        "deployment_name": embedding_deployment_name
                    }
                }
            }
        ],
    }
)

print(completion.model_dump_json(indent=2))