共用方式為


數據 API 參考上的 Azure OpenAI

本文提供適用於新 Azure OpenAI On Data API 的 Python 和 REST 參考檔。 最新的 API 版本是 2024-05-01-previewSwagger 規格

注意

由於 API 版本 2024-02-15-preview ,我們引進了下列與舊版 API 版本比較的重大變更:

  • API 路徑會從 /extensions/chat/completions 變更為 /chat/completions
  • 屬性索引鍵和列舉值的命名慣例會從駱駝大小寫變更為蛇套。 範例: deploymentName 已變更為 deployment_name
  • 資料來源型態 AzureCognitiveSearch 會變更為 azure_search
  • 引文和意圖會從小幫手訊息的內容工具訊息移至已定義明確架構的助理訊息內容根層級。
POST {endpoint}/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}

支援的版本

注意

Azure Machine Learning 索引PineconeElasticsearch 支援為預覽版。

URI 參數

名稱 位於 類型 必要 描述
deployment-id path 字串 True 指定要用於此要求的聊天完成模型部署名稱。
endpoint path 字串 True Azure OpenAI 端點。 例如:https://{YOUR_RESOURCE_NAME}.openai.azure.com
api-version query 字串 True 用於此作業的 API 版本。

要求本文

要求本文會繼承聊天完成 API 要求的相同架構。 下表顯示 Azure OpenAI On Your Data 的唯一參數。

名稱 類型​​ 必要 描述
data_sources DataSource[] True Azure OpenAI On Your Data 的組態專案。 陣列中必須只有一個專案。 如果未 data_sources 提供,服務會直接使用聊天完成模型,而且不會使用 Azure OpenAI On Your Data。 當您指定 data_sources 參數時,將無法使用 logprobstop_logprobs 參數。

回應本文

回應本文會繼承聊天完成 API 回應的相同架構。 回應 聊天訊息 具有 context 屬性,已針對 Azure OpenAI On Your Data 新增。

聊天訊息

回應小幫手訊息架構繼承自聊天完成小幫手 聊天訊息,並以 屬性 context擴充。

名稱 類型​​ 必要 描述
context 內容 False 表示 Azure OpenAI On Your Data 在處理要求時所執行的累加步驟,包括擷取的檔。

上下文

名稱 類型​​ 必要 描述
citations 引文[] False 用來在響應中產生小幫手訊息的數據源擷取結果。 用戶端可以從引文轉譯參考。
intent 字串 False 從聊天記錄偵測到的意圖。 不再需要傳回先前的意圖。 忽略這個屬性。
all_retrieved_documents 擷取的檔[] False 所有擷取的檔。

引文

名稱 類型​​ 必要 描述
content string True 引文的內容。
title 字串 False 引文的標題。
url 字串 False 引文的 URL。
filepath 字串 False 引文的檔案路徑。
chunk_id 字串 False 引文的區塊標識碼。

擷取的檔

名稱 類型​​ 必要 描述
search_queries string[] True 用來擷取檔的搜尋查詢。
data_source_index 整數 True 數據源的索引。
original_search_score double True 所擷取檔的原始搜尋分數。
rerank_score double False 擷取檔的重新評分。
filter_reason 字串 False 表示篩選檔的理由。 如果檔未經過篩選,此欄位將會保持未設定。 score如果檔案是由 所定義的strictness原始搜尋分數臨界值篩選,將會是 。 如果 rerank 檔未依原始搜尋分數閾值進行篩選,但會依重新評分和 top_n_documents來篩選。

資料來源

此清單會顯示支持的數據來源。

範例

此範例示範如何傳遞交談歷程記錄以取得更好的結果。

先決條件:

  • 設定從 Azure OpenAI 系統指派受控識別給 Azure 搜尋服務的角色指派。 必要角色: Search Index Data ReaderSearch Service Contributor
  • 設定從使用者到 Azure OpenAI 資源的角色指派。 必要角色: Cognitive Services OpenAI User
  • 安裝 Az CLI,然後執行 az login
  • 定義下列環境變數:AzureOpenAIEndpoint、、、SearchEndpointSearchIndexChatCompletionsDeploymentName
export AzureOpenAIEndpoint=https://example.openai.azure.com/
export ChatCompletionsDeploymentName=turbo
export SearchEndpoint=https://example.search.windows.net
export SearchIndex=example-index

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

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-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))