Azure ML
Azure ML 是一个用于构建、训练和部署机器学习模型的平台。用户可以在模型目录中探索要部署的模型类型,该目录提供了来自不同提供商的基础模型和通用模型。
本笔记本将介绍如何使用托管在 Azure ML Online Endpoint 上的 LLM。
##Installing the langchain packages needed to use the integration
%pip install -qU langchain-community
from langchain_community.llms.azureml_endpoint import AzureMLOnlineEndpoint
API Reference:AzureMLOnlineEndpoint
设置
您必须在 Azure ML 上部署模型或部署到 Azure AI Studio,并获取以下参数:
endpoint_url: 端点提供的 REST 端点 URL。endpoint_api_type: 将模型部署到 专用端点(托管的托管基础结构)时,请使用endpoint_type='dedicated'。 使用 即用即付(模型即服务)产品部署模型时,请使用endpoint_type='serverless'。endpoint_api_key: 端点提供的 API 密钥。deployment_name: (可选) 使用端点的模型的部署名称。
内容格式化程序
content_formatter 参数是一个处理程序类,用于转换 AzureML 终结点的请求和响应,以匹配所需架构。由于模型目录中存在各种各样的模型,每个模型可能以不同于其他模型的方式处理数据,因此提供了一个 ContentFormatterBase 类,允许用户根据自己的喜好转换数据。提供以下内容格式化程序:
GPT2ContentFormatter: 为 GPT2 格式化请求和响应数据DollyContentFormatter: 为 Dolly-v2 格式化请求和响应数据HFContentFormatter: 为文本生成 Hugging Face 模型格式化请求和响应数据CustomOpenAIContentFormatter: 为 LLaMa2 等遵循 OpenAI API 兼容方案的模型格式化请求和响应数据。
注意:OSSContentFormatter 正在被弃用,并被 GPT2ContentFormatter 取代。逻辑相同,但 GPT2ContentFormatter 是一个更合适的名称。您仍然可以继续使用 OSSContentFormatter,因为这些更改是向后兼容的。
示例
示例:使用实时终结点进行 LlaMa 2 补全
from langchain_community.llms.azureml_endpoint import (
AzureMLEndpointApiType,
CustomOpenAIContentFormatter,
)
from langchain_core.messages import HumanMessage
llm = AzureMLOnlineEndpoint(
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=CustomOpenAIContentFormatter(),
model_kwargs={"temperature": 0.8, "max_new_tokens": 400},
)
response = llm.invoke("Write me a song about sparkling water:")
response
模型参数也可以在调用时指定:
response = llm.invoke("Write me a song about sparkling water:", temperature=0.5)
response