ai

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: May 1, 2025 License: Apache-2.0 Imports: 28 Imported by: 20

Documentation

Index

Constants

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

Variables

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

Default formats get automatically registered on registry init

Functions

func ConfigureFormats added in v0.5.0

func ConfigureFormats(reg *registry.Registry)

ConfigureFormats registers default formats in the registry

func DefineFormat added in v0.5.0

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

DefineFormat defines and registers a new Formatter.

func DefineGenerateAction added in v0.3.0

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

DefineGenerateAction defines a utility generate action.

func GenerateText added in v0.1.0

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

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

func Index added in v0.1.0

func Index(ctx context.Context, r Indexer, opts ...IndexerOption) error

Index calls the indexer with the provided options.

func LoadPromptDir added in v0.3.0

func LoadPromptDir(r *registry.Registry, dir string, namespace string) error

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

Types

type AugmentWithContextOptions added in v0.3.0

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

AugmentWithContextOptions configures how a request is augmented with context.

type BaseDataPoint added in v0.3.0

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

type BaseEvalDataPoint added in v0.3.0

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

type CandidateError added in v0.3.0

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

type CandidateErrorCode added in v0.3.0

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

type CommonGenOption added in v0.5.0

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

func WithMaxTurns added in v0.3.0

func WithMaxTurns(maxTurns int) CommonGenOption

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

func WithMessages added in v0.1.0

func WithMessages(messages ...*Message) CommonGenOption

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

func WithMessagesFn added in v0.3.0

func WithMessagesFn(fn MessagesFn) CommonGenOption

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

func WithMiddleware added in v0.3.0

func WithMiddleware(middleware ...ModelMiddleware) CommonGenOption

WithMiddleware sets middleware to apply to the model request.

func WithModel added in v0.3.0

func WithModel(model ModelArg) CommonGenOption

WithModel sets a resolvable model reference to use for generation.

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 WithReturnToolRequests added in v0.3.0

func WithReturnToolRequests(returnReqs bool) CommonGenOption

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

func WithToolChoice added in v0.3.0

func WithToolChoice(toolChoice ToolChoice) CommonGenOption

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

func WithTools added in v0.1.0

func WithTools(tools ...ToolRef) CommonGenOption

WithTools sets the tools to use for the generate request.

type CommonRerankerOptions added in v0.3.0

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

type CommonRetrieverOptions added in v0.3.0

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

type ConfigOption added in v0.5.0

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

ConfigOption is an option for model configuration.

func WithConfig added in v0.1.0

func WithConfig(config any) ConfigOption

WithConfig sets the configuration.

type ConstrainedSupport added in v0.3.0

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

type Document

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

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

func DocumentFromText

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

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

type DocumentOption added in v0.5.0

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

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

func WithDocs added in v0.3.0

func WithDocs(docs ...*Document) DocumentOption

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

func WithTextDocs added in v0.5.0

func WithTextDocs(text ...string) DocumentOption

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

type DownloadMediaOptions added in v0.3.0

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

DownloadMediaOptions configures how media is downloaded in the DownloadRequestMedia middleware.

type EmbedRequest

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

type EmbedResponse added in v0.0.2

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

func Embed added in v0.1.0

func Embed(ctx context.Context, e Embedder, 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)
}

Embedder represents an embedder that can perform content embedding.

func DefineEmbedder

func DefineEmbedder(
	r *registry.Registry,
	provider, name string,
	embed func(context.Context, *EmbedRequest) (*EmbedResponse, error),
) Embedder

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

func LookupEmbedder

func LookupEmbedder(r *registry.Registry, provider, name string) Embedder

LookupEmbedder looks up an Embedder registered by DefineEmbedder. It returns nil if the embedder was not defined.

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.

type Embedding added in v0.3.0

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

type EvalFnResponse added in v0.3.0

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

type EvalRequest added in v0.3.0

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

type EvalResponse added in v0.3.0

type EvalResponse []any

type EvalStatusEnum added in v0.3.0

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

type EvaluationResult added in v0.3.0

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

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

type Evaluator added in v0.3.0

type Evaluator interface {
	// Name returns the name of the evaluator.
	Name() string
	// Evaluates a dataset.
	Evaluate(ctx context.Context, req *EvaluatorRequest) (*EvaluatorResponse, error)
}

Evaluator represents a evaluator action.

func DefineBatchEvaluator added in v0.3.0

func DefineBatchEvaluator(r *registry.Registry, provider, name string, options *EvaluatorOptions, batchEval func(context.Context, *EvaluatorRequest) (*EvaluatorResponse, error)) (Evaluator, error)

DefineBatchEvaluator registers the given evaluator function as an action, and returns a Evaluator that runs it. This method provide the full EvaluatorRequest to the callback function, giving more flexibilty to the user for processing the data, such as batching or parallelization.

func DefineEvaluator added in v0.3.0

func DefineEvaluator(r *registry.Registry, provider, name string, options *EvaluatorOptions, eval func(context.Context, *EvaluatorCallbackRequest) (*EvaluatorCallbackResponse, error)) (Evaluator, error)

DefineEvaluator registers the given evaluator function as an action, and returns a Evaluator that runs it. This method process the input dataset one-by-one.

func LookupEvaluator added in v0.3.0

func LookupEvaluator(r *registry.Registry, provider, name string) Evaluator

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

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 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 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 {
	DisplayName string `json:"displayName"`
	Definition  string `json:"definition"`
	IsBilled    bool   `json:"isBilled,omitempty"`
}

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 Evaluator, opts ...EvaluatorOption) (*EvaluatorResponse, error)

Evaluate calls the retrivers with provided options.

type Example added in v0.3.0

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

Example is a single example that requires evaluation

type ExecutionOption added in v0.3.0

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

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

func WithStreaming added in v0.1.0

func WithStreaming(callback ModelStreamCallback) ExecutionOption

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

type FinishReason

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

type FormatHandler added in v0.5.0

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

FormatHandler represents the handler part of the Formatter interface.

type Formatter added in v0.5.0

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

Formatter represents the Formatter interface.

type GenerateActionOptions added in v0.3.0

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

type GenerateActionOutputConfig added in v0.3.0

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

type GenerateActionResume added in v0.3.0

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

type GenerateOption added in v0.1.0

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

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

type GenerationCommonConfig

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

GenerationCommonConfig holds configuration for generation.

type GenerationUsage

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

GenerationUsage provides information about the generation process.

type Indexer

type Indexer interface {
	// Name returns the name of the indexer.
	Name() string
	// Index executes the indexing request.
	Index(ctx context.Context, req *IndexerRequest) error
}

Indexer represents a document indexer.

func DefineIndexer

func DefineIndexer(r *registry.Registry, provider, name string, fn IndexerFunc) Indexer

DefineIndexer registers the given index function as an action, and returns an Indexer that runs it.

func LookupIndexer

func LookupIndexer(r *registry.Registry, provider, name string) Indexer

LookupIndexer looks up an Indexer registered by DefineIndexer. It returns nil if the model was not defined.

type IndexerFunc added in v0.5.0

type IndexerFunc = func(context.Context, *IndexerRequest) error

type IndexerOption added in v0.1.0

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

IndexerOption is an option for configuring an embedder request. It applies only to Index.

type IndexerRequest

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

IndexerRequest is the data we pass to add documents to the database. The Options field is specific to the actual retriever implementation.

type InterruptOptions added in v0.3.0

type InterruptOptions struct {
	Metadata map[string]any
}

InterruptOptions provides configuration for tool interruption.

type Media added in v0.3.0

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

type Message

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

Message is the contents of a model response.

func NewMessage

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

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

func NewModelMessage

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

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

func NewModelTextMessage

func NewModelTextMessage(text string) *Message

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

func NewSystemMessage

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

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

func NewSystemTextMessage

func NewSystemTextMessage(text string) *Message

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

func NewTextMessage

func NewTextMessage(role Role, text string) *Message

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

func NewUserMessage

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

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

func NewUserMessageWithMetadata added in v0.3.0

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

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

func NewUserTextMessage

func NewUserTextMessage(text string) *Message

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

func (*Message) Text added in v0.3.0

func (m *Message) Text() string

Text returns the contents of a Message as a string. It returns an empty string if the message has no content.

func (*Message) WithCacheName added in v0.3.0

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

WithCacheName adds cache name to use in the generate request

func (*Message) WithCacheTTL added in v0.3.0

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

WithCacheTTL adds cache TTL configuration for the desired message

type MessagesFn added in v0.5.0

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

MessagesFn is a function that generates messages.

type Model

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

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

func DefineModel

func DefineModel(r *registry.Registry, provider, name string, info *ModelInfo, fn ModelFunc) Model

DefineModel registers the given generate function as an action, and returns a Model that runs it.

func LookupModel

func LookupModel(r *registry.Registry, provider, name string) Model

LookupModel looks up a Model registered by DefineModel. It returns nil if the model was not defined.

func LookupModelByName added in v0.3.0

func LookupModelByName(r *registry.Registry, modelName string) (Model, error)

LookupModelByName looks up a Model registered by DefineModel. It returns an error if the model was not defined.

type ModelArg added in v0.5.0

type ModelArg interface {
	Name() string
}

ModelArg is the interface for model arguments.

type ModelFunc added in v0.3.0

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

type ModelInfo

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

type ModelMiddleware added in v0.3.0

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

func DownloadRequestMedia added in v0.3.0

func DownloadRequestMedia(options *DownloadMediaOptions) ModelMiddleware

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

type ModelOutputConfig added in v0.3.0

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

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

type ModelRef added in v0.5.0

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

ModelRef is a struct to hold model name and configuration.

func NewModelRef added in v0.5.0

func NewModelRef(name string, config any) ModelRef

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

func (ModelRef) Config added in v0.5.0

func (m ModelRef) Config() any

ModelConfig returns the configuration of a ModelRef.

func (ModelRef) Name added in v0.5.0

func (m ModelRef) Name() string

Name returns the name of the ModelRef.

type ModelRequest added in v0.3.0

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

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

func NewModelRequest added in v0.3.0

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

NewModelRequest create a new ModelRequest with provided config and messages.

type ModelResponse added in v0.3.0

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

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

func Generate added in v0.1.0

func Generate(ctx context.Context, r *registry.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 *registry.Registry, opts ...GenerateOption) (*Out, *ModelResponse, error)

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

func GenerateWithRequest added in v0.3.0

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

func (*ModelResponse) History added in v0.3.0

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

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

func (*ModelResponse) Output added in v0.3.0

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

Output unmarshals structured JSON output into the provided struct pointer.

func (*ModelResponse) Text added in v0.3.0

func (gr *ModelResponse) Text() string

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

func (*ModelResponse) ToolRequests added in v0.5.0

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

ToolRequests returns the tool requests from the response.

type ModelResponseChunk added in v0.3.0

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

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

func (*ModelResponseChunk) Text added in v0.3.0

func (c *ModelResponseChunk) Text() string

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

type ModelStage added in v0.3.0

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

type ModelStreamCallback added in v0.3.0

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

ModelStreamCallback is a stream callback of a ModelAction.

type ModelSupports added in v0.3.0

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

type OutputOption added in v0.3.0

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

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

func WithCustomConstrainedOutput added in v0.5.0

func WithCustomConstrainedOutput() OutputOption

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

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

func WithOutputFormat added in v0.1.0

func WithOutputFormat(format string) OutputOption

WithOutputFormat sets the format of the output.

func WithOutputInstructions added in v0.5.0

func WithOutputInstructions(instructions string) OutputOption

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

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

This will automatically set WithCustomConstrainedOutput.

func WithOutputType added in v0.3.0

func WithOutputType(output any) OutputOption

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

type Part

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

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

func NewCustomPart added in v0.5.1

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

NewCustomPart returns a Part containing custom plugin-specific data.

func NewDataPart

func NewDataPart(contents string) *Part

NewDataPart returns a Part containing raw string data.

func NewJSONPart

func NewJSONPart(text string) *Part

NewJSONPart returns a Part containing JSON.

func NewMediaPart

func NewMediaPart(mimeType, contents string) *Part

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

func NewTextPart

func NewTextPart(text string) *Part

NewTextPart returns a Part containing text.

func NewToolRequestPart

func NewToolRequestPart(r *ToolRequest) *Part

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

func NewToolResponsePart

func NewToolResponsePart(r *ToolResponse) *Part

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

func (*Part) IsCustom added in v0.5.1

func (p *Part) IsCustom() bool

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

func (*Part) IsData

func (p *Part) IsData() bool

IsData reports whether the Part contains unstructured data.

func (*Part) IsMedia

func (p *Part) IsMedia() bool

IsMedia reports whether the Part contains structured media data.

func (*Part) IsText

func (p *Part) IsText() bool

IsText reports whether the Part contains plain text.

func (*Part) IsToolRequest

func (p *Part) IsToolRequest() bool

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

func (*Part) IsToolResponse

func (p *Part) IsToolResponse() bool

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

func (Part) JSONSchemaAlias

func (Part) JSONSchemaAlias() any

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

func (*Part) MarshalJSON

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

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

func (*Part) UnmarshalJSON

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

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

func (*Part) UnmarshalYAML added in v0.3.0

func (p *Part) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler for Part.

type PartKind

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

type PathMetadata added in v0.3.0

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

type Prompt

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

Prompt is a prompt template that can be executed to generate a model response.

func DefinePrompt

func DefinePrompt(r *registry.Registry, name string, opts ...PromptOption) (*Prompt, error)

DefinePrompt creates and registers a new Prompt.

func LoadPrompt added in v0.3.0

func LoadPrompt(r *registry.Registry, dir, filename, namespace string) (*Prompt, error)

LoadPrompt loads a single prompt into the registry.

func LookupPrompt

func LookupPrompt(r *registry.Registry, name string) *Prompt

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

func (*Prompt) Execute added in v0.3.0

func (p *Prompt) Execute(ctx context.Context, opts ...PromptExecuteOption) (*ModelResponse, error)

Execute renders a prompt, does variable substitution and passes the rendered template to the AI model specified by the prompt.

func (*Prompt) Name added in v0.3.0

func (p *Prompt) Name() string

Name returns the name of the prompt.

func (*Prompt) Render

func (p *Prompt) Render(ctx context.Context, input any) (*GenerateActionOptions, error)

Render renders the prompt template based on user input.

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 type.

type PromptFn added in v0.5.0

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

PromptFn is a function that generates a prompt.

type PromptOption added in v0.3.0

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

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

func WithDescription added in v0.3.0

func WithDescription(description string) PromptOption

WithDescription sets the description of the prompt.

func WithInputType added in v0.3.0

func WithInputType(input any) PromptOption

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

func WithMetadata added in v0.3.0

func WithMetadata(metadata map[string]any) PromptOption

WithMetadata sets arbitrary metadata for the prompt.

type PromptingOption added in v0.3.0

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

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

func WithPrompt added in v0.5.0

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

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

func WithPromptFn added in v0.3.0

func WithPromptFn(fn PromptFn) PromptingOption

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

func WithSystem added in v0.5.0

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

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

func WithSystemFn added in v0.3.0

func WithSystemFn(fn PromptFn) PromptingOption

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

type RankedDocumentData added in v0.3.0

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

type RankedDocumentMetadata added in v0.3.0

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

type RerankerRequest added in v0.3.0

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

type RerankerResponse added in v0.3.0

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

type 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)
}

Retriever represents a document retriever.

func DefineRetriever

func DefineRetriever(r *registry.Registry, provider, name string, fn RetrieverFunc) Retriever

DefineRetriever registers the given retrieve function as an action, and returns a Retriever that runs it.

func LookupRetriever

func LookupRetriever(r *registry.Registry, provider, name string) Retriever

LookupRetriever looks up a Retriever registered by DefineRetriever. It returns nil if the retriever was not defined.

type RetrieverFunc added in v0.5.0

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

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].

type RetrieverRequest

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

type RetrieverResponse

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

func Retrieve added in v0.1.0

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

Retrieve calls the retriever with the provided options.

type Role

type Role string

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

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

type Score added in v0.3.0

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

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

type ScoreDetails added in v0.3.0

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

type ScoreStatus added in v0.3.0

type ScoreStatus int

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

const (
	ScoreStatusUnknown ScoreStatus = iota
	ScoreStatusFail
	ScoreStatusPass
)

func (ScoreStatus) String added in v0.3.0

func (ss ScoreStatus) String() string

type Tool

type Tool interface {
	// Name returns the name of the tool.
	Name() string
	// Definition returns ToolDefinition for for this tool.
	Definition() *ToolDefinition
	// RunRaw runs this tool using the provided raw input.
	RunRaw(ctx context.Context, input any) (any, error)
}

Tool represents an instance of a tool.

func DefineTool

func DefineTool[In, Out any](r *registry.Registry, name, description string,
	fn func(ctx *ToolContext, input In) (Out, error)) Tool

DefineTool defines a tool function with interrupt capability

func LookupTool added in v0.1.0

func LookupTool(r *registry.Registry, name string) Tool

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

type ToolChoice added in v0.3.0

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

type ToolConfig added in v0.3.0

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

ToolConfig handles configuration around tool calls during generation.

type ToolContext added in v0.3.0

type ToolContext struct {
	context.Context
	// Interrupt is a function that can be used to interrupt the tool execution.
	// Interrupting tool execution returns the control to the caller with the
	// total model response so far.
	Interrupt func(opts *InterruptOptions) error
}

ToolContext provides context and utility functions for tool execution.

type ToolDef added in v0.1.0

type ToolDef[In, Out any] core.ActionDef[In, Out, struct{}]

A ToolDef is an implementation of a single tool.

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]) Name added in v0.3.0

func (t *ToolDef[In, Out]) Name() string

Name returns the name of the tool.

func (*ToolDef[In, Out]) RunRaw added in v0.1.0

func (t *ToolDef[In, Out]) RunRaw(ctx context.Context, input any) (any, error)

RunRaw runs this tool using the provided raw map format data (JSON parsed as map[string]any).

type ToolDefinition

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

A ToolDefinition describes a tool.

type ToolInterruptError added in v0.3.0

type ToolInterruptError struct {
	Metadata map[string]any
}

ToolInterruptError represents an intentional interruption of tool execution.

func (*ToolInterruptError) Error added in v0.3.0

func (e *ToolInterruptError) Error() string

type ToolName added in v0.3.0

type ToolName string

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

func (ToolName) Name added in v0.3.0

func (t ToolName) Name() string

Name returns the name of the tool.

type ToolRef added in v0.3.0

type ToolRef interface {
	Name() string
}

ToolRef is a reference to a tool.

type ToolRequest

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

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

type ToolResponse

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

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

type TraceMetadata added in v0.3.0

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

Jump to

Keyboard shortcuts

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