prmtx
v0.0.1
Published
npm package template with tsup and typescript
Readme
imprompt 🔨
A TypeScript toolkit for building structured AI prompts using proven prompting techniques. Provider-agnostic — outputs plain strings you can send to any LLM (Claude, GPT-4, Gemini, etc.).
Install
npm install impromptQuick Start
Every technique is a function that returns a string. Compose them freely:
import { persona, chainOfThought, instruction, compose } from "imprompt";
const prompt = compose([
persona({
role: "senior TypeScript engineer",
expertiseLevel: "world-class",
specialisations: ["type safety", "performance"],
traits: ["concise", "direct"],
task: "Review this code.",
}),
chainOfThought({
question: "What are the critical issues?",
strategy: "first-principles",
}),
instruction({
task: "Provide a structured code review.",
dos: ["Group by severity", "Include fixes"],
donts: ["Don't repeat the code verbatim"],
outputFormat: "markdown",
}),
]);Techniques
Functions (stateless — preferred API)
| Function | Technique | Best for |
| ------------------ | ---------------- | ---------------------- |
| fewShot() | Few-Shot | Format/style adherence |
| chainOfThought() | Chain-of-Thought | Reasoning & math |
| persona() | Role/Persona | Domain expertise |
| instruction() | Instruction | Structured constraints |
| compose() | Composition | Combining techniques |
Classes (stateful — for multi-step workflows)
| Class | Technique | Best for |
| ---------------- | --------------- | ---------------------- |
| PromptChain | Prompt Chaining | Multi-step pipelines |
| ReActPrompt | ReAct | Agentic tool-use loops |
| PromptTemplate | Templates | Reusable {{var}} |
Usage
1. Few-Shot
import { fewShot } from "imprompt";
const prompt = fewShot({
instruction: "Classify the sentiment of the review.",
examples: [
{ input: "Great product!", output: "Positive" },
{ input: "Terrible experience.", output: "Negative" },
],
query: "Not bad, could be better.",
});2. Chain-of-Thought
import { chainOfThought } from "imprompt";
const prompt = chainOfThought({
question: "If a train travels 60 mph for 2.5 hours, how far does it go?",
strategy: "step-by-step", // "first-principles" | "pros-cons" | "custom"
});3. Persona
import { persona } from "imprompt";
const prompt = persona({
role: "data scientist",
expertiseLevel: "senior", // "junior" | "mid" | "senior" | "world-class"
specialisations: ["NLP", "Python"],
traits: ["concise", "pragmatic"],
task: "Explain transformer attention to a junior engineer.",
});4. Instruction
import { instruction } from "imprompt";
const prompt = instruction({
task: "Summarise this article.",
dos: ["Be concise", "Use bullet points"],
donts: ["Don't add opinions"],
outputFormat: "bullet-list",
tone: "technical",
audience: "engineers",
});5. Compose
Combine any number of sections into one prompt:
import { compose, persona, instruction } from "imprompt";
const prompt = compose([
persona({ role: "copywriter", task: "Write ad copy." }),
instruction({ task: "Write a tagline.", tone: "casual" }),
]);
// Custom divider (default is "\n\n---\n\n")
const prompt2 = compose([...sections], "\n\n");6. Prompt Chain
import { PromptChain } from "imprompt";
const chain = new PromptChain({
initialInput: "The history of the Roman Empire",
steps: [
{
name: "Outline",
instruction: "Create a 5-point outline about: {{input}}",
outputKey: "outline",
},
{
name: "Draft",
instruction: "Write an intro based on:\n{{outline}}",
outputKey: "intro",
},
{ name: "Polish", instruction: "Polish this intro:\n{{intro}}" },
],
});
// Execute step by step, injecting model responses as variables
const step0 = chain.buildStep(0);
// ... send to model, get response ...
chain.setVariable("outline", modelResponse);
const step1 = chain.buildStep(1);7. ReAct
import { ReActPrompt } from "imprompt";
const react = new ReActPrompt({
goal: "Find the current population of Tokyo.",
tools: [
{
name: "search",
description: "Search the web for current facts.",
usageExample: 'search("Tokyo population 2024")',
},
],
maxCycles: 5,
});
const prompt = react.build();
// Continue an in-progress loop:
react
.addHistory({ cycle: "thought", content: "I need to search for this." })
.addHistory({ cycle: "action", content: 'search("Tokyo population 2024")' })
.addHistory({ cycle: "observation", content: "~13.96 million as of 2024." });
const nextPrompt = react.build();8. Template
import { PromptTemplate } from "imprompt";
const template = new PromptTemplate({
template: "Review the following {{language}} code:\n\n```{{language}}\n{{code}}\n```",
variables: [
{ name: "language", defaultValue: "TypeScript" },
{ name: "code", required: true },
],
});
const prompt = template.fill({ code: "const x = 1" }).build();TypeScript Types
All options objects are fully typed:
import type {
FewShotOptions,
FewShotExample,
CoTStrategy,
ChainOfThoughtOptions,
ExpertiseLevel,
PersonaOptions,
ChainStep,
PromptChainOptions,
ReActTool,
ReActOptions,
InstructionOptions,
OutputFormat,
ToneHint,
PromptBuilder,
} from "imprompt";Build
npm install
npm run build
# Output in ./dist — includes .js, .d.ts, and source mapsLicense
MIT
