支持 messages 格式 用于 OpenAI 聊天补全任务
该文档涵盖以下内容:
支持
messages格式,用于 OpenAI 聊天补全任务,在openai风格中。为每种格式记录了模型签名。
发送到 OpenAI chat completion API 的每种格式的有效负载。
每种格式的预期预测输入类型。
messages 带变量
messages 参数接受一个包含 role 和 content 键的字典列表。
content 字段在每条消息中可以包含变量(即命名格式字段)。当已记录的模型被加载并进行预测时,这些变量会被预测输入中的值替换。
单变量
import mlflow
import openai
with mlflow.start_run():
model_info = mlflow.openai.log_model(
name="model",
model="gpt-4o-mini",
task=openai.chat.completions,
messages=[
{
"role": "user",
"content": "Tell me a {adjective} joke",
# ^^^^^^^^^^
# variable
},
# Can contain more messages
],
)
model = mlflow.pyfunc.load_model(model_info.model_uri)
print(model.predict([{"adjective": "funny"}]))
已记录的模型签名:
{
"inputs": [{"type": "string"}],
"outputs": [{"type": "string"}],
}
预期的预测输入类型:
# A list of dictionaries with 'adjective' key
[{"adjective": "funny"}, ...]
# A list of strings
["funny", ...]
发送到 OpenAI chat completion API 的有效载荷:
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "Tell me a funny joke",
}
],
}
多个变量
import mlflow
import openai
with mlflow.start_run():
model_info = mlflow.openai.log_model(
name="model",
model="gpt-4o-mini",
task=openai.chat.completions,
messages=[
{
"role": "user",
"content": "Tell me a {adjective} joke about {thing}.",
# ^^^^^^^^^^ ^^^^^^^
# variable another variable
},
# Can contain more messages
],
)
model = mlflow.pyfunc.load_model(model_info.model_uri)
print(model.predict([{"adjective": "funny", "thing": "vim"}]))
已记录的模型签名:
{
"inputs": [
{"name": "adjective", "type": "string"},
{"name": "thing", "type": "string"},
],
"outputs": [{"type": "string"}],
}
预期的预测输入类型:
# A list of dictionaries with 'adjective' and 'thing' keys
[{"adjective": "funny", "thing": "vim"}, ...]
发送到 OpenAI chat completion API 的负载:
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "Tell me a funny joke about vim",
}
],
}
messages 没有变量
如果没有提供变量,预测输入将被_附加_到已记录的 messages
并带有 role = user.
with mlflow.start_run():
model_info = mlflow.openai.log_model(
name="model",
model="gpt-4o-mini",
task=openai.chat.completions,
messages=[
{
"role": "system",
"content": "You're a frontend engineer.",
}
],
)
model = mlflow.pyfunc.load_model(model_info.model_uri)
print(model.predict(["Tell me a funny joke."]))
已记录的模型签名:
{
"inputs": [{"type": "string"}],
"outputs": [{"type": "string"}],
}
预期的预测输入类型:
包含单个键的字典列表
字符串列表
发送到 OpenAI chat completion API 的有效负载:
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You're a frontend engineer.",
},
{
"role": "user",
"content": "Tell me a funny joke.",
},
],
}
没有 messages
messages 参数是可选的,可以省略。如果省略,预测输入将按原样发送到 OpenAI chat completion API,且 role = user。
import mlflow
import openai
with mlflow.start_run():
model_info = mlflow.openai.log_model(
name="model",
model="gpt-4o-mini",
task=openai.chat.completions,
)
model = mlflow.pyfunc.load_model(model_info.model_uri)
print(model.predict(["Tell me a funny joke."]))
已记录的模型签名:
{
"inputs": [{"type": "string"}],
"outputs": [{"type": "string"}],
}
预期的预测输入类型:
# A list of dictionaries with a single key
[{"<any key>": "Tell me a funny joke."}, ...]
# A list of strings
["Tell me a funny joke.", ...]
发送到 OpenAI chat completion API 的有效载荷:
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "Tell me a funny joke.",
}
],
}