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// ThinkingConfig represents thinking configuration for the Google provider.
 35type ThinkingConfig struct {
 36	ThinkingBudget  *int64 `json:"thinking_budget"`
 37	IncludeThoughts *bool  `json:"include_thoughts"`
 38}
 39
 40// ReasoningMetadata represents reasoning metadata for the Google provider.
 41type ReasoningMetadata struct {
 42	Signature string `json:"signature"`
 43	ToolID    string `json:"tool_id"`
 44}
 45
 46// Options implements the ProviderOptionsData interface for ReasoningMetadata.
 47func (m *ReasoningMetadata) Options() {}
 48
 49// MarshalJSON implements custom JSON marshaling with type info for ReasoningMetadata.
 50func (m ReasoningMetadata) MarshalJSON() ([]byte, error) {
 51	type plain ReasoningMetadata
 52	return fantasy.MarshalProviderType(TypeReasoningMetadata, plain(m))
 53}
 54
 55// UnmarshalJSON implements custom JSON unmarshaling with type info for ReasoningMetadata.
 56func (m *ReasoningMetadata) UnmarshalJSON(data []byte) error {
 57	type plain ReasoningMetadata
 58	var p plain
 59	if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
 60		return err
 61	}
 62	*m = ReasoningMetadata(p)
 63	return nil
 64}
 65
 66// SafetySetting represents safety settings for the Google provider.
 67type SafetySetting struct {
 68	// 'HARM_CATEGORY_UNSPECIFIED',
 69	// 'HARM_CATEGORY_HATE_SPEECH',
 70	// 'HARM_CATEGORY_DANGEROUS_CONTENT',
 71	// 'HARM_CATEGORY_HARASSMENT',
 72	// 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
 73	// 'HARM_CATEGORY_CIVIC_INTEGRITY',
 74	Category string `json:"category"`
 75
 76	// 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
 77	// 'BLOCK_LOW_AND_ABOVE',
 78	// 'BLOCK_MEDIUM_AND_ABOVE',
 79	// 'BLOCK_ONLY_HIGH',
 80	// 'BLOCK_NONE',
 81	// 'OFF',
 82	Threshold string `json:"threshold"`
 83}
 84
 85// ProviderOptions represents additional options for the Google provider.
 86type ProviderOptions struct {
 87	ThinkingConfig *ThinkingConfig `json:"thinking_config"`
 88
 89	// Optional.
 90	// The name of the cached content used as context to serve the prediction.
 91	// Format: cachedContents/{cachedContent}
 92	CachedContent string `json:"cached_content"`
 93
 94	// Optional. A list of unique safety settings for blocking unsafe content.
 95	SafetySettings []SafetySetting `json:"safety_settings"`
 96	// 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
 97	// 'BLOCK_LOW_AND_ABOVE',
 98	// 'BLOCK_MEDIUM_AND_ABOVE',
 99	// 'BLOCK_ONLY_HIGH',
100	// 'BLOCK_NONE',
101	// 'OFF',
102	Threshold string `json:"threshold"`
103}
104
105// Options implements the ProviderOptionsData interface for ProviderOptions.
106func (o *ProviderOptions) Options() {}
107
108// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
109func (o ProviderOptions) MarshalJSON() ([]byte, error) {
110	type plain ProviderOptions
111	return fantasy.MarshalProviderType(TypeProviderOptions, plain(o))
112}
113
114// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
115func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
116	type plain ProviderOptions
117	var p plain
118	if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
119		return err
120	}
121	*o = ProviderOptions(p)
122	return nil
123}
124
125// ParseOptions parses provider options from a map for the Google provider.
126func ParseOptions(data map[string]any) (*ProviderOptions, error) {
127	var options ProviderOptions
128	if err := fantasy.ParseOptions(data, &options); err != nil {
129		return nil, err
130	}
131	return &options, nil
132}