跳到主要內容
Open In ColabOpen on GitHub

AzureMLChatOnlineEndpoint

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

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

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

from langchain_community.chat_models.azureml_endpoint import 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:格式化請求和回應資料,適用於遵循 OpenAI API 規格進行請求和回應的模型,例如 LLaMa2-chat。

注意: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

此頁面是否對您有幫助?