使用 Databricks 查询 AI 模型

使用 Python SDK 的 Databricks

Avi Steinberg

Senior Software Engineer

基础模型(Foundation Models)

基础模型图像

  • 大型预训练神经网络
  • 在多样的大规模数据集上训练
  • 托管在 Databricks 或外部
1 https://docs.databricks.com/aws/en/machine-learning/model-serving/foundation-model-overview
使用 Python SDK 的 Databricks

常见聊天补全模型

托管于 Databricks 之外

  • OpenAI 的 ChatGPT
  • Anthropic 的 Claude
  • Google Cloud 的 Gemini

OpenAI ChatGPT

Anthropic Claude

Google Gemini

托管于 Databricks

  • Meta Llama

$$

Databricks Meta Llama

使用 Python SDK 的 Databricks

Databricks Meta Llama

  • Meta 与 Databricks 合作,在 Databricks 托管 Meta Llama
  • 由 Meta 构建并训练的大语言模型(LLM)
  • 性能上可与 OpenAI GPT 竞争

Databricks Meta Llama

1 1. https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/supported-models#meta-llama-3-3-70b-instruct 2. https://www.databricks.com/product/pricing/foundation-model-serving
使用 Python SDK 的 Databricks

服务端点(Serving endpoints)

  • Databricks 已为基础模型提供 Serving Endpoint
  • 可为外部与自定义模型创建端点
  • 可用 Databricks SDK 查询基础模型

Serving Endpoint

1 https://docs.databricks.com/api/workspace/servingendpoints
使用 Python SDK 的 Databricks

服务端点 API

  • 通过向相应的服务端点发起 API 请求来查询 AI 模型
  • 通过 WorkspaceClientserving_endpoints 属性访问
1 https://docs.databricks.com/api/workspace/servingendpoints
使用 Python SDK 的 Databricks

聊天消息角色

SYSTEM

指示模型如何响应查询的说明

USER:

用户发送给模型的查询

示例提示

"You are a helpful python coding assistant."

示例提示

"How do for loops work in Python?"

1 https://databricks-sdk-py.readthedocs.io/en/stable/dbdataclasses/serving.html#databricks.sdk.service.serving.ChatMessageRole
使用 Python SDK 的 Databricks

使用 Databricks 查询聊天模型

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ChatMessage, ChatMessageRole
w = WorkspaceClient()
response = w.serving_endpoints.query(
    name="databricks-meta-llama-3-3-70b-instruct",
    messages=[
        ChatMessage(
            role=ChatMessageRole.SYSTEM, content="You are a helpful assistant."
        ),
        ChatMessage(role=ChatMessageRole.USER, content="<your-question>"),
    ],
    max_tokens=128)
print(f"RESPONSE:\n{response.choices[0].message.content}")
1 https://docs.databricks.com/aws/en/machine-learning/foundation-model-apis/supported-models#meta-llama-33-70b-instruct
使用 Python SDK 的 Databricks

让我们练习吧!

使用 Python SDK 的 Databricks

Preparing Video For Download...