Unity Gateway 快速入门

通过Unity Gateway向兼容OpenAI的端点发送你的第一个请求。

要求

Authenticate

开始时,使用个人访问令牌。 OAuth 推荐用于生产场景。 将 DATABRICKS_TOKEN 设置为你工作区的令牌。

个人访问令牌

导出 个人访问令牌:

export DATABRICKS_TOKEN=<your-personal-access-token>

OAuth

将 替换为 <workspace-url>。 然后用 Azure Databricks CLI 登录,导出一个短寿命的 OAuth 令牌:

databricks auth login --host https://<workspace-url>
export DATABRICKS_TOKEN=$(databricks auth token --host https://<workspace-url> | jq -r .access_token)

选一个模型

Azure Databricks 提供了现成型模型,您可以在 UI 的模式下system.ai浏览,或在这里查看完整的支持模型列表。

本快速入门使用 system.ai.gpt-5-2。 要查询其他模型,只需将其替换为对应的完全限定名称。

发送您的请求

将 替换为 <workspace-url>。

Bash

curl https://<workspace-url>/ai-gateway/mlflow/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -d '{
    "model": "system.ai.gpt-5-2",
    "max_output_tokens": 256,
    "input": [
      {
        "role": "user",
        "content": [{"type": "input_text", "text": "Hello!"}]
      },
      {
        "role": "assistant",
        "content": [{"type": "output_text", "text": "Hello! How can I assist you today?"}]
      },
      {
        "role": "user",
        "content": [{"type": "input_text", "text": "What is Databricks?"}]
      }
    ]
  }'

Python

from openai import OpenAI
import os

client = OpenAI(
  api_key=os.environ.get("DATABRICKS_TOKEN"),
  base_url="https://<workspace-url>/ai-gateway/mlflow/v1",
)

response = client.responses.create(
  model="system.ai.gpt-5-2",
  max_output_tokens=256,
  input=[
    {"role": "user", "content": [{"type": "input_text", "text": "Hello!"}]},
    {"role": "assistant", "content": [{"type": "output_text", "text": "Hello! How can I assist you today?"}]},
    {"role": "user", "content": [{"type": "input_text", "text": "What is Databricks?"}]},
  ],
)

print(response.output)

TypeScript

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.DATABRICKS_TOKEN,
  baseURL: 'https://<workspace-url>/ai-gateway/mlflow/v1',
});

const response = await client.responses.create({
  model: 'system.ai.gpt-5-2',
  max_output_tokens: 256,
  input: [
    { role: 'user', content: [{ type: 'input_text', text: 'Hello!' }] },
    { role: 'assistant', content: [{ type: 'output_text', text: 'Hello! How can I assist you today?' }] },
    { role: 'user', content: [{ type: 'input_text', text: 'What is Databricks?' }] },
  ],
});

console.log(response.output);

查看您的请求

在目录资源管理器中打开你查询的模型(system.ai.gpt-5-2),选择 “指标 ”标签,就能在使用历史中查看你刚刚发送的请求,以及其令牌使用情况和延迟。 参见 Unity Gateway 可观测性。

后续步骤