在 MLflow 部署服务器中查询端点
部署服务器运行正常后,是时候向其发送一些数据了。您可以使用部署 API 或 REST API 与网关服务器交互。在此示例中,为简单起见,我们将使用部署 API。
我们来详细介绍支持的三种模型类型
-
补全:这种模型用于根据提供的输入生成预测或建议,帮助“补全”序列或模式。
-
聊天:这些模型促进交互式对话,能够以对话方式理解和响应用户输入。
-
嵌入:嵌入模型将输入数据(如文本或图像)转换为数值向量空间,其中相似的项目在该空间中位置接近,从而促进各种机器学习任务。
在接下来的步骤中,我们将探讨如何使用这些模型类型查询网关服务器。
示例 1:补全
补全模型旨在完成句子或响应提示。
要通过 MLflow AI 网关查询这些模型,您需要提供一个 prompt
参数,这是语言模型 (LLM) 将响应的字符串。网关服务器还支持各种其他参数。有关详细信息,请参阅文档。
from mlflow.deployments import get_deploy_client
client = get_deploy_client("https://:5000")
name = "completions"
data = dict(
prompt="Name three potions or spells in harry potter that sound like an insult. Only show the names.",
n=2,
temperature=0.2,
max_tokens=1000,
)
response = client.predict(endpoint=name, inputs=data)
print(response)
示例 2:聊天
聊天模型促进与用户的交互式对话,随着时间的推移逐渐积累上下文。
与其他模型类型相比,创建聊天负载略为复杂,因为它支持来自三种不同角色的无限数量的消息:system
、user
和 assistant
。要通过 MLflow AI 网关设置聊天负载,您需要指定一个 messages
参数。此参数接受一个按如下格式化的字典列表
{"role": "system/user/assistant", "content": "用户指定内容"}
有关更多详细信息,请查阅文档。
from mlflow.deployments import get_deploy_client
client = get_deploy_client("https://:5000")
name = "chat_3.5"
data = dict(
messages=[
{"role": "system", "content": "You are the sorting hat from harry potter."},
{
"role": "user",
"content": "I am brave, hard-working, wise, and backstabbing.",
},
{
"role": "user",
"content": "Which harry potter house am I most likely to belong to?",
},
],
n=3,
temperature=0.5,
)
response = client.predict(endpoint=name, inputs=data)
print(response)
示例 3:嵌入
嵌入模型将令牌转换为数值向量。
要通过 MLflow AI 网关使用嵌入模型,请提供一个 text
参数,该参数可以是字符串或字符串列表。然后,网关服务器处理这些字符串并返回它们各自的数值向量。让我们继续看一个示例...
from mlflow.deployments import get_deploy_client
client = get_deploy_client("https://:5000")
name = "embeddings"
data = dict(
input=[
"Gryffindor: Values bravery, courage, and leadership.",
"Hufflepuff: Known for loyalty, a strong work ethic, and a grounded nature.",
"Ravenclaw: A house for individuals who value wisdom, intellect, and curiosity.",
"Slytherin: Appreciates ambition, cunning, and resourcefulness.",
],
)
response = client.predict(endpoint=name, inputs=data)
print(response)
大功告成!您已成功设置您的第一个网关服务器并提供了三个 OpenAI 模型。