ai

package
v0.0.0-...-88ef194 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildGenerateOptions

func BuildGenerateOptions(opts ...GenerationOption) []core.GenerateOption

BuildGenerateOptions converts GenerationSettings to core.GenerateOption slice.

func GenerateStruct

func GenerateStruct[T any](ctx context.Context, gen core.TextGenerator, sysPrompt string, userReq string) (T, error)

func GetGenkit

func GetGenkit(ctx context.Context) *genkit.Genkit

GetGenkit returns the global Genkit instance, initializing it if necessary. This allows other packages to register models/tools.

func InitMiddlewarePlugin

func InitMiddlewarePlugin(ctx context.Context, g *genkit.Genkit) error

InitMiddlewarePlugin registers the middleware plugin with the Genkit instance. This enables middleware to be visible in the Dev UI.

func NewGenkitAction

func NewGenkitAction(ctx context.Context, modelName string) (core.Action, error)

NewGenkitAction creates a new core.Action backed by a Genkit model. It ensures the Genkit runtime is initialized and looks up the model by name.

func NewGenkitAdapter

func NewGenkitAdapter(model ai.Model, gk *genkit.Genkit) core.TextGenerator

NewGenkitAdapter creates a new adapter from a pre-initialized Genkit model.

Parameters:

  • model: The Genkit model instance.
  • gk: The Genkit runtime instance.

Returns:

  • A core.TextGenerator implementation.

func WithCustomMiddleware

func WithCustomMiddleware(mw ...ai.Middleware) core.GenerateOption

WithCustomMiddleware adds arbitrary genkit middleware to the generation.

func WithFallback

func WithFallback(modelRefs []ai.ModelRef) core.GenerateOption

WithFallback enables model fallback when the primary model fails.

func WithFilesystem

func WithFilesystem(rootDir string, allowWrite bool) core.GenerateOption

WithFilesystem enables scoped file system access for the model.

func WithMiddleware

func WithMiddleware(cfg *MiddlewareConfig) core.GenerateOption

WithMiddleware is a GenerateOption that injects genkit middleware into generation.

func WithRetry

func WithRetry(maxRetries int) core.GenerateOption

WithRetry enables automatic retry with the specified configuration.

func WithRetryAndBackoff

func WithRetryAndBackoff(maxRetries, initialDelayMs, maxDelayMs int) core.GenerateOption

WithRetryAndBackoff enables retry with custom backoff parameters.

func WithToolApproval

func WithToolApproval(allowedTools []string) core.GenerateOption

WithToolApproval requires explicit approval for all tool calls.

Types

type DatalogValidator

type DatalogValidator struct {
	// Validator is called before and after generation to enforce Datalog rules.
	Validator func(ctx context.Context, phase string, req *ai.ModelRequest, resp *ai.ModelResponse) error
}

DatalogValidator is a custom middleware that validates generation input/output using Datalog rules before and after model calls.

func (*DatalogValidator) Name

func (d *DatalogValidator) Name() string

func (*DatalogValidator) New

func (d *DatalogValidator) New(ctx context.Context) (*ai.Hooks, error)

type Example

type Example struct{}

Example: Complete generation with all middleware

func (*Example) GenerateWithFullMiddleware

func (e *Example) GenerateWithFullMiddleware(
	ctx context.Context,
	gen core.TextGenerator,
	prompt string,
) (*core.LLMResponse, error)

GenerateWithFullMiddleware demonstrates using all middleware features.

type FlowRegistry

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

func NewFlowRegistry

func NewFlowRegistry(g *genkit.Genkit) *FlowRegistry

func (*FlowRegistry) Get

func (r *FlowRegistry) Get(name string) (*OODAFlow, bool)

func (*FlowRegistry) Register

func (r *FlowRegistry) Register(name string, flow *OODAFlow)

func (*FlowRegistry) RegisterAndDefine

func (r *FlowRegistry) RegisterAndDefine(name string, flow *OODAFlow)

func (*FlowRegistry) Run

func (r *FlowRegistry) Run(ctx context.Context, name string, input *OODAFlowInput) (*OODAFlowOutput, error)

type GenerationOption

type GenerationOption func(*GenerationSettings)

GenerationOption is a functional option for configuring generation with middleware.

func WithDatalogValidator

func WithDatalogValidator(validator func(ctx context.Context, phase string, req *ai.ModelRequest, resp *ai.ModelResponse) error) GenerationOption

WithDatalogValidator adds Datalog validation middleware.

func WithFallbackMiddleware

func WithFallbackMiddleware(models ...ai.ModelRef) GenerationOption

WithFallbackMiddleware enables fallback to alternative models.

func WithFilesystemMiddleware

func WithFilesystemMiddleware(rootDir string, allowWrite bool) GenerationOption

WithFilesystemMiddleware enables scoped filesystem access.

func WithGenerationConfig

func WithGenerationConfig(cfg *core.GenerationConfig) GenerationOption

WithGenerationConfig sets the base generation config.

func WithLogging

func WithLogging(logger core.Logger) GenerationOption

WithLogging adds logging middleware.

func WithRetryMiddleware

func WithRetryMiddleware(maxRetries int) GenerationOption

WithRetryMiddleware enables retry middleware with default settings.

func WithTelemetry

func WithTelemetry(onGenerate func(ctx context.Context, duration time.Duration, model string, inputTokens, outputTokens int)) GenerationOption

WithTelemetry adds telemetry collection middleware.

func WithToolApprovalMiddleware

func WithToolApprovalMiddleware(allowedTools ...string) GenerationOption

WithToolApprovalMiddleware requires approval for tool execution.

type GenerationSettings

type GenerationSettings struct {
	Config     *core.GenerationConfig
	Middleware *MiddlewareConfig
}

GenerationSettings holds all generation configuration including middleware.

type LLMAction

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

LLMAction is a concrete implementation of core.Action that wraps a core.TextGenerator. It adapts the specific TextGenerator interface to the universal core.Action envelope interface.

func NewLLMAction

func NewLLMAction(name string, generator core.TextGenerator) (*LLMAction, error)

NewLLMAction creates a new LLMAction instance.

Parameters:

  • name: A unique name for this action (used in observability and policies).
  • generator: The TextGenerator implementation to wrap.

Returns:

  • A pointer to the initialized LLMAction.
  • An error if the generator is nil.

func (*LLMAction) Complete

func (a *LLMAction) Complete(ctx context.Context, prompt string) (string, error)

Complete implements core.TextGenerator.

func (*LLMAction) Execute

func (a *LLMAction) Execute(ctx context.Context, input core.Envelope) (core.Envelope, error)

Execute performs the text generation. It expects the input Envelope's Payload to be a string (the prompt). It returns a new Envelope containing the generated text as the Payload.

Parameters:

  • ctx: The execution context.
  • input: The input envelope containing the prompt string.

Returns:

  • A result envelope with the generated text, or an error.

func (*LLMAction) Generate

func (a *LLMAction) Generate(ctx context.Context, prompt string, opts ...core.GenerateOption) (*core.LLMResponse, error)

Generate implements core.TextGenerator.

func (*LLMAction) Metadata

func (a *LLMAction) Metadata() core.ActionMetadata

Metadata returns the static metadata for this action.

func (*LLMAction) Stream

func (a *LLMAction) Stream(ctx context.Context, prompt string) (<-chan string, error)

Stream implements core.TextGenerator.

type LoggingMiddleware

type LoggingMiddleware struct {
	Logger core.Logger
}

LoggingMiddleware is a custom middleware that logs generation details.

func (*LoggingMiddleware) Name

func (l *LoggingMiddleware) Name() string

func (*LoggingMiddleware) New

func (l *LoggingMiddleware) New(ctx context.Context) (*ai.Hooks, error)

type MiddlewareConfig

type MiddlewareConfig struct {
	// Retry configures automatic retry with exponential backoff.
	Retry *middleware.Retry

	// Fallback configures alternative model fallback on failure.
	Fallback *middleware.Fallback

	// ToolApproval requires explicit approval for tool execution.
	ToolApproval *middleware.ToolApproval

	// Filesystem enables scoped file system access for the model.
	Filesystem *middleware.Filesystem

	// CustomMiddlewares allows injecting arbitrary middleware.
	CustomMiddlewares []ai.Middleware
}

MiddlewareConfig holds genkit middleware settings for a generation call. Use this with GenerateOption to configure per-call middleware.

func ComposeMiddleware

func ComposeMiddleware(opts ...GenerationOption) (*MiddlewareConfig, error)

ComposeMiddleware creates a single MiddlewareConfig from multiple options. This is useful when you want to build middleware incrementally.

type OODAExecutor

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

func NewOODAExecutor

func NewOODAExecutor(cfg *OODAExecutorConfig) *OODAExecutor

func (*OODAExecutor) Execute

func (e *OODAExecutor) Execute(ctx context.Context, input *OODAFlowInput) (*OODAFlowOutput, error)

func (*OODAExecutor) WithMaxRetries

func (e *OODAExecutor) WithMaxRetries(maxRetries int) *OODAExecutor

func (*OODAExecutor) WithMiddleware

func (e *OODAExecutor) WithMiddleware(mw ...ai.Middleware) *OODAExecutor

func (*OODAExecutor) WithTimeout

func (e *OODAExecutor) WithTimeout(timeout time.Duration) *OODAExecutor

type OODAExecutorConfig

type OODAExecutorConfig struct {
	MaxRetries     int
	Timeout        time.Duration
	Middleware     []ai.Middleware
	KnowledgeStore ports.KnowledgeStore
	TransientStore ports.TransientStore
}

type OODAFlow

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

func NewOODAFlow

func NewOODAFlow(cfg *OODAFlowConfig) *OODAFlow

func (*OODAFlow) DefineFlow

func (f *OODAFlow) DefineFlow(g *genkit.Genkit, name string)

func (*OODAFlow) DefineStreamingFlow

func (f *OODAFlow) DefineStreamingFlow(g *genkit.Genkit, name string)

func (*OODAFlow) Run

func (f *OODAFlow) Run(ctx context.Context, input *OODAFlowInput) (*OODAFlowOutput, error)

type OODAFlowConfig

type OODAFlowConfig struct {
	Memory          ooda.Memory
	Brain           ooda.Brain
	Executor        ooda.Executor
	Dispatcher      *ooda.Dispatcher
	KnowledgeStore  ports.KnowledgeStore
	TransientStore  ports.TransientStore
	MaxRetries      int
	Timeout         time.Duration
	GenerateOptions []core.GenerateOption
}

type OODAFlowInput

type OODAFlowInput struct {
	Input    string        `json:"input"`
	Intent   string        `json:"intent,omitempty"`
	TaskType ooda.TaskType `json:"task_type,omitempty"`
}

type OODAFlowOutput

type OODAFlowOutput struct {
	Output         string                       `json:"output"`
	Status         ooda.VerifyStatus            `json:"status"`
	PhaseDurations map[ooda.Phase]time.Duration `json:"phase_durations"`
	RetryCount     int                          `json:"retry_count"`
	AuditTrail     string                       `json:"audit_trail,omitempty"`
	Error          string                       `json:"error,omitempty"`
}

type OODAFlowWithMiddleware

type OODAFlowWithMiddleware struct {
	*OODAFlow
	// contains filtered or unexported fields
}

func NewOODAFlowWithGenkit

func NewOODAFlowWithGenkit(cfg *OODAFlowConfig, g *genkit.Genkit) *OODAFlowWithMiddleware

func (*OODAFlowWithMiddleware) Run

type OODAGenkitBridge

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

func NewOODAGenkitBridge

func NewOODAGenkitBridge(g *genkit.Genkit, cfg *OODAFlowConfig) *OODAGenkitBridge

func (*OODAGenkitBridge) DefineOODAFlow

func (b *OODAGenkitBridge) DefineOODAFlow(name string)

func (*OODAGenkitBridge) DefineOODAStreamingFlow

func (b *OODAGenkitBridge) DefineOODAStreamingFlow(name string)

func (*OODAGenkitBridge) RunOODA

func (b *OODAGenkitBridge) RunOODA(ctx context.Context, input *OODAFlowInput) (*OODAFlowOutput, error)

type TelemetryMiddleware

type TelemetryMiddleware struct {
	OnGenerate func(ctx context.Context, duration time.Duration, model string, inputTokens, outputTokens int)
}

TelemetryMiddleware is a custom middleware that records generation metrics.

func (*TelemetryMiddleware) Name

func (t *TelemetryMiddleware) Name() string

func (*TelemetryMiddleware) New

func (t *TelemetryMiddleware) New(ctx context.Context) (*ai.Hooks, error)

Jump to

Keyboard shortcuts

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