OpenAI 聊天补全任务支持的 messages 格式
本文档涵盖以下内容
在
openai模块中,OpenAI 聊天补全任务支持的messages格式。每种格式记录的模型签名。
每种格式发送到 OpenAI 聊天补全 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 聊天补全 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 聊天补全 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 聊天补全 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 聊天补全 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 聊天补全 API 的载荷
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "Tell me a funny joke.",
}
],
}