@polyweave/patterns (1.0.2)
Installation
@polyweave:registry=https://hub.kl1.tenere.ai/api/packages/Polyweave/npm/npm install @polyweave/patterns@1.0.2"@polyweave/patterns": "1.0.2"About this package
patterns
Reusable prompting patterns and LLM-backed tools.
Published as @polyweave/patterns.
Synopsis
import {
createRankingTool,
createPairwiseJudgeTool,
createConfidenceTool,
createRepeatTwiceTemplateNode
} from '@polyweave/patterns';
const judge = createPairwiseJudgeTool(myAdapter, {
criterion: 'Which implementation is more correct?'
});
judge.sink.write({
type: 'TOOL_CALL',
content: { left: 'Solution A', right: 'Solution B' }
});
judge.source.pipe(mySink);
Install
npm install @polyweave/patterns
Description
The patterns package provides reusable LLM-backed prompting patterns and tools for common agent interaction scenarios. Each module implements a specific judgment or interaction pattern as a Polyweave frame duplex tool that can be plugged into any agent harness.
Four pattern modules are included:
- Ranking (
createRankingTool) — Ranks a list of items from best to worst, returning ordered IDs. Supports customizable item templates, criteria blocks, and ID strategies (UUIDs, alphabetic labels). - Pairwise Judge (
createPairwiseJudgeTool) — Compares two items (LEFT vs RIGHT) and returns a winner (LEFT, RIGHT, or TIE) with intensity (1-9) and rationale. Output is structured XML. - Confidence (
createConfidenceTool) — Elicits confidence estimates from an LLM using three modes:p_true(probability the answer is correct given a forced-answer setup),p_sufficient(probability the given information is sufficient), andverbal(verbal confidence mapping). - Repeat (
createRepeatTwiceTemplateNode) — Creates a@polyweave/smart-templatestemplate node that repeats core content N times with optional disclaimer text.
API
createRankingTool(adapter, options)
Creates a ranking judge duplex tool.
Parameters:
adapter— LLM adapter duplex{ source, sink }.options.criterion— Ranking criterion text.options.context— Optional context text.options.itemTemplate— Template for formatting items (default'ID ${id}: ${text}').options.promptTemplate— Full prompt template override.options.outputTemplate— Output instruction template.options.idStrategy—'alphabetic'or'uuid'(default'alphabetic').options.idLabel— Custom label for ID references (default'IDs').options.temperature— LLM temperature override.
TOOL_CALL args: { items: [{id, text}], criterion, context }
Returns: { source, sink } — Results include { ranking: [{id, position}], rawText }.
createPairwiseJudgeTool(adapter, options)
Creates a pairwise comparison duplex tool.
Parameters:
adapter— LLM adapter duplex.options.criterion— Comparison criterion.options.context— Optional context text.options.promptTemplate— Full prompt template override.options.outputTemplate— Output instruction template override.options.temperature— LLM temperature override.
TOOL_CALL args: { left, right, criterion, context }
Returns: { source, sink } — Results include { winner: 'LEFT'|'RIGHT'|'TIE', intensity: 1-9, rationale }.
createConfidenceTool(adapter, options)
Creates a confidence elicitation duplex tool.
Parameters:
adapter— LLM adapter duplex.options.mode—'ptrue','psufficient', or'verbal'(default'ptrue').options.templates—{ ptrue, psufficient, verbal }— template overrides.options.temperature— LLM temperature override.
TOOL_CALL args: { question, answer, context, mode, templates }
Returns: { source, sink } — Results include { confidence: number, mode, rawText }.
createRepeatTwiceTemplateNode(options)
Creates a smart-templates node that repeats content.
Parameters:
options.coreTemplate— The template to repeat.options.endTemplate— Template appended after repetitions.options.repeatCount— Number of repetitions (default 2).options.separator— Separator between repetitions (default'\n\n').options.includeDisclaimer— Whether to include disclaimer text (default true).options.disclaimerTemplate— Disclaimer text override.
Returns: A template node compatible with @polyweave/smart-templates.
Examples
// Ranking multiple candidates
const ranker = createRankingTool(llmAdapter, {
criterion: 'Code quality and readability',
idStrategy: 'uuid'
});
ranker.sink.write({
type: 'TOOL_CALL',
content: {
items: [
{ id: 'a1', text: 'function add(a,b){return a+b}' },
{ id: 'b2', text: 'const add = (a: number, b: number): number => a + b' }
]
}
});
// Confidence on a generated answer
const confidence = createConfidenceTool(llmAdapter, { mode: 'ptrue' });
confidence.sink.write({
type: 'TOOL_CALL',
content: {
question: 'What is the capital of Australia?',
answer: 'Canberra',
context: 'The user is a geography student.'
}
});
Tests
npm test
Tests cover ranking tool creation and execution, alphabetical and UUID ID strategies, pairwise judgment with winner/intensity/rationale parsing, p_true/p_sufficient/verbal confidence modes, repeat template node generation, and error handling for missing/invalid arguments.
Requirements
Node.js 18 or later. ESM only.
Caveats
- All pattern tools require an injected LLM adapter; they do not create adapters internally.
- Confidence scores from
p_trueandp_sufficientmodes are LLM-generated estimates, not calibrated probabilities. They should be interpreted as ordinal signals, not precise measurements. - The repeat template node produces combined prompts that grow linearly with
repeatCount; ensure token limits are respected.
License
MIT
Dependencies
Dependencies
| ID | Version |
|---|---|
| @polyweave/core | * |
| @polyweave/smart-templates | * |
| @polyweave/tools | * |
| @polyweave/uncertainty | * |
Development Dependencies
| ID | Version |
|---|---|
| @rigor/core | * |