共用方式為


查詢基礎模型

在本文中,您將瞭解如何格式化基礎模型的查詢要求,並將其傳送至您的模型服務端點。

如需傳統 ML 或 Python 模型查詢要求,請參閱 查詢自定義模型的端點服務。

Databricks 模型服務 支持 基礎模型 API外部模型來存取基礎模型 ,並使用統一的 OpenAI 相容 API 和 SDK 來查詢它們。 這可讓您針對支援雲端和提供者的生產環境進行實驗和自定義基礎模型。

Databricks 模型服務提供下列選項,可將評分要求傳送至基礎模型:

方法 詳細資料
OpenAI 用戶端 使用 OpenAI 用戶端查詢 Databricks 模型服務端點所裝載的模型。 指定提供端點名稱的 model 模型做為輸入。 支援由基礎模型 API 或外部模型提供的聊天、內嵌和完成模型。
提供UI 從 [服務端點] 頁面選取 [查詢端點]。 插入 JSON 格式模型輸入數據,然後按兩下 [ 傳送要求]。 如果模型已記錄輸入範例,請使用 顯示範例 來載入它。
REST API 使用 REST API 呼叫和查詢模型。 如需詳細資訊,請參閱 POST /serving-endpoints/{name}/調用 。 如需為多個模型服務的端點評分要求,請參閱 查詢端點背後的個別模型。
MLflow 部署 SDK 使用 MLflow 部署 SDK 的 predict() 函式來查詢模型。
Databricks GenAI SDK Databricks GenAI SDK 是 REST API 之上的一層。 它會處理低階詳細數據,例如驗證和將模型標識符對應至端點 URL,讓您更輕鬆地與模型互動。 SDK 的設計目的是要從 Databricks 筆記本內使用。
SQL 函式 使用 SQL 函式直接從 SQL 叫用 ai_query 模型推斷。 請參閱 使用 ai_query() 查詢服務模型。

需求

  • 提供 端點的模型。
  • 支持區域中的 Databricks 工作區。
  • 若要透過OpenAI用戶端、REST API或 MLflow 部署 SDK 傳送評分要求,您必須具有 Databricks API 令牌。

重要

Databricks 是生產案例的安全性最佳做法,建議您在生產期間使用 機器對機器 OAuth 令牌 進行驗證。

為了測試和開發,Databricks 建議使用屬於 服務主體 的個人存取令牌,而不是工作區使用者。 若要建立服務主體的令牌,請參閱 管理服務主體的令牌。

安裝套件

選取查詢方法之後,您必須先將適當的套件安裝到叢集。

Openai 用戶端

若要使用 OpenAI 用戶端, openai 套件必須安裝在叢集上。 在您的筆記本或本機終端機中執行下列命令:

!pip install openai

只有在 Databricks Notebook 上安裝套件時,才需要下列專案

dbutils.library.restartPython()

Rest API

Databricks Runtime 中提供對服務 REST API 的存取權,適用於 機器學習。

Mlflow 部署 sdk

!pip install mlflow

只有在 Databricks Notebook 上安裝套件時,才需要下列專案

dbutils.library.restartPython()

Databricks genai sdk

 !pip install databricks-genai

只有在 Databricks Notebook 上安裝套件時,才需要下列專案

 dbutils.library.restartPython()

查詢聊天完成模型

以下是查詢聊天模型的範例。

如需批次推斷範例,請參閱 使用基礎模型 API 進行批次推斷。

Openai 用戶端

以下是您工作區中基礎模型 API 依令牌付費端點 databricks-dbrx-instruct 所提供的 DBRX 指示模型聊天要求。

若要使用 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-dbrx-instruct",
    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

以下是您工作區中基礎模型 API 依令牌付費端點 databricks-dbrx-instruct 所提供的 DBRX 指示模型聊天要求。

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/databricks-dbrx-instruct/invocations \

Mlflow 部署 sdk

重要

下列範例使用 predict() MLflow 部署 SDK 中的 API。

以下是您工作區中基礎模型 API 依令牌付費端點 databricks-dbrx-instruct 所提供的 DBRX 指示模型聊天要求。


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-dbrx-instruct",
    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 genai sdk

以下是您工作區中基礎模型 API 依令牌付費端點 databricks-dbrx-instruct 所提供的 DBRX 指示模型聊天要求。

from databricks_genai_inference import ChatCompletion

# 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"

response = ChatCompletion.create(model="databricks-dbrx-instruct",
                                messages=[{"role": "system", "content": "You are a helpful assistant."},
                                          {"role": "user","content": "What is a mixture of experts model?"}],
                                max_tokens=128)
print(f"response.message:{response.message}")

Langchain

若要 使用 LangChain 查詢基礎模型端點,您可以執行下列其中一項:

  • 匯入 Databricks LLM 類別並指定 endpoint_name 與 。 transform_input_fn
  • 匯入 ChatDatabricks ChatModel 類別並指定 endpoint

下列範例會Databricks使用 LangChain 中的 LLM 類別來查詢基礎模型 API 依令牌付費端點 。 databricks-dbrx-instruct 基礎模型 API 預期 messages 在要求字典中,而 LangChain Databricks LLM 預設會在要求字典中提供 prompt 。 使用函 transform_input 式將要求字典準備成預期的格式。

from langchain.llms import Databricks
from langchain_core.messages import HumanMessage, SystemMessage

def transform_input(**request):
  request["messages"] = [
    {
      "role": "user",
      "content": request["prompt"]
    }
  ]
  del request["prompt"]
  return request

llm = Databricks(endpoint_name="databricks-dbrx-instruct", transform_input_fn=transform_input)
llm("What is a mixture of experts model?")

下列範例使用 ChatDatabricks ChatModel 類別,並指定 endpoint

from langchain.chat_models import ChatDatabricks
from langchain_core.messages import HumanMessage, SystemMessage

messages = [
    SystemMessage(content="You're a helpful assistant"),
    HumanMessage(content="What is a mixture of experts model?"),
]
chat_model = ChatDatabricks(endpoint="databricks-dbrx-instruct", max_tokens=500)
chat_model.invoke(messages)

SQL

重要

下列範例使用內建的 SQL 函式, ai_query。 此函式為 公開預覽 ,而且定義可能會變更。 請參閱 使用 ai_query() 查詢服務模型。

以下是您工作區中基礎模型 API 按令牌付費端點databricks-llama-2-70b-chat提供的聊天要求llama-2-70b-chat

注意

ai_query() 式不支援提供 DBRX 或 DBRX 指示模型的查詢端點。

SELECT ai_query(
    "databricks-llama-2-70b-chat",
    "Can you explain AI in ten words?"
  )

以下是聊天模型的預期要求格式。 針對外部模型,您可以包含適用於指定提供者和端點組態的其他參數。 請參閱 其他查詢參數

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

以下是預期的回應格式:

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

聊天工作階段

Databricks GenAI SDK 提供 ChatSession 類別來管理多輪聊天交談。 其提供的功能包括:

函式 傳回 描述
reply (string) 取得新的使用者訊息
last 字串 助理的最後一則訊息
history 聽寫清單 聊天記錄中的訊息,包括角色。
count int 到目前為止進行的聊天回合數目。

若要初始化 ChatSession,您可以使用與 ChatCompletion相同的自變數集,而且這些自變數會在整個聊天會話中使用。


from databricks_genai_inference import ChatSession

chat = ChatSession(model="llama-2-70b-chat", system_message="You are a helpful assistant.", max_tokens=128)
chat.reply("Knock, knock!")
chat.last # return "Hello! Who's there?"
chat.reply("Guess who!")
chat.last # return "Okay, I'll play along! Is it a person, a place, or a thing?"

chat.history
# return: [
#     {'role': 'system', 'content': 'You are a helpful assistant.'},
#     {'role': 'user', 'content': 'Knock, knock.'},
#     {'role': 'assistant', 'content': "Hello! Who's there?"},
#     {'role': 'user', 'content': 'Guess who!'},
#     {'role': 'assistant', 'content': "Okay, I'll play along! Is it a person, a place, or a thing?"}
# ]

查詢內嵌模型

以下是基礎模型 API 所提供模型的內嵌要求 bge-large-en

Openai 用戶端

若要使用 OpenAI 用戶端,請將服務端點名稱的 model 模型指定為輸入。 下列範例假設您有 Databricks API 令牌,並 openai 安裝在叢集上。


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.embeddings.create(
  model="databricks-bge-large-en",
  input="what is databricks"
)

Rest API

重要

下列範例會使用 REST API 參數來查詢服務基礎模型的端點。 這些參數為 公開預覽 ,而且定義可能會變更。 請參閱 POST /serving-endpoints/{name}/invocations


curl \
-u token:$DATABRICKS_TOKEN \
-X POST \
-H "Content-Type: application/json" \
-d  '{ "input": "Embed this sentence!"}' \
https://<workspace_host>.databricks.com/serving-endpoints/databricks-bge-large-en/invocations

Mlflow 部署 sdk

重要

下列範例使用 predict() MLflow 部署 SDK 中的 API。


import mlflow.deployments

export DATABRICKS_HOST="https://<workspace_host>.databricks.com"
export DATABRICKS_TOKEN="dapi-your-databricks-token"

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

embeddings_response = client.predict(
    endpoint="databricks-bge-large-en",
    inputs={
        "input": "Here is some text to embed"
    }
)

Databricks genai sdk


from databricks_genai_inference import Embedding

# 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"

response = Embedding.create(
    model="bge-large-en",
    input="3D ActionSLAM: wearable person tracking in multi-floor environments")
print(f'embeddings: {response.embeddings}')

Langchain

若要在 LangChain 中使用 Databricks Foundation Model API 模型作為內嵌模型,請匯DatabricksEmbeddings入 類別並指定 endpoint 參數,如下所示:

from langchain.embeddings import DatabricksEmbeddings

embeddings = DatabricksEmbeddings(endpoint="databricks-bge-large-en")
embeddings.embed_query("Can you explain AI in ten words?")

SQL

重要

下列範例使用內建的 SQL 函式, ai_query。 此函式為 公開預覽 ,而且定義可能會變更。 請參閱 使用 ai_query() 查詢服務模型。


SELECT ai_query(
    "databricks-bge-large-en",
    "Can you explain AI in ten words?"
  )

以下是內嵌模型的預期要求格式。 針對外部模型,您可以包含適用於指定提供者和端點組態的其他參數。 請參閱 其他查詢參數


{
  "input": [
    "embedding text"
  ]
}

以下是預期的回應格式:

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": []
    }
  ],
  "model": "text-embedding-ada-002-v2",
  "usage": {
    "prompt_tokens": 2,
    "total_tokens": 2
  }
}

查詢文字完成模型

以下是基礎模型 API 所提供的模型完成要求 databricks-mpt-30b-instruct 。 如需參數和語法,請參閱 完成工作

Openai 用戶端

若要使用 OpenAI 用戶端,請將服務端點名稱的 model 模型指定為輸入。 下列範例假設您有 Databricks API 令牌,並 openai 安裝在叢集上。


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

completion = client.completions.create(
  model="databricks-mpt-30b-instruct",
  prompt="what is databricks",
  temperature=1.0
)

Rest API

重要

下列範例會使用 REST API 參數來查詢服務基礎模型的端點。 這些參數為 公開預覽 ,而且定義可能會變更。 請參閱 POST /serving-endpoints/{name}/invocations


curl \
 -u token:$DATABRICKS_TOKEN \
 -X POST \
 -H "Content-Type: application/json" \
 -d '{"prompt": "What is a quoll?", "max_tokens": 64}' \
https://<workspace_host>.databricks.com/serving-endpoints/databricks-mpt-30b-instruct/invocations

Mlflow 部署 sdk

重要

下列範例使用 predict() MLflow 部署 SDK 中的 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")

completions_response = client.predict(
    endpoint="databricks-mpt-30b-instruct",
    inputs={
        "prompt": "What is the capital of France?",
        "temperature": 0.1,
        "max_tokens": 10,
        "n": 2
    }
)

Databricks genai sdk


from databricks_genai_inference import Completion

# 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"

response = Completion.create(
    model="databricks-mpt-30b-instruct",
    prompt="Write 3 reasons why you should train an AI model on domain specific data sets.",
    max_tokens=128)
print(f"response.text:{response.text:}")

SQL

重要

下列範例使用內建的 SQL 函式, ai_query。 此函式為 公開預覽 ,而且定義可能會變更。 請參閱 使用 ai_query() 查詢服務模型。

SELECT ai_query(
    "databricks-mpt-30b-instruct",
    "Can you explain AI in ten words?"
  )

以下是完成模型的預期要求格式。 針對外部模型,您可以包含適用於指定提供者和端點組態的其他參數。 請參閱 其他查詢參數

{
  "prompt": "What is mlflow?",
  "max_tokens": 100,
  "temperature": 0.1,
  "stop": [
    "Human:"
  ],
  "n": 1,
  "stream": false,
  "extra_params":{
    "top_p": 0.9
  }
}

以下是預期的回應格式:

{
  "id": "cmpl-8FwDGc22M13XMnRuessZ15dG622BH",
  "object": "text_completion",
  "created": 1698809382,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
    "text": "MLflow is an open-source platform for managing the end-to-end machine learning lifecycle. It provides tools for tracking experiments, managing and deploying models, and collaborating on projects. MLflow also supports various machine learning frameworks and languages, making it easier to work with different tools and environments. It is designed to help data scientists and machine learning engineers streamline their workflows and improve the reproducibility and scalability of their models.",
    "index": 0,
    "logprobs": null,
    "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 83,
    "total_tokens": 88
  }
}

使用 AI 遊樂場與支援的 LLM 聊天

您可以使用 AI 遊樂場與支援的大型語言模型互動。 AI 遊樂場是類似聊天的環境,您可以從 Azure Databricks 工作區測試、提示和比較 LLM。

AI 遊樂場

其他資源