ops

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ops provides parameterised, use-case-oriented LLM operations.

Three generic type parameters drive every operation:

F — factory params: static configuration decided at wiring time
I — runtime input type
O — runtime output type

Pattern:

op := ops.Classify.New(provider, ops.ClassifyParams{Labels: []string{"pos", "neg"}})
result, err := op.Run(ctx, "This is great!")
// result.Label == "pos"

Index

Constants

This section is empty.

Variables

View Source
var Classify classifyFactory

Classify is the built-in factory for text classification.

Preset: Temperature=0, Thinking=off.

op := ops.Classify.New(provider, ops.ClassifyParams{
    Labels: []string{"positive", "negative", "neutral"},
})
result, err := op.Run(ctx, "This is great!")
// result.Label == "positive"
View Source
var Generate generateFactory

Generate is the built-in factory for free-form text generation.

Preset: Temperature=0.7 (when unset), Thinking=auto.

op := ops.Generate.New(provider, ops.GenerateParams{SystemPrompt: "Be concise."})
result, err := op.Run(ctx, "Write a haiku about Go channels.")
View Source
var Intent intentFactory

Intent is the built-in factory for intent detection.

Unlike Classify, each intent carries a description and optional few-shot examples, making it significantly more reliable for NLU tasks.

Preset: Temperature=0, Thinking=off.

op := ops.Intent.New(provider, ops.IntentParams{
    Intents: []ops.IntentDef{
        {Name: "book_flight", Description: "User wants to book a flight",
         Examples: []string{"fly me to Paris", "book a ticket to Berlin"}},
        {Name: "cancel_order", Description: "User wants to cancel an order",
         Examples: []string{"cancel my order", "I want a refund"}},
    },
})
result, err := op.Run(ctx, "I need to get to Berlin next week.")
// result.Name == "book_flight"

Functions

func Temp

func Temp(t float64) *float64

Temp returns a pointer to t for use with GenerateParams.Temperature. This avoids taking the address of a float64 literal at the call site:

ops.GenerateParams{Temperature: ops.Temp(0.0)} // deterministic
ops.GenerateParams{Temperature: ops.Temp(1.5)} // creative

Types

type ClassifyParams

type ClassifyParams struct {
	// Labels is the set of valid output categories. At least one required.
	Labels []string
	// Model is optional; defaults to llm.ModelDefault.
	Model string
	// Hint is optional extra context appended to the system prompt,
	// e.g. "The text may use informal abbreviations."
	Hint string
}

ClassifyParams configures a Classify operation.

type ClassifyResult

type ClassifyResult struct {
	// Label is the canonical label from ClassifyParams.Labels (case-normalised
	// to the original casing in Labels).
	Label string
}

ClassifyResult holds the output of a Classify operation.

type Factory

type Factory[F, I, O any] interface {
	New(provider llm.Provider, params F) Operation[I, O]
}

Factory[F, I, O] creates Operations parameterised by F.

func NewFactory

func NewFactory[F, I, O any](fn func(llm.Provider, F) Operation[I, O]) Factory[F, I, O]

NewFactory wraps a function into a Factory — the primary helper for users building custom operations:

var Summarize = ops.NewFactory(func(p llm.Provider, params SummarizeParams) ops.Operation[string, SummaryResult] {
    return ops.NewMap[SummaryResult](p, ops.MapParams{Hint: "Summarize concisely."})
})

type GenerateParams

type GenerateParams struct {
	// Model is optional; defaults to llm.ModelDefault.
	Model string
	// SystemPrompt is sent as a system message when non-empty.
	SystemPrompt string
	// Temperature controls output randomness (valid range: 0.0–2.0).
	// nil (the default) applies a sensible default of 0.7 for creative/conversational tasks.
	// Use ops.Temp(t) to set an explicit value, including 0 for deterministic output.
	Temperature *float64
}

GenerateParams configures a Generate operation.

type GenerateResult

type GenerateResult struct {
	Text string
}

GenerateResult holds the output of a Generate operation.

type IntentDef

type IntentDef struct {
	// Name is the canonical identifier returned in IntentResult.
	Name string
	// Description explains when this intent applies.
	Description string
	// Examples are representative user utterances (few-shot guidance).
	// Optional but strongly recommended.
	Examples []string
}

IntentDef defines a single intent with a name, description, and optional few-shot examples. Examples give the model concrete guidance and improve accuracy over plain Classify for NLU tasks.

type IntentParams

type IntentParams struct {
	// Intents is the set of possible intents. At least one required.
	Intents []IntentDef
	// Model is optional; defaults to llm.ModelDefault.
	Model string
	// Hint is optional extra context appended to the system prompt,
	// e.g. "The user may write in informal language."
	Hint string
}

IntentParams configures an Intent operation.

type IntentResult

type IntentResult struct {
	// Name matches the Name field of the matched IntentDef.
	Name string
}

IntentResult holds the output of an Intent operation.

type MapFactory

type MapFactory[O any] struct{}

MapFactory[O] implements Factory[MapParams, string, O]. Use when you need to store a Factory in a typed variable:

var f ops.Factory[ops.MapParams, string, InvoiceData] = ops.MapFactory[InvoiceData]{}
op := f.New(provider, params)

func (MapFactory[O]) New

func (MapFactory[O]) New(provider llm.Provider, params MapParams) Operation[string, O]

type MapMode

type MapMode string

MapMode selects the internal extraction strategy.

const (
	// MapModeTool (default) defines the output type as a tool schema and
	// forces the model to call it. More reliable across providers.
	MapModeTool MapMode = "tool"
	// MapModeJSON uses OutputFormat=JSON with a schema-describing system prompt.
	// Simpler but the model can deviate from the schema.
	MapModeJSON MapMode = "json"
)

type MapParams

type MapParams struct {
	// Model is optional; defaults to llm.ModelDefault.
	Model string
	// Hint is appended to the system prompt to provide extra context,
	// e.g. "The document is in German." or "Focus on line-item amounts only."
	Hint string
	// Mode selects the extraction strategy. Defaults to MapModeTool.
	Mode MapMode
}

MapParams configures a Map operation.

type Operation

type Operation[I, O any] interface {
	Run(ctx context.Context, input I) (*O, error)
}

Operation[I, O] executes a single LLM-backed operation and blocks until the result is available.

func NewMap

func NewMap[O any](provider llm.Provider, params MapParams) Operation[string, O]

NewMap is a convenience constructor (avoids explicit struct instantiation):

op := ops.NewMap[InvoiceData](provider, ops.MapParams{Hint: "Extract invoice fields."})

type OperationFunc

type OperationFunc[I, O any] func(context.Context, I) (*O, error)

OperationFunc adapts a plain function to the Operation interface.

op := ops.OperationFunc[string, MyResult](func(ctx context.Context, input string) (*MyResult, error) {
    return &MyResult{Value: process(input)}, nil
})

func (OperationFunc[I, O]) Run

func (f OperationFunc[I, O]) Run(ctx context.Context, input I) (*O, error)

Jump to

Keyboard shortcuts

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