oneprompt
1.0.0
快速工程的超级框架。
一个基于 XML 的框架,用于以结构化方式定义、管理和验证 AI/LLM 提示。
它通过提供用于变量处理、条件逻辑和模块化提示设计的强大系统,将软件工程原理引入提示工程。
无论您是构建复杂的 AI 应用程序还是管理提示库,One Prompt 都有助于确保提示工程工作流程的一致性、可维护性和可靠性。
# Using npm
npm install oneprompt
# Using yarn
yarn add oneprompt
# Using pnpm
pnpm add oneprompt
# Using bun
bun add oneprompt
<!-- Basic metadata about the prompt -->
< metadata >
< title >Support Request</ title >
</ metadata >
<!-- Variable definitions with required/optional flags -->
< variables >
< var name = " issue_category " required = " true " />
</ variables >
<!-- Main template with conditional logic and variable placeholders -->
< template >
Thank you for contacting support regarding your {{issue_category}} issue.
Our team strives to provide timely and effective solutions. Here's what you can expect:
< oneprompt : if var = " issue_category " equals = " billing " show = " billing_info " />
We'll be in touch within 24-48 hours with a detailed response.
Best regards,
Support Team
</ template >
<!-- Reusable content parts referenced by conditionals -->
< part name = " billing_info " >
For billing-related inquiries, please have your latest invoice and account number ready.
</ part >
import { OnePrompt } from 'oneprompt' ;
const prompt = ... ; // your XML prompt or prompt object
const rendered = OnePrompt . renderWithVariables ( prompt , {
name : "John" ,
style : "casual"
} ) ;
// Output: "Hey John!"
元数据部分定义有关提示本身的信息:
< metadata >
< title >Customer Response</ title >
< description >Template for customer inquiries</ description >
< team >Support</ team >
</ metadata >
变量是被实际值替换的占位符。他们可以是:
< variables >
< var name = " customer_name " required = " true " />
< var name = " priority " required = " false " >normal</ var >
< var name = " response_type " required = " false " >standard</ var >
</ variables >
变量占位符和条件逻辑的主要内容:
< template >
Dear {{customer_name}},
< oneprompt : if var = " priority " equals = " high " show = " urgent_response " else = " standard_response " />
Best regards,
Support Team
</ template >
可以在条件中引用的可重用内容块:
< part name = " urgent_response " >
We have marked your case as high priority and assigned a dedicated specialist...
</ part >
< part name = " standard_response " >
Thank you for your inquiry. We will process your request...
</ part >
parseFromXml(xml: string): Prompt
将 XML 字符串解析为经过验证的 Prompt 对象。
// Basic usage
const xml = `
<metadata>
<title>Customer Greeting</title>
</metadata>
<variables>
<var name="name" required="true" />
<var name="language" required="false">english</var>
</variables>
<template>
<oneprompt:if var="language" equals="spanish" show="spanish_greeting" else="english_greeting" />
{{name}}!
</template>
<part name="spanish_greeting">¡Hola</part>
<part name="english_greeting">Hello</part>
` ;
try {
const prompt = OnePrompt . parseFromXml ( xml ) ;
console . log ( prompt ) ;
// {
// metadata: { title: "Customer Greeting" },
// variables: [
// { name: "name", required: true },
// { name: "language", required: false, default: "english" }
// ],
// template: "...",
// parts: [...]
// }
} catch ( error ) {
if ( error instanceof OnePromptError ) {
console . error ( "Invalid prompt XML:" , error . message ) ;
}
}
convertToXml(prompt: Prompt): string
将 Prompt 对象转换回 XML 格式。
const prompt : Prompt = {
metadata : {
title : "Meeting Request" ,
author : "Support Team"
} ,
variables : [
{ name : "requester" , required : true } ,
{ name : "time" , required : false , default : "tomorrow" }
] ,
template : "Meeting requested by {{requester}} for {{time}}" ,
parts : [ ]
} ;
try {
const xml = OnePrompt . convertToXml ( prompt ) ;
console . log ( xml ) ;
// <metadata>
// <title>Meeting Request</title>
// <author>Support Team</author>
// </metadata>
// <variables>
// <var name="requester" required="true" />
// <var name="time" required="false">tomorrow</var>
// </variables>
// <template>Meeting requested by {{requester}} for {{time}}</template>
} catch ( error ) {
console . error ( "Conversion failed:" , error . message ) ;
}
validate(prompt: Prompt): void
验证提示的结构和关系。
const prompt : Prompt = {
metadata : { title : "Email Template" } ,
variables : [
{ name : "recipient" , required : true } ,
{ name : "tone" , required : false , default : "formal" }
] ,
template : `
<oneprompt:if var="tone" equals="formal" show="formal_greeting" else="casual_greeting" />
{{recipient}},
{{message}} // This will cause validation error - 'message' not defined
` ,
parts : [
{ name : "formal_greeting" , content : "Dear" } ,
{ name : "casual_greeting" , content : "Hi" }
]
} ;
try {
OnePrompt . validate ( prompt ) ;
} catch ( error ) {
if ( error instanceof OnePromptError ) {
// Error: "Variable 'message' used in template but not declared"
console . error ( error . message ) ;
}
}
renderWithVariables(source: string | Prompt, variables: PromptInputVariables): string
使用提供的变量处理提示以创建最终输出。
// Using prompt object
const prompt : Prompt = {
metadata : { title : "Support Response" } ,
variables : [
{ name : "ticket_id" , required : true } ,
{ name : "priority" , required : false , default : "normal" }
] ,
template : `
Ticket #{{ticket_id}}
<oneprompt:if var="priority" equals="high" show="urgent_notice" />
` ,
parts : [
{ name : "urgent_notice" , content : "URGENT: Requires immediate attention" }
]
} ;
// Success case
try {
const output1 = OnePrompt . renderWithVariables ( prompt , {
ticket_id : "T-123" ,
priority : "high"
} ) ;
console . log ( output1 ) ;
// Ticket #T-123
// URGENT: Requires immediate attention
const output2 = OnePrompt . renderWithVariables ( prompt , {
ticket_id : "T-456"
} ) ;
console . log ( output2 ) ;
// Ticket #T-456
// (no urgent notice because priority is "normal" by default)
} catch ( error ) {
console . error ( "Render failed:" , error . message ) ;
}
// Error case - missing required variable
try {
OnePrompt . renderWithVariables ( prompt , {
priority : "high"
// Missing ticket_id
} ) ;
} catch ( error ) {
// Error: "Missing required variable: ticket_id"
console . error ( error . message ) ;
}
extractTemplateVariables(template: string): string[]
从模板字符串中提取变量名称。
const template = `
Dear {{recipient}},
Your order #{{order_id}} has been {{status}}.
<oneprompt:if var="tracking_number" equals="available" show="tracking_info" />
` ;
const variables = OnePromptUtils . extractTemplateVariables ( template ) ;
console . log ( variables ) ;
// ["recipient", "order_id", "status"]
validateAndProcessVariables(promptVars: PromptVariable[], inputVars: PromptInputVariables): PromptInputVariables
根据规范验证输入变量并应用默认值。
const promptVariables : PromptVariable [ ] = [
{ name : "user" , required : true } ,
{ name : "role" , required : false , default : "viewer" } ,
{ name : "team" , required : false , default : "general" }
] ;
// Success case - with defaults
const processed1 = OnePromptUtils . validateAndProcessVariables (
promptVariables ,
{ user : "john.doe" }
) ;
console . log ( processed1 ) ;
// {
// user: "john.doe",
// role: "viewer",
// team: "general"
// }
// Success case - override defaults
const processed2 = OnePromptUtils . validateAndProcessVariables (
promptVariables ,
{ user : "jane.doe" , role : "admin" , team : "engineering" }
) ;
console . log ( processed2 ) ;
// {
// user: "jane.doe",
// role: "admin",
// team: "engineering"
// }
// Error case - missing required
try {
OnePromptUtils . validateAndProcessVariables (
promptVariables ,
{ role : "admin" } // Missing required 'user'
) ;
} catch ( error ) {
console . error ( error . message ) ; // "Missing required variable: user"
}
substituteTemplateVariables(template: string, variables: PromptInputVariables): string
将变量占位符替换为其值。
const template = `
Project: {{project_name}}
Owner: {{owner}}
Status: {{status}}
{{#if has_description}}
Description: {{description}}
{{/if}}
` ;
const result = OnePromptUtils . substituteTemplateVariables (
template ,
{
project_name : "OnePrompt" ,
owner : "DevTeam" ,
status : "Active" ,
// description not provided
}
) ;
console . log ( result ) ;
// Project: OnePrompt
// Owner: DevTeam
// Status: Active
// {{#if has_description}}
// Description: {{description}}
// {{/if}}
processConditionals(template: string, variables: PromptInputVariables, parts: PromptPart[]): string
处理条件表达式并包含适当的部分。
const template = `
<oneprompt:if var="access_level" equals="admin" show="admin_actions" else="user_actions" />
<oneprompt:if var="has_notifications" equals="true" show="notification_panel" />
` ;
const variables = {
access_level : "admin" ,
has_notifications : "true"
} ;
const parts : PromptPart [ ] = [
{
name : "admin_actions" ,
content : "- Create Usern- Delete Usern- Modify Settings"
} ,
{
name : "user_actions" ,
content : "- View Profilen- Update Settings"
} ,
{
name : "notification_panel" ,
content : "You have new notifications!"
}
] ;
const processed = OnePromptUtils . processConditionals (
template ,
variables ,
parts
) ;
console . log ( processed ) ;
// - Create User
// - Delete User
// - Modify Settings
//
// You have new notifications!
随着 AI 和 LLM 应用程序变得越来越复杂,将提示作为简单字符串进行管理变得越来越容易出错且难以维护。我们需要一种方法来:
选择基于 XML 的方法是为了提供清晰的结构,同时保持可读性并允许元数据和验证规则与提示模板本身一起存在。这使得提示能够自我记录,并使得提示工程师、开发人员和其他利益相关者之间能够更好地协作。
One Prompt 旨在将软件工程最佳实践引入提示工程,同时保持开发人员体验简单直观。
对于错误和功能请求,请提出问题。
麻省理工学院