provider_options.go

  1// Package google provides an implementation of the fantasy AI SDK for Google's language models.
  2package google
  3
  4import (
  5	"encoding/json"
  6
  7	"charm.land/fantasy"
  8)
  9
 10// Global type identifiers for Google-specific provider data.
 11const (
 12	TypeProviderOptions   = Name + ".options"
 13	TypeReasoningMetadata = Name + ".reasoning_metadata"
 14)
 15
 16// Register Google provider-specific types with the global registry.
 17func init() {
 18	fantasy.RegisterProviderType(TypeProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
 19		var v ProviderOptions
 20		if err := json.Unmarshal(data, &v); err != nil {
 21			return nil, err
 22		}
 23		return &v, nil
 24	})
 25	fantasy.RegisterProviderType(TypeReasoningMetadata, func(data []byte) (fantasy.ProviderOptionsData, error) {
 26		var v ReasoningMetadata
 27		if err := json.Unmarshal(data, &v); err != nil {
 28			return nil, err
 29		}
 30		return &v, nil
 31	})
 32}
 33
 34// ThinkingLevel controls the amount of thinking a model does.
 35// Use this for Gemini 3+ models instead of ThinkingBudget.
 36// Mutually exclusive with ThinkingBudget.
 37type ThinkingLevel = string
 38
 39// Predefined thinking levels for the Google provider.
 40const (
 41	ThinkingLevelLow     ThinkingLevel = "LOW"
 42	ThinkingLevelMedium  ThinkingLevel = "MEDIUM"
 43	ThinkingLevelHigh    ThinkingLevel = "HIGH"
 44	ThinkingLevelMinimal ThinkingLevel = "MINIMAL"
 45)
 46
 47// ThinkingConfig represents thinking configuration for the Google provider.
 48type ThinkingConfig struct {
 49	ThinkingBudget  *int64  `json:"thinking_budget,omitempty"`
 50	IncludeThoughts *bool   `json:"include_thoughts,omitempty"`
 51	ThinkingLevel   *string `json:"thinking_level,omitempty"`
 52}
 53
 54// ReasoningMetadata represents reasoning metadata for the Google provider.
 55type ReasoningMetadata struct {
 56	Signature string `json:"signature"`
 57	ToolID    string `json:"tool_id"`
 58}
 59
 60// Options implements the ProviderOptionsData interface for ReasoningMetadata.
 61func (m *ReasoningMetadata) Options() {}
 62
 63// MarshalJSON implements custom JSON marshaling with type info for ReasoningMetadata.
 64func (m ReasoningMetadata) MarshalJSON() ([]byte, error) {
 65	type plain ReasoningMetadata
 66	return fantasy.MarshalProviderType(TypeReasoningMetadata, plain(m))
 67}
 68
 69// UnmarshalJSON implements custom JSON unmarshaling with type info for ReasoningMetadata.
 70func (m *ReasoningMetadata) UnmarshalJSON(data []byte) error {
 71	type plain ReasoningMetadata
 72	var p plain
 73	if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
 74		return err
 75	}
 76	*m = ReasoningMetadata(p)
 77	return nil
 78}
 79
 80// SafetySetting represents safety settings for the Google provider.
 81type SafetySetting struct {
 82	// 'HARM_CATEGORY_UNSPECIFIED',
 83	// 'HARM_CATEGORY_HATE_SPEECH',
 84	// 'HARM_CATEGORY_DANGEROUS_CONTENT',
 85	// 'HARM_CATEGORY_HARASSMENT',
 86	// 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
 87	// 'HARM_CATEGORY_CIVIC_INTEGRITY',
 88	Category string `json:"category"`
 89
 90	// 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
 91	// 'BLOCK_LOW_AND_ABOVE',
 92	// 'BLOCK_MEDIUM_AND_ABOVE',
 93	// 'BLOCK_ONLY_HIGH',
 94	// 'BLOCK_NONE',
 95	// 'OFF',
 96	Threshold string `json:"threshold"`
 97}
 98
 99// ProviderOptions represents additional options for the Google provider.
100type ProviderOptions struct {
101	ThinkingConfig *ThinkingConfig `json:"thinking_config"`
102
103	// Optional.
104	// The name of the cached content used as context to serve the prediction.
105	// Format: cachedContents/{cachedContent}
106	CachedContent string `json:"cached_content"`
107
108	// Optional. A list of unique safety settings for blocking unsafe content.
109	SafetySettings []SafetySetting `json:"safety_settings"`
110	// 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
111	// 'BLOCK_LOW_AND_ABOVE',
112	// 'BLOCK_MEDIUM_AND_ABOVE',
113	// 'BLOCK_ONLY_HIGH',
114	// 'BLOCK_NONE',
115	// 'OFF',
116	Threshold string `json:"threshold"`
117}
118
119// Options implements the ProviderOptionsData interface for ProviderOptions.
120func (o *ProviderOptions) Options() {}
121
122// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
123func (o ProviderOptions) MarshalJSON() ([]byte, error) {
124	type plain ProviderOptions
125	return fantasy.MarshalProviderType(TypeProviderOptions, plain(o))
126}
127
128// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
129func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
130	type plain ProviderOptions
131	var p plain
132	if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
133		return err
134	}
135	*o = ProviderOptions(p)
136	return nil
137}
138
139// ParseOptions parses provider options from a map for the Google provider.
140func ParseOptions(data map[string]any) (*ProviderOptions, error) {
141	var options ProviderOptions
142	if err := fantasy.ParseOptions(data, &options); err != nil {
143		return nil, err
144	}
145	return &options, nil
146}