Documentation ¶
Index ¶
- Constants
- Variables
- func ConfigureFormats(reg *registry.Registry)
- func DefineFormat(r *registry.Registry, name string, formatter Formatter)
- func DefineGenerateAction(ctx context.Context, r *registry.Registry) *generateAction
- func GenerateText(ctx context.Context, r *registry.Registry, opts ...GenerateOption) (string, error)
- func Index(ctx context.Context, r Indexer, opts ...IndexerOption) error
- func LoadPromptDir(r *registry.Registry, dir string, namespace string) error
- type AugmentWithContextOptions
- type BaseDataPoint
- type BaseEvalDataPoint
- type CandidateError
- type CandidateErrorCode
- 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 WithReturnToolRequests(returnReqs bool) CommonGenOption
- func WithToolChoice(toolChoice ToolChoice) CommonGenOption
- func WithTools(tools ...ToolRef) CommonGenOption
- type CommonRerankerOptions
- type CommonRetrieverOptions
- type ConfigOption
- type ConstrainedSupport
- type Document
- type DocumentOption
- type DownloadMediaOptions
- type EmbedRequest
- type EmbedResponse
- type Embedder
- type EmbedderOption
- type Embedding
- type EvalFnResponse
- type EvalRequest
- type EvalResponse
- type EvalStatusEnum
- type EvaluationResult
- type Evaluator
- func DefineBatchEvaluator(r *registry.Registry, provider, name string, options *EvaluatorOptions, ...) (Evaluator, error)
- func DefineEvaluator(r *registry.Registry, provider, name string, options *EvaluatorOptions, ...) (Evaluator, error)
- func LookupEvaluator(r *registry.Registry, provider, name string) Evaluator
- type EvaluatorCallbackRequest
- type EvaluatorCallbackResponse
- type EvaluatorOption
- type EvaluatorOptions
- 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 GenkitError
- type GenkitErrorData
- type GenkitErrorDetails
- type Indexer
- type IndexerFunc
- type IndexerOption
- type IndexerRequest
- 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 ModelOutputConfig
- type ModelRef
- type ModelRequest
- type ModelResponse
- func Generate(ctx context.Context, r *registry.Registry, opts ...GenerateOption) (*ModelResponse, error)
- func GenerateData[Out any](ctx context.Context, r *registry.Registry, opts ...GenerateOption) (*Out, *ModelResponse, error)
- func GenerateWithRequest(ctx context.Context, r *registry.Registry, opts *GenerateActionOptions, ...) (*ModelResponse, error)
- type ModelResponseChunk
- type ModelStage
- type ModelStreamCallback
- type ModelSupports
- type 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 NewTextPart(text string) *Part
- func NewToolRequestPart(r *ToolRequest) *Part
- func NewToolResponsePart(r *ToolResponse) *Part
- func (p *Part) IsCustom() bool
- func (p *Part) IsData() bool
- func (p *Part) IsMedia() bool
- func (p *Part) IsText() bool
- func (p *Part) IsToolRequest() bool
- func (p *Part) IsToolResponse() bool
- func (Part) JSONSchemaAlias() any
- func (p *Part) MarshalJSON() ([]byte, error)
- func (p *Part) UnmarshalJSON(b []byte) error
- func (p *Part) UnmarshalYAML(value *yaml.Node) error
- type PartKind
- type PathMetadata
- type Prompt
- type PromptExecuteOption
- type PromptFn
- type PromptOption
- type PromptingOption
- type RankedDocumentData
- type RankedDocumentMetadata
- type RerankerRequest
- type RerankerResponse
- type Retriever
- type RetrieverFunc
- type RetrieverOption
- type RetrieverRequest
- type RetrieverResponse
- type Role
- type Score
- type ScoreDetails
- type ScoreStatus
- type Tool
- type ToolChoice
- type ToolConfig
- type ToolContext
- type ToolDef
- type ToolDefinition
- type ToolInterruptError
- type ToolName
- type ToolRef
- type ToolRequest
- type ToolResponse
- type TraceMetadata
Constants ¶
Variables ¶
var DEFAULT_FORMATS = []Formatter{ jsonFormatter{}, jsonlFormatter{}, textFormatter{}, arrayFormatter{}, enumFormatter{}, }
Default formats get automatically registered on registry init
Functions ¶
func ConfigureFormats¶added inv0.5.0
ConfigureFormats registers default formats in the registry
func DefineFormat¶added inv0.5.0
DefineFormat defines and registers a new Formatter.
func DefineGenerateAction¶added inv0.3.0
DefineGenerateAction defines a utility generate action.
func GenerateText¶added inv0.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.
Types ¶
type AugmentWithContextOptions¶added inv0.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 inv0.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 inv0.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 inv0.3.0
type CandidateError struct { Code CandidateErrorCode `json:"code,omitempty"` Index float64 `json:"index,omitempty"` Message string `json:"message,omitempty"` }
type CandidateErrorCode¶added inv0.3.0
type CandidateErrorCode string
const ( CandidateErrorCodeBlocked CandidateErrorCode = "blocked" CandidateErrorCodeOther CandidateErrorCode = "other" CandidateErrorCodeUnknown CandidateErrorCode = "unknown" )
type CommonGenOption¶added inv0.5.0
type CommonGenOption interface { // contains filtered or unexported methods }
func WithMaxTurns¶added inv0.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 inv0.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 inv0.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 inv0.3.0
func WithMiddleware(middleware ...ModelMiddleware) CommonGenOption
WithMiddleware sets middleware to apply to the model request.
func WithModel¶added inv0.3.0
func WithModel(model ModelArg) CommonGenOption
WithModel sets a resolvable model reference to use for generation.
func WithModelName¶added inv0.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 inv0.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 inv0.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 inv0.1.0
func WithTools(tools ...ToolRef) CommonGenOption
WithTools sets the tools to use for the generate request.
type CommonRerankerOptions¶added inv0.3.0
type CommonRerankerOptions struct { // Number of documents to rerank K float64 `json:"k,omitempty"` }
type CommonRetrieverOptions¶added inv0.3.0
type CommonRetrieverOptions struct { // Number of documents to retrieve K float64 `json:"k,omitempty"` }
type ConfigOption¶added inv0.5.0
type ConfigOption interface { // contains filtered or unexported methods }
ConfigOption is an option for model configuration.
func WithConfig¶added inv0.1.0
func WithConfig(config any) ConfigOption
WithConfig sets the configuration.
type ConstrainedSupport¶added inv0.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.
type DocumentOption¶added inv0.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 inv0.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 inv0.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 inv0.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 EmbedResponse¶added inv0.0.2
type EmbedResponse struct { Embeddings []*Embedding `json:"embeddings,omitempty"` }
func Embed¶added inv0.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¶
LookupEmbedder looks up an Embedder registered by DefineEmbedder. It returns nil if the embedder was not defined.
type EmbedderOption¶added inv0.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 EvalFnResponse¶added inv0.3.0
type EvalRequest¶added inv0.3.0
type EvalRequest struct { Dataset []*BaseDataPoint `json:"dataset,omitempty"` EvalRunID string `json:"evalRunId,omitempty"` Options any `json:"options,omitempty"` }
type EvalResponse¶added inv0.3.0
type EvalResponse []any
type EvalStatusEnum¶added inv0.3.0
type EvalStatusEnum string
const ( EvalStatusEnumUNKNOWN EvalStatusEnum = "UNKNOWN" EvalStatusEnumPASS EvalStatusEnum = "PASS" EvalStatusEnumFAIL EvalStatusEnum = "FAIL" )
type EvaluationResult¶added inv0.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 inv0.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 inv0.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 inv0.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 inv0.3.0
LookupEvaluator looks up an Evaluator registered by DefineEvaluator. It returns nil if the evaluator was not defined.
type EvaluatorCallbackRequest¶added inv0.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 inv0.3.0
type EvaluatorCallbackResponse = EvaluationResult
EvaluatorCallbackResponse is the result on evaluating a single Example
type EvaluatorOption¶added inv0.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 inv0.5.0
func WithDataset(examples ...*Example) EvaluatorOption
WithDataset sets the dataset to do evaluation on.
func WithID¶added inv0.5.0
func WithID(ID string) EvaluatorOption
WithID sets the ID of the evaluation to uniquely identify it.
type EvaluatorOptions¶added inv0.3.0
type EvaluatorRequest¶added inv0.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 inv0.3.0
type EvaluatorResponse = []EvaluationResult
EvaluatorResponse is a collection of EvaluationResult structs, it represents the result on the entire input dataset.
func Evaluate¶added inv0.3.0
func Evaluate(ctx context.Context, r Evaluator, opts ...EvaluatorOption) (*EvaluatorResponse, error)
Evaluate calls the retrivers with provided options.
type Example¶added inv0.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 inv0.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 inv0.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 inv0.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 inv0.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 inv0.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 inv0.3.0
type GenerateActionResume¶added inv0.3.0
type GenerateOption¶added inv0.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 GenkitError¶added inv0.3.0
type GenkitError struct { Data *GenkitErrorData `json:"data,omitempty"` Details any `json:"details,omitempty"` Message string `json:"message,omitempty"` Stack string `json:"stack,omitempty"` }
type GenkitErrorData¶added inv0.3.0
type GenkitErrorData struct { GenkitErrorDetails *GenkitErrorDetails `json:"genkitErrorDetails,omitempty"` GenkitErrorMessage string `json:"genkitErrorMessage,omitempty"` }
type GenkitErrorDetails¶added inv0.3.0
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¶
LookupIndexer looks up an Indexer registered by DefineIndexer. It returns nil if the model was not defined.
type IndexerFunc¶added inv0.5.0
type IndexerFunc = func(context.Context, *IndexerRequest) error
type IndexerOption¶added inv0.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 inv0.3.0
InterruptOptions provides configuration for tool interruption.
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¶
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.
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.
func NewUserMessageWithMetadata¶added inv0.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.
func (*Message) Text¶added inv0.3.0
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 inv0.3.0
WithCacheName adds cache name to use in the generate request
func (*Message) WithCacheTTL¶added inv0.3.0
WithCacheTTL adds cache TTL configuration for the desired message
type MessagesFn¶added inv0.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) }
Model represents a model that can generate content based on a request.
func DefineModel¶
DefineModel registers the given generate function as an action, and returns a Model that runs it.
func LookupModel¶
LookupModel looks up a Model registered by DefineModel. It returns nil if the model was not defined.
func LookupModelByName¶added inv0.3.0
LookupModelByName looks up a Model registered by DefineModel. It returns an error if the model was not defined.
type ModelArg¶added inv0.5.0
type ModelArg interface { Name() string}
ModelArg is the interface for model arguments.
type ModelFunc¶added inv0.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 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 inv0.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 inv0.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 inv0.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 inv0.5.0
type ModelRef struct { // contains filtered or unexported fields }
ModelRef is a struct to hold model name and configuration.
func NewModelRef¶added inv0.5.0
NewModelRef creates a new ModelRef with the given name and configuration.
type ModelRequest¶added inv0.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 inv0.3.0
func NewModelRequest(config any, messages ...*Message) *ModelRequest
NewModelRequest create a new ModelRequest with provided config and messages.
type ModelResponse¶added inv0.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 inv0.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 inv0.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 inv0.3.0
func GenerateWithRequest(ctx context.Context, r *registry.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 inv0.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 inv0.3.0
func (mr *ModelResponse) Output(v any) error
Output unmarshals structured JSON output into the provided struct pointer.
func (*ModelResponse) Text¶added inv0.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 inv0.5.0
func (mr *ModelResponse) ToolRequests() ([]*ToolRequest, error)
ToolRequests returns the tool requests from the response.
type ModelResponseChunk¶added inv0.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 inv0.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 inv0.3.0
type ModelStage string
const ( ModelStageFeatured ModelStage = "featured" ModelStageStable ModelStage = "stable" ModelStageUnstable ModelStage = "unstable" ModelStageLegacy ModelStage = "legacy" ModelStageDeprecated ModelStage = "deprecated" )
type ModelStreamCallback¶added inv0.3.0
type ModelStreamCallback = func(context.Context, *ModelResponseChunk) error
ModelStreamCallback is a stream callback of a ModelAction.
type ModelSupports¶added inv0.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 inv0.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 inv0.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 inv0.1.0
func WithOutputFormat(format string) OutputOption
WithOutputFormat sets the format of the output.
func WithOutputInstructions¶added inv0.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 inv0.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 inv0.5.1
NewCustomPart returns a Part containing custom plugin-specific data.
func NewDataPart¶
NewDataPart returns a Part containing raw string data.
func NewMediaPart¶
NewMediaPart returns a Part containing structured data described by the given mimeType.
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 inv0.5.1
IsCustom reports whether the Part contains custom plugin-specific data.
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.
func (*Part) UnmarshalYAML¶added inv0.3.0
UnmarshalYAML implements yaml.Unmarshaler for Part.
type PathMetadata¶added inv0.3.0
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¶
DefinePrompt creates and registers a new Prompt.
func LoadPrompt¶added inv0.3.0
LoadPrompt loads a single prompt into the registry.
func LookupPrompt¶
LookupPrompt looks up a Prompt registered by DefinePrompt. It returns nil if the prompt was not defined.
func (*Prompt) Execute¶added inv0.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.
type PromptExecuteOption¶added inv0.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 inv0.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 PromptOption¶added inv0.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 inv0.3.0
func WithDescription(description string) PromptOption
WithDescription sets the description of the prompt.
func WithInputType¶added inv0.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 inv0.3.0
func WithMetadata(metadata map[string]any) PromptOption
WithMetadata sets arbitrary metadata for the prompt.
type PromptingOption¶added inv0.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 inv0.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 inv0.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 inv0.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 inv0.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 inv0.3.0
type RankedDocumentData struct { Content []*Part `json:"content,omitempty"` Metadata *RankedDocumentMetadata `json:"metadata,omitempty"` }
type RankedDocumentMetadata¶added inv0.3.0
type RankedDocumentMetadata struct { Score float64 `json:"score,omitempty"` }
type RerankerRequest¶added inv0.3.0
type RerankerResponse¶added inv0.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¶
LookupRetriever looks up a Retriever registered by DefineRetriever. It returns nil if the retriever was not defined.
type RetrieverFunc¶added inv0.5.0
type RetrieverFunc = func(context.Context, *RetrieverRequest) (*RetrieverResponse, error)
type RetrieverOption¶added inv0.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 RetrieverResponse¶
type RetrieverResponse struct { Documents []*Document `json:"documents,omitempty"` }
func Retrieve¶added inv0.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 inv0.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 inv0.3.0
type ScoreDetails struct { Reasoning string `json:"reasoning,omitempty"` }
type ScoreStatus¶added inv0.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 inv0.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
type ToolChoice¶added inv0.3.0
type ToolChoice string
const ( ToolChoiceAuto ToolChoice = "auto" ToolChoiceRequired ToolChoice = "required" ToolChoiceNone ToolChoice = "none" )
type ToolConfig¶added inv0.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 inv0.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 inv0.1.0
A ToolDef is an implementation of a single tool.
func (*ToolDef[In, Out]) Definition¶added inv0.1.0
func (t *ToolDef[In, Out]) Definition() *ToolDefinition
Definition returns ToolDefinition for for this tool.
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 inv0.3.0
ToolInterruptError represents an intentional interruption of tool execution.
func (*ToolInterruptError) Error¶added inv0.3.0
func (e *ToolInterruptError) Error() string
type ToolName¶added inv0.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 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 inv0.3.0
type TraceMetadata struct { FeatureName string `json:"featureName,omitempty"` Paths []*PathMetadata `json:"paths,omitempty"` Timestamp float64 `json:"timestamp,omitempty"` }