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)
14
15// ThinkingConfig represents thinking configuration for the Google provider.
16type ThinkingConfig struct {
17 ThinkingBudget *int64 `json:"thinking_budget"`
18 IncludeThoughts *bool `json:"include_thoughts"`
19}
20
21// SafetySetting represents safety settings for the Google provider.
22type SafetySetting struct {
23 // 'HARM_CATEGORY_UNSPECIFIED',
24 // 'HARM_CATEGORY_HATE_SPEECH',
25 // 'HARM_CATEGORY_DANGEROUS_CONTENT',
26 // 'HARM_CATEGORY_HARASSMENT',
27 // 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
28 // 'HARM_CATEGORY_CIVIC_INTEGRITY',
29 Category string `json:"category"`
30
31 // 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
32 // 'BLOCK_LOW_AND_ABOVE',
33 // 'BLOCK_MEDIUM_AND_ABOVE',
34 // 'BLOCK_ONLY_HIGH',
35 // 'BLOCK_NONE',
36 // 'OFF',
37 Threshold string `json:"threshold"`
38}
39
40// ProviderOptions represents additional options for the Google provider.
41type ProviderOptions struct {
42 ThinkingConfig *ThinkingConfig `json:"thinking_config"`
43
44 // Optional.
45 // The name of the cached content used as context to serve the prediction.
46 // Format: cachedContents/{cachedContent}
47 CachedContent string `json:"cached_content"`
48
49 // Optional. A list of unique safety settings for blocking unsafe content.
50 SafetySettings []SafetySetting `json:"safety_settings"`
51 // 'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
52 // 'BLOCK_LOW_AND_ABOVE',
53 // 'BLOCK_MEDIUM_AND_ABOVE',
54 // 'BLOCK_ONLY_HIGH',
55 // 'BLOCK_NONE',
56 // 'OFF',
57 Threshold string `json:"threshold"`
58}
59
60// Options implements the ProviderOptionsData interface for ProviderOptions.
61func (o *ProviderOptions) Options() {}
62
63// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
64func (o ProviderOptions) MarshalJSON() ([]byte, error) {
65 type plain ProviderOptions
66 raw, err := json.Marshal(plain(o))
67 if err != nil {
68 return nil, err
69 }
70 return json.Marshal(struct {
71 Type string `json:"type"`
72 Data json.RawMessage `json:"data"`
73 }{
74 Type: TypeProviderOptions,
75 Data: raw,
76 })
77}
78
79// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
80func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
81 type plain ProviderOptions
82 var oo plain
83 err := json.Unmarshal(data, &oo)
84 if err != nil {
85 return err
86 }
87 *o = ProviderOptions(oo)
88 return nil
89}
90
91// ParseOptions parses provider options from a map for the Google provider.
92func ParseOptions(data map[string]any) (*ProviderOptions, error) {
93 var options ProviderOptions
94 if err := fantasy.ParseOptions(data, &options); err != nil {
95 return nil, err
96 }
97 return &options, nil
98}
99
100// Register Google provider-specific types with the global registry.
101func init() {
102 fantasy.RegisterProviderType(TypeProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
103 var v ProviderOptions
104 if err := json.Unmarshal(data, &v); err != nil {
105 return nil, err
106 }
107 return &v, nil
108 })
109}