ai

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: Apache-2.0 Imports: 28 Imported by: 49

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	OutputFormatText  string = "text"
	OutputFormatJSON  string = "json"
	OutputFormatJSONL string = "jsonl"
	OutputFormatMedia string = "media"
	OutputFormatArray string = "array"
	OutputFormatEnum  string = "enum"
)

Variables ΒΆ

View Source
var DEFAULT_FORMATS = []Formatter{
	jsonFormatter{},
	jsonlFormatter{},
	textFormatter{},
	arrayFormatter{},
	enumFormatter{},
}

Default formats get automatically registered on registry init

Functions ΒΆ

func ConfigureFormats ΒΆ added in v0.5.0

func ConfigureFormats(reg api.Registry)

ConfigureFormats registers default formats in the registry

func DefineFormat ΒΆ added in v0.5.0

func DefineFormat(r api.Registry, name string, formatter Formatter)

DefineFormat defines and registers a new Formatter.

func DefineGenerateAction ΒΆ added in v0.3.0

func DefineGenerateAction(ctx context.Context, r api.Registry) *generateAction

DefineGenerateAction defines a utility generate action.

func FindMatchingResource ΒΆ added in v0.7.0

func FindMatchingResource(r api.Registry, uri string) (Resource, *ResourceInput, error)

FindMatchingResource finds a resource that matches the given URI.

func GenerateText ΒΆ added in v0.1.0

func GenerateText(ctx context.Context, r api.Registry, opts ...GenerateOption) (string, error)

GenerateText run generate request for this model. Returns generated text only.

func IsAudioContentType ΒΆ added in v1.0.0

func IsAudioContentType(contentType string) bool

IsAudioContentType checks if the content type represents an audio file.

func IsImageContentType ΒΆ added in v1.0.0

func IsImageContentType(contentType string) bool

IsImageContentType checks if the content type represents an image.

func IsVideoContentType ΒΆ added in v1.0.0

func IsVideoContentType(contentType string) bool

IsVideoContentType checks if the content type represents a video.

func LoadPromptDir ΒΆ added in v0.3.0

func LoadPromptDir(r api.Registry, dir string, namespace string)

LoadPromptDir loads prompts and partials from the input directory for the given namespace.

Types ΒΆ

type AugmentWithContextOptions ΒΆ added in v0.3.0

type AugmentWithContextOptions struct {
	Preface      *string                                                                // Preceding text to place before the rendered context documents.
	ItemTemplate func(d Document, index int, options *AugmentWithContextOptions) string // A function to render a document into a text part to be included in the message.
	CitationKey  *string                                                                // Metadata key to use for citation reference. Pass `nil` to provide no citations.
}

AugmentWithContextOptions configures how a request is augmented with context.

type BaseDataPoint ΒΆ added in v0.3.0

type BaseDataPoint struct {
	Context    map[string]any `json:"context,omitempty"`
	Input      map[string]any `json:"input,omitempty"`
	Output     map[string]any `json:"output,omitempty"`
	Reference  map[string]any `json:"reference,omitempty"`
	TestCaseID string         `json:"testCaseId,omitempty"`
	TraceIDs   []string       `json:"traceIds,omitempty"`
}

type BaseEvalDataPoint ΒΆ added in v0.3.0

type BaseEvalDataPoint struct {
	Context    map[string]any `json:"context,omitempty"`
	Input      map[string]any `json:"input,omitempty"`
	Output     map[string]any `json:"output,omitempty"`
	Reference  map[string]any `json:"reference,omitempty"`
	TestCaseID string         `json:"testCaseId,omitempty"`
	TraceIDs   []string       `json:"traceIds,omitempty"`
}

type BatchEvaluatorFunc ΒΆ added in v0.7.0

type BatchEvaluatorFunc = func(context.Context, *EvaluatorRequest) (*EvaluatorResponse, error)

BatchEvaluatorFunc is the function type for batch evaluator implementations.

type CandidateError ΒΆ added in v0.3.0

type CandidateError struct {
	Code    CandidateErrorCode `json:"code,omitempty"`
	Index   float64            `json:"index,omitempty"`
	Message string             `json:"message,omitempty"`
}

type CandidateErrorCode ΒΆ added in v0.3.0

type CandidateErrorCode string
const (
	CandidateErrorCodeBlocked CandidateErrorCode = "blocked"
	CandidateErrorCodeOther   CandidateErrorCode = "other"
	CandidateErrorCodeUnknown CandidateErrorCode = "unknown"
)

type CommonGenOption ΒΆ added in v0.5.0

type CommonGenOption interface {
	// contains filtered or unexported methods
}

func WithMaxTurns ΒΆ added in v0.3.0

func WithMaxTurns(maxTurns int) CommonGenOption

WithMaxTurns sets the maximum number of tool call iterations before erroring. A tool call happens when tools are provided in the request and a model decides to call one or more as a response. Each round trip, including multiple tools in parallel, counts as one turn.

func WithMessages ΒΆ added in v0.1.0

func WithMessages(messages ...*Message) CommonGenOption

WithMessages sets the messages. These messages will be sandwiched between the system and user prompts.

func WithMessagesFn ΒΆ added in v0.3.0

func WithMessagesFn(fn MessagesFn) CommonGenOption

WithMessagesFn sets the request messages to the result of the function. These messages will be sandwiched between the system and user messages.

func WithMiddleware ΒΆ added in v0.3.0

func WithMiddleware(middleware ...ModelMiddleware) CommonGenOption

WithMiddleware sets middleware to apply to the model request.

func WithModel ΒΆ added in v0.3.0

func WithModel(model ModelArg) CommonGenOption

WithModel sets either a Model or a ModelRef that may contain a config. Passing WithConfig will take precedence over the config in WithModel.

func WithModelName ΒΆ added in v0.3.0

func WithModelName(name string) CommonGenOption

WithModelName sets the model name to call for generation. The model name will be resolved to a Model and may error if the reference is invalid.

func WithResources ΒΆ added in v0.7.0

func WithResources(resources ...Resource) CommonGenOption

WithResources specifies resources to be temporarily available during generation. Resources are unregistered resources that get attached to a temporary registry during the generation request and cleaned up afterward.

func WithReturnToolRequests ΒΆ added in v0.3.0

func WithReturnToolRequests(returnReqs bool) CommonGenOption

WithReturnToolRequests configures whether to return tool requests instead of making the tool calls and continuing the generation.

func WithToolChoice ΒΆ added in v0.3.0

func WithToolChoice(toolChoice ToolChoice) CommonGenOption

WithToolChoice configures whether by default tool calls are required, disabled, or optional for the prompt.

func WithTools ΒΆ added in v0.1.0

func WithTools(tools ...ToolRef) CommonGenOption

WithTools sets the tools to use for the generate request.

type CommonRerankerOptions ΒΆ added in v0.3.0

type CommonRerankerOptions struct {
	// Number of documents to rerank
	K float64 `json:"k,omitempty"`
}

type CommonRetrieverOptions ΒΆ added in v0.3.0

type CommonRetrieverOptions struct {
	// Number of documents to retrieve
	K float64 `json:"k,omitempty"`
}

type ConfigOption ΒΆ added in v0.5.0

type ConfigOption interface {
	// contains filtered or unexported methods
}

ConfigOption is an option for model configuration.

func WithConfig ΒΆ added in v0.1.0

func WithConfig(config any) ConfigOption

WithConfig sets the configuration.

type ConstrainedSupport ΒΆ added in v0.3.0

type ConstrainedSupport string
const (
	ConstrainedSupportNone    ConstrainedSupport = "none"
	ConstrainedSupportAll     ConstrainedSupport = "all"
	ConstrainedSupportNoTools ConstrainedSupport = "no-tools"
)

type Document ΒΆ

type Document struct {
	// The data that is part of this document.
	Content []*Part `json:"content,omitempty"`
	// The metadata for this document.
	Metadata map[string]any `json:"metadata,omitempty"`
}

A Document is a piece of data that can be embedded, indexed, or retrieved. It includes metadata. It can contain multiple parts.

func DocumentFromText ΒΆ

func DocumentFromText(text string, metadata map[string]any) *Document

DocumentFromText returns a Document containing a single plain text part. This takes ownership of the metadata map.

type DocumentOption ΒΆ added in v0.5.0

type DocumentOption interface {
	// contains filtered or unexported methods
}

DocumentOption is an option for providing context or input documents. It applies only to Generate and [prompt.Execute].

func WithDocs ΒΆ added in v0.3.0

func WithDocs(docs ...*Document) DocumentOption

WithDocs sets the documents to be used as context for generation or as input to an embedder.

func WithTextDocs ΒΆ added in v0.5.0

func WithTextDocs(text ...string) DocumentOption

WithTextDocs sets the text to be used as context documents for generation or as input to an embedder.

type DownloadMediaOptions ΒΆ added in v0.3.0

type DownloadMediaOptions struct {
	MaxBytes int64                 // Maximum number of bytes to download.
	Filter   func(part *Part) bool // Filter to apply to parts that are media URLs.
}

DownloadMediaOptions configures how media is downloaded in the DownloadRequestMedia middleware.

type EmbedRequest ΒΆ

type EmbedRequest struct {
	Input   []*Document `json:"input,omitempty"`
	Options any         `json:"options,omitempty"`
}

type EmbedResponse ΒΆ added in v0.0.2

type EmbedResponse struct {
	Embeddings []*Embedding `json:"embeddings,omitempty"`
}

func Embed ΒΆ added in v0.1.0

func Embed(ctx context.Context, r api.Registry, opts ...EmbedderOption) (*EmbedResponse, error)

Embed invokes the embedder with provided options.

type Embedder ΒΆ

type Embedder interface {
	// Name returns the registry name of the embedder.
	Name() string
	// Embed embeds to content as part of the [EmbedRequest].
	Embed(ctx context.Context, req *EmbedRequest) (*EmbedResponse, error)
	// Register registers the embedder with the given registry.
	Register(r api.Registry)
}

Embedder represents an embedder that can perform content embedding.

func DefineEmbedder ΒΆ

func DefineEmbedder(r api.Registry, name string, opts *EmbedderOptions, fn EmbedderFunc) Embedder

DefineEmbedder registers the given embed function as an action, and returns an Embedder that runs it.

func LookupEmbedder ΒΆ

func LookupEmbedder(r api.Registry, name string) Embedder

LookupEmbedder looks up an Embedder registered by DefineEmbedder. It will try to resolve the embedder dynamically if the embedder is not found. It returns nil if the embedder was not resolved.

func NewEmbedder ΒΆ added in v0.7.0

func NewEmbedder(name string, opts *EmbedderOptions, fn EmbedderFunc) Embedder

NewEmbedder creates a new Embedder.

type EmbedderArg ΒΆ added in v0.7.0

type EmbedderArg interface {
	Name() string
}

EmbedderArg is the interface for embedder arguments. It can either be the embedder action itself or a reference to be looked up.

type EmbedderFunc ΒΆ added in v0.7.0

type EmbedderFunc = func(context.Context, *EmbedRequest) (*EmbedResponse, error)

EmbedderFunc is the function type for embedding documents.

type EmbedderOption ΒΆ added in v0.5.0

type EmbedderOption interface {
	// contains filtered or unexported methods
}

EmbedderOption is an option for configuring an embedder request. It applies only to Embed.

func WithEmbedder ΒΆ added in v0.7.0

func WithEmbedder(embedder EmbedderArg) EmbedderOption

WithEmbedder sets either a Embedder or a EmbedderRef that may contain a config. Passing WithConfig will take precedence over the config in WithEmbedder.

func WithEmbedderName ΒΆ added in v0.7.0

func WithEmbedderName(name string) EmbedderOption

WithEmbedderName sets the embedder name to call for document embedding. The embedder name will be resolved to a Embedder and may error if the reference is invalid.

type EmbedderOptions ΒΆ added in v0.7.0

type EmbedderOptions struct {
	// ConfigSchema is the JSON schema for the embedder's config.
	ConfigSchema map[string]any `json:"configSchema,omitempty"`
	// Label is a user-friendly name for the embedder model (e.g., "Google AI - Gemini Pro").
	Label string `json:"label,omitempty"`
	// Supports defines the capabilities of the embedder, such as input types and multilingual support.
	Supports *EmbedderSupports `json:"supports,omitempty"`
	// Dimensions specifies the number of dimensions in the embedding vector.
	Dimensions int `json:"dimensions,omitempty"`
}

EmbedderOptions represents the configuration options for an embedder.

type EmbedderRef ΒΆ added in v0.7.0

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

EmbedderRef is a struct to hold embedder name and configuration.

func NewEmbedderRef ΒΆ added in v0.7.0

func NewEmbedderRef(name string, config any) EmbedderRef

NewEmbedderRef creates a new EmbedderRef with the given name and configuration.

func (EmbedderRef) Config ΒΆ added in v0.7.0

func (e EmbedderRef) Config() any

Config returns the configuration to use by default for this embedder.

func (EmbedderRef) Name ΒΆ added in v0.7.0

func (e EmbedderRef) Name() string

Name returns the name of the embedder.

type EmbedderSupports ΒΆ added in v0.7.0

type EmbedderSupports struct {
	// Input lists the types of data the model can process (e.g., "text", "image", "video").
	Input []string `json:"input,omitempty"`
	// Multilingual indicates whether the model supports multiple languages.
	Multilingual bool `json:"multilingual,omitempty"`
}

EmbedderSupports represents the supported capabilities of the embedder model.

type Embedding ΒΆ added in v0.3.0

type Embedding struct {
	Embedding []float32      `json:"embedding,omitempty"`
	Metadata  map[string]any `json:"metadata,omitempty"`
}

type EvalFnResponse ΒΆ added in v0.3.0

type EvalFnResponse struct {
	Evaluation  any     `json:"evaluation,omitempty"`
	SampleIndex float64 `json:"sampleIndex,omitempty"`
	SpanID      string  `json:"spanId,omitempty"`
	TestCaseID  string  `json:"testCaseId,omitempty"`
	TraceID     string  `json:"traceId,omitempty"`
}

type EvalRequest ΒΆ added in v0.3.0

type EvalRequest struct {
	Dataset   []*BaseDataPoint `json:"dataset,omitempty"`
	EvalRunID string           `json:"evalRunId,omitempty"`
	Options   any              `json:"options,omitempty"`
}

type EvalResponse ΒΆ added in v0.3.0

type EvalResponse []any

type EvalStatusEnum ΒΆ added in v0.3.0

type EvalStatusEnum string
const (
	EvalStatusEnumUNKNOWN EvalStatusEnum = "UNKNOWN"
	EvalStatusEnumPASS    EvalStatusEnum = "PASS"
	EvalStatusEnumFAIL    EvalStatusEnum = "FAIL"
)

type EvaluationResult ΒΆ added in v0.3.0

type EvaluationResult struct {
	TestCaseId string  `json:"testCaseId"`
	TraceID    string  `json:"traceId,omitempty"`
	SpanID     string  `json:"spanId,omitempty"`
	Evaluation []Score `json:"evaluation"`
}

EvaluationResult is the result of running the evaluator on a single Example. An evaluator may provide multiple scores simultaneously (e.g. if they are using an API to score on multiple criteria)

type Evaluator ΒΆ added in v0.3.0

type Evaluator interface {
	// Name returns the name of the evaluator.
	Name() string
	// Evaluates a dataset.
	Evaluate(ctx context.Context, req *EvaluatorRequest) (*EvaluatorResponse, error)
	// Register registers the evaluator with the given registry.
	Register(r api.Registry)
}

Evaluator represents a evaluator action.

func DefineBatchEvaluator ΒΆ added in v0.3.0

func DefineBatchEvaluator(r api.Registry, name string, opts *EvaluatorOptions, fn BatchEvaluatorFunc) Evaluator

DefineBatchEvaluator creates a new Evaluator and registers it. This method provides the full EvaluatorRequest to the callback function, giving more flexibility to the user for processing the data, such as batching or parallelization.

func DefineEvaluator ΒΆ added in v0.3.0

func DefineEvaluator(r api.Registry, name string, opts *EvaluatorOptions, fn EvaluatorFunc) Evaluator

DefineEvaluator creates a new Evaluator and registers it. This method processes the input dataset one-by-one.

func LookupEvaluator ΒΆ added in v0.3.0

func LookupEvaluator(r api.Registry, name string) Evaluator

LookupEvaluator looks up an Evaluator registered by DefineEvaluator. It returns nil if the evaluator was not defined.

func NewBatchEvaluator ΒΆ added in v0.7.0

func NewBatchEvaluator(name string, opts *EvaluatorOptions, fn BatchEvaluatorFunc) Evaluator

NewBatchEvaluator creates a new Evaluator. This method provides the full EvaluatorRequest to the callback function, giving more flexibility to the user for processing the data, such as batching or parallelization.

func NewEvaluator ΒΆ added in v0.7.0

func NewEvaluator(name string, opts *EvaluatorOptions, fn EvaluatorFunc) Evaluator

NewEvaluator creates a new Evaluator. This method processes the input dataset one-by-one.

type EvaluatorArg ΒΆ added in v0.7.0

type EvaluatorArg interface {
	Name() string
}

EvaluatorArg is the interface for evaluator arguments. It can either be the evaluator action itself or a reference to be looked up.

type EvaluatorCallbackRequest ΒΆ added in v0.3.0

type EvaluatorCallbackRequest struct {
	Input   Example `json:"input"`
	Options any     `json:"options,omitempty"`
}

EvaluatorCallbackRequest is the data we pass to the callback function provided in defineEvaluator. The Options field is specific to the actual evaluator implementation.

type EvaluatorCallbackResponse ΒΆ added in v0.3.0

type EvaluatorCallbackResponse = EvaluationResult

EvaluatorCallbackResponse is the result on evaluating a single Example

type EvaluatorFunc ΒΆ added in v0.7.0

EvaluatorFunc is the function type for evaluator implementations.

type EvaluatorOption ΒΆ added in v0.5.0

type EvaluatorOption interface {
	// contains filtered or unexported methods
}

EvaluatorOption is an option for providing a dataset to evaluate. It applies only to [Evaluator.Evaluate].

func WithDataset ΒΆ added in v0.5.0

func WithDataset(examples ...*Example) EvaluatorOption

WithDataset sets the dataset to do evaluation on.

func WithEvaluator ΒΆ added in v0.7.0

func WithEvaluator(evaluator EvaluatorArg) EvaluatorOption

WithEvaluator sets either a Evaluator or a EvaluatorRef that may contain a config. Passing WithConfig will take precedence over the config in WithEvaluator.

func WithEvaluatorName ΒΆ added in v0.7.0

func WithEvaluatorName(name string) EvaluatorOption

WithEvaluatorName sets the evaluator name to call for document evaluation. The evaluator name will be resolved to a Evaluator and may error if the reference is invalid.

func WithID ΒΆ added in v0.5.0

func WithID(ID string) EvaluatorOption

WithID sets the ID of the evaluation to uniquely identify it.

type EvaluatorOptions ΒΆ added in v0.3.0

type EvaluatorOptions struct {
	// ConfigSchema is the JSON schema for the evaluator's config.
	ConfigSchema map[string]any `json:"configSchema,omitempty"`
	// DisplayName is the name of the evaluator as it appears in the UI.
	DisplayName string `json:"displayName"`
	// Definition is the definition of the evaluator.
	Definition string `json:"definition"`
	// IsBilled is a flag indicating if the evaluator is billed.
	IsBilled bool `json:"isBilled,omitempty"`
}

type EvaluatorRef ΒΆ added in v0.7.0

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

EvaluatorRef is a struct to hold evaluator name and configuration.

func NewEvaluatorRef ΒΆ added in v0.7.0

func NewEvaluatorRef(name string, config any) EvaluatorRef

NewEvaluatorRef creates a new EvaluatorRef with the given name and configuration.

func (EvaluatorRef) Config ΒΆ added in v0.7.0

func (e EvaluatorRef) Config() any

Config returns the configuration to use by default for this evaluator.

func (EvaluatorRef) Name ΒΆ added in v0.7.0

func (e EvaluatorRef) Name() string

Name returns the name of the evaluator.

type EvaluatorRequest ΒΆ added in v0.3.0

type EvaluatorRequest struct {
	Dataset      []*Example `json:"dataset"`
	EvaluationId string     `json:"evalRunId"`
	Options      any        `json:"options,omitempty"`
}

EvaluatorRequest is the data we pass to evaluate a dataset. The Options field is specific to the actual evaluator implementation.

type EvaluatorResponse ΒΆ added in v0.3.0

type EvaluatorResponse = []EvaluationResult

EvaluatorResponse is a collection of EvaluationResult structs, it represents the result on the entire input dataset.

func Evaluate ΒΆ added in v0.3.0

func Evaluate(ctx context.Context, r api.Registry, opts ...EvaluatorOption) (*EvaluatorResponse, error)

Evaluate calls the retrivers with provided options.

type Example ΒΆ added in v0.3.0

type Example struct {
	TestCaseId string   `json:"testCaseId,omitempty"`
	Input      any      `json:"input"`
	Output     any      `json:"output,omitempty"`
	Context    []any    `json:"context,omitempty"`
	Reference  any      `json:"reference,omitempty"`
	TraceIds   []string `json:"traceIds,omitempty"`
}

Example is a single example that requires evaluation

type ExecutionOption ΒΆ added in v0.3.0

type ExecutionOption interface {
	// contains filtered or unexported methods
}

ExecutionOption is an option for the execution of a prompt or generate request. It applies only to Generate() and prompt.Execute().

func WithStreaming ΒΆ added in v0.1.0

func WithStreaming(callback ModelStreamCallback) ExecutionOption

WithStreaming sets the stream callback for the generate request. A callback is a function that is called with each chunk of the generated response before the final response is returned.

type FinishReason ΒΆ

type FinishReason string
const (
	FinishReasonStop        FinishReason = "stop"
	FinishReasonLength      FinishReason = "length"
	FinishReasonBlocked     FinishReason = "blocked"
	FinishReasonInterrupted FinishReason = "interrupted"
	FinishReasonOther       FinishReason = "other"
	FinishReasonUnknown     FinishReason = "unknown"
)

type FormatHandler ΒΆ added in v0.5.0

type FormatHandler interface {
	// ParseMessage parses the message and returns a new formatted message.
	ParseMessage(message *Message) (*Message, error)
	// Instructions returns the formatter instructions to embed in the prompt.
	Instructions() string
	// Config returns the output config for the model request.
	Config() ModelOutputConfig
}

FormatHandler represents the handler part of the Formatter interface.

type Formatter ΒΆ added in v0.5.0

type Formatter interface {
	// Name returns the name of the formatter.
	Name() string
	// Handler returns the handler for the formatter.
	Handler(schema map[string]any) (FormatHandler, error)
}

Formatter represents the Formatter interface.

type GenerateActionOptions ΒΆ added in v0.3.0

type GenerateActionOptions struct {
	Config             any                         `json:"config,omitempty"`
	Docs               []*Document                 `json:"docs,omitempty"`
	MaxTurns           int                         `json:"maxTurns,omitempty"`
	Messages           []*Message                  `json:"messages,omitempty"`
	Model              string                      `json:"model,omitempty"`
	Output             *GenerateActionOutputConfig `json:"output,omitempty"`
	Resume             *GenerateActionResume       `json:"resume,omitempty"`
	ReturnToolRequests bool                        `json:"returnToolRequests,omitempty"`
	StepName           string                      `json:"stepName,omitempty"`
	ToolChoice         ToolChoice                  `json:"toolChoice,omitempty"`
	Tools              []string                    `json:"tools,omitempty"`
}

type GenerateActionOutputConfig ΒΆ added in v0.3.0

type GenerateActionOutputConfig struct {
	Constrained  bool           `json:"constrained,omitempty"`
	ContentType  string         `json:"contentType,omitempty"`
	Format       string         `json:"format,omitempty"`
	Instructions *string        `json:"instructions,omitempty"`
	JsonSchema   map[string]any `json:"jsonSchema,omitempty"`
}

type GenerateActionResume ΒΆ added in v0.3.0

type GenerateActionResume struct {
	Metadata map[string]any      `json:"metadata,omitempty"`
	Respond  []*toolResponsePart `json:"respond,omitempty"`
	Restart  []*toolRequestPart  `json:"restart,omitempty"`
}

type GenerateOption ΒΆ added in v0.1.0

type GenerateOption interface {
	// contains filtered or unexported methods
}

GenerateOption is an option for generating a model response. It applies only to Generate().

func WithToolResponses ΒΆ added in v0.6.0

func WithToolResponses(parts ...*Part) GenerateOption

WithToolResponses sets the tool responses to return from interrupted tool calls.

func WithToolRestarts ΒΆ added in v0.6.0

func WithToolRestarts(parts ...*Part) GenerateOption

WithToolRestarts sets the tool requests to restart interrupted tools with.

type GenerationCommonConfig ΒΆ

type GenerationCommonConfig struct {
	MaxOutputTokens int      `json:"maxOutputTokens,omitempty"`
	StopSequences   []string `json:"stopSequences,omitempty"`
	Temperature     float64  `json:"temperature,omitempty"`
	TopK            int      `json:"topK,omitempty"`
	TopP            float64  `json:"topP,omitempty"`
	Version         string   `json:"version,omitempty"`
}

GenerationCommonConfig holds configuration for generation.

type GenerationUsage ΒΆ

type GenerationUsage struct {
	CachedContentTokens int                `json:"cachedContentTokens,omitempty"`
	Custom              map[string]float64 `json:"custom,omitempty"`
	InputAudioFiles     int                `json:"inputAudioFiles,omitempty"`
	InputCharacters     int                `json:"inputCharacters,omitempty"`
	InputImages         int                `json:"inputImages,omitempty"`
	InputTokens         int                `json:"inputTokens,omitempty"`
	InputVideos         int                `json:"inputVideos,omitempty"`
	OutputAudioFiles    int                `json:"outputAudioFiles,omitempty"`
	OutputCharacters    int                `json:"outputCharacters,omitempty"`
	OutputImages        int                `json:"outputImages,omitempty"`
	OutputTokens        int                `json:"outputTokens,omitempty"`
	OutputVideos        int                `json:"outputVideos,omitempty"`
	ThoughtsTokens      int                `json:"thoughtsTokens,omitempty"`
	TotalTokens         int                `json:"totalTokens,omitempty"`
}

GenerationUsage provides information about the generation process.

type InterruptOptions ΒΆ added in v0.3.0

type InterruptOptions struct {
	Metadata map[string]any
}

InterruptOptions provides configuration for tool interruption.

type Media ΒΆ added in v0.3.0

type Media struct {
	ContentType string `json:"contentType,omitempty"`
	Url         string `json:"url,omitempty"`
}

type Message ΒΆ

type Message struct {
	Content  []*Part        `json:"content,omitempty"`
	Metadata map[string]any `json:"metadata,omitempty"`
	Role     Role           `json:"role,omitempty"`
}

Message is the contents of a model response.

func NewMessage ΒΆ

func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message

NewMessage creates a new Message with the provided role, metadata and parts. Use NewTextMessage if you have a text-only message.

func NewModelMessage ΒΆ

func NewModelMessage(parts ...*Part) *Message

NewModelMessage creates a new Message with role "model" and provided parts. Use NewModelTextMessage if you have a text-only message.

func NewModelTextMessage ΒΆ

func NewModelTextMessage(text string) *Message

NewUserTextMessage creates a new Message with role "model" and content with a single text part with the content of provided text.

func NewSystemMessage ΒΆ

func NewSystemMessage(parts ...*Part) *Message

NewSystemMessage creates a new Message with role "system" and provided parts. Use NewSystemTextMessage if you have a text-only message.

func NewSystemTextMessage ΒΆ

func NewSystemTextMessage(text string) *Message

NewUserTextMessage creates a new Message with role "system" and content with a single text part with the content of provided text.

func NewTextMessage ΒΆ

func NewTextMessage(role Role, text string) *Message

NewTextMessage creates a new Message with the provided role and content with a single part containint provided text.

func NewUserMessage ΒΆ

func NewUserMessage(parts ...*Part) *Message

NewUserMessage creates a new Message with role "user" and provided parts. Use NewUserTextMessage if you have a text-only message.

func NewUserMessageWithMetadata ΒΆ added in v0.3.0

func NewUserMessageWithMetadata(metadata map[string]any, parts ...*Part) *Message

NewUserMessageWithMetadata creates a new Message with role "user" with provided metadata and parts. Use NewUserTextMessage if you have a text-only message.

func NewUserTextMessage ΒΆ

func NewUserTextMessage(text string) *Message

NewUserTextMessage creates a new Message with role "user" and content with a single text part with the content of provided text.

func (*Message) Text ΒΆ added in v0.3.0

func (m *Message) Text() string

Text returns the contents of a Message as a string. It returns an empty string if the message has no content. If you want to get reasoning from the message, use Reasoning() instead.

func (*Message) WithCacheName ΒΆ added in v0.3.0

func (m *Message) WithCacheName(n string) *Message

WithCacheName adds cache name to use in the generate request

func (*Message) WithCacheTTL ΒΆ added in v0.3.0

func (m *Message) WithCacheTTL(ttlSeconds int) *Message

WithCacheTTL adds cache TTL configuration for the desired message

type MessagesFn ΒΆ added in v0.5.0

type MessagesFn = func(context.Context, any) ([]*Message, error)

MessagesFn is a function that generates messages.

type Model ΒΆ

type Model interface {
	// Name returns the registry name of the model.
	Name() string
	// Generate applies the [Model] to provided request, handling tool requests and handles streaming.
	Generate(ctx context.Context, req *ModelRequest, cb ModelStreamCallback) (*ModelResponse, error)
	// Register registers the model with the given registry.
	Register(r api.Registry)
}

Model represents a model that can generate content based on a request.

func DefineModel ΒΆ

func DefineModel(r api.Registry, name string, opts *ModelOptions, fn ModelFunc) Model

DefineModel creates a new Model and registers it.

func LookupModel ΒΆ

func LookupModel(r api.Registry, name string) Model

LookupModel looks up a Model registered by DefineModel. It will try to resolve the model dynamically if the model is not found. It returns nil if the model was not resolved.

func NewModel ΒΆ added in v0.7.0

func NewModel(name string, opts *ModelOptions, fn ModelFunc) Model

NewModel creates a new Model.

type ModelArg ΒΆ added in v0.5.0

type ModelArg interface {
	Name() string
}

ModelArg is the interface for model arguments. It can either be the retriever action itself or a reference to be looked up.

type ModelFunc ΒΆ added in v0.3.0

ModelFunc is a streaming function that takes in a ModelRequest and generates a ModelResponse, optionally streaming ModelResponseChunks.

type ModelInfo ΒΆ

type ModelInfo struct {
	ConfigSchema map[string]any `json:"configSchema,omitempty"`
	Label        string         `json:"label,omitempty"`
	Stage        ModelStage     `json:"stage,omitempty"`
	Supports     *ModelSupports `json:"supports,omitempty"`
	Versions     []string       `json:"versions,omitempty"`
}

type ModelMiddleware ΒΆ added in v0.3.0

ModelMiddleware is middleware for model generate requests that takes in a ModelFunc, does something, then returns another ModelFunc.

func DownloadRequestMedia ΒΆ added in v0.3.0

func DownloadRequestMedia(opts *DownloadMediaOptions) ModelMiddleware

DownloadRequestMedia downloads media from a URL and replaces the media part with a base64 encoded string.

type ModelOptions ΒΆ added in v0.7.0

type ModelOptions struct {
	// ConfigSchema is the JSON schema for the model's config.
	ConfigSchema map[string]any `json:"configSchema,omitempty"`
	// Label is a user-friendly name for the model.
	Label string `json:"label,omitempty"`
	// Stage indicates the maturity stage of the model.
	Stage ModelStage `json:"stage,omitempty"`
	// Supports defines the capabilities of the model.
	Supports *ModelSupports `json:"supports,omitempty"`
	// Versions lists the available versions of the model.
	Versions []string `json:"versions,omitempty"`
}

ModelOptions represents the configuration options for a model.

type ModelOutputConfig ΒΆ added in v0.3.0

type ModelOutputConfig struct {
	Constrained bool           `json:"constrained,omitempty"`
	ContentType string         `json:"contentType,omitempty"`
	Format      string         `json:"format,omitempty"`
	Schema      map[string]any `json:"schema,omitempty"`
}

OutputConfig describes the structure that the model's output should conform to. If Format is OutputFormatJSON, then Schema can describe the desired form of the generated JSON.

type ModelRef ΒΆ added in v0.5.0

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

ModelRef is a struct to hold model name and configuration.

func NewModelRef ΒΆ added in v0.5.0

func NewModelRef(name string, config any) ModelRef

NewModelRef creates a new ModelRef with the given name and configuration.

func (ModelRef) Config ΒΆ added in v0.5.0

func (m ModelRef) Config() any

Config returns the configuration to use by default for this model.

func (ModelRef) Name ΒΆ added in v0.5.0

func (m ModelRef) Name() string

Name returns the name of the model.

type ModelRequest ΒΆ added in v0.3.0

type ModelRequest struct {
	Config   any         `json:"config,omitempty"`
	Docs     []*Document `json:"docs,omitempty"`
	Messages []*Message  `json:"messages,omitempty"`
	// Output describes the desired response format.
	Output     *ModelOutputConfig `json:"output,omitempty"`
	ToolChoice ToolChoice         `json:"toolChoice,omitempty"`
	// Tools lists the available tools that the model can ask the client to run.
	Tools []*ToolDefinition `json:"tools,omitempty"`
}

A ModelRequest is a request to generate completions from a model.

func NewModelRequest ΒΆ added in v0.3.0

func NewModelRequest(config any, messages ...*Message) *ModelRequest

NewModelRequest create a new ModelRequest with provided config and messages.

type ModelResponse ΒΆ added in v0.3.0

type ModelResponse struct {
	Custom        any          `json:"custom,omitempty"`
	FinishMessage string       `json:"finishMessage,omitempty"`
	FinishReason  FinishReason `json:"finishReason,omitempty"`
	// LatencyMs is the time the request took in milliseconds.
	LatencyMs float64  `json:"latencyMs,omitempty"`
	Message   *Message `json:"message,omitempty"`
	// Request is the [ModelRequest] struct used to trigger this response.
	Request *ModelRequest `json:"request,omitempty"`
	// Usage describes how many resources were used by this generation request.
	Usage *GenerationUsage `json:"usage,omitempty"`
}

A ModelResponse is a model's response to a ModelRequest.

func Generate ΒΆ added in v0.1.0

func Generate(ctx context.Context, r api.Registry, opts ...GenerateOption) (*ModelResponse, error)

Generate generates a model response based on the provided options.

func GenerateData ΒΆ added in v0.1.0

func GenerateData[Out any](ctx context.Context, r api.Registry, opts ...GenerateOption) (*Out, *ModelResponse, error)

Generate run generate request for this model. Returns ModelResponse struct. TODO: Stream GenerateData with partial JSON

func GenerateWithRequest ΒΆ added in v0.3.0

GenerateWithRequest is the central generation implementation for ai.Generate(), prompt.Execute(), and the GenerateAction direct call.

func (*ModelResponse) History ΒΆ added in v0.3.0

func (mr *ModelResponse) History() []*Message

History returns messages from the request combined with the response message to represent the conversation history.

func (*ModelResponse) Interrupts ΒΆ added in v1.0.3

func (mr *ModelResponse) Interrupts() []*Part

Interrupts returns the interrupted tool request parts from the response.

func (*ModelResponse) Output ΒΆ added in v0.3.0

func (mr *ModelResponse) Output(v any) error

Output unmarshals structured JSON output into the provided struct pointer.

func (*ModelResponse) Reasoning ΒΆ added in v0.6.0

func (mr *ModelResponse) Reasoning() string

Reasoning concatenates all reasoning parts present in the message

func (*ModelResponse) Text ΒΆ added in v0.3.0

func (gr *ModelResponse) Text() string

Text returns the contents of the first candidate in a ModelResponse as a string. It returns an empty string if there are no candidates or if the candidate has no message.

func (*ModelResponse) ToolRequests ΒΆ added in v0.5.0

func (mr *ModelResponse) ToolRequests() []*ToolRequest

ToolRequests returns the tool requests from the response.

type ModelResponseChunk ΒΆ added in v0.3.0

type ModelResponseChunk struct {
	Aggregated bool    `json:"aggregated,omitempty"`
	Content    []*Part `json:"content,omitempty"`
	Custom     any     `json:"custom,omitempty"`
	Index      int     `json:"index,omitempty"`
	Role       Role    `json:"role,omitempty"`
}

A ModelResponseChunk is the portion of the ModelResponse that is passed to a streaming callback.

func (*ModelResponseChunk) Text ΒΆ added in v0.3.0

func (c *ModelResponseChunk) Text() string

Text returns the text content of the ModelResponseChunk as a string. It returns an error if there is no Content in the response chunk.

type ModelStage ΒΆ added in v0.3.0

type ModelStage string
const (
	ModelStageFeatured   ModelStage = "featured"
	ModelStageStable     ModelStage = "stable"
	ModelStageUnstable   ModelStage = "unstable"
	ModelStageLegacy     ModelStage = "legacy"
	ModelStageDeprecated ModelStage = "deprecated"
)

type ModelStreamCallback ΒΆ added in v0.3.0

type ModelStreamCallback = func(context.Context, *ModelResponseChunk) error

ModelStreamCallback is a stream callback of a ModelAction.

type ModelSupports ΒΆ added in v0.3.0

type ModelSupports struct {
	Constrained ConstrainedSupport `json:"constrained,omitempty"`
	ContentType []string           `json:"contentType,omitempty"`
	Context     bool               `json:"context,omitempty"`
	Media       bool               `json:"media,omitempty"`
	Multiturn   bool               `json:"multiturn,omitempty"`
	Output      []string           `json:"output,omitempty"`
	SystemRole  bool               `json:"systemRole,omitempty"`
	ToolChoice  bool               `json:"toolChoice,omitempty"`
	Tools       bool               `json:"tools,omitempty"`
}

type OutputOption ΒΆ added in v0.3.0

type OutputOption interface {
	// contains filtered or unexported methods
}

OutputOption is an option for the output of a prompt or generate request. It applies only to DefinePrompt() and Generate().

func WithCustomConstrainedOutput ΒΆ added in v0.5.0

func WithCustomConstrainedOutput() OutputOption

WithCustomConstrainedOutput opts out of using the model's native constrained output generation.

By default, the system will use the model's native constrained output capabilities when available. When this option is set, or when the model doesn't support native constraints, the system will use custom implementation to guide the model toward producing properly formatted output.

func WithOutputFormat ΒΆ added in v0.1.0

func WithOutputFormat(format string) OutputOption

WithOutputFormat sets the format of the output.

func WithOutputInstructions ΒΆ added in v0.5.0

func WithOutputInstructions(instructions string) OutputOption

WithOutputInstructions sets custom instructions for constraining output format in the prompt.

When WithOutputType is used without this option, default instructions will be automatically set. If you provide empty instructions, no instructions will be added to the prompt.

This will automatically set WithCustomConstrainedOutput.

func WithOutputType ΒΆ added in v0.3.0

func WithOutputType(output any) OutputOption

WithOutputType sets the schema and format of the output based on the value provided.

type Part ΒΆ

type Part struct {
	Kind         PartKind       `json:"kind,omitempty"`
	ContentType  string         `json:"contentType,omitempty"`  // valid for kind==blob
	Text         string         `json:"text,omitempty"`         // valid for kind∈{text,blob}
	ToolRequest  *ToolRequest   `json:"toolRequest,omitempty"`  // valid for kind==partToolRequest
	ToolResponse *ToolResponse  `json:"toolResponse,omitempty"` // valid for kind==partToolResponse
	Resource     *ResourcePart  `json:"resource,omitempty"`     // valid for kind==partResource
	Custom       map[string]any `json:"custom,omitempty"`       // valid for plugin-specific custom parts
	Metadata     map[string]any `json:"metadata,omitempty"`     // valid for all kinds
}

A Part is one part of a Document. This may be plain text or it may be a URL (possibly a "data:" URL with embedded data).

func NewCustomPart ΒΆ added in v0.5.1

func NewCustomPart(customData map[string]any) *Part

NewCustomPart returns a Part containing custom plugin-specific data.

func NewDataPart ΒΆ

func NewDataPart(contents string) *Part

NewDataPart returns a Part containing raw string data.

func NewJSONPart ΒΆ

func NewJSONPart(text string) *Part

NewJSONPart returns a Part containing JSON.

func NewMediaPart ΒΆ

func NewMediaPart(mimeType, contents string) *Part

NewMediaPart returns a Part containing structured data described by the given mimeType.

func NewReasoningPart ΒΆ added in v0.6.0

func NewReasoningPart(text string, signature []byte) *Part

NewReasoningPart returns a Part containing reasoning text

func NewResourcePart ΒΆ added in v0.7.0

func NewResourcePart(uri string) *Part

NewResourcePart returns a Part containing a resource reference.

func NewResponseForToolRequest ΒΆ added in v0.6.0

func NewResponseForToolRequest(p *Part, output any) *Part

NewResponseForToolRequest returns a Part containing the results of executing the tool request part.

func NewTextPart ΒΆ

func NewTextPart(text string) *Part

NewTextPart returns a Part containing text.

func NewToolRequestPart ΒΆ

func NewToolRequestPart(r *ToolRequest) *Part

NewToolRequestPart returns a Part containing a request from the model to the client to run a Tool. (Only genkit plugins should need to use this function.)

func NewToolResponsePart ΒΆ

func NewToolResponsePart(r *ToolResponse) *Part

NewToolResponsePart returns a Part containing the results of applying a Tool that the model requested.

func (*Part) IsAudio ΒΆ added in v1.0.0

func (p *Part) IsAudio() bool

IsAudio reports whether the Part contains an audio file.

func (*Part) IsCustom ΒΆ added in v0.5.1

func (p *Part) IsCustom() bool

IsCustom reports whether the Part contains custom plugin-specific data.

func (*Part) IsData ΒΆ

func (p *Part) IsData() bool

IsData reports whether the Part contains unstructured data.

func (*Part) IsImage ΒΆ added in v1.0.0

func (p *Part) IsImage() bool

IsImage reports whether the Part contains an image.

func (*Part) IsInterrupt ΒΆ added in v1.0.3

func (p *Part) IsInterrupt() bool

IsInterrupt reports whether the Part contains a tool request that was interrupted.

func (*Part) IsMedia ΒΆ

func (p *Part) IsMedia() bool

IsMedia reports whether the Part contains structured media data.

func (*Part) IsReasoning ΒΆ added in v0.6.0

func (p *Part) IsReasoning() bool

IsReasoning reports whether the Part contains a reasoning text

func (*Part) IsResource ΒΆ added in v0.7.0

func (p *Part) IsResource() bool

IsResource reports whether the Part contains a resource reference.

func (*Part) IsText ΒΆ

func (p *Part) IsText() bool

IsText reports whether the Part contains plain text.

func (*Part) IsToolRequest ΒΆ

func (p *Part) IsToolRequest() bool

IsToolRequest reports whether the Part contains a request to run a tool.

func (*Part) IsToolResponse ΒΆ

func (p *Part) IsToolResponse() bool

IsToolResponse reports whether the Part contains the result of running a tool.

func (*Part) IsVideo ΒΆ added in v1.0.0

func (p *Part) IsVideo() bool

IsVideo reports whether the Part contains a video.

func (Part) JSONSchemaAlias ΒΆ

func (Part) JSONSchemaAlias() any

JSONSchemaAlias tells the JSON schema reflection code to use a different type for the schema for this type. This is needed because the JSON marshaling of Part uses a schema that matches the TypeScript code, rather than the natural JSON marshaling. This matters because the current JSON validation code works by marshaling the JSON.

func (*Part) MarshalJSON ΒΆ

func (p *Part) MarshalJSON() ([]byte, error)

MarshalJSON is called by the JSON marshaler to write out a Part.

func (*Part) UnmarshalJSON ΒΆ

func (p *Part) UnmarshalJSON(b []byte) error

UnmarshalJSON is called by the JSON unmarshaler to read a Part.

func (*Part) UnmarshalYAML ΒΆ added in v0.3.0

func (p *Part) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements goccy/go-yaml library's InterfaceUnmarshaler interface.

type PartKind ΒΆ

type PartKind int8
const (
	PartText PartKind = iota
	PartMedia
	PartData
	PartToolRequest
	PartToolResponse
	PartCustom
	PartReasoning
	PartResource
)

type PathMetadata ΒΆ added in v0.3.0

type PathMetadata struct {
	Error   string  `json:"error,omitempty"`
	Latency float64 `json:"latency,omitempty"`
	Path    string  `json:"path,omitempty"`
	Status  string  `json:"status,omitempty"`
}

type Prompt ΒΆ

type Prompt interface {
	// Name returns the name of the prompt.
	Name() string
	// Execute executes the prompt with the given options and returns a [ModelResponse].
	Execute(ctx context.Context, opts ...PromptExecuteOption) (*ModelResponse, error)
	// Render renders the prompt with the given input and returns a [GenerateActionOptions] to be used with [GenerateWithRequest].
	Render(ctx context.Context, input any) (*GenerateActionOptions, error)
}

Prompt is the interface for a prompt that can be executed and rendered.

func DefinePrompt ΒΆ

func DefinePrompt(r api.Registry, name string, opts ...PromptOption) Prompt

DefinePrompt creates a new Prompt and registers it.

func LoadPrompt ΒΆ added in v0.3.0

func LoadPrompt(r api.Registry, dir, filename, namespace string) Prompt

LoadPrompt loads a single prompt into the registry.

func LookupPrompt ΒΆ

func LookupPrompt(r api.Registry, name string) Prompt

LookupPrompt looks up a Prompt registered by DefinePrompt. It returns nil if the prompt was not defined.

type PromptExecuteOption ΒΆ added in v0.5.0

type PromptExecuteOption interface {
	// contains filtered or unexported methods
}

PromptExecuteOption is an option for executing a prompt. It applies only to [prompt.Execute].

func WithInput ΒΆ added in v0.3.0

func WithInput(input any) PromptExecuteOption

WithInput sets the input for the prompt request. Input must conform to the prompt's input schema and can either be a map[string]any or a struct of the same api.

type PromptFn ΒΆ added in v0.5.0

type PromptFn = func(context.Context, any) (string, error)

PromptFn is a function that generates a prompt.

type PromptOption ΒΆ added in v0.3.0

type PromptOption interface {
	// contains filtered or unexported methods
}

PromptOption is an option for defining a prompt. It applies only to DefinePrompt().

func WithDescription ΒΆ added in v0.3.0

func WithDescription(description string) PromptOption

WithDescription sets the description of the prompt.

func WithInputType ΒΆ added in v0.3.0

func WithInputType(input any) PromptOption

WithInputType uses the type provided to derive the input schema. The inputted value will serve as the default input if no input is given at generation time. Only supports structs and map[string]any api.

func WithMetadata ΒΆ added in v0.3.0

func WithMetadata(metadata map[string]any) PromptOption

WithMetadata sets arbitrary metadata for the prompt.

type PromptingOption ΒΆ added in v0.3.0

type PromptingOption interface {
	// contains filtered or unexported methods
}

PromptingOption is an option for the system and user prompts of a prompt or generate request. It applies only to DefinePrompt() and Generate().

func WithPrompt ΒΆ added in v0.5.0

func WithPrompt(text string, args ...any) PromptingOption

WithPrompt sets the user prompt message. The user prompt is always the last message in the list.

func WithPromptFn ΒΆ added in v0.3.0

func WithPromptFn(fn PromptFn) PromptingOption

WithPromptFn sets the function that generates the user prompt message. The user prompt is always the last message in the list.

func WithSystem ΒΆ added in v0.5.0

func WithSystem(text string, args ...any) PromptingOption

WithSystem sets the system prompt message. The system prompt is always the first message in the list.

func WithSystemFn ΒΆ added in v0.3.0

func WithSystemFn(fn PromptFn) PromptingOption

WithSystemFn sets the function that generates the system prompt message. The system prompt is always the first message in the list.

type RankedDocumentData ΒΆ added in v0.3.0

type RankedDocumentData struct {
	Content  []*Part                 `json:"content,omitempty"`
	Metadata *RankedDocumentMetadata `json:"metadata,omitempty"`
}

type RankedDocumentMetadata ΒΆ added in v0.3.0

type RankedDocumentMetadata struct {
	Score float64 `json:"score,omitempty"`
}

type RerankerRequest ΒΆ added in v0.3.0

type RerankerRequest struct {
	Documents []*Document `json:"documents,omitempty"`
	Options   any         `json:"options,omitempty"`
	Query     *Document   `json:"query,omitempty"`
}

type RerankerResponse ΒΆ added in v0.3.0

type RerankerResponse struct {
	Documents []*RankedDocumentData `json:"documents,omitempty"`
}

type Resource ΒΆ added in v0.7.0

type Resource interface {
	// Name returns the name of the resource.
	Name() string
	// Matches reports whether this resource matches the given URI.
	Matches(uri string) bool
	// ExtractVariables extracts variables from a URI using this resource's template.
	ExtractVariables(uri string) (map[string]string, error)
	// Execute runs the resource with the given input.
	Execute(ctx context.Context, input *ResourceInput) (*ResourceOutput, error)
	// Register registers the resource with the given registry.
	Register(r api.Registry)
}

Resource represents an instance of a resource.

func DefineResource ΒΆ added in v0.7.0

func DefineResource(r api.Registry, name string, opts *ResourceOptions, fn ResourceFunc) Resource

DefineResource creates a resource and registers it with the given Registry.

func LookupResource ΒΆ added in v0.7.0

func LookupResource(r api.Registry, name string) Resource

LookupResource looks up the resource in the registry by provided name and returns it.

func NewResource ΒΆ added in v0.7.0

func NewResource(name string, opts *ResourceOptions, fn ResourceFunc) Resource

NewResource creates a resource but does not register it in the registry. It can be registered later via the Register method.

type ResourceFunc ΒΆ added in v0.7.0

type ResourceFunc = func(context.Context, *ResourceInput) (*ResourceOutput, error)

ResourceFunc is a function that loads content for a resource.

type ResourceInput ΒΆ added in v0.7.0

type ResourceInput struct {
	URI       string            `json:"uri"`       // The resource URI
	Variables map[string]string `json:"variables"` // Extracted variables from URI template matching
}

ResourceInput represents the input to a resource function.

type ResourceOptions ΒΆ added in v0.7.0

type ResourceOptions struct {
	URI         string         // Static URI (mutually exclusive with Template)
	Template    string         // URI template (mutually exclusive with URI)
	Description string         // Optional description
	Metadata    map[string]any // Optional metadata
}

ResourceOptions configures a resource definition.

type ResourceOutput ΒΆ added in v0.7.0

type ResourceOutput struct {
	Content []*Part `json:"content"` // The content parts returned by the resource
}

ResourceOutput represents the output from a resource function.

type ResourcePart ΒΆ added in v0.7.0

type ResourcePart struct {
	Uri string `json:"uri,omitempty"`
}

type RespondOptions ΒΆ added in v0.6.0

type RespondOptions struct {
	// Metadata is additional metadata to include in the response.
	Metadata map[string]any
}

RespondOptions provides configuration options for responding to a tool request.

type RestartOptions ΒΆ added in v0.6.0

type RestartOptions struct {
	// ReplaceInput allows replacing the existing input arguments to the tool with different ones,
	// for example if the user revised an action before confirming. When input is replaced,
	// the existing tool request will be amended in the message history.
	ReplaceInput any
	// ResumedMetadata is the metadata you want to provide to the tool to aide in reprocessing.
	// Defaults to true if none is supplied.
	ResumedMetadata any
}

RestartOptions provides configuration options for restarting a tool.

type Retriever ΒΆ

type Retriever interface {
	// Name returns the name of the retriever.
	Name() string
	// Retrieve retrieves the documents.
	Retrieve(ctx context.Context, req *RetrieverRequest) (*RetrieverResponse, error)
	// Register registers the retriever with the given registry.
	Register(r api.Registry)
}

Retriever represents a document retriever.

func DefineRetriever ΒΆ

func DefineRetriever(r api.Registry, name string, opts *RetrieverOptions, fn RetrieverFunc) Retriever

DefineRetriever creates a new Retriever and registers it.

func LookupRetriever ΒΆ

func LookupRetriever(r api.Registry, name string) Retriever

LookupRetriever looks up a Retriever registered by DefineRetriever. It will try to resolve the retriever dynamically if the retriever is not found. It returns nil if the retriever was not resolved.

func NewRetriever ΒΆ added in v0.7.0

func NewRetriever(name string, opts *RetrieverOptions, fn RetrieverFunc) Retriever

NewRetriever creates a new Retriever.

type RetrieverArg ΒΆ added in v0.7.0

type RetrieverArg interface {
	Name() string
}

RetrieverArg is the interface for retriever arguments. It can either be the retriever action itself or a reference to be looked up.

type RetrieverFunc ΒΆ added in v0.5.0

type RetrieverFunc = func(context.Context, *RetrieverRequest) (*RetrieverResponse, error)

RetrieverFunc is the function type for retriever implementations.

type RetrieverOption ΒΆ added in v0.5.0

type RetrieverOption interface {
	// contains filtered or unexported methods
}

RetrieverOption is an option for configuring a retriever request. It applies only to [Retriever.Retrieve].

func WithRetriever ΒΆ added in v0.7.0

func WithRetriever(retriever RetrieverArg) RetrieverOption

WithRetriever sets either a Retriever or a RetrieverRef that may contain a config. Passing WithConfig will take precedence over the config in WithRetriever.

func WithRetrieverName ΒΆ added in v0.7.0

func WithRetrieverName(name string) RetrieverOption

WithRetrieverName sets the retriever name to call for document retrieval. The retriever name will be resolved to a Retriever and may error if the reference is invalid.

type RetrieverOptions ΒΆ added in v0.7.0

type RetrieverOptions struct {
	// ConfigSchema is the JSON schema for the retriever's config.
	ConfigSchema map[string]any `json:"configSchema,omitempty"`
	// Label is a user-friendly name for the retriever.
	Label string `json:"label,omitempty"`
	// Supports defines the capabilities of the retriever, such as media support.
	Supports *RetrieverSupports `json:"supports,omitempty"`
}

RetrieverOptions represents the configuration options for a retriever.

type RetrieverRef ΒΆ added in v0.7.0

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

RetrieverRef is a struct to hold retriever name and configuration.

func NewRetrieverRef ΒΆ added in v0.7.0

func NewRetrieverRef(name string, config any) RetrieverRef

NewRetrieverRef creates a new RetrieverRef with the given name and configuration.

func (RetrieverRef) Config ΒΆ added in v0.7.0

func (r RetrieverRef) Config() any

Config returns the configuration to use by default for this retriever.

func (RetrieverRef) Name ΒΆ added in v0.7.0

func (r RetrieverRef) Name() string

Name returns the name of the retriever.

type RetrieverRequest ΒΆ

type RetrieverRequest struct {
	Options any       `json:"options,omitempty"`
	Query   *Document `json:"query,omitempty"`
}

type RetrieverResponse ΒΆ

type RetrieverResponse struct {
	Documents []*Document `json:"documents,omitempty"`
}

func Retrieve ΒΆ added in v0.1.0

func Retrieve(ctx context.Context, r api.Registry, opts ...RetrieverOption) (*RetrieverResponse, error)

Retrieve calls the retriever with the provided options.

type RetrieverSupports ΒΆ added in v0.7.0

type RetrieverSupports struct {
	// Media indicates whether the retriever supports media content.
	Media bool `json:"media,omitempty"`
}

RetrieverSupports defines the supported capabilities of the retriever.

type Role ΒΆ

type Role string

Role indicates which entity is responsible for the content of a message.

const (
	// RoleSystem indicates this message is user-independent context.
	RoleSystem Role = "system"
	// RoleUser indicates this message was generated by the client.
	RoleUser Role = "user"
	// RoleModel indicates this message was generated by the model during a previous interaction.
	RoleModel Role = "model"
	// RoleTool indicates this message was generated by a local tool, likely triggered by a request
	// from the model in one of its previous responses.
	RoleTool Role = "tool"
)

type Score ΒΆ added in v0.3.0

type Score struct {
	Id      string         `json:"id,omitempty"`
	Score   any            `json:"score,omitempty"`
	Status  string         `json:"status,omitempty" jsonschema:"enum=UNKNOWN,enum=FAIL,enum=PASS"`
	Error   string         `json:"error,omitempty"`
	Details map[string]any `json:"details,omitempty"`
}

Score is the evaluation score that represents the result of an evaluator. This struct includes information such as the score (numeric, string or other types), the reasoning provided for this score (if any), the score status (if any) and other details.

type ScoreDetails ΒΆ added in v0.3.0

type ScoreDetails struct {
	Reasoning string `json:"reasoning,omitempty"`
}

type ScoreStatus ΒΆ added in v0.3.0

type ScoreStatus int

ScoreStatus is an enum used to indicate if a Score has passed or failed. This drives additional features in tooling / the Dev UI.

const (
	ScoreStatusUnknown ScoreStatus = iota
	ScoreStatusFail
	ScoreStatusPass
)

func (ScoreStatus) String ΒΆ added in v0.3.0

func (ss ScoreStatus) String() string

type Tool ΒΆ

type Tool interface {
	// Name returns the name of the tool.
	Name() string
	// Definition returns the definition for this tool to be passed to models.
	Definition() *ToolDefinition
	// RunRaw runs this tool using the provided raw input.
	RunRaw(ctx context.Context, input any) (any, error)
	// Respond constructs a [Part] with a [ToolResponse] for a given interrupted tool request.
	Respond(toolReq *Part, outputData any, opts *RespondOptions) *Part
	// Restart constructs a [Part] with a new [ToolRequest] to re-trigger a tool,
	// potentially with new input and metadata.
	Restart(toolReq *Part, opts *RestartOptions) *Part
	// Register registers the tool with the given registry.
	Register(r api.Registry)
}

Tool represents a tool that can be called by a model.

func DefineTool ΒΆ

func DefineTool[In, Out any](
	r api.Registry,
	name, description string,
	fn ToolFunc[In, Out],
) Tool

DefineTool creates a new Tool and registers it.

func DefineToolWithInputSchema ΒΆ added in v0.6.0

func DefineToolWithInputSchema[Out any](
	r api.Registry,
	name, description string,
	inputSchema map[string]any,
	fn ToolFunc[any, Out],
) Tool

DefineToolWithInputSchema creates a new Tool with a custom input schema and registers it.

func LookupTool ΒΆ added in v0.1.0

func LookupTool(r api.Registry, name string) Tool

LookupTool looks up the tool in the registry by provided name and returns it.

func NewTool ΒΆ added in v0.6.0

func NewTool[In, Out any](name, description string, fn ToolFunc[In, Out]) Tool

NewTool creates a new Tool. It can be passed directly to Generate.

func NewToolWithInputSchema ΒΆ added in v1.0.0

func NewToolWithInputSchema[Out any](name, description string, inputSchema map[string]any, fn ToolFunc[any, Out]) Tool

NewToolWithInputSchema creates a new Tool with a custom input schema. It can be passed directly to Generate.

type ToolChoice ΒΆ added in v0.3.0

type ToolChoice string
const (
	ToolChoiceAuto     ToolChoice = "auto"
	ToolChoiceRequired ToolChoice = "required"
	ToolChoiceNone     ToolChoice = "none"
)

type ToolConfig ΒΆ added in v0.3.0

type ToolConfig struct {
	MaxTurns           int  // Maximum number of tool call iterations before erroring.
	ReturnToolRequests bool // Whether to return tool requests instead of making the tool calls and continuing the generation.
}

ToolConfig handles configuration around tool calls during generation.

type ToolContext ΒΆ added in v0.3.0

type ToolContext struct {
	context.Context
	// Interrupt is a function that can be used to interrupt the tool execution.
	// Interrupting tool execution returns the control to the caller with the
	// total model response so far.
	Interrupt func(opts *InterruptOptions) error
	// Resumed is optional metadata that can be used to resume the tool execution.
	// Map is not nil only if the tool was interrupted.
	Resumed map[string]any
	// OriginalInput is the original input to the tool if the tool was interrupted, otherwise nil.
	OriginalInput any
}

ToolContext provides context and utility functions for tool execution.

type ToolDefinition ΒΆ

type ToolDefinition struct {
	Description string `json:"description,omitempty"`
	// Valid JSON Schema representing the input of the tool.
	InputSchema map[string]any `json:"inputSchema,omitempty"`
	// additional metadata for this tool definition
	Metadata map[string]any `json:"metadata,omitempty"`
	Name     string         `json:"name,omitempty"`
	// Valid JSON Schema describing the output of the tool.
	OutputSchema map[string]any `json:"outputSchema,omitempty"`
}

A ToolDefinition describes a tool.

type ToolFunc ΒΆ added in v0.7.0

type ToolFunc[In, Out any] = func(ctx *ToolContext, input In) (Out, error)

ToolFunc is the function type for tool implementations.

type ToolName ΒΆ added in v0.3.0

type ToolName string

ToolName is a distinct type for a tool name. It is meant to be passed where a ToolRef is expected but no Tool is had.

func (ToolName) Name ΒΆ added in v0.3.0

func (t ToolName) Name() string

Name returns the name of the tool.

type ToolRef ΒΆ added in v0.3.0

type ToolRef interface {
	Name() string
}

ToolRef is a reference to a tool.

type ToolRequest ΒΆ

type ToolRequest struct {
	// Input is a JSON object describing the input values to the tool.
	// An example might be map[string]any{"country":"USA", "president":3}.
	Input any    `json:"input,omitempty"`
	Name  string `json:"name,omitempty"`
	Ref   string `json:"ref,omitempty"`
}

A ToolRequest is a message from the model to the client that it should run a specific tool and pass a ToolResponse to the model on the next chat request it makes. Any ToolRequest will correspond to some ToolDefinition previously sent by the client.

type ToolResponse ΒΆ

type ToolResponse struct {
	Name string `json:"name,omitempty"`
	// Output is a JSON object describing the results of running the tool.
	// An example might be map[string]any{"name":"Thomas Jefferson", "born":1743}.
	Output any    `json:"output,omitempty"`
	Ref    string `json:"ref,omitempty"`
}

A ToolResponse is a message from the client to the model containing the results of running a specific tool on the arguments passed to the client by the model in a ToolRequest.

type TraceMetadata ΒΆ added in v0.3.0

type TraceMetadata struct {
	FeatureName string          `json:"featureName,omitempty"`
	Paths       []*PathMetadata `json:"paths,omitempty"`
	Timestamp   float64         `json:"timestamp,omitempty"`
}

Jump to

Keyboard shortcuts

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