跳到主要内容

查询 MLflow 部署服务器中的端点

现在部署服务器已运行,是时候向其发送一些数据了。您可以通过部署 API 或 REST API 与网关服务器进行交互。在此示例中,我们将使用部署 API 以简化操作。

让我们详细说明三种支持的模型类型

  1. Completions:此模型类型用于根据提供的输入生成预测或建议,帮助“完成”序列或模式。

  2. Chat:这些模型有助于进行交互式对话,能够以对话方式理解并响应用户输入。

  3. Embeddings:嵌入模型将输入数据(如文本或图像)转换为数值向量空间,其中相似的项目在空间中彼此靠近,从而便于进行各种机器学习任务。

在接下来的步骤中,我们将探讨如何使用这些模型类型来查询网关服务器。

示例 1:Completions

Completions 模型旨在完成句子或响应提示。

要通过 MLflow AI Gateway 查询这些模型,您需要提供一个 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:Chat

Chat 模型有助于与用户进行交互式对话,并随着时间的推移逐渐累积上下文。

创建聊天负载比其他模型类型稍微复杂一些,因为它支持来自三种不同角色的无限数量的消息:systemuserassistant。要通过 MLflow AI Gateway 设置聊天负载,您需要指定一个 messages 参数。此参数接受一个字典列表,格式如下:

{"role": "system/user/assistant", "content": "user-specified 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:Embeddings

Embeddings 模型将 token 转换为数值向量。

要通过 MLflow AI Gateway 使用 Embeddings 模型,请提供一个 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 模型。