Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateInputOutputUsage(req *ModelRequest, resp *ModelResponse)
- func ConfigureFormats(reg api.Registry)
- func DefineFormat(r api.Registry, name string, formatter Formatter)
- func DefineGenerateAction(ctx context.Context, r api.Registry) *generateAction
- func FindMatchingResource(r api.Registry, uri string) (Resource, *ResourceInput, error)
- func GenerateDataStream[Out any](ctx context.Context, r api.Registry, opts ...GenerateOption) iter.Seq2[*StreamValue[Out, Out], error]
- func GenerateStream(ctx context.Context, r api.Registry, opts ...GenerateOption) iter.Seq2[*ModelStreamValue, error]
- func GenerateText(ctx context.Context, r api.Registry, opts ...GenerateOption) (string, error)
- func InterruptAs[T any](p *Part) (T, bool)
- func InterruptWith[T any](tc *ToolContext, meta T) error
- func IsAudioContentType(contentType string) bool
- func IsImageContentType(contentType string) bool
- func IsToolInterruptError(err error) (bool, map[string]any)
- func IsVideoContentType(contentType string) bool
- func LoadPromptDir(r api.Registry, dir string, namespace string)
- func LoadPromptDirFromFS(r api.Registry, fsys fs.FS, dir, namespace string)
- func OriginalInputAs[T any](tc *ToolContext) (T, bool)
- func OutputFrom[Out any](src outputer) Out
- func ResumedValue[T any](tc *ToolContext, key string) (T, bool)
- type AugmentWithContextOptions
- type BackgroundModel
- type BackgroundModelOptions
- type BatchEvaluatorFunc
- type CancelModelOpFunc
- type CheckModelOpFunc
- type CommonGenOption
- func WithMaxTurns(maxTurns int) CommonGenOption
- func WithMessages(messages ...*Message) CommonGenOption
- func WithMessagesFn(fn MessagesFn) CommonGenOption
- func WithMiddleware(middleware ...ModelMiddleware) CommonGenOption
- func WithModel(model ModelArg) CommonGenOption
- func WithModelName(name string) CommonGenOption
- func WithResources(resources ...Resource) CommonGenOption
- func WithReturnToolRequests(returnReqs bool) CommonGenOption
- func WithToolChoice(toolChoice ToolChoice) CommonGenOption
- func WithTools(tools ...ToolRef) CommonGenOption
- type ConfigOption
- type ConstrainedSupport
- type DataPrompt
- func (p *DataPrompt) Desc() api.ActionDesc
- func (dp *DataPrompt[In, Out]) Execute(ctx context.Context, input In, opts ...PromptExecuteOption) (Out, *ModelResponse, error)
- func (dp *DataPrompt[In, Out]) ExecuteStream(ctx context.Context, input In, opts ...PromptExecuteOption) iter.Seq2[*StreamValue[Out, Out], error]
- func (dp *DataPrompt[In, Out]) Render(ctx context.Context, input In) (*GenerateActionOptions, error)
- type Document
- type DocumentOption
- type DownloadMediaOptions
- type EmbedRequest
- type EmbedResponse
- type Embedder
- type EmbedderArg
- type EmbedderFunc
- type EmbedderOption
- type EmbedderOptions
- type EmbedderRef
- type EmbedderSupports
- type Embedding
- type EvaluationResult
- type Evaluator
- func DefineBatchEvaluator(r api.Registry, name string, opts *EvaluatorOptions, fn BatchEvaluatorFunc) Evaluator
- func DefineEvaluator(r api.Registry, name string, opts *EvaluatorOptions, fn EvaluatorFunc) Evaluator
- func LookupEvaluator(r api.Registry, name string) Evaluator
- func NewBatchEvaluator(name string, opts *EvaluatorOptions, fn BatchEvaluatorFunc) Evaluator
- func NewEvaluator(name string, opts *EvaluatorOptions, fn EvaluatorFunc) Evaluator
- type EvaluatorArg
- type EvaluatorCallbackRequest
- type EvaluatorCallbackResponse
- type EvaluatorFunc
- type EvaluatorOption
- type EvaluatorOptions
- type EvaluatorRef
- type EvaluatorRequest
- type EvaluatorResponse
- type Example
- type ExecutionOption
- type FinishReason
- type FormatHandler
- type Formatter
- type GenerateActionOptions
- type GenerateActionOutputConfig
- type GenerateActionResume
- type GenerateOption
- type GenerationCommonConfig
- type GenerationUsage
- type InputOption
- type InterruptOptions
- type Media
- type Message
- func NewMessage(role Role, metadata map[string]any, parts ...*Part) *Message
- func NewModelMessage(parts ...*Part) *Message
- func NewModelTextMessage(text string) *Message
- func NewSystemMessage(parts ...*Part) *Message
- func NewSystemTextMessage(text string) *Message
- func NewTextMessage(role Role, text string) *Message
- func NewUserMessage(parts ...*Part) *Message
- func NewUserMessageWithMetadata(metadata map[string]any, parts ...*Part) *Message
- func NewUserTextMessage(text string) *Message
- type MessagesFn
- type Model
- type ModelArg
- type ModelFunc
- type ModelInfo
- type ModelMiddleware
- type ModelOperation
- type ModelOptions
- type ModelOutputConfig
- type ModelRef
- type ModelRequest
- type ModelResponse
- func Generate(ctx context.Context, r api.Registry, opts ...GenerateOption) (*ModelResponse, error)
- func GenerateData[Out any](ctx context.Context, r api.Registry, opts ...GenerateOption) (*Out, *ModelResponse, error)
- func GenerateWithRequest(ctx context.Context, r api.Registry, opts *GenerateActionOptions, ...) (*ModelResponse, error)
- func (mr *ModelResponse) History() []*Message
- func (mr *ModelResponse) Interrupts() []*Part
- func (mr *ModelResponse) Media() string
- func (mr *ModelResponse) Output(v any) error
- func (mr *ModelResponse) Reasoning() string
- func (mr *ModelResponse) Text() string
- func (mr *ModelResponse) ToolRequests() []*ToolRequest
- type ModelResponseChunk
- type ModelStage
- type ModelStreamCallback
- type ModelStreamValue
- type ModelSupports
- type MultipartToolFunc
- type MultipartToolResponse
- type Operation
- type OperationError
- type OutputOption
- func WithCustomConstrainedOutput() OutputOption
- func WithOutputEnums[T ~string](values ...T) OutputOption
- func WithOutputFormat(format string) OutputOption
- func WithOutputInstructions(instructions string) OutputOption
- func WithOutputSchema(schema map[string]any) OutputOption
- func WithOutputSchemaName(name string) OutputOption
- func WithOutputType(output any) OutputOption
- type Part
- func NewCustomPart(customData map[string]any) *Part
- func NewDataPart(contents string) *Part
- func NewJSONPart(text string) *Part
- func NewMediaPart(mimeType, contents string) *Part
- func NewReasoningPart(text string, signature []byte) *Part
- func NewResourcePart(uri string) *Part
- func NewResponseForToolRequest(p *Part, output any) *Part
- func NewTextPart(text string) *Part
- func NewToolRequestPart(r *ToolRequest) *Part
- func NewToolResponsePart(r *ToolResponse) *Part
- func (p *Part) IsAudio() bool
- func (p *Part) IsCustom() bool
- func (p *Part) IsData() bool
- func (p *Part) IsImage() bool
- func (p *Part) IsInterrupt() bool
- func (p *Part) IsMedia() bool
- func (p *Part) IsReasoning() bool
- func (p *Part) IsResource() bool
- func (p *Part) IsText() bool
- func (p *Part) IsToolRequest() bool
- func (p *Part) IsToolResponse() bool
- func (p *Part) IsVideo() bool
- func (Part) JSONSchemaAlias() any
- func (p *Part) MarshalJSON() ([]byte, error)
- func (p *Part) UnmarshalJSON(b []byte) error
- func (p *Part) UnmarshalYAML(unmarshal func(any) error) error
- type PartKind
- type PathMetadata
- type Prompt
- func DefinePrompt(r api.Registry, name string, opts ...PromptOption) Prompt
- func LoadPrompt(r api.Registry, dir, filename, namespace string) Prompt
- func LoadPromptFromFS(r api.Registry, fsys fs.FS, dir, filename, namespace string) Prompt
- func LoadPromptFromSource(r api.Registry, source, name, namespace string) (Prompt, error)
- func LookupPrompt(r api.Registry, name string) Prompt
- type PromptExecuteOption
- type PromptFn
- type PromptOption
- type PromptingOption
- type RankedDocumentData
- type RankedDocumentMetadata
- type RerankerRequest
- type RerankerResponse
- type Resource
- type ResourceFunc
- type ResourceInput
- type ResourceOptions
- type ResourceOutput
- type ResourcePart
- type RespondOptions
- type RespondWithOption
- type RestartOptions
- type RestartWithOption
- type Retriever
- type RetrieverArg
- type RetrieverFunc
- type RetrieverOption
- type RetrieverOptions
- type RetrieverRef
- type RetrieverRequest
- type RetrieverResponse
- type RetrieverSupports
- type Role
- type Score
- type ScoreDetails
- type ScoreStatus
- type StartModelOpFunc
- type StreamValue
- type StreamingFormatHandler
- type Tool
- type ToolChoice
- type ToolConfig
- type ToolContext
- type ToolDef
- func DefineMultipartTool[In any](r api.Registry, name, description string, fn MultipartToolFunc[In], ...) *ToolDef[In, *MultipartToolResponse]
- func DefineTool[In, Out any](r api.Registry, name, description string, fn ToolFunc[In, Out], ...) *ToolDef[In, Out]
- func DefineToolWithInputSchema[Out any](r api.Registry, name, description string, inputSchema map[string]any, ...) *ToolDef[any, Out]deprecated
- func NewMultipartTool[In any](name, description string, fn MultipartToolFunc[In], opts ...ToolOption) *ToolDef[In, *MultipartToolResponse]
- func NewTool[In, Out any](name, description string, fn ToolFunc[In, Out], opts ...ToolOption) *ToolDef[In, Out]
- func NewToolWithInputSchema[Out any](name, description string, inputSchema map[string]any, fn ToolFunc[any, Out]) *ToolDef[any, Out]deprecated
- func (t *ToolDef[In, Out]) Definition() *ToolDefinition
- func (t *ToolDef[In, Out]) IsMultipart() bool
- func (t *ToolDef[In, Out]) Name() string
- func (t *ToolDef[In, Out]) Register(r api.Registry)
- func (t *ToolDef[In, Out]) Respond(toolReq *Part, output any, opts *RespondOptions) *Partdeprecated
- func (t *ToolDef[In, Out]) RespondWith(toolReq *Part, output Out, opts ...RespondWithOption[Out]) (*Part, error)
- func (t *ToolDef[In, Out]) Restart(p *Part, opts *RestartOptions) *Partdeprecated
- func (t *ToolDef[In, Out]) RestartWith(toolReq *Part, opts ...RestartWithOption[In]) (*Part, error)
- func (t *ToolDef[In, Out]) RunRaw(ctx context.Context, input any) (any, error)
- func (t *ToolDef[In, Out]) RunRawMultipart(ctx context.Context, input any) (*MultipartToolResponse, error)
- type ToolDefinition
- type ToolFunc
- type ToolName
- type ToolOption
- type ToolRef
- type ToolRequest
- type ToolResponse
- type TraceMetadata
Examples ¶
Constants ¶
const ( // OutputFormatText is the default format. OutputFormatText string = "text" // OutputFormatJSON is the legacy format for JSON content. // For streaming, each chunk represents the full object received up to that point. OutputFormatJSON string = "json" // OutputFormatJSONL is the format for JSONL content. // For streaming, each chunk represents new items since the last chunk. OutputFormatJSONL string = "jsonl" // OutputFormatMedia is the format for media content. OutputFormatMedia string = "media" // OutputFormatArray is the format for array content. // For streaming, each chunk represents new items since the last chunk. OutputFormatArray string = "array" // OutputFormatEnum is the format for enum content. // The value must be a string. OutputFormatEnum string = "enum" )
Variables ¶
var DEFAULT_FORMATS = []Formatter{
textFormatter{},
jsonFormatter{},
jsonlFormatter{},
arrayFormatter{},
enumFormatter{},
}
Default formats get automatically registered on registry init
Functions ¶
func CalculateInputOutputUsage ¶ added in v1.3.0
func CalculateInputOutputUsage(req *ModelRequest, resp *ModelResponse)
func ConfigureFormats ¶ added in v0.5.0
ConfigureFormats registers default formats in the registry
func DefineFormat ¶ added in v0.5.0
DefineFormat defines and registers a new Formatter.
func DefineGenerateAction ¶ added in v0.3.0
DefineGenerateAction defines a utility generate action.
func FindMatchingResource ¶ added in v0.7.0
FindMatchingResource finds a resource that matches the given URI.
func GenerateDataStream ¶ added in v1.3.0
func GenerateDataStream[Out any](ctx context.Context, r api.Registry, opts ...GenerateOption) iter.Seq2[*StreamValue[Out, Out], error]
GenerateDataStream generates a model response with streaming and returns strongly-typed output. It returns an iterator that yields streaming results.
If the yield function is passed a non-nil error, generation has failed with that error; the yield function will not be called again.
If the yield function's StreamValue argument has Done == true, the value's Output and Response fields contain the final typed output and response; the yield function will not be called again.
Otherwise the Chunk field of the passed StreamValue holds a streamed chunk.
func GenerateStream ¶ added in v1.3.0
func GenerateStream(ctx context.Context, r api.Registry, opts ...GenerateOption) iter.Seq2[*ModelStreamValue, error]
GenerateStream generates a model response and streams the output. It returns an iterator that yields streaming results.
If the yield function is passed a non-nil error, generation has failed with that error; the yield function will not be called again.
If the yield function's ModelStreamValue argument has Done == true, the value's Response field contains the final response; the yield function will not be called again.
Otherwise the Chunk field of the passed ModelStreamValue holds a streamed chunk.
func GenerateText ¶ added in v0.1.0
GenerateText run generate request for this model. Returns generated text only.
func InterruptAs ¶ added in v1.4.0
InterruptAs extracts strongly-typed metadata from an interrupted tool request Part. Returns the zero value and false if the part is not an interrupt or the type doesn't match.
func InterruptWith ¶ added in v1.4.0
func InterruptWith[T any](tc *ToolContext, meta T) error
InterruptWith is a convenience function to interrupt a tool with a strongly-typed metadata value. The metadata is converted to map[string]any via JSON marshaling.
func IsAudioContentType ¶ added in v1.0.0
IsAudioContentType checks if the content type represents an audio file.
func IsImageContentType ¶ added in v1.0.0
IsImageContentType checks if the content type represents an image.
func IsToolInterruptError ¶ added in v1.1.0
IsToolInterruptError determines whether the error is an interrupt error returned by the tool.
func IsVideoContentType ¶ added in v1.0.0
IsVideoContentType checks if the content type represents a video.
func LoadPromptDir ¶ added in v0.3.0
LoadPromptDir loads prompts and partials from a directory on the local filesystem.
func LoadPromptDirFromFS ¶ added in v1.3.0
LoadPromptDirFromFS loads prompts and partials from a filesystem for the given namespace. The fsys parameter should be an fs.FS implementation (e.g., embed.FS or os.DirFS). The dir parameter specifies the directory within the filesystem where prompts are located.
func OriginalInputAs ¶ added in v1.4.0
func OriginalInputAs[T any](tc *ToolContext) (T, bool)
OriginalInputAs returns the original input typed appropriately. Returns the zero value and false if not resumed or type doesn't match.
func OutputFrom ¶ added in v1.3.0
func OutputFrom[Out any](src outputer) Out
OutputFrom is a convenience function that parses structured output from a ModelResponse or ModelResponseChunk and returns it as a typed value. This is equivalent to calling Output() but returns the value directly instead of requiring a pointer argument. If you need to handle the error, use Output() instead.
func ResumedValue ¶ added in v1.4.0
func ResumedValue[T any](tc *ToolContext, key string) (T, bool)
ResumedValue retrieves a typed value from the Resumed metadata. Returns the zero value and false if the key doesn't exist or the type doesn't match.
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 BackgroundModel ¶ added in v1.3.0
type BackgroundModel interface {
// Name returns the registry name of the background model.
Name() string
// Register registers the model with the given registry.
Register(r api.Registry)
// Start starts a background operation.
Start(ctx context.Context, req *ModelRequest) (*ModelOperation, error)
// Check checks the status of a background operation.
Check(ctx context.Context, op *ModelOperation) (*ModelOperation, error)
// Cancel cancels a background operation.
Cancel(ctx context.Context, op *ModelOperation) (*ModelOperation, error)
// SupportsCancel returns whether the background action supports cancellation.
SupportsCancel() bool
}
BackgroundModel represents a model that can run operations in the background.
func DefineBackgroundModel ¶ added in v1.3.0
func DefineBackgroundModel(r *registry.Registry, name string, opts *BackgroundModelOptions, fn StartModelOpFunc, checkFn CheckModelOpFunc) BackgroundModel
DefineBackgroundModel defines and registers a new model that runs in the background.
func LookupBackgroundModel ¶ added in v1.3.0
func LookupBackgroundModel(r api.Registry, name string) BackgroundModel
LookupBackgroundModel looks up a BackgroundAction registered by DefineBackgroundModel. It returns nil if the background model was not found.
func NewBackgroundModel ¶ added in v1.3.0
func NewBackgroundModel(name string, opts *BackgroundModelOptions, startFn StartModelOpFunc, checkFn CheckModelOpFunc) BackgroundModel
NewBackgroundModel defines a new model that runs in the background.
type BackgroundModelOptions ¶ added in v1.3.0
type BackgroundModelOptions struct {
ModelOptions
Cancel CancelModelOpFunc // Function that cancels a background model operation.
Metadata map[string]any // Additional metadata.
}
BackgroundModelOptions holds configuration for defining a background model
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 CancelModelOpFunc ¶ added in v1.3.0
type CancelModelOpFunc = func(ctx context.Context, op *ModelOperation) (*ModelOperation, error)
CancelOperationFunc cancels a background model operation.
type CheckModelOpFunc ¶ added in v1.3.0
type CheckModelOpFunc = func(ctx context.Context, op *ModelOperation) (*ModelOperation, error)
CheckOperationFunc checks the status of a background model operation.
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 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
ConstrainedSupport indicates the level of constrained generation support.
const ( ConstrainedSupportNone ConstrainedSupport = "none" ConstrainedSupportAll ConstrainedSupport = "all" ConstrainedSupportNoTools ConstrainedSupport = "no-tools" )
type DataPrompt ¶ added in v1.3.0
type DataPrompt[In, Out any] struct { // contains filtered or unexported fields }
DataPrompt is a prompt with strongly-typed input and output. It wraps an underlying Prompt and provides type-safe Execute and Render methods. The Out type parameter can be string for text outputs or any struct type for JSON outputs.
func AsDataPrompt ¶ added in v1.3.0
func AsDataPrompt[In, Out any](p Prompt) *DataPrompt[In, Out]
AsDataPrompt wraps an existing Prompt with type information, returning a DataPrompt. This is useful for adding strong typing to a dynamically obtained prompt.
func DefineDataPrompt ¶ added in v1.3.0
func DefineDataPrompt[In, Out any](r api.Registry, name string, opts ...PromptOption) *DataPrompt[In, Out]
DefineDataPrompt creates a new data prompt and registers it. It automatically infers input schema from the In type parameter and configures output schema and JSON format from the Out type parameter (unless Out is string).
func LookupDataPrompt ¶ added in v1.3.0
func LookupDataPrompt[In, Out any](r api.Registry, name string) *DataPrompt[In, Out]
LookupDataPrompt looks up a prompt by name and wraps it with type information. This is useful for wrapping prompts loaded from .prompt files with strong types. It returns nil if the prompt was not found.
func (*DataPrompt) Desc ¶ added in v1.3.0
func (p *DataPrompt) Desc() api.ActionDesc
Desc returns a descriptor of the prompt with resolved schema references.
func (*DataPrompt[In, Out]) Execute ¶ added in v1.3.0
func (dp *DataPrompt[In, Out]) Execute(ctx context.Context, input In, opts ...PromptExecuteOption) (Out, *ModelResponse, error)
Execute executes the typed prompt and returns the strongly-typed output along with the full model response. For structured output types (non-string Out), the prompt must be configured with the appropriate output schema, either through DefineDataPrompt or by using WithOutputType when defining the prompt.
func (*DataPrompt[In, Out]) ExecuteStream ¶ added in v1.3.0
func (dp *DataPrompt[In, Out]) ExecuteStream(ctx context.Context, input In, opts ...PromptExecuteOption) iter.Seq2[*StreamValue[Out, Out], error]
ExecuteStream executes the typed prompt with streaming and returns an iterator.
If the yield function is passed a non-nil error, execution has failed with that error; the yield function will not be called again.
If the yield function's StreamValue argument has Done == true, the value's Output and Response fields contain the final typed output and response; the yield function will not be called again.
Otherwise the Chunk field of the passed StreamValue holds a streamed chunk.
For structured output types (non-string Out), the prompt must be configured with the appropriate output schema, either through DefineDataPrompt or by using WithOutputType when defining the prompt.
func (*DataPrompt[In, Out]) Render ¶ added in v1.3.0
func (dp *DataPrompt[In, Out]) Render(ctx context.Context, input In) (*GenerateActionOptions, error)
Render renders the typed prompt template with the given input.
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.
Example ¶
This example demonstrates the Document type used in RAG applications.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a document with text content
doc := &ai.Document{
Content: []*ai.Part{
ai.NewTextPart("This is the document content."),
},
Metadata: map[string]any{
"source": "knowledge-base",
"page": 42,
},
}
fmt.Println("Content:", doc.Content[0].Text)
fmt.Println("Source:", doc.Metadata["source"])
}
Output: Content: This is the document content. Source: knowledge-base
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 is the array of documents to generate embeddings for.
Input []*Document `json:"input,omitempty"`
// Options contains embedder-specific configuration parameters.
Options any `json:"options,omitempty"`
}
EmbedRequest represents a request to generate embeddings for documents.
type EmbedResponse ¶ added in v0.0.2
type EmbedResponse struct {
// Embeddings is the array of generated embedding vectors with metadata.
Embeddings []*Embedding `json:"embeddings,omitempty"`
}
EmbedResponse contains the generated embeddings from an embed request.
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 ¶
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 is the vector representation of the input.
Embedding []float32 `json:"embedding,omitempty"`
// Metadata identifies which part of a document this embedding corresponds to.
Metadata map[string]any `json:"metadata,omitempty"`
}
Embedding represents a vector embedding with associated metadata.
Example ¶
This example demonstrates creating an Embedding for vector search.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create an embedding (typically returned by an embedder)
embedding := &ai.Embedding{
Embedding: []float32{0.1, 0.2, 0.3, 0.4, 0.5},
Metadata: map[string]any{
"source": "document-1",
},
}
fmt.Printf("Embedding dimensions: %d\n", len(embedding.Embedding))
fmt.Printf("First value: %.1f\n", embedding.Embedding[0])
}
Output: Embedding dimensions: 5 First value: 0.1
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
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
type EvaluatorFunc = func(context.Context, *EvaluatorCallbackRequest) (*EvaluatorCallbackResponse, error)
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
FinishReason indicates why generation stopped.
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.
//
// Legacy: New format handlers should implement this as a no-op passthrough and implement [StreamingFormatHandler] instead.
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 is a handler for formatting messages. A new instance is created via [Formatter.Handler] for each request.
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 contains configuration parameters for the generation request.
Config any `json:"config,omitempty"`
// Docs provides retrieved documents to be used as context for this generation.
Docs []*Document `json:"docs,omitempty"`
// MaxTurns is the maximum number of tool call iterations that can be performed
// in a single generate call. Defaults to 5.
MaxTurns int `json:"maxTurns,omitempty"`
// Messages contains the conversation history for multi-turn prompting when supported.
Messages []*Message `json:"messages,omitempty"`
// Model is a model name (e.g., "vertexai/gemini-1.0-pro").
Model string `json:"model,omitempty"`
// Output specifies the desired output format. Defaults to the model's default if unspecified.
Output *GenerateActionOutputConfig `json:"output,omitempty"`
// Resume provides options for resuming an interrupted generation.
Resume *GenerateActionResume `json:"resume,omitempty"`
// ReturnToolRequests, when true, returns tool calls for manual processing instead of
// automatically resolving them.
ReturnToolRequests bool `json:"returnToolRequests,omitempty"`
// StepName is a custom step name for this generate call to display in trace views.
// Defaults to "generate".
StepName string `json:"stepName,omitempty"`
// ToolChoice controls tool calling mode. Auto lets the model decide, required forces
// the model to choose a tool, and none forces the model not to use any tools. Defaults to auto.
ToolChoice ToolChoice `json:"toolChoice,omitempty"`
// Tools is a list of registered tool names for this generation if supported.
Tools []string `json:"tools,omitempty"`
}
GenerateActionOptions holds configuration for a generate action request.
type GenerateActionOutputConfig ¶ added in v0.3.0
type GenerateActionOutputConfig struct {
// Constrained indicates whether to enforce strict adherence to the schema.
Constrained bool `json:"constrained,omitempty"`
// ContentType specifies the MIME type of the output content.
ContentType string `json:"contentType,omitempty"`
// Format specifies the desired output format (e.g., "json", "text").
Format string `json:"format,omitempty"`
// Instructions provides additional guidance for the output format.
Instructions *string `json:"instructions,omitempty"`
// JsonSchema is a JSON Schema describing the desired structure of JSON output.
JsonSchema map[string]any `json:"jsonSchema,omitempty"`
}
GenerateActionOutputConfig specifies the desired output format for a generate action.
type GenerateActionResume ¶ added in v0.3.0
type GenerateActionResume struct {
// Metadata contains additional context for resuming the generation.
Metadata map[string]any `json:"metadata,omitempty"`
// Respond contains tool response parts to send to the model when resuming.
Respond []*toolResponsePart `json:"respond,omitempty"`
// Restart contains tool request parts to restart when resuming.
Restart []*toolRequestPart `json:"restart,omitempty"`
}
GenerateActionResume holds options for resuming an interrupted generation.
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 provides resolved responses for interrupted tool calls. Use this when you already have the result and want to skip re-executing the tool.
func WithToolRestarts ¶ added in v0.6.0
func WithToolRestarts(parts ...*Part) GenerateOption
WithToolRestarts re-executes interrupted tool calls with additional metadata. Use this when the original call lacked required context (e.g., auth, user confirmation) that should now allow the tool to complete successfully.
type GenerationCommonConfig ¶
type GenerationCommonConfig struct {
// MaxOutputTokens limits the maximum number of tokens generated in the response.
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
// StopSequences specifies sequences that will cause generation to stop when encountered.
StopSequences []string `json:"stopSequences,omitempty"`
// Temperature controls randomness in generation. Higher values (e.g., 0.9) make output more random,
// while lower values (e.g., 0.1) make it more deterministic. Typical range is 0.0 to 1.0.
Temperature float64 `json:"temperature,omitempty"`
// TopK limits sampling to the K most likely tokens at each step.
TopK int `json:"topK,omitempty"`
// TopP (nucleus sampling) limits sampling to tokens whose cumulative probability exceeds P.
TopP float64 `json:"topP,omitempty"`
// Version specifies a particular version of a model family,
// e.g., "gemini-1.0-pro-001" for the "gemini-1.0-pro" family.
Version string `json:"version,omitempty"`
}
GenerationCommonConfig holds configuration parameters for model generation requests.
type GenerationUsage ¶
type GenerationUsage struct {
// CachedContentTokens counts tokens that were served from cache.
CachedContentTokens int `json:"cachedContentTokens,omitempty"`
// Custom contains additional usage metrics specific to the model provider.
Custom map[string]float64 `json:"custom,omitempty"`
// InputAudioFiles is the number of audio files in the input.
InputAudioFiles int `json:"inputAudioFiles,omitempty"`
// InputCharacters is the number of characters in the input.
InputCharacters int `json:"inputCharacters,omitempty"`
// InputImages is the number of images in the input.
InputImages int `json:"inputImages,omitempty"`
// InputTokens is the number of tokens in the input prompt.
InputTokens int `json:"inputTokens,omitempty"`
// InputVideos is the number of videos in the input.
InputVideos int `json:"inputVideos,omitempty"`
// OutputAudioFiles is the number of audio files generated in the output.
OutputAudioFiles int `json:"outputAudioFiles,omitempty"`
// OutputCharacters is the number of characters generated in the output.
OutputCharacters int `json:"outputCharacters,omitempty"`
// OutputImages is the number of images generated in the output.
OutputImages int `json:"outputImages,omitempty"`
// OutputTokens is the number of tokens generated in the response.
OutputTokens int `json:"outputTokens,omitempty"`
// OutputVideos is the number of videos generated in the output.
OutputVideos int `json:"outputVideos,omitempty"`
// ThoughtsTokens counts tokens used in reasoning or thinking processes.
ThoughtsTokens int `json:"thoughtsTokens,omitempty"`
// TotalTokens is the sum of input and output tokens.
TotalTokens int `json:"totalTokens,omitempty"`
}
GenerationUsage provides information about resource consumption during generation.
type InputOption ¶ added in v1.3.0
type InputOption interface {
// contains filtered or unexported methods
}
InputOption is an option for the input of a prompt. It applies only to DefinePrompt().
func WithInputSchema ¶ added in v1.3.0
func WithInputSchema(schema map[string]any) InputOption
WithInputSchema manually provides a schema map for the prompt's input.
func WithInputSchemaName ¶ added in v1.3.0
func WithInputSchemaName(name string) InputOption
WithInputSchemaName sets a pre-registered schema by name for the input. The schema will be resolved lazily at execution time using [DefineSchema].
func WithInputType ¶ added in v0.3.0
func WithInputType(input any) InputOption
WithInputType uses the type provided to derive the input schema. The inputted value may serve as the default input if no input is given at generation time depending on the action. Only supports structs and map[string]any.
type InterruptOptions ¶ added in v0.3.0
InterruptOptions provides configuration for tool interruption.
type Media ¶ added in v0.3.0
type Media struct {
// ContentType specifies the MIME type of the media. Inferred from the data URI if not provided.
ContentType string `json:"contentType,omitempty"`
// Url is a "data:" or "https:" URI containing the media content.
Url string `json:"url,omitempty"`
}
Media represents media content with a URL and content type.
type Message ¶
type Message struct {
// Content holds the message parts (text, media, tool calls, etc.).
Content []*Part `json:"content,omitempty"`
// Metadata contains arbitrary key-value data associated with this message.
Metadata map[string]any `json:"metadata,omitempty"`
// Role indicates which entity (system, user, model, or tool) generated this message.
Role Role `json:"role,omitempty"`
}
Message represents the contents of a model message in a conversation.
func NewMessage ¶
NewMessage creates a new Message with the provided role, metadata and parts. Use NewTextMessage if you have a text-only message.
func NewModelMessage ¶
NewModelMessage creates a new Message with role "model" and provided parts. Use NewModelTextMessage if you have a text-only message.
func NewModelTextMessage ¶
NewUserTextMessage creates a new Message with role "model" and content with a single text part with the content of provided text.
func NewSystemMessage ¶
NewSystemMessage creates a new Message with role "system" and provided parts. Use NewSystemTextMessage if you have a text-only message.
func NewSystemTextMessage ¶
NewUserTextMessage creates a new Message with role "system" and content with a single text part with the content of provided text.
Example ¶
This example demonstrates creating system and model messages.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a system message
sysMsg := ai.NewSystemTextMessage("You are a helpful assistant.")
fmt.Println("System role:", sysMsg.Role)
// Create a model response message
modelMsg := ai.NewModelTextMessage("I'm here to help!")
fmt.Println("Model role:", modelMsg.Role)
}
Output: System role: system Model role: model
func NewTextMessage ¶
NewTextMessage creates a new Message with the provided role and content with a single part containint provided text.
func NewUserMessage ¶
NewUserMessage creates a new Message with role "user" and provided parts. Use NewUserTextMessage if you have a text-only message.
Example ¶
This example demonstrates building a multi-turn conversation.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Build a conversation with multiple parts
userMsg := ai.NewUserMessage(
ai.NewTextPart("What's in this image?"),
ai.NewMediaPart("image/jpeg", "base64data..."),
)
fmt.Println("Role:", userMsg.Role)
fmt.Println("Parts:", len(userMsg.Content))
}
Output: Role: user Parts: 2
func NewUserMessageWithMetadata ¶ added in v0.3.0
NewUserMessageWithMetadata creates a new Message with role "user" with provided metadata and parts. Use NewUserTextMessage if you have a text-only message.
func NewUserTextMessage ¶
NewUserTextMessage creates a new Message with role "user" and content with a single text part with the content of provided text.
Example ¶
This example demonstrates creating a message with text content.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a user message with text
msg := ai.NewUserTextMessage("What is the capital of France?")
fmt.Println("Role:", msg.Role)
fmt.Println("Text:", msg.Content[0].Text)
}
Output: Role: user Text: What is the capital of France?
func (*Message) Text ¶ added in v0.3.0
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
WithCacheName adds cache name to use in the generate request
func (*Message) WithCacheTTL ¶ added in v0.3.0
WithCacheTTL adds cache TTL configuration for the desired message
type MessagesFn ¶ added in v0.5.0
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 ¶
DefineModel creates a new Model and registers it.
func LookupModel ¶
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.
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
type ModelFunc = core.StreamingFunc[*ModelRequest, *ModelResponse, *ModelResponseChunk]
ModelFunc is a streaming function that takes in a ModelRequest and generates a ModelResponse, optionally streaming ModelResponseChunks.
type ModelInfo ¶
type ModelInfo struct {
// ConfigSchema defines the model-specific configuration schema.
ConfigSchema map[string]any `json:"configSchema,omitempty"`
// Label is a friendly display name for this model (e.g., "Google AI - Gemini Pro").
Label string `json:"label,omitempty"`
// Stage indicates the development stage of this model.
// Featured models are recommended for general use, stable models are well-tested,
// unstable models are experimental, legacy models are not recommended for new projects,
// and deprecated models may be removed in future versions.
Stage ModelStage `json:"stage,omitempty"`
// Supports describes the capabilities that this model supports.
Supports *ModelSupports `json:"supports,omitempty"`
// Versions lists acceptable names for this model (e.g., different versions).
Versions []string `json:"versions,omitempty"`
}
ModelInfo contains metadata about a model's capabilities and characteristics.
type ModelMiddleware ¶ added in v0.3.0
type ModelMiddleware = core.Middleware[*ModelRequest, *ModelResponse, *ModelResponseChunk]
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 ModelOperation ¶ added in v1.3.0
type ModelOperation = core.Operation[*ModelResponse]
ModelOperation is a background operation for a model.
func CheckModelOperation ¶ added in v1.3.0
func CheckModelOperation(ctx context.Context, r api.Registry, op *ModelOperation) (*ModelOperation, error)
CheckModelOperation checks the status of a background model operation by looking up the model and calling its Check method.
func GenerateOperation ¶ added in v1.3.0
func GenerateOperation(ctx context.Context, r *registry.Registry, opts ...GenerateOption) (*ModelOperation, error)
GenerateOperation generates a model response as a long-running operation based on the provided options.
type ModelOptions ¶ added in v0.7.0
type ModelOptions struct {
ConfigSchema map[string]any // JSON schema for the model's config.
Label string // User-friendly name for the model.
Stage ModelStage // Indicates the maturity stage of the model.
Supports *ModelSupports // Capabilities of the model.
Versions []string // Available versions of the model.
}
ModelOptions represents the configuration options for a model.
type ModelOutputConfig ¶ added in v0.3.0
type ModelOutputConfig struct {
// Constrained indicates whether to enforce strict adherence to the schema.
Constrained bool `json:"constrained,omitempty"`
// ContentType specifies the MIME type of the output content.
ContentType string `json:"contentType,omitempty"`
// Format specifies the desired output format (e.g., "json", "text").
Format string `json:"format,omitempty"`
// Schema is a JSON Schema describing the desired structure of the output.
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
NewModelRef creates a new ModelRef with the given name and configuration.
Example ¶
This example demonstrates creating a model reference with configuration.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a reference to a model with custom configuration
// The config type depends on the model provider
modelRef := ai.NewModelRef("googleai/gemini-2.5-flash", map[string]any{
"temperature": 0.7,
})
fmt.Println("Model name:", modelRef.Name())
}
Output: Model name: googleai/gemini-2.5-flash
type ModelRequest ¶ added in v0.3.0
type ModelRequest struct {
// Config holds model-specific configuration parameters.
Config any `json:"config,omitempty"`
// Docs provides retrieved documents to be used as context for this generation.
Docs []*Document `json:"docs,omitempty"`
// Messages contains the conversation history for the model.
Messages []*Message `json:"messages,omitempty"`
// Output describes the desired response format.
Output *ModelOutputConfig `json:"output,omitempty"`
// ToolChoice controls how the model uses tools (auto, required, or none).
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 contains model-specific extra information. Deprecated: use Raw instead.
Custom any `json:"custom,omitempty"`
// FinishMessage provides additional details about why generation finished.
FinishMessage string `json:"finishMessage,omitempty"`
// FinishReason indicates why generation stopped (e.g., stop, length, blocked).
FinishReason FinishReason `json:"finishReason,omitempty"`
// LatencyMs is the time the request took in milliseconds.
LatencyMs float64 `json:"latencyMs,omitempty"`
// Message contains the generated response content.
Message *Message `json:"message,omitempty"`
// Operation provides information about a long-running background task if applicable.
Operation *Operation `json:"operation,omitempty"`
// Raw contains the unprocessed model-specific response data.
Raw any `json:"raw,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"`
// contains filtered or unexported fields
}
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)
GenerateData runs a generate request and returns strongly-typed output. If the response doesn't contain text output (e.g., contains tool requests or interrupts instead), the output will be nil and no error is returned. Check resp.Interrupts() or resp.ToolRequests() to handle these cases.
func GenerateWithRequest ¶ added in v0.3.0
func GenerateWithRequest(ctx context.Context, r api.Registry, opts *GenerateActionOptions, mw []ModelMiddleware, cb ModelStreamCallback) (*ModelResponse, error)
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) Media ¶ added in v1.1.0
func (mr *ModelResponse) Media() string
Media returns the media content of the ModelResponse as a string.
func (*ModelResponse) Output ¶ added in v0.3.0
func (mr *ModelResponse) Output(v any) error
Output parses the structured output from the response and unmarshals it into v. If a format handler is set, it uses the handler's ParseOutput method. Otherwise, it falls back to parsing the response text as JSON.
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 (mr *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 indicates whether the chunk includes all data from previous chunks.
// If false, the chunk is considered incremental.
Aggregated bool `json:"aggregated,omitempty"`
// Content is the chunk of message parts to stream right now.
Content []*Part `json:"content,omitempty"`
// Custom contains model-specific extra information attached to this chunk.
Custom any `json:"custom,omitempty"`
// Index of the message this chunk belongs to.
Index int `json:"index"`
// Role indicates the entity that generated this chunk.
Role Role `json:"role,omitempty"`
// contains filtered or unexported fields
}
A ModelResponseChunk is the portion of the ModelResponse that is passed to a streaming callback.
func (*ModelResponseChunk) Output ¶ added in v1.3.0
func (c *ModelResponseChunk) Output(v any) error
Output parses the chunk using the format handler and unmarshals the result into v. Returns an error if the format handler is not set or does not support parsing chunks.
func (*ModelResponseChunk) Reasoning ¶ added in v1.3.0
func (c *ModelResponseChunk) Reasoning() string
Reasoning returns the reasoning content of the ModelResponseChunk as a string. It returns an empty string if there is no Content in the response chunk.
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 empty string if there is no Content in the response chunk. For the parsed structured output, use ModelResponseChunk.Output instead.
type ModelStage ¶ added in v0.3.0
type ModelStage string
ModelStage indicates the development stage of a model.
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 ModelStreamValue ¶ added in v1.3.0
type ModelStreamValue = StreamValue[struct{}, *ModelResponseChunk]
ModelStreamValue is a stream value for a model response. Out is never set because the output is already available in the Response field.
type ModelSupports ¶ added in v0.3.0
type ModelSupports struct {
// Constrained indicates the level of constrained generation support (none, all, or no-tools).
Constrained ConstrainedSupport `json:"constrained,omitempty"`
// ContentType lists the content types the model supports for output.
ContentType []string `json:"contentType,omitempty"`
// Context indicates whether the model can natively support document-based context grounding.
Context bool `json:"context,omitempty"`
// LongRunning indicates whether the model supports long-running operations.
LongRunning bool `json:"longRunning,omitempty"`
// Media indicates whether the model can process media as part of the prompt (multimodal input).
Media bool `json:"media,omitempty"`
// Multiturn indicates whether the model can process historical messages passed with a prompt.
Multiturn bool `json:"multiturn,omitempty"`
// Output lists the types of data the model can generate.
Output []string `json:"output,omitempty"`
// SystemRole indicates whether the model can accept messages with role "system".
SystemRole bool `json:"systemRole,omitempty"`
// ToolChoice indicates whether the model supports controlling tool choice (e.g., forced tool calling).
ToolChoice bool `json:"toolChoice,omitempty"`
// Tools indicates whether the model can perform tool calls.
Tools bool `json:"tools,omitempty"`
}
ModelSupports describes the capabilities that a model supports.
type MultipartToolFunc ¶ added in v1.3.0
type MultipartToolFunc[In any] = func(ctx *ToolContext, input In) (*MultipartToolResponse, error)
MultipartToolFunc is the function type for multipart tool implementations. Unlike regular tools that return just an output value, multipart tools can return both an output value and additional content parts (like media).
type MultipartToolResponse ¶ added in v1.3.0
type MultipartToolResponse struct {
// Content holds additional message parts providing context or details.
Content []*Part `json:"content,omitempty"`
// Output contains the structured output data from the tool.
Output any `json:"output,omitempty"`
}
MultipartToolResponse represents a tool response with both structured output and content parts.
type Operation ¶ added in v1.3.0
type Operation struct {
// Action is the name of the action being performed by this operation.
Action string `json:"action,omitempty"`
// Done indicates whether the operation has completed.
Done bool `json:"done,omitempty"`
// Error contains error information if the operation failed.
Error *OperationError `json:"error,omitempty"`
// Id is the unique identifier for this operation.
Id string `json:"id,omitempty"`
// Metadata contains additional information about the operation.
Metadata map[string]any `json:"metadata,omitempty"`
// Output contains the result of the operation if it has completed successfully.
Output any `json:"output,omitempty"`
}
Operation represents a long-running background task.
type OperationError ¶ added in v1.3.0
type OperationError struct {
// Message describes the error that occurred.
Message string `json:"message,omitempty"`
}
OperationError contains error information for a failed operation.
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 WithOutputEnums ¶ added in v1.3.0
func WithOutputEnums[T ~string](values ...T) OutputOption
WithOutputEnums sets the output format to enum and the schema based on the given values. Accepts any string-based type (e.g. type MyEnum string).
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 WithOutputSchema ¶ added in v0.1.0
func WithOutputSchema(schema map[string]any) OutputOption
WithOutputSchema manually provides a schema map for the prompt's output. The outputted value will serve as the default output if no output is given at generation time.
func WithOutputSchemaName ¶ added in v1.3.0
func WithOutputSchemaName(name string) OutputOption
WithOutputSchemaName sets the schema name that will be resolved at execution time.
func WithOutputType ¶ added in v0.3.0
func WithOutputType(output any) OutputOption
WithOutputType sets the output format to JSON and the schema derived from the given value.
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
NewCustomPart returns a Part containing custom plugin-specific data.
func NewDataPart ¶
NewDataPart returns a Part containing raw string data.
Example ¶
This example demonstrates creating a data part for raw string content.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a data part with raw string content
part := ai.NewDataPart(`{"name": "Alice", "age": 30}`)
fmt.Println("Is data part:", part.IsData())
fmt.Println("Content:", part.Text)
}
Output: Is data part: true Content: {"name": "Alice", "age": 30}
func NewMediaPart ¶
NewMediaPart returns a Part containing structured data described by the given mimeType.
Example ¶
This example demonstrates creating a media part for images or other media.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a media part with base64-encoded image data
// In practice, you would encode actual image bytes
imageData := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJ..."
part := ai.NewMediaPart("image/png", imageData)
fmt.Println("Is media:", part.IsMedia())
fmt.Println("Content type:", part.ContentType)
}
Output: Is media: true Content type: image/png
func NewReasoningPart ¶ added in v0.6.0
NewReasoningPart returns a Part containing reasoning text
func NewResourcePart ¶ added in v0.7.0
NewResourcePart returns a Part containing a resource reference.
func NewResponseForToolRequest ¶ added in v0.6.0
NewResponseForToolRequest returns a Part containing the results of executing the tool request part.
func NewTextPart ¶
NewTextPart returns a Part containing text.
Example ¶
This example demonstrates creating different types of message parts.
package main
import (
"fmt"
"github.com/firebase/genkit/go/ai"
)
func main() {
// Create a text part
part := ai.NewTextPart("Hello, world!")
fmt.Println(part.Text)
}
Output: Hello, world!
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) IsCustom ¶ added in v0.5.1
IsCustom reports whether the Part contains custom plugin-specific data.
func (*Part) IsInterrupt ¶ added in v1.0.3
IsInterrupt reports whether the Part contains a tool request that was interrupted.
func (*Part) IsReasoning ¶ added in v0.6.0
IsReasoning reports whether the Part contains a reasoning text
func (*Part) IsResource ¶ added in v0.7.0
IsResource reports whether the Part contains a resource reference.
func (*Part) IsToolRequest ¶
IsToolRequest reports whether the Part contains a request to run a tool.
func (*Part) IsToolResponse ¶
IsToolResponse reports whether the Part contains the result of running a tool.
func (Part) JSONSchemaAlias ¶
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 ¶
MarshalJSON is called by the JSON marshaler to write out a Part.
func (*Part) UnmarshalJSON ¶
UnmarshalJSON is called by the JSON unmarshaler to read a Part.
type PathMetadata ¶ added in v0.3.0
type PathMetadata struct {
// Error contains error information if the path failed.
Error string `json:"error,omitempty"`
// Latency is the execution time for this path in milliseconds.
Latency float64 `json:"latency,omitempty"`
// Path is the identifier for this execution path.
Path string `json:"path,omitempty"`
// Status indicates the outcome of this path.
Status string `json:"status,omitempty"`
}
PathMetadata contains metadata about a single execution path in a trace.
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)
// ExecuteStream executes the prompt with streaming and returns an iterator.
ExecuteStream(ctx context.Context, opts ...PromptExecuteOption) iter.Seq2[*ModelStreamValue, 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
LoadPrompt loads a single prompt from a directory on the local filesystem into the registry.
func LoadPromptFromFS ¶ added in v1.3.0
LoadPromptFromFS loads a single prompt from a filesystem into the registry. The fsys parameter should be an fs.FS implementation (e.g., embed.FS or os.DirFS). The dir parameter specifies the directory within the filesystem where the prompt is located.
func LoadPromptFromSource ¶ added in v1.3.0
LoadPromptFromSource loads a prompt from raw .prompt file content. The source parameter should contain the complete .prompt file text (frontmatter + template). The name parameter is the prompt name (may include variant suffix like "myPrompt.variant").
func LookupPrompt ¶
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 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 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 holds the document's parts (text and media).
Content []*Part `json:"content,omitempty"`
// Metadata contains the reranking score and other arbitrary key-value data.
Metadata *RankedDocumentMetadata `json:"metadata,omitempty"`
}
RankedDocumentData represents a document with a relevance score from reranking.
type RankedDocumentMetadata ¶ added in v0.3.0
type RankedDocumentMetadata struct {
// Score is the relevance score assigned by the reranker.
Score float64 `json:"score,omitempty"`
}
RankedDocumentMetadata contains the relevance score and other metadata for a reranked document.
type RerankerRequest ¶ added in v0.3.0
type RerankerRequest struct {
// Documents is the array of documents to rerank.
Documents []*Document `json:"documents,omitempty"`
// Options contains reranker-specific configuration parameters.
Options any `json:"options,omitempty"`
// Query is the document to use for reranking.
Query *Document `json:"query,omitempty"`
}
RerankerRequest represents a request to rerank documents based on relevance.
type RerankerResponse ¶ added in v0.3.0
type RerankerResponse struct {
// Documents is the array of reranked documents with scores.
Documents []*RankedDocumentData `json:"documents,omitempty"`
}
RerankerResponse contains the reranked documents with relevance scores.
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
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 is the URI of the external resource.
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 RespondWithOption ¶ added in v1.4.0
type RespondWithOption[Out any] interface { // contains filtered or unexported methods }
RespondWithOption is a functional option for ToolDef.RespondWith.
func WithResponseMetadata ¶ added in v1.4.0
func WithResponseMetadata[Out any](meta map[string]any) RespondWithOption[Out]
WithResponseMetadata sets metadata for the response.
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 RestartWithOption ¶ added in v1.4.0
type RestartWithOption[In any] interface { // contains filtered or unexported methods }
RestartWithOption is a functional option for ToolDef.RestartWith.
func WithNewInput ¶ added in v1.4.0
func WithNewInput[In any](input In) RestartWithOption[In]
WithNewInput sets a new input value to replace the original tool request input.
func WithResumedMetadata ¶ added in v1.4.0
func WithResumedMetadata[In any](meta map[string]any) RestartWithOption[In]
WithResumedMetadata sets metadata to pass to the resumed tool execution. The metadata will be available in the tool's [ToolContext.Resumed] field.
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 ¶
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 contains retriever-specific configuration parameters.
Options any `json:"options,omitempty"`
// Query is the document to use for retrieval.
Query *Document `json:"query,omitempty"`
}
RetrieverRequest represents a request to retrieve relevant documents.
type RetrieverResponse ¶
type RetrieverResponse struct {
// Documents is the array of retrieved documents.
Documents []*Document `json:"documents,omitempty"`
}
RetrieverResponse contains the retrieved documents from a retriever request.
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 explains the rationale behind the score.
Reasoning string `json:"reasoning,omitempty"`
}
ScoreDetails provides additional context and explanation for an evaluation score.
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 StartModelOpFunc ¶ added in v1.3.0
type StartModelOpFunc = func(ctx context.Context, req *ModelRequest) (*ModelOperation, error)
StartModelOpFunc starts a background model operation.
type StreamValue ¶ added in v1.3.0
type StreamValue[Out, Stream any] struct { Done bool Chunk Stream // valid if Done is false Output Out // valid if Done is true Response *ModelResponse // valid if Done is true }
StreamValue is either a streamed chunk or the final response of a generate request.
type StreamingFormatHandler ¶ added in v1.3.0
type StreamingFormatHandler interface {
// ParseOutput parses the final output and returns parsed output.
ParseOutput(message *Message) (any, error)
// ParseChunk processes a streaming chunk and returns parsed output.
// The handler maintains its own internal state. When the chunk's index changes, the state is reset for the new turn.
// Returns parsed output, or nil if nothing can be parsed yet.
ParseChunk(chunk *ModelResponseChunk) (any, error)
}
StreamingFormatHandler is a handler for formatting messages that supports streaming. This interface must be implemented to be able to use ModelResponse.Output and ModelResponseChunk.Output.
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 and returns just the output.
RunRaw(ctx context.Context, input any) (any, error)
// RunRawMultipart runs this tool and returns the full [MultipartToolResponse].
RunRawMultipart(ctx context.Context, input any) (*MultipartToolResponse, 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 LookupTool ¶ added in v0.1.0
LookupTool looks up the tool in the registry by provided name and returns it. It checks for "tool.v2" first, then falls back to "tool" for legacy compatibility. Since the types are not known at lookup time, it returns a type-erased tool.
type ToolChoice ¶ added in v0.3.0
type ToolChoice string
ToolChoice controls how the model uses tools.
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
// 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.
func (*ToolContext) Interrupt ¶ added in v0.3.0
func (tc *ToolContext) Interrupt(opts *InterruptOptions) error
Interrupt interrupts the tool execution and returns control to the caller with the total model response so far. The provided metadata is preserved and passed back via [ToolContext.Resumed] when the tool is restarted.
func (*ToolContext) IsResumed ¶ added in v1.4.0
func (tc *ToolContext) IsResumed() bool
IsResumed returns true if this tool execution is a resumption after an interrupt.
type ToolDef ¶ added in v0.1.0
type ToolDef[In, Out any] struct { // contains filtered or unexported fields }
ToolDef is an action with functions specific to tools. Internally, all tools use the v2 format (returning MultipartToolResponse). For regular tools, RunRaw unwraps the Output field for backward compatibility.
func DefineMultipartTool ¶ added in v1.3.0
func DefineMultipartTool[In any]( r api.Registry, name, description string, fn MultipartToolFunc[In], opts ...ToolOption, ) *ToolDef[In, *MultipartToolResponse]
DefineMultipartTool creates a new multipart ToolDef and registers it. Multipart tools can return both output data and additional content parts (like media). Use WithInputSchema to provide a custom JSON schema instead of inferring from the type parameter.
func DefineTool ¶
func DefineTool[In, Out any]( r api.Registry, name, description string, fn ToolFunc[In, Out], opts ...ToolOption, ) *ToolDef[In, Out]
DefineTool creates a new ToolDef and registers it. Use WithInputSchema to provide a custom JSON schema instead of inferring from the type parameter.
func DefineToolWithInputSchema
deprecated
added in
v0.6.0
func DefineToolWithInputSchema[Out any]( r api.Registry, name, description string, inputSchema map[string]any, fn ToolFunc[any, Out], ) *ToolDef[any, Out]
DefineToolWithInputSchema creates a new ToolDef with a custom input schema and registers it.
Deprecated: Use DefineTool with WithInputSchema instead.
func NewMultipartTool ¶ added in v1.3.0
func NewMultipartTool[In any](name, description string, fn MultipartToolFunc[In], opts ...ToolOption) *ToolDef[In, *MultipartToolResponse]
NewMultipartTool creates a new multipart ToolDef. It can be passed directly to Generate. Multipart tools can return both output data and additional content parts (like media). Use WithInputSchema to provide a custom JSON schema instead of inferring from the type parameter.
func NewTool ¶ added in v0.6.0
func NewTool[In, Out any](name, description string, fn ToolFunc[In, Out], opts ...ToolOption) *ToolDef[In, Out]
NewTool creates a new ToolDef. It can be passed directly to Generate. Use WithInputSchema to provide a custom JSON schema instead of inferring from the type parameter.
func NewToolWithInputSchema
deprecated
added in
v1.0.0
func (*ToolDef[In, Out]) Definition ¶ added in v0.1.0
func (t *ToolDef[In, Out]) Definition() *ToolDefinition
Definition returns ToolDefinition for for this tool.
func (*ToolDef[In, Out]) IsMultipart ¶ added in v1.4.0
IsMultipart returns true if the tool is a multipart tool (tool.v2 only).
func (*ToolDef[In, Out]) Register ¶ added in v1.4.0
Register registers the tool with the given registry.
func (*ToolDef[In, Out]) Respond
deprecated
added in
v1.4.0
func (t *ToolDef[In, Out]) Respond(toolReq *Part, output any, opts *RespondOptions) *Part
Respond creates a part for WithToolResponses to provide a resolved response for an interrupted tool call. Returns nil if the part is not a tool request.
Deprecated: Use ToolDef.RespondWith instead for strongly-typed options.
func (*ToolDef[In, Out]) RespondWith ¶ added in v1.4.0
func (t *ToolDef[In, Out]) RespondWith(toolReq *Part, output Out, opts ...RespondWithOption[Out]) (*Part, error)
RespondWith creates a part for WithToolResponses to provide a resolved response for an interrupted tool call.
Example:
part, err := myTool.RespondWith(toolReq, output, WithResponseMetadata[MyOutput](meta))
func (*ToolDef[In, Out]) Restart
deprecated
added in
v1.4.0
func (t *ToolDef[In, Out]) Restart(p *Part, opts *RestartOptions) *Part
Restart creates a part for WithToolRestarts to re-execute an interrupted tool call with additional context. Returns nil if the part is not a tool request.
Deprecated: Use ToolDef.RestartWith instead for strongly-typed options.
func (*ToolDef[In, Out]) RestartWith ¶ added in v1.4.0
func (t *ToolDef[In, Out]) RestartWith(toolReq *Part, opts ...RestartWithOption[In]) (*Part, error)
RestartWith creates a part for WithToolRestarts to re-execute an interrupted tool call with additional context.
Example:
part, err := myTool.RestartWith(toolReq, WithNewInput(newInput), WithResumedMetadata[MyInput](meta))
func (*ToolDef[In, Out]) RunRaw ¶ added in v0.1.0
RunRaw runs this tool using the provided raw map format data (JSON parsed as map[string]any).
func (*ToolDef[In, Out]) RunRawMultipart ¶ added in v1.4.0
func (t *ToolDef[In, Out]) RunRawMultipart(ctx context.Context, input any) (*MultipartToolResponse, error)
RunRawMultipart runs this tool using the provided raw map format data (JSON parsed as map[string]any). It returns the full multipart response.
type ToolDefinition ¶
type ToolDefinition struct {
// Description explains what the tool does and when to use it.
Description string `json:"description,omitempty"`
// InputSchema is a valid JSON Schema representing the input parameters of the tool.
InputSchema map[string]any `json:"inputSchema,omitempty"`
// Metadata contains additional information about this tool definition.
Metadata map[string]any `json:"metadata,omitempty"`
// Name is the unique identifier for this tool.
Name string `json:"name,omitempty"`
// OutputSchema is a 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.
type ToolOption ¶ added in v1.3.0
type ToolOption interface {
// contains filtered or unexported methods
}
ToolOption is an option for defining a 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 containing the input parameters for the tool.
// For example: map[string]any{"country":"USA", "president":3}.
Input any `json:"input,omitempty"`
// Name is the name of the tool to call.
Name string `json:"name,omitempty"`
// Partial indicates whether this is a partial streaming chunk.
Partial bool `json:"partial,omitempty"`
// Ref is the call ID or reference for this specific request.
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 {
// Content holds additional message parts that provide context or details about the tool response.
Content []*Part `json:"content,omitempty"`
// Name is the name of the tool that was executed.
Name string `json:"name,omitempty"`
// Output is a JSON object describing the results of running the tool.
// For example: map[string]any{"name":"Thomas Jefferson", "born":1743}.
Output any `json:"output,omitempty"`
// Ref is the call ID or reference matching the original request.
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 identifies the feature being traced.
FeatureName string `json:"featureName,omitempty"`
// Paths contains metadata for each path executed during the trace.
Paths []*PathMetadata `json:"paths,omitempty"`
// Timestamp is when the trace was created.
Timestamp float64 `json:"timestamp,omitempty"`
}
TraceMetadata contains metadata about a trace execution.