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 ¶
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"
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.")
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 ¶
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 ¶
Factory[F, I, O] creates Operations parameterised by F.
func NewFactory ¶
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)
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 ¶
Operation[I, O] executes a single LLM-backed operation and blocks until the result is available.
type OperationFunc ¶
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
})