Documentation
¶
Index ¶
- func DefineTool(definition *ToolDefinition, metadata map[string]any, ...)
- func RunTool(ctx context.Context, name string, input map[string]any) (map[string]any, error)
- type Candidate
- type Document
- type DocumentEmbedding
- type EmbedRequest
- type EmbedResponse
- type Embedder
- type FinishReason
- type GenerateRequest
- type GenerateRequestOutput
- type GenerateResponse
- type GenerateResponseChunk
- type GenerationCommonConfig
- type GenerationUsage
- type Indexer
- 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 Retriever
- type RetrieverRequest
- type RetrieverResponse
- type Role
- type Tool
- type ToolDefinition
- type ToolRequest
- type ToolResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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"` }
type Embedder ¶
type Embedder core.Action[*EmbedRequest, *EmbedResponse, struct{}]
An Embedder is used to convert a document to a multidimensional vector.
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 [EmbedderAction] that runs it.
func LookupEmbedder ¶
LookupEmbedder looks up an [EmbedderAction] registered by DefineEmbedder. It returns nil if the embedder was not defined.
func (*Embedder) Embed ¶
func (e *Embedder) Embed(ctx context.Context, req *EmbedRequest) (*EmbedResponse, error)
Embed runs the given Embedder.
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 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 (*GenerateResponse) Text ¶
func (gr *GenerateResponse) Text() (string, error)
Text returns the contents of the first candidate in a GenerateResponse as a string. It returns an error if there are no candidates or if the candidate has no message.
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, error)
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 core.Action[*IndexerRequest, struct{}, struct{}]
An Indexer is used to index documents in a store.
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 a Indexer registered by DefineIndexer. It returns nil if the model was not defined.
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 core.Action[*GenerateRequest, *GenerateResponse, *GenerateResponseChunk]
A Model is used to generate content from an AI model.
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 [ModelAction] that runs it.
func LookupModel ¶
LookupModel looks up a [ModelAction] registered by DefineModel. It returns nil if the model was not defined.
func (*Model) Generate ¶
func (m *Model) Generate(ctx context.Context, req *GenerateRequest, cb ModelStreamingCallback) (*GenerateResponse, error)
Generate applies the Model to some input, handling tool requests.
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 Retriever ¶
type Retriever core.Action[*RetrieverRequest, *RetrieverResponse, struct{}]
A Retriever is used to retrieve indexed documents.
func DefineRetriever ¶
func DefineRetriever(provider, name string, ret func(context.Context, *RetrieverRequest) (*RetrieverResponse, error)) *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 model was not defined.
func (*Retriever) Retrieve ¶
func (r *Retriever) Retrieve(ctx context.Context, req *RetrieverRequest) (*RetrieverResponse, error)
Retrieve runs the given Retriever.
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.
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 struct { Definition *ToolDefinition Fn func(context.Context, map[string]any) (map[string]any, error) }
A Tool is an implementation of a single tool. The ToolDefinition has JSON schemas that describe the types. TODO: This should be generic over the function input and output types, and something in the general code should handle the JSON conversion.
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.