查询聊天模型

本文介绍如何为已针对聊天和常规用途任务进行优化的基础模型编写查询请求,并将其发送到模型服务终结点。

本文中的示例适用于查询使用以下任一方法提供的基础模型:

要求

查询示例

本节中的示例演示如何使用不同的databricks-claude-sonnet-4-5*查询基础模型 API 按令牌付费终结点提供的人类 Claude Sonnet 4.5 模型。

OpenAI 客户端

要使用 OpenAI 客户端,需将模型服务终结点名称指定为 model 输入。 以下示例假定你有 一个 Databricks API 令牌 ,并 openai 安装在计算中。 还需要 Databricks 工作区实例 才能将 OpenAI 客户端连接到 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.chat.completions.create(
    model="databricks-claude-sonnet-4-5",
    messages=[
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "What is a mixture of experts model?",
      }
    ],
    max_tokens=256
)

REST API

重要

以下示例使用 REST API 参数来查询为外部模型提供服务的终结点。 这些参数为 公共预览版 ,定义可能会更改。 请参阅 POST /serving-endpoints/{name}/invocations

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

MLflow 部署 SDK

重要

以下示例使用来自predict() API。


import mlflow.deployments

# Only required when running this example outside of a Databricks Notebook
export DATABRICKS_HOST="https://<workspace_host>.databricks.com"
export DATABRICKS_TOKEN="dapi-your-databricks-token"

client = mlflow.deployments.get_deploy_client("databricks")

chat_response = client.predict(
    endpoint="databricks-claude-sonnet-4-5",
    inputs={
        "messages": [
            {
              "role": "user",
              "content": "Hello!"
            },
            {
              "role": "assistant",
              "content": "Hello! How can I assist you today?"
            },
            {
              "role": "user",
              "content": "What is a mixture of experts model??"
            }
        ],
        "temperature": 0.1,
        "max_tokens": 20
    }
)

Databricks Python SDK

此代码必须在工作区的笔记本中运行。 请参阅在 Azure Databricks 笔记本中使用 Databricks SDK for Python

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ChatMessage, ChatMessageRole

w = WorkspaceClient()
response = w.serving_endpoints.query(
    name="databricks-claude-sonnet-4-5",
    messages=[
        ChatMessage(
            role=ChatMessageRole.SYSTEM, content="You are a helpful assistant."
        ),
        ChatMessage(
            role=ChatMessageRole.USER, content="What is a mixture of experts model?"
        ),
    ],
    max_tokens=128,
)
print(f"RESPONSE:\n{response.choices[0].message.content}")

例如,以下是使用 REST API 时聊天模型的预期请求格式。 对于外部模型,可以包含对给定提供程序和终结点配置有效的其他参数。 请参阅其他查询参数

{
  "messages": [
    {
      "role": "user",
      "content": "What is a mixture of experts model?"
    }
  ],
  "max_tokens": 100,
  "temperature": 0.1
}

以下是使用 REST API 发出的请求的预期响应格式:

{
  "model": "databricks-claude-sonnet-4-5",
  "choices": [
    {
      "message": {},
      "index": 0,
      "finish_reason": null
    }
  ],
  "usage": {
    "prompt_tokens": 7,
    "completion_tokens": 74,
    "total_tokens": 81
  },
  "object": "chat.completion",
  "id": null,
  "created": 1698824353
}

支持的模型

有关支持的聊天模型,请参阅 基础模型类型

其他资源