跳至内容

数据模式

可以强制让LLM生成符合特定模式的数据。 这种技术效果相当不错,GenAIScript还提供了自动验证功能"以防万一"。

您会注意到GenAIScript支持的架构比完整的JSON架构规范要简单得多。我们建议使用简单的架构以避免混淆大语言模型;之后再将它们移植到您的应用程序特定的数据格式中。

defSchema

使用defSchema为提示输出定义JSON/YAML模式。

const schema = defSchema("CITY_SCHEMA", {
type: "array",
description: "A list of cities with population and elevation information.",
items: {
type: "object",
description: "A city with population and elevation information.",
properties: {
name: { type: "string", description: "The name of the city." },
population: {
type: "number",
description: "The population of the city.",
},
url: {
type: "string",
description: "The URL of the city's Wikipedia page.",
},
},
required: ["name", "population", "url"],
},
})
$`Generate data using JSON compliant with ${schema}.`
👤 user
CITY_SCHEMA:
```typescript-schema
// A list of cities with population and elevation information.
type CITY_SCHEMA = Array<{
// The name of the city.
name: string,
// The population of the city.
population: number,
// The URL of the city's Wikipedia page.
url: string,
}>
```
Generate data using JSON compliant with CITY_SCHEMA.
🤖 assistant
File ./data.json:
```json schema=CITY_SCHEMA
[
{
"name": "New York",
"population": 8398748,
"url": "https://en.wikipedia.org/wiki/New_York_City"
},
{
"name": "Los Angeles",
"population": 3990456,
"url": "https://en.wikipedia.org/wiki/Los_Angeles"
},
{
"name": "Chicago",
"population": 2705994,
"url": "https://en.wikipedia.org/wiki/Chicago"
}
]
```

原生zod支持

GenAIScript runtime 提供了 z 模块。

可以将Zod类型传入defSchema,它会自动转换为JSON模式。 为了方便起见,GenAIScript还从Zod导出了z对象。

// import from genaiscript
import { z } from "genaiscript/runtime"
// or directly from zod
// import { z } from "zod"
// create schema using zod
const CitySchema = z.array(
z.object({
name: z.string(),
population: z.number(),
url: z.string(),
})
)
// JSON schema to constrain the output of the tool.
const schema = defSchema("CITY_SCHEMA", CitySchema)

提示编码

遵循TypeChat提出的“All You Need Is Types”方法,在将模式注入LLM提示之前,会先将其转换为TypeScript类型。

// A list of cities with population and elevation information.
type CITY_SCHEMA = Array<{
// The name of the city.
name: string
// The population of the city.
population: number
// The URL of the city's Wikipedia page.
url: string
}>

您可以通过使用{ format: "json" }选项来更改此行为。

const schema = defSchema("CITY_SCHEMA", {...}, { format: "json" })

使用该模式

然后告诉LLM使用这个模式来生成数据。

const schema = defSchema(...)
$`Use ${schema} for the JSON schema.`

验证

当使用模式标识符生成JSON/YAML有效负载时,GenAIScript会自动根据模式验证该有效负载。

修复

GenAIScript会自动尝试通过向LLM发送带有解析输出的附加消息来修复数据问题。

运行时验证

在运行脚本时使用parsers.validateJSON来验证JSON。

const validation = parsers.validateJSON(schema, json)