跳至内容

Prompty

GenAIScript支持将.prompty文件作为脚本运行(存在一些限制)或在脚本中导入它们。

什么是prompty?

Prompty是一种类似markdown的文件格式,用于存储参数化提示词以及模型信息。

basic.prompty
---
name: Basic Prompt
description: A basic prompt that uses the chat API to answer questions
model:
api: chat
configuration:
type: azure_openai
azure_deployment: gpt-4o
parameters:
max_tokens: 128
temperature: 0.2
inputs:
question:
type: string
sample:
"question": "Who is the most famous person in the world?"
---
system:
You are an AI assistant who helps people find information.
As the assistant, you answer questions briefly, succinctly.
user:
{{question}}
{{hint}}

有两种方式可以利用prompty文件与GenAIScript:

  • 直接通过GenAIScript运行它们
  • 在脚本中使用importTemplate导入它们

使用GenAIScript运行.prompty文件

你可以从cli或Visual Studio Code中运行.prompty文件,就像运行其他.genai.mjs脚本一样。

GenAIScript会将.prompty内容转换为脚本并执行。它支持大部分前置选项,但主要忽略模型配置部分。

这是basic.prompty文件编译后的结果:

basic.prompty.genai.mts
script({
model: "openai:gpt-4o",
title: "Basic Prompt",
description: "A basic prompt that uses the chat API to answer questions",
parameters: {
question: {
type: "string",
default: "Who is the most famous person in the world?",
},
},
temperature: 0.2,
maxTokens: 128,
})
writeText(
`You are an AI assistant who helps people find information.
As the assistant, you answer questions briefly, succinctly.`,
{ role: "system" }
)
$`{{question}}
{{hint}}`.jinja(env.vars)

导入.prompty文件

你也可以在生成提示时使用importTemplate导入并渲染一个.prompty文件。

importTemplate("basic.prompty", {
question: "what is the capital of france?",
hint: "starts with p",
})

在此场景中,.prompty文件不会作为脚本执行,而是作为模板导入。importTemplate函数将使用提供的参数渲染该模板。

支持的功能

  • name, description, temperature, max_tokens, top_p, …0
  • inputs 转换为 parameters
  • sample 值填充参数 default 部分
  • outputs 转换为 responseSchema
  • Jinja2 模板引擎

限制条件

  • 模型配置使用GenAIScript .env文件(参见configuration)。
  • 目前还不支持图片

扩展功能

genaiscript使用的额外字段:

  • files 用于指定一个或多个文件来填充 env.files
  • tests 用于指定一个或多个测试