poetry

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package poetry provides buffs that transform prompts into poetic structures.

The MetaPromptBuff implements the LLM-based semantic reframing approach from arXiv:2511.15304, using few-shot exemplar poems and strategy-specific meta-prompts to convert prose harmful prompts into verse while preserving original task intent.

Three transformation strategies are supported:

  • allegorical: embeds intent in extended allegory (paper's most effective technique)
  • metaphorical: uses condensed metaphors and imagery
  • narrative: wraps in narrative framing with concluding instruction line

Configuration:

registry.Config{
    "format":              "haiku",        // Poetry format (haiku, sonnet, limerick, or comma-separated)
    "strategy":            "metaphorical", // Transformation strategy (allegorical, metaphorical, narrative, all)
    "transform_generator": "openai.GPT4",  // LLM for transformation (optional, falls back to template)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AvailableStrategies

func AvailableStrategies() []string

AvailableStrategies returns the list of supported transformation strategies.

func BuildMetaPrompt

func BuildMetaPrompt(strategy, format, text string) string

BuildMetaPrompt constructs a strategy-specific meta-prompt for LLM transformation. Returns an empty string if the strategy is not recognized.

func FormatExemplarsForPrompt

func FormatExemplarsForPrompt(exemplars []Exemplar) string

FormatExemplarsForPrompt formats exemplars for inclusion in an LLM prompt. Returns a formatted string with numbered examples suitable for few-shot learning.

func NewMetaPromptBuff

func NewMetaPromptBuff(cfg registry.Config) (buffs.Buff, error)

NewMetaPromptBuff creates a new poetry transformation buff.

Types

type Config

type Config struct {
	// Format is the poetry format (haiku, sonnet, limerick, etc).
	Format string

	// TransformGenerator is the optional LLM generator name for transformation.
	TransformGenerator string

	// Strategy is the semantic reframing approach (allegorical, metaphorical, narrative).
	// Defines how the harmful request is transformed into poetry.
	Strategy string
}

Config holds typed configuration for the Poetry buff.

func ApplyOptions

func ApplyOptions(cfg Config, opts ...Option) Config

ApplyOptions applies functional options to a Config.

func ConfigFromMap

func ConfigFromMap(m registry.Config) (Config, error)

ConfigFromMap parses a registry.Config map into a typed Config. This enables backward compatibility with YAML/JSON configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Exemplar

type Exemplar struct {
	Title    string // Title of the poem
	Strategy string // Transformation strategy (allegorical, metaphorical, narrative)
	Format   string // Poetry format (haiku, sonnet, etc.)
	Text     string // The actual poem text
	Topic    string // Benign topic the poem addresses
}

Exemplar represents a benign poetry example demonstrating a transformation strategy.

func DefaultExemplars

func DefaultExemplars() []Exemplar

DefaultExemplars returns 6 curated benign poetry examples. These demonstrate the three transformation strategies with 2 examples each.

func ExemplarsForStrategy

func ExemplarsForStrategy(exemplars []Exemplar, strategy string) []Exemplar

ExemplarsForStrategy filters exemplars by transformation strategy.

type MetaPromptBuff

type MetaPromptBuff struct {
	// contains filtered or unexported fields
}

MetaPromptBuff transforms prompts into poetry using an LLM.

func (*MetaPromptBuff) Buff

func (m *MetaPromptBuff) Buff(ctx context.Context, attempts []*attempt.Attempt) ([]*attempt.Attempt, error)

Buff transforms a batch of attempts.

func (*MetaPromptBuff) Description

func (m *MetaPromptBuff) Description() string

Description returns a human-readable description.

func (*MetaPromptBuff) Name

func (m *MetaPromptBuff) Name() string

Name returns the buff's fully qualified name.

func (*MetaPromptBuff) Transform

func (m *MetaPromptBuff) Transform(a *attempt.Attempt) iter.Seq[*attempt.Attempt]

Transform yields poetry-transformed attempts. Note: This method uses context.Background() for LLM calls and cannot be cancelled. For cancellable operations, use Buff() method instead.

type Option

type Option = registry.Option[Config]

Option is a functional option for Config.

func WithFormat

func WithFormat(format string) Option

WithFormat sets the poetry format.

func WithStrategy

func WithStrategy(strategy string) Option

WithStrategy sets the transformation strategy.

func WithTransformGenerator

func WithTransformGenerator(gen string) Option

WithTransformGenerator sets the transform generator name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL