Documentation ¶
Index ¶
- func DefineRetriever(provider, name string, ...) *retrieverActionDef
- func GenerateText(ctx context.Context, m Model, opts ...GenerateOption) (string, error)
- func Index(ctx context.Context, r Indexer, opts ...IndexerOption) error
- func IsDefinedEmbedder(provider, name string) bool
- func IsDefinedIndexer(provider, name string) bool
- func IsDefinedModel(provider, name string) bool
- func IsDefinedPrompt(provider, name string) bool
- func IsDefinedRetriever(provider, name string) bool
- type Candidate
- type Document
- type DocumentEmbedding
- type EmbedOption
- type EmbedRequest
- type EmbedResponse
- type Embedder
- type FinishReason
- type GenerateOption
- func WithCandidates(c int) GenerateOption
- func WithConfig(config any) GenerateOption
- func WithContext(c ...any) GenerateOption
- func WithHistory(history ...*Message) GenerateOption
- func WithMessages(messages ...*Message) GenerateOption
- func WithOutputFormat(format OutputFormat) GenerateOption
- func WithOutputSchema(schema any) GenerateOption
- func WithStreaming(cb ModelStreamingCallback) GenerateOption
- func WithSystemPrompt(prompt string) GenerateOption
- func WithTextPrompt(prompt string) GenerateOption
- func WithTools(tools ...Tool) GenerateOption
- type GenerateRequest
- type GenerateRequestOutput
- type GenerateResponse
- type GenerateResponseChunk
- type GenerationCommonConfig
- type GenerationUsage
- type Indexer
- type IndexerOption
- type IndexerRequest
- 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 NewUserTextMessage(text string) *Message
- type Model
- type ModelCapabilities
- type ModelInfo
- type ModelInfoSupports
- type ModelMetadata
- type ModelStreamingCallback
- type OutputFormat
- type Part
- type PartKind
- type Prompt
- type RetrieveOption
- type Retriever
- type RetrieverRequest
- type RetrieverResponse
- type Role
- type Tool
- type ToolDef
- type ToolDefinition
- type ToolRequest
- type ToolResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefineRetriever ¶
func DefineRetriever(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
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 IsDefinedIndexer ¶ added in v0.1.0
IsDefinedIndexer reports whether an Indexer is defined.
func IsDefinedModel ¶ added in v0.1.0
IsDefinedModel reports whether a model is defined.
func IsDefinedPrompt ¶ added in v0.1.0
IsDefinedPrompt reports whether a Prompt is defined.
func IsDefinedRetriever ¶ added in v0.1.0
IsDefinedRetriever reports whether a Retriever is defined.
Types ¶
type Candidate ¶
type Candidate struct { Custom any `json:"custom,omitempty"` FinishMessage string `json:"finishMessage,omitempty"` FinishReason FinishReason `json:"finishReason,omitempty"` Index int `json:"index"` Message *Message `json:"message,omitempty"` Usage *GenerationUsage `json:"usage,omitempty"` }
A Candidate is one of several possible generated responses from a generation request. It contains a single generated message along with additional metadata about its generation. A generation request may result in multiple Candidates.
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 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(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 FinishReason ¶
type FinishReason string
FinishReason is the reason why a model stopped generating tokens.
const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonBlocked FinishReason = "blocked" FinishReasonOther FinishReason = "other" FinishReasonUnknown FinishReason = "unknown" )
type GenerateOption ¶ added in v0.1.0
type GenerateOption func(req *generateParams) error
GenerateOption configures params of the Generate call.
func WithCandidates ¶ added in v0.1.0
func WithCandidates(c int) GenerateOption
WithCandidates adds provided candidate count to GenerateRequest.
func WithConfig ¶ added in v0.1.0
func WithConfig(config any) GenerateOption
WithConfig adds provided config to GenerateRequest.
func WithContext ¶ added in v0.1.0
func WithContext(c ...any) GenerateOption
WithContext adds provided context to GenerateRequest.
func WithHistory ¶ added in v0.1.0
func WithHistory(history ...*Message) GenerateOption
WithHistory adds provided history messages to the begining of GenerateRequest.Messages. History messages will always be put first in the list of messages, with the exception of system prompt which will always be first. WithMessages and WithTextPrompt will insert messages after system prompt and history.
func WithMessages ¶ added in v0.1.0
func WithMessages(messages ...*Message) GenerateOption
WithMessages adds provided messages to GenerateRequest.
func WithOutputFormat ¶ added in v0.1.0
func WithOutputFormat(format OutputFormat) GenerateOption
WithOutputFormat adds provided output format to GenerateRequest.
func WithOutputSchema ¶ added in v0.1.0
func WithOutputSchema(schema any) GenerateOption
WithOutputSchema adds provided output schema to GenerateRequest.
func WithStreaming ¶ added in v0.1.0
func WithStreaming(cb ModelStreamingCallback) GenerateOption
WithStreaming adds a streaming callback to the generate request.
func WithSystemPrompt ¶ added in v0.1.0
func WithSystemPrompt(prompt string) GenerateOption
WithSystemPrompt adds a simple text system prompt as the first message in GenerateRequest. System prompt will always be put first in the list of messages.
func WithTextPrompt ¶ added in v0.1.0
func WithTextPrompt(prompt string) GenerateOption
WithTextPrompt adds a simple text user prompt to GenerateRequest.
func WithTools ¶ added in v0.1.0
func WithTools(tools ...Tool) GenerateOption
WithTools adds provided tools to GenerateRequest.
type GenerateRequest ¶
type GenerateRequest struct { // Candidates indicates the number of responses the model should generate. // Normally this would be set to 1. Candidates int `json:"candidates,omitempty"` Config any `json:"config,omitempty"` Context []any `json:"context,omitempty"` // Messages is a list of messages to pass to the model. The first n-1 Messages // are treated as history. The last Message is the current request. Messages []*Message `json:"messages,omitempty"` // Output describes the desired response format. Output *GenerateRequestOutput `json:"output,omitempty"` // Tools lists the available tools that the model can ask the client to run. Tools []*ToolDefinition `json:"tools,omitempty"` }
A GenerateRequest is a request to generate completions from a model.
func NewGenerateRequest ¶
func NewGenerateRequest(config any, messages ...*Message) *GenerateRequest
NewGenerateRequest create a new GenerateRequest with provided config and messages.
type GenerateRequestOutput ¶
type GenerateRequestOutput struct { Format OutputFormat `json:"format,omitempty"` Schema map[string]any `json:"schema,omitempty"` }
GenerateRequestOutput 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 GenerateResponse ¶
type GenerateResponse struct { // Candidates are the requested responses from the model. The length of this // slice will be equal to [GenerateRequest.Candidates]. Candidates []*Candidate `json:"candidates,omitempty"` Custom any `json:"custom,omitempty"` // LatencyMs is the time the request took in milliseconds. LatencyMs float64 `json:"latencyMs,omitempty"` // Request is the [GenerateRequest] struct used to trigger this response. Request *GenerateRequest `json:"request,omitempty"` // Usage describes how many resources were used by this generation request. Usage *GenerationUsage `json:"usage,omitempty"` }
A GenerateResponse is a model's response to a GenerateRequest.
func Generate ¶ added in v0.1.0
func Generate(ctx context.Context, m Model, opts ...GenerateOption) (*GenerateResponse, error)
Generate run generate request for this model. Returns GenerateResponse struct.
func GenerateData ¶ added in v0.1.0
func GenerateData(ctx context.Context, m Model, value any, opts ...GenerateOption) (*GenerateResponse, error)
Generate run generate request for this model. Returns GenerateResponse struct. TODO: Stream GenerateData with partial JSON
func (*GenerateResponse) History ¶ added in v0.1.0
func (gr *GenerateResponse) History() []*Message
History returns messages from the request combined with the reponse message to represent the conversation history.
func (*GenerateResponse) Text ¶
func (gr *GenerateResponse) Text() string
Text returns the contents of the first candidate in a GenerateResponse as a string. It returns an empty string if there are no candidates or if the candidate has no message.
func (*GenerateResponse) UnmarshalOutput ¶ added in v0.1.0
func (gr *GenerateResponse) UnmarshalOutput(v any) error
UnmarshalOutput unmarshals structured JSON output into the provided struct pointer.
type GenerateResponseChunk ¶
type GenerateResponseChunk struct { Content []*Part `json:"content,omitempty"` Custom any `json:"custom,omitempty"` Index int `json:"index,omitempty"` }
A GenerateResponseChunk is the portion of the GenerateResponse that is passed to a streaming callback.
func (*GenerateResponseChunk) Text ¶
func (c *GenerateResponseChunk) Text() string
Text returns the text content of the GenerateResponseChunk as a string. It returns an error if there is no Content in the response chunk.
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"` InputCharacters int `json:"inputCharacters,omitempty"` InputImages int `json:"inputImages,omitempty"` InputTokens int `json:"inputTokens,omitempty"` OutputCharacters int `json:"outputCharacters,omitempty"` OutputImages int `json:"outputImages,omitempty"` OutputTokens int `json:"outputTokens,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 retriever.
func DefineIndexer ¶
func DefineIndexer(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:"docs"` 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 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 NewUserTextMessage ¶
NewUserTextMessage creates a new Message with role "user" and content with a single text part with the content of provided text.
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 *GenerateRequest, cb ModelStreamingCallback) (*GenerateResponse, error) }
Model represents a model that can perform content generation tasks.
func DefineModel ¶
func DefineModel(provider, name string, metadata *ModelMetadata, generate func(context.Context, *GenerateRequest, ModelStreamingCallback) (*GenerateResponse, error)) Model
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.
type ModelCapabilities ¶
type ModelCapabilities struct { Multiturn bool // the model can handle multiple request-response interactions Media bool // the model supports media as well as text input Tools bool // the model supports tools SystemRole bool // the model supports a system prompt or role }
ModelCapabilities describes various capabilities of the model.
type ModelInfo ¶
type ModelInfo struct { Label string `json:"label,omitempty"` Supports *ModelInfoSupports `json:"supports,omitempty"` Versions []string `json:"versions,omitempty"` }
type ModelInfoSupports ¶
type ModelMetadata ¶
type ModelMetadata struct { Label string Supports ModelCapabilities }
ModelMetadata is the metadata of the model, specifying things like nice user-visible label, capabilities, etc.
type ModelStreamingCallback ¶
type ModelStreamingCallback = func(context.Context, *GenerateResponseChunk) error
ModelStreamingCallback is the type for the streaming callback of a model.
type OutputFormat ¶
type OutputFormat string
OutputFormat is the format that the model's output should produce.
const ( OutputFormatJSON OutputFormat = "json" OutputFormatText OutputFormat = "text" OutputFormatMedia OutputFormat = "media" )
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:"toolreq,omitempty"` // valid for kind==partToolRequest ToolResponse *ToolResponse `json:"toolresp,omitempty"` // valid for kind==partToolResponse }
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.
type Prompt ¶
type Prompt core.Action[any, *GenerateRequest, struct{}]
A Prompt is used to render a prompt template, producing a GenerateRequest that may be passed to a Model.
func DefinePrompt ¶
func DefinePrompt(provider, name string, metadata map[string]any, inputSchema *jsonschema.Schema, render func(context.Context, any) (*GenerateRequest, error)) *Prompt
DefinePrompt takes a function that renders a prompt template into a GenerateRequest that may be passed to a Model. The prompt expects some input described by inputSchema. DefinePrompt registers the function as an action, and returns a Prompt that runs it.
func LookupPrompt ¶
LookupPrompt looks up a Prompt registered by DefinePrompt. It returns nil if the prompt was not defined.
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 model was not defined.
type RetrieverRequest ¶
type RetrieverRequest struct { Document *Document `json:"content"` 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 Tool ¶
type Tool interface { // Definition returns ToolDefinition for for this tool. Definition() *ToolDefinition // Action returns the action instance that backs this tools. Action() action.Action // RunRaw runs this tool using the provided raw map format data (JSON parsed // as map[string]any). RunRaw(ctx context.Context, input map[string]any) (any, error) }
Tool represents an instance of a tool.
func LookupTool ¶ added in v0.1.0
LookupTool looks up the tool in the registry by provided name and returns it.
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](name, description string, fn func(ctx context.Context, input In) (Out, error)) *ToolDef[In, Out]
DefineTool defines a tool function.
func (*ToolDef[In, Out]) Action ¶ added in v0.1.0
Action returns the action instance that backs this tools.
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"` 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 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 map[string]any `json:"input,omitempty"` Name string `json:"name,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 map[string]any `json:"output,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.