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