Azure OpenAI On Your Data API Reference (classic)

適用対象:Foundry (クラシック) ポータル。 この記事は、新しい Foundry ポータルでは使用できません。 新しいポータルの詳細を確認します

Note

この記事のリンクは、現在表示している Foundry (クラシック) ドキュメントではなく、新しい Microsoft Foundry ドキュメントのコンテンツを開く場合があります。

Important

Azure OpenAI On Your Dataは廃止されており、まもなく廃止間近です。

Microsoftは新しいモデルのAzure OpenAI On Your Dataへのオンボーディングを停止しました。 この機能は以下のモデルのみをサポートしています:

  • GPT-4o(バージョン 2024-05-13、2024-08-06、2024-11-20)
  • GPT-4o-mini(バージョン 2024-07-18)このモデルが終了すると、すべてのOpenAI On Your Data APIエンドポイントおよびサポートされたデータソースコネクター Azure機能が停止します。

OpenAI On Your DataのワークロードAzure、Foundry IQでコンテンツを取得し、データから根拠のある回答を生成するために、Foundry Agent Serviceに移行することをお勧めします。 始めるには、 Connect a Foundry IQナレッジベースをご覧ください。

この記事では、新しいAzure OpenAI On Your Data APIのPythonとRESTに関する参考ドキュメントを提供します。 最新のAPIバージョンは2024-05-01-previewSwagger specです。

Note

APIバージョン 2024-02-15-preview 以降、以前のAPIバージョンと比較して以下の重要な変更を導入しました。

  • APIパスは /extensions/chat/completions から /chat/completionsに変更されます。
  • プロパティキーとエナム値の命名規則はキャメルケーシングからスネークケーシングに変更されました。 例: deploymentNamedeployment_nameに変更されます。
  • データソースの種類 AzureCognitiveSearchazure_searchに変更されます。
  • 引用と意図はアシスタントメッセージのコンテキストツールメッセージから、明示的な スキーマを定義したコンテキストルートレベルへと移されます。
POST {endpoint}/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}

Supported versions

Note

PineconeElasticsearch はプレビューとしてサポートされています。

URI parameters

Name In タイプ Required Description
deployment-id パス 文字列 True このリクエストで使用するチャット完了モデルの展開名を指定します。
endpoint パス 文字列 True Azure OpenAI endpoints. 例えば: https://{YOUR_RESOURCE_NAME}.openai.azure.com
api-version クエリ 文字列 True この操作に使用する API バージョン。

Request body

リクエスト本体はチャット完了APIリクエストのスキーマを継承します。 この表は、あなたのデータ上でAzure OpenAIに固有のパラメータを示しています。

Name タイプ Required Description
data_sources DataSource[] True Azure OpenAIの構成エントリーはあなたのデータにあります。 配列には必ず一つの要素が存在しなければなりません。 data_sourcesが提供されていない場合、サービスはチャット完了モデルを直接使用し、OpenAI Azure On Your Dataは使用しません。 data_sourcesパラメータを指定すると、logprobstop_logprobsパラメータは使えなくなります。

Response body

レスポンスボディはチャット完了のAPIレスポンスの同じスキーマを受け継ぎます。 responseチャットメッセージにはcontextプロパティがあり、これはOpenAIをあなたのデータに付けるために追加Azureます。

Chat message

レスポンスアシスタントメッセージスキーマはチャット完了アシスタント チャットメッセージから継承し、プロパティ contextで拡張されます。

Name タイプ Required Description
context Context False リクエスト処理中にAzure OpenAIがあなたのデータに対して行った段階的なステップ(取得済み文書を含む)を表します。

Context

Name タイプ Required Description
citations Citation[] False データソースの取得結果は、応答のアシスタントメッセージを生成するために使用されます。 クライアントは引用から参照を表示できます。
intent 文字列 False チャット履歴から検出された意図。 以前の意図を返す必要はなくなりました。 この性質は無視してください。
all_retrieved_documents Retrieved documents[] False 回収されたすべての文書。

Citation

Name タイプ Required Description
content 文字列 True 引用内容。
title 文字列 False 表彰状のタイトル。
url 文字列 False 引用のURLです。
filepath 文字列 False 引用のファイルパス。
chunk_id 文字列 False 引用のチャンクIDです。

Retrieved documents

Name タイプ Required Description
search_queries string[] True 文書を取得するために使われる検索クエリ。
data_source_index 整数 True データソースのインデックス。
original_search_score double True 取得した文書の元の検索スコア。
rerank_score double False 取得した文書の再ランクスコア。
filter_reason 文字列 False 文書をフィルタリングする理由を表しています。 文書がフィルタリングを受けない場合、このフィールドは設定されていないままです。 文書がstrictnessによって定義された元の検索スコアの閾値でフィルタリングされている場合、scoreとなります。 元の検索スコアの閾値でフィルタリングされず、リランクスコアとtop_n_documentsでフィルタリングされている場合はrerankとなります。

Data source

このリストはサポートされているデータソースを示しています。

Examples

この例は、より良い結果を得るために会話履歴を渡す方法を示しています。

Prerequisites:

  • Azure OpenAIシステム割り当て管理型IDからAzure検索サービスに役割割り当てを設定してください。 必須役割: Search Index Data ReaderSearch Service Contributor
  • ユーザーからAzure OpenAIリソースへの役割割り当てを設定してください。 必須役割: Cognitive Services OpenAI User
  • Az CLIをインストールしてaz loginを実行します。
  • 以下の環境変数を定義します: AzureOpenAIEndpointChatCompletionsDeploymentNameSearchEndpointSearchIndex
export AzureOpenAIEndpoint=https://example.openai.azure.com/
export ChatCompletionsDeploymentName=turbo
export SearchEndpoint=https://example.search.windows.net
export SearchIndex=example-index

azure-identity openai最新のPIPパッケージをインストールしてください。

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")
search_endpoint = os.environ.get("SearchEndpoint")
search_index = os.environ.get("SearchIndex")

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

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

completion = client.chat.completions.create(
    model=deployment,
    messages=[
        {
            "role": "user",
            "content": "Who is DRI?",
        },
        {
            "role": "assistant",
            "content": "DRI stands for Directly Responsible Individual of a service. Which service are you asking about?"
        },
        {
            "role": "user",
            "content": "Opinion mining service"
        }
    ],
    extra_body={
        "data_sources": [
            {
                "type": "azure_search",
                "parameters": {
                    "endpoint": search_endpoint,
                    "index_name": search_index,
                    "authentication": {
                        "type": "system_assigned_managed_identity"
                    }
                }
            }
        ]
    }
)

print(completion.model_dump_json(indent=2))

# render the citations

content = completion.choices[0].message.content
context = completion.choices[0].message.context
for citation_index, citation in enumerate(context["citations"]):
    citation_reference = f"[doc{citation_index + 1}]"
    url = "https://example.com/?redirect=" + citation["url"] # replace with actual host and encode the URL
    filepath = citation["filepath"]
    title = citation["title"]
    snippet = citation["content"]
    chunk_id = citation["chunk_id"]
    replaced_html = f"<a href='{url}' title='{title}\n{snippet}''>(See from file {filepath}, Part {chunk_id})</a>"
    content = content.replace(citation_reference, replaced_html)
print(content)