跳到主要內容

AzureMLChatOnlineEndpoint

Azure Machine Learning 是一個用於構建、訓練和部署機器學習模型的平台。使用者可以瀏覽模型目錄中要部署的模型類型,其中提供了來自不同供應商的基礎和通用模型。

一般來說,您需要部署模型才能使用其預測(推論)。在 Azure Machine Learning 中,線上端點 用於部署這些模型並提供即時服務。它們基於 端點部署 的概念,使您可以將生產工作負載的介面與為其提供服務的實作分離。

本筆記本介紹如何使用託管在 Azure Machine Learning Endpoint 上的聊天模型。

from langchain_community.chat_models.azureml_endpoint import AzureMLChatOnlineEndpoint
API 參考文檔:AzureMLChatOnlineEndpoint

設定

您必須在 Azure ML 上部署模型在 Azure AI Studio 上部署模型 並獲取以下參數

  • endpoint_url:端點提供的 REST 端點 URL。
  • endpoint_api_type:當將模型部署到專用端點(託管的託管基礎架構)時,請使用 endpoint_type='dedicated'。當使用隨用隨付產品(模型即服務)部署模型時,請使用 endpoint_type='serverless'
  • endpoint_api_key:端點提供的 API 金鑰

內容格式器

content_formatter 參數是用於轉換 AzureML 端點的請求和響應以匹配所需架構的處理常式類別。由於模型目錄中有各種各樣的模型,每個模型處理數據的方式可能彼此不同,因此提供了 ContentFormatterBase 類別,允許使用者根據自己的喜好轉換數據。以下內容格式器已提供

  • CustomOpenAIChatContentFormatter:格式化請求和響應數據,用於像 LLaMa2-chat 這樣遵循 OpenAI API 規範進行請求和響應的模型。

注意:langchain.chat_models.azureml_endpoint.LlamaChatContentFormatter 正在被棄用,並被 langchain.chat_models.azureml_endpoint.CustomOpenAIChatContentFormatter 取代。

您可以為您的模型實作特定的自訂內容格式器,這些格式器衍生自 langchain_community.llms.azureml_endpoint.ContentFormatterBase 類別。

範例

以下章節包含有關如何使用此類別的範例

範例:使用即時端點的聊天完成

from langchain_community.chat_models.azureml_endpoint import (
AzureMLEndpointApiType,
CustomOpenAIChatContentFormatter,
)
from langchain_core.messages import HumanMessage

chat = AzureMLChatOnlineEndpoint(
endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score",
endpoint_api_type=AzureMLEndpointApiType.dedicated,
endpoint_api_key="my-api-key",
content_formatter=CustomOpenAIChatContentFormatter(),
)
response = chat.invoke(
[HumanMessage(content="Will the Collatz conjecture ever be solved?")]
)
response
AIMessage(content='  The Collatz Conjecture is one of the most famous unsolved problems in mathematics, and it has been the subject of much study and research for many years. While it is impossible to predict with certainty whether the conjecture will ever be solved, there are several reasons why it is considered a challenging and important problem:\n\n1. Simple yet elusive: The Collatz Conjecture is a deceptively simple statement that has proven to be extraordinarily difficult to prove or disprove. Despite its simplicity, the conjecture has eluded some of the brightest minds in mathematics, and it remains one of the most famous open problems in the field.\n2. Wide-ranging implications: The Collatz Conjecture has far-reaching implications for many areas of mathematics, including number theory, algebra, and analysis. A solution to the conjecture could have significant impacts on these fields and potentially lead to new insights and discoveries.\n3. Computational evidence: While the conjecture remains unproven, extensive computational evidence supports its validity. In fact, no counterexample to the conjecture has been found for any starting value up to 2^64 (a number', additional_kwargs={}, example=False)

範例:使用隨用隨付部署的聊天完成(模型即服務)

chat = AzureMLChatOnlineEndpoint(
endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/v1/chat/completions",
endpoint_api_type=AzureMLEndpointApiType.serverless,
endpoint_api_key="my-api-key",
content_formatter=CustomOpenAIChatContentFormatter,
)
response = chat.invoke(
[HumanMessage(content="Will the Collatz conjecture ever be solved?")]
)
response

如果您需要將其他參數傳遞給模型,請使用 model_kwargs 參數。

chat = AzureMLChatOnlineEndpoint(
endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/v1/chat/completions",
endpoint_api_type=AzureMLEndpointApiType.serverless,
endpoint_api_key="my-api-key",
content_formatter=CustomOpenAIChatContentFormatter,
model_kwargs={"temperature": 0.8},
)

參數也可以在調用期間傳遞。

response = chat.invoke(
[HumanMessage(content="Will the Collatz conjecture ever be solved?")],
max_tokens=512,
)
response

此頁面是否有幫助?