-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtransformer.go
262 lines (239 loc) · 7.99 KB
/
transformer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package genai
import (
"fmt"
"log"
"regexp"
"strings"
)
func tResourceName(ac *apiClient, resourceName string, collectionIdentifier string, collectionHierarchyDepth int) string {
shouldPrependCollectionIdentifier := !strings.HasPrefix(resourceName, collectionIdentifier+"/") &&
strings.Count(collectionIdentifier+"/"+resourceName, "/")+1 == collectionHierarchyDepth
switch ac.clientConfig.Backend {
case BackendVertexAI:
if strings.HasPrefix(resourceName, "projects/") {
return resourceName
} else if strings.HasPrefix(resourceName, "locations/") {
return fmt.Sprintf("projects/%s/%s", ac.clientConfig.Project, resourceName)
} else if strings.HasPrefix(resourceName, collectionIdentifier+"/") {
return fmt.Sprintf("projects/%s/locations/%s/%s", ac.clientConfig.Project, ac.clientConfig.Location, resourceName)
} else if shouldPrependCollectionIdentifier {
return fmt.Sprintf("projects/%s/locations/%s/%s/%s", ac.clientConfig.Project, ac.clientConfig.Location, collectionIdentifier, resourceName)
} else {
return resourceName
}
default:
if shouldPrependCollectionIdentifier {
return fmt.Sprintf("%s/%s", collectionIdentifier, resourceName)
} else {
return resourceName
}
}
}
func tCachedContentName(ac *apiClient, name any) (string, error) {
return tResourceName(ac, name.(string), "cachedContents", 2), nil
}
func tModel(ac *apiClient, origin any) (string, error) {
switch model := origin.(type) {
case string:
if model == "" {
return "", fmt.Errorf("tModel: model is empty")
}
if ac.clientConfig.Backend == BackendVertexAI {
if strings.HasPrefix(model, "projects/") || strings.HasPrefix(model, "models/") || strings.HasPrefix(model, "publishers/") {
return model, nil
} else if strings.Contains(model, "/") {
parts := strings.SplitN(model, "/", 2)
return fmt.Sprintf("publishers/%s/models/%s", parts[0], parts[1]), nil
} else {
return fmt.Sprintf("publishers/google/models/%s", model), nil
}
} else {
if strings.HasPrefix(model, "models/") || strings.HasPrefix(model, "tunedModels/") {
return model, nil
} else {
return fmt.Sprintf("models/%s", model), nil
}
}
default:
return "", fmt.Errorf("tModel: model is not a string")
}
}
func tModelFullName(ac *apiClient, origin any) (string, error) {
switch model := origin.(type) {
case string:
name, err := tModel(ac, model)
if err != nil {
return "", fmt.Errorf("tModelFullName: %w", err)
}
if strings.HasPrefix(name, "publishers/") && ac.clientConfig.Backend == BackendVertexAI {
return fmt.Sprintf("projects/%s/locations/%s/%s", ac.clientConfig.Project, ac.clientConfig.Location, name), nil
} else if strings.HasPrefix(name, "models/") && ac.clientConfig.Backend == BackendVertexAI {
return fmt.Sprintf("projects/%s/locations/%s/publishers/google/%s", ac.clientConfig.Project, ac.clientConfig.Location, name), nil
} else {
return name, nil
}
default:
return "", fmt.Errorf("tModelFullName: model is not a string")
}
}
func tCachesModel(ac *apiClient, origin any) (string, error) {
return tModelFullName(ac, origin)
}
func tContent(_ *apiClient, content any) (any, error) {
return content, nil
}
func tContents(_ *apiClient, contents any) (any, error) {
return contents, nil
}
func tTool(_ *apiClient, tool any) (any, error) {
return tool, nil
}
func tTools(_ *apiClient, tools any) (any, error) {
return tools, nil
}
func tSchema(apiClient *apiClient, origin any) (any, error) {
return origin, nil
}
func tSpeechConfig(_ *apiClient, speechConfig any) (any, error) {
return speechConfig, nil
}
func tBytes(_ *apiClient, fromImageBytes any) (any, error) {
// TODO(b/389133914): Remove dummy bytes converter.
return fromImageBytes, nil
}
func tContentsForEmbed(ac *apiClient, contents any) (any, error) {
if ac.clientConfig.Backend == BackendVertexAI {
switch v := contents.(type) {
case []any:
texts := []string{}
for _, content := range v {
parts, ok := content.(map[string]any)["parts"].([]any)
if !ok || len(parts) == 0 {
return nil, fmt.Errorf("tContentsForEmbed: content parts is not a non-empty list")
}
text, ok := parts[0].(map[string]any)["text"].(string)
if !ok {
return nil, fmt.Errorf("tContentsForEmbed: content part text is not a string")
}
texts = append(texts, text)
}
return texts, nil
default:
return nil, fmt.Errorf("tContentsForEmbed: contents is not a list")
}
} else {
return contents, nil
}
}
func tModelsURL(ac *apiClient, baseModels any) (string, error) {
if ac.clientConfig.Backend == BackendVertexAI {
if baseModels.(bool) {
return "publishers/google/models", nil
} else {
return "models", nil
}
} else {
if baseModels.(bool) {
return "models", nil
} else {
return "tunedModels", nil
}
}
}
func tExtractModels(ac *apiClient, response any) (any, error) {
switch response := response.(type) {
case map[string]any:
if models, ok := response["models"]; ok {
return models, nil
} else if tunedModels, ok := response["tunedModels"]; ok {
return tunedModels, nil
} else if publisherModels, ok := response["publisherModels"]; ok {
return publisherModels, nil
} else {
log.Printf("Warning: Cannot find the models type(models, tunedModels, publisherModels) for response: %s", response)
return []any{}, nil
}
default:
return nil, fmt.Errorf("tExtractModels: response is not a map")
}
}
func tFileName(ac *apiClient, name any) (string, error) {
switch name := name.(type) {
case string:
{
if strings.HasPrefix(name, "https://") || strings.HasPrefix(name, "http://") {
parts := strings.SplitN(name, "files/", 2)
if len(parts) < 2 {
return "", fmt.Errorf("could not find 'files/' in URI: %s", name)
}
suffix := parts[1]
re := regexp.MustCompile("^[a-z0-9]+")
match := re.FindStringSubmatch(suffix)
if len(match) == 0 {
return "", fmt.Errorf("could not extract file name from URI: %s", name)
}
name = match[0]
} else if strings.HasPrefix(name, "files/") {
name = strings.TrimPrefix(name, "files/")
}
return name, nil
}
default:
return "", fmt.Errorf("tFileName: name is not a string")
}
}
func tBlobs(ac *apiClient, blobs any) (any, error) {
switch blobs := blobs.(type) {
case []any:
// The only use case of this tBlobs function is for LiveSendRealtimeInputParameters.Media field.
// The Media field is a Blob type, not a list of Blob. So this branch will never be executed.
// If tBlobs function is used for other purposes in the future, uncomment the following line to
// enable this branch.
// applyConverterToSlice(ac, blobs, tBlob)
return nil, fmt.Errorf("unimplemented")
default:
blob, err := tBlob(ac, blobs)
if err != nil {
return nil, err
}
return []any{blob}, nil
}
}
func tBlob(ac *apiClient, blob any) (any, error) {
return blob, nil
}
func tImageBlob(ac *apiClient, blob any) (any, error) {
switch blob := blob.(type) {
case map[string]any:
if strings.HasPrefix(blob["mimeType"].(string), "image/") {
return blob, nil
}
return nil, fmt.Errorf("Unsupported mime type: %s", blob["mimeType"])
default:
return nil, fmt.Errorf("tImageBlob: blob is not a map")
}
}
func tAudioBlob(ac *apiClient, blob any) (any, error) {
switch blob := blob.(type) {
case map[string]any:
if strings.HasPrefix(blob["mimeType"].(string), "audio/") {
return blob, nil
}
return nil, fmt.Errorf("Unsupported mime type: %s", blob["mimeType"])
default:
return nil, fmt.Errorf("tAudioBlob: blob is not a map")
}
}