Ein super Framework für schnelles Engineering.
Ein XML-basiertes Framework zum strukturierten Definieren, Verwalten und Validieren von AI/LLM-Eingabeaufforderungen.
Es bringt Prinzipien des Software-Engineerings in das Prompt-Engineering ein, indem es ein robustes System für die Handhabung von Variablen, bedingte Logik und modulares Prompt-Design bereitstellt.
Unabhängig davon, ob Sie komplexe KI-Anwendungen erstellen oder eine Eingabeaufforderungsbibliothek verwalten, sorgt One Prompt für Konsistenz, Wartbarkeit und Zuverlässigkeit in Ihrem Eingabeaufforderungs-Engineering-Workflow.
# 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!"
Der Metadatenabschnitt definiert Informationen über die Eingabeaufforderung selbst:
< metadata >
< title >Customer Response</ title >
< description >Template for customer inquiries</ description >
< team >Support</ team >
</ metadata >
Variablen sind Platzhalter, die durch tatsächliche Werte ersetzt werden. Dies können sein:
< variables >
< var name = " customer_name " required = " true " />
< var name = " priority " required = " false " >normal</ var >
< var name = " response_type " required = " false " >standard</ var >
</ variables >
Der Hauptinhalt mit variablen Platzhaltern und bedingter Logik:
< template >
Dear {{customer_name}},
< oneprompt : if var = " priority " equals = " high " show = " urgent_response " else = " standard_response " />
Best regards,
Support Team
</ template >
Wiederverwendbare Inhaltsblöcke, auf die in Bedingungen verwiesen werden kann:
< 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
Analysiert eine XML-Zeichenfolge in ein validiertes Prompt-Objekt.
// 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
Konvertiert ein Prompt-Objekt zurück in das XML-Format.
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
Validiert die Struktur und Beziehungen einer Eingabeaufforderung.
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
Verarbeitet eine Eingabeaufforderung mit bereitgestellten Variablen, um eine endgültige Ausgabe zu erstellen.
// 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[]
Extrahiert Variablennamen aus einer Vorlagenzeichenfolge.
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
Validiert Eingabevariablen anhand von Spezifikationen und wendet Standardwerte an.
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
Ersetzt Variablenplatzhalter durch ihre Werte.
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
Verarbeitet bedingte Ausdrücke und schließt entsprechende Teile ein.
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!
Da KI- und LLM-Anwendungen immer komplexer werden, wird die Verwaltung von Eingabeaufforderungen als einfache Zeichenfolgen immer fehleranfälliger und schwieriger zu warten. Wir brauchten eine Möglichkeit:
Der XML-basierte Ansatz wurde gewählt, um eine klare Struktur zu bieten und gleichzeitig die Lesbarkeit zu gewährleisten und zu ermöglichen, dass Metadaten und Validierungsregeln neben der Eingabeaufforderungsvorlage selbst vorhanden sind. Dadurch werden Eingabeaufforderungen selbstdokumentierend und ermöglichen eine bessere Zusammenarbeit zwischen Eingabeaufforderungsingenieuren, Entwicklern und anderen Beteiligten.
One Prompt zielt darauf ab, Best Practices für das Software-Engineering in das Prompt-Engineering zu integrieren und gleichzeitig das Entwicklererlebnis einfach und intuitiv zu halten.
Bei Fehlern und Funktionswünschen öffnen Sie bitte ein Problem.
MIT