Documentation
¶
Index ¶
- func DefineGenerateAction(ctx context.Context, r *registry.Registry) *generateAction
- func DefineRetriever(r *registry.Registry, provider, name string, ...) *retrieverActionDef
- func GenerateText(ctx context.Context, r *registry.Registry, opts ...GenerateOption) (string, error)
- func Index(ctx context.Context, r Indexer, opts ...IndexerOption) error
- func IsDefinedEmbedder(r *registry.Registry, provider, name string) bool
- func IsDefinedEvaluator(r *registry.Registry, provider, name string) bool
- func IsDefinedIndexer(r *registry.Registry, provider, name string) bool
- func IsDefinedRetriever(r *registry.Registry, provider, name string) bool
- func LoadPromptDir(r *registry.Registry, dir string, namespace string) error
- type AugmentWithContextOptions
- type BaseDataPoint
- type BaseEvalDataPoint
- type CandidateError
- type CandidateErrorCode
- type CommonOption
- func WithConfig(config any) CommonOption
- func WithMaxTurns(maxTurns int) CommonOption
- func WithMessages(messages ...*Message) CommonOption
- func WithMessagesFn(fn messagesFn) CommonOption
- func WithMiddleware(middleware ...ModelMiddleware) CommonOption
- func WithModel(model Model) CommonOption
- func WithModelName(name string) CommonOption
- func WithReturnToolRequests(returnReqs bool) CommonOption
- func WithToolChoice(toolChoice ToolChoice) CommonOption
- func WithTools(tools ...ToolRef) CommonOption
- type CommonRerankerOptions
- type CommonRetrieverOptions
- type ConstrainedSupport
- type Dataset
- type Document
- type DocumentEmbedding
- type DownloadMediaOptions
- type EmbedOption
- type EmbedRequest
- type EmbedResponse
- type Embedder
- type Embedding
- type EvalFnResponse
- type EvalRequest
- type EvalResponse
- type EvalStatusEnum
- type EvaluateOption
- 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 EvaluatorOptions
- type EvaluatorRequest
- type EvaluatorResponse
- type Example
- type ExecutionOption
- type FinishReason
- type GenerateActionOptions
- type GenerateActionOutputConfig
- type GenerateActionResume
- type GenerateOption
- type GenerationCommonConfig
- type GenerationUsage
- type GenkitError
- type GenkitErrorData
- type GenkitErrorDetails
- type Indexer
- 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 Model
- type ModelAction
- type ModelFunc
- type ModelInfo
- type ModelMiddleware
- type ModelOutputConfig
- type ModelRequest
- type ModelResponse
- func Generate(ctx context.Context, r *registry.Registry, opts ...GenerateOption) (*ModelResponse, error)
- func GenerateData(ctx context.Context, r *registry.Registry, value any, opts ...GenerateOption) (*ModelResponse, error)
- func GenerateWithRequest(ctx context.Context, r *registry.Registry, opts *GenerateActionOptions, ...) (*ModelResponse, error)
- type ModelResponseChunk
- type ModelStage
- type ModelStreamCallback
- type ModelSupports
- type OutputFormat
- type OutputOption
- type Part
- 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 PromptGenerateOption
- type PromptOption
- type PromptingOption
- type RankedDocumentData
- type RankedDocumentMetadata
- type RerankerRequest
- type RerankerResponse
- type RetrieveOption
- type Retriever
- 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 ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefineGenerateAction ¶ added in v0.3.0
DefineGenerateAction defines a utility generate action.
func DefineRetriever ¶
func DefineRetriever(r *registry.Registry, provider, name string, ret func(context.Context, *RetrieverRequest) (*RetrieverResponse, error)) *retrieverActionDef
DefineRetriever registers the given retrieve function as an action, and returns a Retriever that runs it.
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 retrivers with provided options.
func IsDefinedEmbedder ¶ added in v0.1.0
IsDefinedEmbedder reports whether an embedder is defined.
func IsDefinedEvaluator ¶ added in v0.3.0
IsDefinedEvaluator reports whether an Evaluator is defined.
func IsDefinedIndexer ¶ added in v0.1.0
IsDefinedIndexer reports whether an Indexer is defined.
func IsDefinedRetriever ¶ added in v0.1.0
IsDefinedRetriever reports whether a Retriever is defined.
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 CommonOption ¶ added in v0.3.0
type CommonOption interface {
// contains filtered or unexported methods
}
func WithConfig ¶ added in v0.1.0
func WithConfig(config any) CommonOption
WithConfig sets the model configuration.
func WithMaxTurns ¶ added in v0.3.0
func WithMaxTurns(maxTurns int) CommonOption
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) CommonOption
WithMessages sets the messages. These messages will be sandwiched between the system and user messages.
func WithMessagesFn ¶ added in v0.3.0
func WithMessagesFn(fn messagesFn) CommonOption
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) CommonOption
WithMiddleware sets middleware to apply to the model request.
func WithModel ¶ added in v0.3.0
func WithModel(model Model) CommonOption
WithModel sets the model to call for generation.
func WithModelName ¶ added in v0.3.0
func WithModelName(name string) CommonOption
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) CommonOption
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) CommonOption
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) CommonOption
WithTools sets the tools to use for the generate request. Tools cannot be combined with WithToolChoice(ToolChoiceNone).
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 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.
type DocumentEmbedding ¶ added in v0.0.2
type DocumentEmbedding struct { // The vector for the embedding. Embedding []float32 `json:"embedding"` }
DocumentEmbedding holds emdedding information about a single document.
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 EmbedOption ¶ added in v0.1.0
type EmbedOption func(req *EmbedRequest) error
EmbedOption configures params of the Embed call.
func WithEmbedDocs ¶ added in v0.1.0
func WithEmbedDocs(docs ...*Document) EmbedOption
WithEmbedDocs adds documents to EmbedRequest
func WithEmbedOptions ¶ added in v0.1.0
func WithEmbedOptions(opts any) EmbedOption
WithEmbedOptions set embedder options on EmbedRequest
func WithEmbedText ¶ added in v0.1.0
func WithEmbedText(text ...string) EmbedOption
WithEmbedText adds simple text documents to EmbedRequest
type EmbedRequest ¶
type EmbedRequest struct { Documents []*Document `json:"input"` Options any `json:"options,omitempty"` }
EmbedRequest is the data we pass to convert one or more documents to a multidimensional vector.
type EmbedResponse ¶ added in v0.0.2
type EmbedResponse struct { // One embedding for each Document in the request, in the same order. Embeddings []*DocumentEmbedding `json:"embeddings"` }
func Embed ¶ added in v0.1.0
func Embed(ctx context.Context, e Embedder, opts ...EmbedOption) (*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 EvalFnResponse ¶ added in v0.3.0
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 EvaluateOption ¶ added in v0.3.0
type EvaluateOption func(req *EvaluatorRequest) error
EvaluateOption configures params of the Embed call.
func WithEvaluateDataset ¶ added in v0.3.0
func WithEvaluateDataset(dataset *Dataset) EvaluateOption
WithEvaluateDataset set the dataset on EvaluatorRequest
func WithEvaluateId ¶ added in v0.3.0
func WithEvaluateId(evaluationId string) EvaluateOption
WithEvaluateId set evaluation ID on EvaluatorRequest
func WithEvaluateOptions ¶ added in v0.3.0
func WithEvaluateOptions(opts any) EvaluateOption
WithEvaluateOptions set evaluator options on EvaluatorRequest
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
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 EvaluatorOptions ¶ added in v0.3.0
type EvaluatorRequest ¶ added in v0.3.0
type EvaluatorRequest struct { Dataset *Dataset `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 ...EvaluateOption) (*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 WithDocs ¶ added in v0.3.0
func WithDocs(docs ...*Document) ExecutionOption
WithDocs sets the retrieved documents to be used as context for the generate request.
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 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 GenerateActionResume ¶ added in v0.3.0
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 GenkitError ¶ added in v0.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 in v0.3.0
type GenkitErrorData struct { GenkitErrorDetails *GenkitErrorDetails `json:"genkitErrorDetails,omitempty"` GenkitErrorMessage string `json:"genkitErrorMessage,omitempty"` }
type GenkitErrorDetails ¶ added in v0.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 retriever.
func DefineIndexer ¶
func DefineIndexer(r *registry.Registry, provider, name string, index func(context.Context, *IndexerRequest) error) 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 IndexerOption ¶ added in v0.1.0
type IndexerOption func(req *IndexerRequest) error
IndexerOption configures params of the Index call.
func WithIndexerDocs ¶ added in v0.1.0
func WithIndexerDocs(docs ...*Document) IndexerOption
WithIndexerDoc adds a document to IndexRequest.
func WithIndexerOpts ¶ added in v0.1.0
func WithIndexerOpts(opts any) IndexerOption
WithIndexerOpts sets indexer options on IndexRequest.
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
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 in v0.3.0
NewUserMessageWithMetadata creates a new Message with role "user" with provided metadata and parts. Use NewUserTextMessage if you have a text-only message.
func NewUserTextMessage ¶
NewUserTextMessage creates a new Message with role "user" and content with a single text part with the content of provided text.
func (*Message) Text ¶ added in v0.3.0
Text returns the contents of a Message as a string. It returns an empty string if the message has no content.
func (*Message) WithCacheName ¶ added in v0.3.0
WithCacheName adds cache name to use in the generate request
func (*Message) WithCacheTTL ¶ added in v0.3.0
WithCacheTTL adds cache TTL configuration for the desired message
type 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 in v0.3.0
LookupModelByName looks up a Model registered by DefineModel. It returns an error if the model was not defined.
type ModelAction ¶ added in v0.3.0
type ModelAction = core.ActionDef[*ModelRequest, *ModelResponse, *ModelResponseChunk]
ModelAction is the type for model generation actions.
type ModelFunc ¶ added in v0.3.0
type ModelFunc = core.StreamingFunc[*ModelRequest, *ModelResponse, *ModelResponseChunk]
ModelFunc is a streaming function that takes in a ModelRequest and generates a ModelResponse, optionally streaming ModelResponseChunks.
type ModelInfo ¶
type ModelInfo struct { 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
type ModelMiddleware = core.Middleware[*ModelRequest, *ModelResponse, *ModelResponseChunk]
ModelMiddleware is middleware for model generate requests that takes in a ModelFunc, does something, then returns another ModelFunc.
func DownloadRequestMedia ¶ added in v0.3.0
func DownloadRequestMedia(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"` Instructions string `json:"instructions,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 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(ctx context.Context, r *registry.Registry, value any, opts ...GenerateOption) (*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
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 in v0.3.0
func (gr *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 (gr *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.
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 OutputFormat ¶
type OutputFormat string
const ( OutputFormatText OutputFormat = "text" OutputFormatJSON OutputFormat = "json" OutputFormatMedia OutputFormat = "media" )
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 WithOutputFormat ¶ added in v0.1.0
func WithOutputFormat(format OutputFormat) OutputOption
WithOutputFormat sets the format of the output.
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 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 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) 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 in v0.3.0
UnmarshalYAML implements yaml.Unmarshaler for Part.
type PathMetadata ¶ added in v0.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 in v0.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 in v0.3.0
func (p *Prompt) Execute(ctx context.Context, opts ...PromptGenerateOption) (*ModelResponse, error)
Execute renders a prompt, does variable substitution and passes the rendered template to the AI model specified by the prompt.
type PromptGenerateOption ¶ added in v0.3.0
type PromptGenerateOption interface {
// contains filtered or unexported methods
}
PromptGenerateOption is an option for executing a prompt. It applies only to prompt.Execute().
func WithInput ¶ added in v0.3.0
func WithInput(input any) PromptGenerateOption
WithInput sets the input for the prompt request.
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 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 WithPromptText ¶ added in v0.3.0
func WithPromptText(text string) PromptingOption
WithPromptText sets the user prompt message. The user prompt is always the last 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.
func WithSystemText ¶ added in v0.3.0
func WithSystemText(text string) PromptingOption
WithSystemText sets 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 RerankerResponse ¶ added in v0.3.0
type RerankerResponse struct {
Documents []*RankedDocumentData `json:"documents,omitempty"`
}
type RetrieveOption ¶ added in v0.1.0
type RetrieveOption func(req *RetrieverRequest) error
RetrieveOption configures params of the Retrieve call.
func WithRetrieverDoc ¶ added in v0.1.0
func WithRetrieverDoc(doc *Document) RetrieveOption
WithRetrieverDoc adds a document to RetrieveRequest.
func WithRetrieverOpts ¶ added in v0.1.0
func WithRetrieverOpts(opts any) RetrieveOption
WithRetrieverOpts retriever options to RetrieveRequest.
func WithRetrieverText ¶ added in v0.1.0
func WithRetrieverText(text string) RetrieveOption
WithRetrieverText adds a simple text as document to RetrieveRequest.
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 LookupRetriever ¶
LookupRetriever looks up a Retriever registered by DefineRetriever. It returns nil if the retriever was not defined.
type RetrieverRequest ¶
type RetrieverRequest struct { Query *Document `json:"query"` Options any `json:"options,omitempty"` }
RetrieverRequest is the data we pass to retrieve documents from the database. The Options field is specific to the actual retriever implementation.
type RetrieverResponse ¶
type RetrieverResponse struct {
Documents []*Document `json:"documents"`
}
RetrieverResponse is the response to a document lookup.
func Retrieve ¶ added in v0.1.0
func Retrieve(ctx context.Context, r Retriever, opts ...RetrieveOption) (*RetrieverResponse, error)
Retrieve calls the retrivers with 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.
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 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] struct { // contains filtered or unexported fields }
A ToolDef is an implementation of a single tool.
func DefineTool ¶
func DefineTool[In, Out any](r *registry.Registry, name, description string, fn func(ctx *ToolContext, input In) (Out, error)) *ToolDef[In, Out]
DefineTool defines a tool function with interrupt capability
func (*ToolDef[In, Out]) Definition ¶ added in v0.1.0
func (ta *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 in v0.3.0
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.
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"` }