vv-ai-promt-store
v2.0.2
Published
Helper for store promt in file
Readme
vv-ai-promt-store
A lightweight TypeScript library for storing and managing AI prompts in a simple text format.
Features
- Simple text-based format for storing prompts
- Support for system and user prompts
- Custom parameters for each prompt
- Multiple prompts in a single file
- Parse and serialize prompts to/from text
Installation
npm install vv-ai-promt-storeFormat
The library uses a simple text format with special markers:
$$begin
$$options
temperature=0.7
maxTokens=4096
$$system
System prompt text here
$$user
User prompt text here
$$jsonresponse
{"type": "object", "properties": {"name": {"type": "string"}}}
$$segment=segmentName
Segment content here
$$endFormat rules:
$$begin- Start of a prompt block$$end- End of a prompt block$$user- User prompt (required)$$system- System prompt (optional)$$options- LLM settings section (optional)$$jsonresponse- JSON Schema for structured response output (optional)$$segment=name- Named text segments (optional)- Text before the first
$$beginand after the last$$endis ignored - Section order within a block doesn't matter
- All sections except
$$userare optional - Multiple segments with different names can be defined
Usage
Parsing prompts from text
import { PromtLoad } from 'vv-ai-promt-store'
const text = `
$$begin
$$options
temperature=0.7
maxTokens=4096
$$system
You are a helpful assistant
$$user
What is 2+2?
$$end
`
const prompts = PromtLoad(text)
console.log(prompts)
// [{
// system: 'You are a helpful assistant',
// user: 'What is 2+2?',
// options: { temperature: 0.7, maxTokens: 4096 }
// }]Serializing prompts to text
import { PromtStore, TPromt } from 'vv-ai-promt-store'
const prompts: TPromt[] = [{
system: 'You are a helpful assistant',
user: 'Hello, world!',
options: {
temperature: 0.7,
maxTokens: 4096
}
}]
const text = PromtStore(prompts)
console.log(text)
// $$begin
// $$options
// temperature=0.7
// maxTokens=4096
// $$system
// You are a helpful assistant
// $$user
// Hello, world!
// $$endMultiple prompts
import { PromtLoad } from 'vv-ai-promt-store'
const text = `
$$begin
$$user
First prompt
$$end
$$begin
$$system
Different system prompt
$$user
Second prompt
$$end
`
const prompts = PromtLoad(text)
console.log(prompts.length) // 2Working with JSON Schema responses
The $$jsonresponse section allows you to define a JSON Schema for structured response output. This is useful when you need the AI to return data in a specific format:
import { PromtLoad, PromtStore, TPromt } from 'vv-ai-promt-store'
const prompts: TPromt[] = [{
user: 'Generate a user profile',
jsonresponse: JSON.stringify({
type: 'object',
required: ['name', 'age'],
properties: {
name: { type: 'string' },
age: { type: 'number' },
email: { type: 'string', format: 'email' }
}
})
}]
const text = PromtStore(prompts)
console.log(text)
// $$begin
// $$user
// Generate a user profile
// $$jsonresponse
// {"type":"object","required":["name","age"],"properties":{"name":{"type":"string"},"age":{"type":"number"},"email":{"type":"string","format":"email"}}}
// $$end
const parsed = PromtLoad(text)
console.log(JSON.parse(parsed[0].jsonresponse)) // Access the JSON SchemaWorking with segments
Segments allow you to store named blocks of text within a prompt:
import { PromtLoad, PromtStore, TPromt } from 'vv-ai-promt-store'
const prompts: TPromt[] = [{
user: 'Analyze this code',
segment: {
code: 'function hello() { return "world"; }',
tests: 'test("hello", () => { expect(hello()).toBe("world"); })'
}
}]
const text = PromtStore(prompts)
console.log(text)
// $$begin
// $$user
// Analyze this code
// $$segment=code
// function hello() { return "world"; }
// $$segment=tests
// test("hello", () => { expect(hello()).toBe("world"); })
// $$end
const parsed = PromtLoad(text)
console.log(parsed[0].segment.code) // Access segment contentAPI
Types
type TPromtOptions = {
temperature?: number
topP?: number
topK?: number
minP?: number
maxTokens?: number
repeatPenalty?: number
repeatPenaltyNum?: number
presencePenalty?: number
frequencyPenalty?: number
mirostat?: number
mirostatTau?: number
mirostatEta?: number
penalizeNewline?: boolean
stopSequences?: string[]
trimWhitespace?: boolean
}
type TPromt = {
system?: string
user: string
options?: TPromtOptions
segment?: Record<string, string>
jsonresponse?: string
}Options format
The $$options section supports various formats for values:
Numbers:
- Decimal:
0.7,2,2.4 - With comma separator:
0,7,2,4 - In quotes:
"0.7",'0.9'
Booleans:
- Standard:
true,false - Numeric:
1,0 - Short:
y,n(case insensitive) - In quotes:
"true",'false'
Arrays:
- JSON format:
stopSequences=["stop1", "stop2"]
Undefined:
- Empty value:
minP=(parameter will be undefined)
Functions
PromtLoad(raw: string, use?: 'core' | 'json'): TPromt[]
Parses text and returns an array of prompts.
Parameters:
raw- Text containing prompts in the specified formatuse- Schema type for options validation (optional, default:'core'):'core'- Standard AI model settings (higher temperature, creativity)'json'- Structured JSON output settings (lower temperature, deterministic)
Returns:
- Array of
TPromtobjects
Example:
const prompts = PromtLoad(text, 'json') // Use JSON schema defaultsPromtOptionsParse(use: 'core' | 'json', raw?: object, useAllOptions?: boolean): TPromtOptions
Parses and validates prompt options from a raw object.
Parameters:
use- Schema type:'core'for standard AI models,'json'for structured JSON outputraw- Raw object containing option values to parse (optional)useAllOptions- Iftrue, returns all options with defaults; iffalse, returns only specified options (optional, default:true)
Returns:
- Validated
TPromtOptionsobject. Invalid values are replaced with defaults. Never throws errors.
Example:
// Get all options with defaults
const options = PromtOptionsParse('core', { temperature: 0.7 })
// Returns: { temperature: 0.7, topP: 0.9, topK: 40, ... all other defaults }
// Get only specified options
const options = PromtOptionsParse('core', { temperature: 0.7 }, false)
// Returns: { temperature: 0.7 }PromtStore(promt: TPromt[]): string
Serializes an array of prompts to text format.
Parameters:
promt- Array ofTPromtobjects
Returns:
- String in the specified format
License
MIT
