使用 OpenAI 回應 API 查詢

這很重要

Responses API 僅相容於 OpenAI 按代幣付費的基金模型及外部模型。 若要使用跨所有供應商的統一 API,請使用 Chat Completions API

OpenAI 回應 API 是聊天完成 API 的替代方案,為 OpenAI 模型提供額外功能,包括自訂工具與多步驟工作流程。

需求

查詢範例

本節範例展示了如何使用 OpenAI 回應 API 查詢 Foundation Model API 的按代幣付費端點。

Python

要使用 OpenAI Responses API,請將模型服務端點名稱指定為 model 輸入。 以下範例假設你的運算上安裝了an Azure Databricks API token 以及 openai。 你還需要你的 Azure Databricks workspace instance 來將 OpenAI 客戶端連接到 Azure Databricks。

import os
import openai
from openai import OpenAI

client = OpenAI(
    api_key="dapi-your-databricks-token",
    base_url="https://example.staging.cloud.databricks.com/serving-endpoints"
)

response = client.responses.create(
    model="databricks-gpt-5",
    input=[
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "What is a mixture of experts model?",
      }
    ],
    max_output_tokens=256
)

REST API

curl \
-u token:$DATABRICKS_TOKEN \
-X POST \
-H "Content-Type: application/json" \
-d '{
  "model": "databricks-gpt-5",
  "input": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "What is a mixture of experts model?"
    }
  ],
  "max_output_tokens": 256
}' \
https://<workspace_host>.databricks.com/serving-endpoints/responses

自訂工具

自訂工具允許模型回傳任意字串輸出,而非 JSON 格式的函式參數。 這對於程式碼產生、套用修補程式或其他不需要結構化 JSON 的應用場景非常有用。

備註

自定義工具僅限於通過 Responses API 支援 GPT-5 系列模型(databricks-gpt-5, databricks-gpt-5-1, databricks-gpt-5-2, databricks-gpt-5-4, databricks-gpt-5-5, databricks-gpt-5-5-pro)。

from databricks_openai import DatabricksOpenAI

client = DatabricksOpenAI()

response = client.responses.create(
    model="databricks-gpt-5",
    input=[{"role": "user", "content": "Write a Python function to calculate factorial"}],
    tools=[
        {
            "type": "custom",
            "name": "code_exec",
            "description": "Executes arbitrary Python code. Return only valid Python code."
        }
    ],
    max_output_tokens=1024
)

內建工具

內建工具允許模型呼叫平台提供的功能,而無需你自行實作工具後端。 這些工具會回傳結構化的輸出,並由平台完全管理。

from databricks_openai import DatabricksOpenAI

client = DatabricksOpenAI()

response = client.responses.create(
    model="databricks-gpt-5",
    input=[{
        "role": "user",
        "content": "Add input validation to the factorial function in main.py."
    }],
    tools=[
        {
            "type": "apply_patch"
        }
    ],
    max_output_tokens=1024
)

print(response.output_text)

支援的模型

外部模型

  • OpenAI 模型提供者
  • Azure OpenAI 模型提供者

支援的輸入類型

Azure Databricks 上的 OpenAI GPT 模型接受文字與圖片輸入。 請參閱 查詢視覺模型 以了解影像格式與尺寸要求。 關於每個模型的輸入類型,請參閱 Foundation Model API 中提供的 Databricks 託管基礎模型

局限性

以下限制僅適用於 按代幣付費的基金會模型 。 外部模型支援所有回應 API 參數與工具。

以下參數不支援,若指定會回傳 400 錯誤:

  • background — 不支援背景處理。
  • store — 不支援儲存回應。
  • previous_response_id — 不支援儲存回應。
  • service_tier — 服務層級選擇由Azure Databricks管理。

以下工具類型支援用於按代幣付費的基金會模型:

  • function — 傳統結構化函式呼叫
  • custom — 自訂使用者定義工具
  • apply_patch — 程式碼補丁操作
  • shell — Shell 指令執行
  • image_generation — 影像生成
  • mcp — 模型情境協定工具
  • web_search — 網路搜尋

其他資源