Documentation
¶
Index ¶
- Constants
- func AutoToolChoice() func(Turn) ToolChoice
- func EstimateTokens(text string) int
- func ForceToolChoice(name string) func(Turn) ToolChoice
- func MarshalJSON(v any) (json.RawMessage, error)
- func MustMarshalJSON(v any) json.RawMessage
- func NoToolChoice() func(Turn) ToolChoice
- func RegisterModelPricing(provider ProviderID, model string, p ModelPricing)
- func RequireToolChoice() func(Turn) ToolChoice
- type CacheControl
- type CacheMode
- type Client
- type Document
- type Image
- type JSONSchema
- type Message
- type ModelPricing
- type Options
- type Part
- func DocumentText(name, mediaType, text string) *Part
- func ImageBytes(mediaType string, data []byte) *Part
- func ImageURL(mediaType, url string) *Part
- func RedactedThinking(data string) *Part
- func Text(s string) *Part
- func Thinking(thinking, signature string) *Part
- func ToolCallPart(call ToolCall) *Part
- func ToolResultPart(res ToolResult) *Part
- type PartType
- type Price
- type ProviderID
- type RedactedThinkingBlock
- type Request
- type Response
- type ResponseFormat
- type ResponseFormatType
- type Role
- type ThinkingBlock
- type Tool
- type ToolCall
- type ToolChoice
- type ToolChoiceType
- type ToolResult
- type Turn
- type Usage
Constants ¶
const PriceDenomination = 100_000_000_000
Variables ¶
This section is empty.
Functions ¶
func AutoToolChoice ¶
func AutoToolChoice() func(Turn) ToolChoice
func EstimateTokens ¶
EstimateTokens returns an approximate token count for the given text. Uses an older GPT-style BPE tokenizer. The count is approximate because: - Different models use different tokenizers - This tokenizer is from an older OpenAI model Generally overestimates slightly, which is fine for threshold checks.
func ForceToolChoice ¶
func ForceToolChoice(name string) func(Turn) ToolChoice
func MarshalJSON ¶
func MarshalJSON(v any) (json.RawMessage, error)
func MustMarshalJSON ¶
func MustMarshalJSON(v any) json.RawMessage
func NoToolChoice ¶
func NoToolChoice() func(Turn) ToolChoice
func RegisterModelPricing ¶
func RegisterModelPricing(provider ProviderID, model string, p ModelPricing)
RegisterModelPricing overrides pricing for a (provider, model) pair. Model matching is exact against the normalized model string (trimmed, lowercased).
func RequireToolChoice ¶
func RequireToolChoice() func(Turn) ToolChoice
Types ¶
type CacheControl ¶
type CacheControl string
const ( CacheDefault CacheControl = "" CacheEphemeral CacheControl = "ephemeral" )
type CacheMode ¶
type CacheMode int
func ParseCacheMode ¶
func (CacheMode) MarshalText ¶
func (*CacheMode) UnmarshalText ¶
type JSONSchema ¶
type JSONSchema struct {
Name string
Schema json.RawMessage
Strict bool
}
type Message ¶
type Message struct {
Role Role
Name string
Parts []*Part
// IsCacheBreakpoint marks this message as a prompt-caching breakpoint (if supported by the provider).
// When Request.CacheMode != CacheModeNone, providers may translate this into their own cache breakpoint markers.
IsCacheBreakpoint bool
}
func AssistantMsg ¶
func ToolResultMsg ¶
func ToolResultMsg(res ToolResult) Message
type ModelPricing ¶
type ModelPricing struct {
InputTokenPrice Price
OutputTokenPrice Price
// Cached input token prices (if the provider reports these token types).
CacheReadInputTokenPrice Price
CacheCreationInputTokenPrice Price
}
ModelPricing defines per-token prices for a model.
Price is in 1/1_000_000_000 of a cent (see usage.go).
func LookupModelPricing ¶
func LookupModelPricing(provider ProviderID, model string) (ModelPricing, bool)
type Options ¶
type Options struct {
Model string
MaxOutputTokens int
Temperature float64
TopP float64
Stop []string
Tools []*Tool
ToolChoice func(Turn) ToolChoice
ResponseFormat ResponseFormat
}
func DefaultOptions ¶
func DefaultOptions() Options
type Part ¶
type Part struct {
Type PartType
Text string
Image *Image
Document *Document
Thinking *ThinkingBlock
Redacted *RedactedThinkingBlock
ToolCall *ToolCall
ToolResult *ToolResult
CacheControl CacheControl
}
func DocumentText ¶
func ImageBytes ¶
func RedactedThinking ¶
func ToolCallPart ¶
func ToolResultPart ¶
func ToolResultPart(res ToolResult) *Part
func (*Part) WithCacheControl ¶
func (p *Part) WithCacheControl(c CacheControl) *Part
type Price ¶
type Price int64
Price is an amount in 1/1_000_000_000 of a cent (1e-9 cent).
I.e. $2 per 1M tokens = $0.002 per 1K tokens = Price(200_000) per token.
func EstimateCost ¶
func EstimateCost(provider ProviderID, model string, u Usage) (Price, bool)
EstimateCost returns the estimated cost for a response (usage + model pricing). If the model pricing is unknown, it returns (0, false).
type ProviderID ¶
type ProviderID string
const ( ProviderOpenAI ProviderID = "openai" ProviderAnthropic ProviderID = "anthropic" ProviderGemini ProviderID = "gemini" )
type RedactedThinkingBlock ¶
type RedactedThinkingBlock struct {
Data string
}
type Request ¶
type Request struct {
Turn Turn
Messages []Message
Options Options
Metadata map[string]string
// ConfigureRequest is an optional hook invoked by provider clients that use
// httpcall (OpenAI/Anthropic/Gemini) right before executing the request.
//
// This allows call sites to attach logging, metrics, retry tweaks, etc.
ConfigureRequest func(r *httpcall.Request)
// CacheMode controls provider-specific prompt caching behavior (if supported).
CacheMode CacheMode
// ThinkingBudget enables provider-specific extended thinking (if supported).
// Zero disables extended thinking.
ThinkingBudget int
}
type Response ¶
type ResponseFormat ¶
type ResponseFormat struct {
Type ResponseFormatType
JSONSchema *JSONSchema
}
type ResponseFormatType ¶
type ResponseFormatType string
const ( ResponseFormatText ResponseFormatType = "text" ResponseFormatJSONObject ResponseFormatType = "json_object" ResponseFormatJSONSchema ResponseFormatType = "json_schema" )
type ThinkingBlock ¶
type Tool ¶
type Tool struct {
Name string
Description string
// InputSchema is a JSON Schema object.
InputSchema json.RawMessage
// Run executes the tool.
//
// If you use tool calling with agent.Run, this function is invoked whenever
// the model requests a tool call with Tool.Name.
//
// It should typically return a ToolResult with ToolCallID set to call.ID
// and Name set to call.Name (but agent.Run fills missing fields
// defensively).
Run func(ctx context.Context, call ToolCall) (ToolResult, error)
}
type ToolCall ¶
type ToolCall struct {
ID string
Name string
Input json.RawMessage
ThoughtSignature string
}
func (*ToolCall) UnmarshalInput ¶
type ToolChoice ¶
type ToolChoice struct {
Type ToolChoiceType
Name string
}
type ToolChoiceType ¶
type ToolChoiceType string
const ( ToolChoiceAuto ToolChoiceType = "auto" ToolChoiceNone ToolChoiceType = "none" ToolChoiceRequired ToolChoiceType = "required" ToolChoiceTool ToolChoiceType = "tool" )