Polyweave

@polyweave/patterns (1.0.4)

Published 2026-07-11 00:55:14 +00:00 by Dvorak

Installation

@polyweave:registry=https://hub.kl1.tenere.ai/api/packages/Polyweave/npm/
npm install @polyweave/patterns@1.0.4
"@polyweave/patterns": "1.0.4"

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), and verbal (verbal confidence mapping).
  • Repeat (createRepeatTwiceTemplateNode) — Creates a @polyweave/smart-templates template 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_true and p_sufficient modes 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 *

Keywords

polyweave patterns prompting tools llm
Details
npm
2026-07-11 00:55:14 +00:00
4
John Dvorak
SEE LICENSE IN LICENSE
7.5 KiB
Assets (1)
Versions (5) View all
1.0.7 2026-07-11
1.0.4 2026-07-11
1.0.3 2026-07-11
1.0.2 2026-07-10
1.0.1 2026-07-10