Documentation
¶
Index ¶
- Constants
- Variables
- func Register(scheme string, creator NewFunc)
- type Action
- type BashCodeExecutionResult
- type Blob
- type BlobData
- type CallParams
- type Candidate
- type CodeExecutionResult
- type Compaction
- type Configurable
- type DocumentBuilder
- type DocumentData
- type DocumentType
- type EntityLabel
- type EntityLabels
- type Feature
- type Field
- type GenParams
- type GenResponse
- type GenVideoMask
- type GenVideoReferenceImage
- type GenVideoReferenceImages
- type Generated
- type Image
- type ImageBuilder
- type ImageData
- type ImageType
- type InputSchema
- type Model
- type MsgBuilder
- type NewFunc
- type Operation
- type OperationResponse
- type OutputImage
- type OutputImageMask
- type OutputVideo
- type Params
- type Part
- type RawMessage
- type ReferenceImage
- type ReferenceImageType
- type Restriction
- type Results
- type SafetyAttributes
- type SearchToolBm25Result
- type SearchToolRegexResult
- type Service
- type StopReason
- type StringEnum
- type TextEditorCodeExecutionResult
- type Thinking
- type Tool
- type ToolBase
- type ToolResult
- type ToolUse
- type ValueLimit
- type Video
- type VideoType
- type WaitParams
- type WebFetchResult
- type WebSearchResult
- type WebSearchResultItem
- type WebSearchTool
Constants ¶
const ( ToolWebSearch = "std/web_search" ToolWebFetch = "std/web_fetch" ToolCodeExecution = "std/code_execution" ToolBashCodeExecution = "std/bash_code_execution" ToolTextEditorCodeExecution = "std/text_editor_code_execution" ToolSearchToolRegex = "std/tool_search_tool_regex" ToolSearchToolBm25 = "std/tool_search_tool_bm25" )
all standard tool names start with "std/". The rest of the name should be unique and descriptive of the tool's function. For example, "std/web_search" for a web search tool, "std/code_execution" for a code execution tool, etc.
Variables ¶
var ( // ErrNotFound is returned when a requested resource is not found, such as a model // or an action that does not exist. ErrNotFound = errors.New("not found") // ErrUnknownScheme is returned when an unknown scheme is encountered in a URL. ErrUnknownScheme = errors.New("unknown scheme") )
Functions ¶
Types ¶
type Action ¶
type Action string
Action represents a specific operation that can be performed with a model, such as generating a video, editing an image, etc. The available actions may vary depending on the model and service being used. You can use the `Actions` method of a `Service` to get the list of supported actions for a given model, and then use the `Operation` method to get an `Operation` instance for a specific action.
type BashCodeExecutionResult ¶
type BashCodeExecutionResult struct {
}
type BlobData ¶
BlobData represents the raw data of a blob, which can be an image or a document. It provides methods to retrieve the raw bytes or the base64-encoded string of the data.
func BlobFromBase64 ¶
BlobFromBase64 creates a BlobData from a base64-encoded string.
func BlobFromRaw ¶
BlobFromRaw creates a BlobData from raw bytes.
type CallParams ¶
type CallParams interface {
// Set sets a parameter for the operation. You can call this method multiple
// times to set multiple parameters.
Set(name string, val any) CallParams
// BaseURL sets the base URL for the API endpoint.
BaseURL(string) CallParams
// Timeout sets a timeout for the API request. If the request takes longer than
// the specified duration, it will be aborted and an error will be returned.
Timeout(time.Duration) CallParams
}
type Candidate ¶
type Candidate interface {
Parts() int
Part(i int) Part
StopReason() StopReason
ToMsg() MsgBuilder
}
Candidate represents a single candidate response from the model. A GenResponse may contain multiple candidates, and the model may return them in different orders based on the sampling parameters used in the request.
type CodeExecutionResult ¶
type Compaction ¶
type Compaction struct {
Data string
}
type Configurable ¶
type Configurable interface {
// Schema returns the schema of the configurable object, which defines the
// parameters that can be set for the object.
Schema() InputSchema
// Params returns a `Params` that can be used to set parameters for the object.
Params() Params
}
Configurable represents an object that can be configured with parameters defined in an InputSchema.
type DocumentBuilder ¶
type DocumentBuilder interface {
From(mime DocumentType, displayName string, src io.Reader) (DocumentData, error)
FromLocal(mime DocumentType, fileName string) (DocumentData, error)
FromBase64(mime DocumentType, displayName string, base64 string) (DocumentData, error)
FromBytes(mime DocumentType, displayName string, data []byte) DocumentData
PlainText(text string) DocumentData
}
type DocumentData ¶
type DocumentData interface {
DocumentType() DocumentType
}
type DocumentType ¶
type DocumentType string
const ( DocPlainText DocumentType = "text/plain" DocPDF DocumentType = "application/pdf" )
type EntityLabel ¶
type EntityLabel struct {
// Optional. The label of the segmented entity.
Label string
// Optional. The confidence score of the detected label.
Score float32
}
An entity representing the segmented area.
type EntityLabels ¶
type EntityLabels interface {
// Len returns the number of detected entities.
Len() int
// At retrieves a detected entity by index.
At(i int) EntityLabel
}
type Feature ¶
type Feature int
Feature represents the capabilities of a Service. It is used to indicate which features are supported by a particular Service implementation, such as whether it supports the Gen API, GenStream API, or long-running operations.
Services can support multiple features, which can be combined using bitwise OR. For example, a Service that supports both the Gen API and long-running operations would have a Feature value of `FeatureGen | FeatureOperation`.
type GenParams ¶
type GenParams interface {
// Set sets a parameter with the given name and value. This can be used to set any
// parameter that is not explicitly covered by the other methods in this interface.
Set(name string, val any) GenParams
// System prompt.
//
// A system prompt is a way of providing context and instructions to AI, such
// as specifying a particular goal or role.
System(prompt ...string) GenParams
// Input messages.
//
// AI models are trained to operate on alternating `user` and `assistant`
// conversational turns. When creating a new `Message`, you specify the prior
// conversational turns with the `messages` parameter, and the model then generates
// the next `Message` in the conversation. Consecutive `user` or `assistant` turns
// in your request will be combined into a single turn.
//
// Each input message must be an object with a `role` and `content`. You can
// specify a single `user`-role message, or you can include multiple `user` and
// `assistant` messages.
//
// If the final message uses the `assistant` role, the response content will
// continue immediately from the content in that message. This can be used to
// constrain part of the model's response.
//
// Example with a single `user` message:
//
// “`json
// [{ "role": "user", "content": "Hello, AI" }]
// “`
//
// Example with multiple conversational turns:
//
// “`json
// [
//
// { "role": "user", "content": "Hello there." },
// { "role": "assistant", "content": "Hi, I'm AI. How can I help you?" },
// { "role": "user", "content": "Can you explain LLMs in plain English?" }
//
// ]
// “`
//
// Example with a partially-filled response from AI:
//
// “`json
// [
//
// {
// "role": "user",
// "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
// },
// { "role": "assistant", "content": "The best answer is (" }
//
// ]
// “`
//
// Each input message `content` may be either a single `string` or an array of
// content blocks, where each block has a specific `type`. Using a `string` for
// `content` is shorthand for an array of one content block of type `"text"`. The
// following input messages are equivalent:
//
// “`json
// { "role": "user", "content": "Hello, AI" }
// “`
//
// “`json
// { "role": "user", "content": [{ "type": "text", "text": "Hello, AI" }] }
// “`
//
// Note that if you want to include a system prompt, you can use the
// top-level `system` parameter — there is no `"system"` role for input messages in
// the Messages API.
//
// There is a limit of 100,000 messages in a single request.
Messages(msgs ...MsgBuilder) GenParams
// Reference of tools that the model may use. The tool can be:
// 1) User-defined tools defined with `ToolDef`.
// 2) Std-tools, e.g. WebSearchTool, etc.
//
// If you include `tools` in your API request, the model may return `tool_use`
// content blocks that represent the model's use of those tools. You can then run
// those tools using the tool input generated by the model and then optionally
// return results back to the model using `tool_result` content blocks.
Tools(tools ...ToolBase) GenParams
// The model that will complete your prompt.
Model(Model) GenParams
// The maximum number of tokens to generate before stopping.
//
// Note that our models may stop _before_ reaching this maximum. This parameter
// only specifies the absolute maximum number of tokens to generate.
//
// Different models have different maximum values for this parameter.
MaxOutputTokens(int64) GenParams
// Compact when the input tokens exceed the given value. When the model triggers
// compaction, it will return a compaction block in the response and discard part
// of the conversation history. The content of the compaction block is an opaque
// string that is passed back by the provider when the compaction is triggered.
// The format of the content is provider-specific.
Compact(maxInputTokens int64) GenParams
// Amount of randomness injected into the response.
//
// Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`
// for analytical / multiple choice, and closer to `1.0` for creative and
// generative tasks.
//
// Note that even with `temperature` of `0.0`, the results will not be fully
// deterministic.
Temperature(float64) GenParams
// Use nucleus sampling.
//
// In nucleus sampling, we compute the cumulative distribution over all the options
// for each subsequent token in decreasing probability order and cut it off once it
// reaches a particular probability specified by `top_p`. You should either alter
// `temperature` or `top_p`, but not both.
//
// Recommended for advanced use cases only. You usually only need to use
// `temperature`.
TopP(float64) GenParams
// BaseURL sets the base URL for the API endpoint.
BaseURL(string) GenParams
// Timeout sets a timeout for the API request. If the request takes longer than
// the specified duration, it will be aborted and an error will be returned.
Timeout(time.Duration) GenParams
}
GenParams is used to build the parameters for a generation request. It provides methods for setting various parameters such as the system prompt, input messages, tools, and generation parameters like `max_tokens`, `temperature`, etc. The methods in this interface return the GenParams itself, allowing for method chaining when building the parameters for a request.
type GenResponse ¶
GenResponse represents the response from a generation request. It contains one or more candidates, which are the different possible completions generated by the model for the given input and parameters.
type GenVideoMask ¶
type GenVideoMask any
GenVideoMask is a reference image with a mask mode for video generation.
type GenVideoReferenceImage ¶
type GenVideoReferenceImage struct {
// The reference image.
Image Image
// The type of the reference image, which defines how the reference
// image will be used to generate the video.
//
// ReferenceType = "ASSET":
// A reference image that provides assets to the generated video,
// such as the scene, an object, a character, etc.
//
// ReferenceType = "STYLE":
// A reference image that provides aesthetics including colors,
// lighting, texture, etc., to be used as the style of the generated video,
// such as 'anime', 'photography', 'origami', etc.
ReferenceType string
}
A reference image for video generation.
type GenVideoReferenceImages ¶
type GenVideoReferenceImages any
type Generated ¶
type Generated interface {
// contains filtered or unexported methods
}
Generated represents a generated image or video. It can be one of the following types:
- OutputVideo: represents a generated video, which is returned by GenVideo action.
- OutputImage: represents a generated image, which is returned by GenImage, EditImage, RecontextImage, UpscaleImage actions.
- OutputImageMask: represents a generated image mask with detected entity labels, which is returned by SegmentImage action.
type ImageBuilder ¶
type ImageBuilder interface {
From(mime ImageType, displayName string, src io.Reader) (ImageData, error)
FromLocal(mime ImageType, fileName string) (ImageData, error)
FromBase64(mime ImageType, displayName string, base64 string) (ImageData, error)
FromBytes(mime ImageType, displayName string, data []byte) ImageData
}
type InputSchema ¶
type InputSchema interface {
// Fields returns the list of fields defined in the schema.
Fields() []Field
// Restriction returns the `Restriction` for the parameter with the given name. It
// returns nil if there is no restriction for the parameter.
Restriction(name string) *Restriction
}
InputSchema represents the schema of `Params`.
type MsgBuilder ¶
type MsgBuilder interface {
Text(text string) MsgBuilder
Image(image ImageData) MsgBuilder
ImageURL(mime ImageType, url string) MsgBuilder
ImageFile(mime ImageType, fileID string) MsgBuilder
Doc(doc DocumentData) MsgBuilder
DocURL(mime DocumentType, url string) MsgBuilder
DocFile(mime DocumentType, fileID string) MsgBuilder
// Part is used to add a part of the GenResponse message to the content.
Part(Part) MsgBuilder
// Thinking is used to add a thinking block to the content. The content
// is an opaque string that is passed back by the provider when the thinking
// is triggered.
Thinking(Thinking) MsgBuilder
// ToolUse is used to add a tool use block to the content. The tool ID
// should be a unique identifier for the tool being used, and should
// match the ID used in ToolResult.
//
// The Input expects anything that can be marshaled to JSON, including
// RawMessage.
ToolUse(ToolUse) MsgBuilder
// ToolResult is used to add the result of a tool use to the content.
// The tool ID should match the ID used in ToolUse. The content depends
// on the tool. If IsError is true, the content will be treated as an
// error interface.
//
// For standard tools (those with names starting with "std/"), the Result
// should be a specific struct defined for that tool. For example, the web
// search tool expects a WebSearchResult struct.
//
// For non-standard tools, the content expects anything that can be marshaled
// to JSON, including RawMessage.
ToolResult(ToolResult) MsgBuilder
// Compaction is used to add a compaction block to the content. The content
// is an opaque string that is passed back by the provider when the compaction
// is triggered. The format of the content is provider-specific.
Compaction(data string) MsgBuilder
}
type Operation ¶
type Operation interface {
// InputSchema returns the schema for the input parameters of this operation. This
// schema defines the parameters that can be set for this operation, such as the
// type and name of each parameter. You can use this schema to understand what
// parameters are required or optional for this operation, and to set them correctly
// before calling the operation.
InputSchema() InputSchema
// CallParams creates a `CallParams` instance that can be used to set parameters for
// this operation. You can use the `Set` method of `CallParams` to set parameters by
// name and value, and then pass the `CallParams` to the `Call` method to start the
// operation.
CallParams() CallParams
// Call starts the operation with the given options. It returns an `OperationResponse`
// that can be used to check the status of the operation and retrieve results when
// it's done.
Call(ctx context.Context, params CallParams) (OperationResponse, error)
}
Operation represents a long-running task that may take some time to complete, such as generating a video or editing an image. You can use an `Operation` to set parameters for the action and then call it with a prompt to start the operation.
type OperationResponse ¶
type OperationResponse interface {
// Done returns true if the operation is completed.
Done() bool
// Results returns the result from the operation.
Results() Results
// WaitParams returns a `WaitParams` that can be used to set parameters for waiting
// on the operation.
WaitParams() WaitParams
// Wait waits for the operation to be completed. It repeatedly checks the status
// of the operation and calls the provided progress function with the current
// operation response. Once the operation is done, it returns the results.
Wait(ctx context.Context, __xgo_optional_params WaitParams) (Results, error)
}
OperationResponse represents the response from an `Operation`. It provides methods to check the status of the operation, retrieve results when it's done.
type OutputImage ¶
type OutputImage struct {
// The output image data.
Image
// Optional. Watermarked image, anti-hotlinking format.
Watermarked Image
// Optional. Responsible AI filter reason if the image is filtered out of the
// response.
RAIFilteredReason string
// Optional. Safety attributes of the image. Lists of RAI categories and their
// scores of each content.
SafetyAttributes *SafetyAttributes
// Optional. The rewritten prompt used for the image generation if the prompt
// enhancer is enabled.
EnhancedPrompt string
}
type OutputImageMask ¶
type OutputImageMask struct {
// The generated image mask.
Mask Image
// The detected entities on the segmented area.
Labels EntityLabels
}
type OutputVideo ¶
type Params ¶
type Params interface {
// Set sets a parameter for the operation. You can call this method multiple
// times to set multiple parameters.
Set(name string, val any) Params
}
Params represents the parameters that can be set.
type RawMessage ¶
type RawMessage = json.RawMessage
type ReferenceImage ¶
type ReferenceImage any
ReferenceImage is an interface that represents a generic reference image.
type ReferenceImageType ¶
type ReferenceImageType int
ReferenceImageType represents the type of a reference image, which defines how the reference image will be used.
const ( RawReferenceImage ReferenceImageType = iota MaskReferenceImage ControlReferenceImage StyleReferenceImage SubjectReferenceImage ContentReferenceImage )
type Restriction ¶
type Restriction struct {
// Limit defines the limit of the parameter value. It can be nil if there is
// no limit for the parameter.
Limit ValueLimit
// If NotAllowedIf is not nil, it indicates that the parameter is not allowed if
// the parameters in NotAllowedIf exist together.
NotAllowedIf []string
// If OptionalIf is not nil, it indicates that the parameter is optional only if the
// parameters in OptionalIf exist together. If OptionalIf is nil, the parameter is
// either required or optional based on the Required field.
OptionalIf []string
// Required indicates whether the parameter is required.
// If a parameter is required, it must be provided by the user.
Required bool
}
Restriction represents the restrictions on a parameter. It defines the limit of the parameter value, as well as the parameters that should or should not exist together with the parameter.
type Results ¶
type Results interface {
// XGo_Attr ($name) retrieves a property value from the results by name.
XGo_Attr(name string) any
// Len returns the number of generated images or videos.
Len() int
// At retrieves a generated image or video from the results by index.
// For GenVideo, returns *OutputVideo;
// For SegmentImage, returns *OutputImageMask;
// For GenImage, EditImage, RecontextImage, UpscaleImage, returns *OutputImage.
At(i int) Generated
}
Results represents the results of an `Operation`.
type SafetyAttributes ¶
type SearchToolBm25Result ¶
type SearchToolBm25Result struct {
}
type SearchToolRegexResult ¶
type SearchToolRegexResult struct {
}
type Service ¶
type Service interface {
// Features returns the capabilities of this Service, which indicates which
// features are supported by this Service implementation, such as whether it
// supports the Gen API, GenStream API, or long-running operations.
Features() Feature
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The Gen API can be used for either single queries or stateless multi-turn
// conversations.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
Gen(ctx context.Context, params GenParams) (GenResponse, error)
// Send a structured list of input messages with text and/or image content, and the
// model will generate the next message in the conversation.
//
// The GenStream API can be used for either single queries or stateless multi-turn
// conversations.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
GenStream(ctx context.Context, params GenParams) iter.Seq2[GenResponse, error]
// GenParams creates a `GenParams` that can be used to build the parameters for
// generation requests. This includes setting the system prompt, input messages,
// tools, and generation parameters like `max_tokens`, `temperature`, etc.
GenParams() GenParams
// Images returns an `ImageBuilder` that can be used to create image content.
Images() ImageBuilder
// Docs returns a `DocumentBuilder` that can be used to create document content.
Docs() DocumentBuilder
// UserMsg creates a message with the `user` role, which represents input from
// a user in a conversation. The content of the message can be built using the
// returned `MsgBuilder`.
UserMsg() MsgBuilder
// AssistantMsg creates a message with the `assistant` role, which represents
// output from the AI assistant in a conversation. The content of the message
// can be built using the returned `MsgBuilder`.
AssistantMsg() MsgBuilder
// WebSearchTool returns a reference to a standard web search tool that the model
// may use to perform web searches during a conversation.
WebSearchTool() WebSearchTool
// ToolDef defines a tool that the model may use. The tool is identified by a unique
// name, and has a description that explains its functionality and usage to the model.
// The tool definition can also include additional metadata or parameters that are
// relevant for the tool's operation. The model can then choose to use this tool in
// its response by including a `tool_use` content block with the corresponding tool
// name.
ToolDef(name string) Tool
// Tool returns a user-defined tool that the model may use, identified by its unique
// name. This is used to refer to a tool that has been defined with `ToolDef`.
Tool(name string) Tool
// contains filtered or unexported methods
}
type StopReason ¶
type StopReason string
StopReason represents the reason why the model stopped generating tokens. This can be used to determine whether the model stopped because it reached the end of the response, or because it hit a maximum token limit, or for some other reason.
const ( EndTurn StopReason = "end_turn" StopCompaction StopReason = "compaction" StopMaxTokens StopReason = "max_tokens" StopSequence StopReason = "stop_sequence" MalformedToolUse StopReason = "malformed_tool_use" PauseTurn StopReason = "pause_turn" Refusal StopReason = "refusal" Recitation StopReason = "recitation" ModelContextWindowExceeded StopReason = "model_context_window_exceeded" UnsupportedLanguage StopReason = "unsupported_language" Unspecified StopReason = "unspecified" )
type StringEnum ¶
type StringEnum struct {
Values []string
}
type TextEditorCodeExecutionResult ¶
type TextEditorCodeExecutionResult struct {
}
type ToolResult ¶
type ToolResult struct {
ID string // tool ID
Name string // tool Name
// The result of the tool use. The content depends on the tool.
// If IsError is true, the content should be treated as an error interface.
//
// For standard tools (those with names starting with "std/"), the Result
// should be a specific struct defined for that tool. For example, the web
// search tool expects a WebSearchResult struct.
Result any
IsError bool
Underlying any // for provider-specific extensions
}
type ValueLimit ¶
type ValueLimit interface {
// contains filtered or unexported methods
}
type WaitParams ¶
type WaitParams interface {
// BaseURL sets the base URL for the API endpoint.
BaseURL(string) WaitParams
// Timeout sets a timeout for the API request. If the request takes longer than
// the specified duration, it will be aborted and an error will be returned.
Timeout(time.Duration) WaitParams
// Progress sets a callback function that will be called with the current
// `OperationResponse` each time the operation status is checked. This can be
// used to provide progress updates to the user while waiting for the operation
// to complete.
Progress(func(OperationResponse)) WaitParams
}
WaitParams represents the parameters that can be set when waiting for an `Operation` to complete.
type WebFetchResult ¶
type WebSearchResult ¶
type WebSearchResult struct {
Result []WebSearchResultItem
Underlying any // for provider-specific extensions
}
type WebSearchResultItem ¶
type WebSearchTool ¶
type WebSearchTool interface {
ToolBase
MaxUses(int64) WebSearchTool
AllowedDomains(...string) WebSearchTool
BlockedDomains(...string) WebSearchTool
}