@@ -16,64 +16,25 @@ import '../api.dart'
16
16
show
17
17
BlockReason,
18
18
Candidate,
19
- Citation,
20
- CitationMetadata,
21
19
CountTokensResponse,
22
20
FinishReason,
23
21
GenerateContentResponse,
24
22
GenerationConfig,
25
- GroundingChunk,
26
- GroundingMetadata,
27
- GroundingSupport,
28
23
HarmBlockThreshold,
29
24
HarmCategory,
30
25
HarmProbability,
31
26
PromptFeedback,
32
27
SafetyRating,
33
28
SafetySetting,
34
- SearchEntryPoint,
35
- Segment,
36
29
SerializationStrategy,
37
- WebGroundingChunk,
38
- parseUsageMetadata;
30
+ parseUsageMetadata,
31
+ parseCitationMetadata,
32
+ parseGroundingMetadata;
39
33
import '../content.dart' show Content, parseContent;
40
34
import '../error.dart' ;
41
35
import '../tool.dart' show Tool, ToolConfig;
42
36
43
- HarmProbability _parseHarmProbability (Object jsonObject) =>
44
- switch (jsonObject) {
45
- 'UNSPECIFIED' => HarmProbability .unknown,
46
- 'NEGLIGIBLE' => HarmProbability .negligible,
47
- 'LOW' => HarmProbability .low,
48
- 'MEDIUM' => HarmProbability .medium,
49
- 'HIGH' => HarmProbability .high,
50
- _ => throw unhandledFormat ('HarmProbability' , jsonObject),
51
- };
52
- HarmCategory _parseHarmCategory (Object jsonObject) => switch (jsonObject) {
53
- 'HARM_CATEGORY_UNSPECIFIED' => HarmCategory .unknown,
54
- 'HARM_CATEGORY_HARASSMENT' => HarmCategory .harassment,
55
- 'HARM_CATEGORY_HATE_SPEECH' => HarmCategory .hateSpeech,
56
- 'HARM_CATEGORY_SEXUALLY_EXPLICIT' => HarmCategory .sexuallyExplicit,
57
- 'HARM_CATEGORY_DANGEROUS_CONTENT' => HarmCategory .dangerousContent,
58
- _ => throw unhandledFormat ('HarmCategory' , jsonObject),
59
- };
60
-
61
- FinishReason _parseFinishReason (Object jsonObject) => switch (jsonObject) {
62
- 'UNSPECIFIED' => FinishReason .unknown,
63
- 'STOP' => FinishReason .stop,
64
- 'MAX_TOKENS' => FinishReason .maxTokens,
65
- 'SAFETY' => FinishReason .safety,
66
- 'RECITATION' => FinishReason .recitation,
67
- 'OTHER' => FinishReason .other,
68
- _ => throw unhandledFormat ('FinishReason' , jsonObject),
69
- };
70
- BlockReason _parseBlockReason (String jsonObject) => switch (jsonObject) {
71
- 'BLOCK_REASON_UNSPECIFIED' => BlockReason .unknown,
72
- 'SAFETY' => BlockReason .safety,
73
- 'OTHER' => BlockReason .other,
74
- _ => throw unhandledFormat ('BlockReason' , jsonObject),
75
- };
76
- String _harmBlockThresholdtoJson (HarmBlockThreshold ? threshold) =>
37
+ String _harmBlockThresholdToJson (HarmBlockThreshold ? threshold) =>
77
38
switch (threshold) {
78
39
null => 'HARM_BLOCK_THRESHOLD_UNSPECIFIED' ,
79
40
HarmBlockThreshold .low => 'BLOCK_LOW_AND_ABOVE' ,
@@ -97,7 +58,7 @@ Object _safetySettingToJson(SafetySetting safetySetting) {
97
58
}
98
59
return {
99
60
'category' : _harmCategoryToJson (safetySetting.category),
100
- 'threshold' : _harmBlockThresholdtoJson (safetySetting.threshold)
61
+ 'threshold' : _harmBlockThresholdToJson (safetySetting.threshold)
101
62
};
102
63
}
103
64
@@ -180,6 +141,7 @@ final class DeveloperSerialization implements SerializationStrategy {
180
141
};
181
142
}
182
143
144
+ // Developer API and Vertex AI has different _parseSafetyRating logic.
183
145
Candidate _parseCandidate (Object ? jsonObject) {
184
146
if (jsonObject is ! Map ) {
185
147
throw unhandledFormat ('Candidate' , jsonObject);
@@ -196,12 +158,12 @@ Candidate _parseCandidate(Object? jsonObject) {
196
158
},
197
159
switch (jsonObject) {
198
160
{'citationMetadata' : final Object citationMetadata} =>
199
- _parseCitationMetadata (citationMetadata),
161
+ parseCitationMetadata (citationMetadata),
200
162
_ => null
201
163
},
202
164
switch (jsonObject) {
203
165
{'finishReason' : final Object finishReason} =>
204
- _parseFinishReason (finishReason),
166
+ FinishReason . parseValue (finishReason),
205
167
_ => null
206
168
},
207
169
switch (jsonObject) {
@@ -210,12 +172,13 @@ Candidate _parseCandidate(Object? jsonObject) {
210
172
},
211
173
groundingMetadata: switch (jsonObject) {
212
174
{'groundingMetadata' : final Object groundingMetadata} =>
213
- _parseGroundingMetadata (groundingMetadata),
175
+ parseGroundingMetadata (groundingMetadata),
214
176
_ => null
215
177
},
216
178
);
217
179
}
218
180
181
+ // Developer API and Vertex AI has different _parseSafetyRating logic.
219
182
PromptFeedback _parsePromptFeedback (Object jsonObject) {
220
183
return switch (jsonObject) {
221
184
{
@@ -224,7 +187,7 @@ PromptFeedback _parsePromptFeedback(Object jsonObject) {
224
187
PromptFeedback (
225
188
switch (jsonObject) {
226
189
{'blockReason' : final String blockReason} =>
227
- _parseBlockReason (blockReason),
190
+ BlockReason . parseValue (blockReason),
228
191
_ => null ,
229
192
},
230
193
switch (jsonObject) {
@@ -241,147 +204,36 @@ SafetyRating _parseSafetyRating(Object? jsonObject) {
241
204
return switch (jsonObject) {
242
205
{
243
206
'category' : final Object category,
244
- 'probability' : final Object probability
207
+ 'probability' : final Object probability,
208
+ 'blocked' : final bool ? isBlocked,
209
+ } =>
210
+ SafetyRating (
211
+ _parseHarmCategory (category), _parseHarmProbability (probability),
212
+ isBlocked: isBlocked),
213
+ {
214
+ 'category' : final Object category,
215
+ 'probability' : final Object probability,
245
216
} =>
246
217
SafetyRating (
247
218
_parseHarmCategory (category), _parseHarmProbability (probability)),
248
219
_ => throw unhandledFormat ('SafetyRating' , jsonObject),
249
220
};
250
221
}
251
222
252
- CitationMetadata _parseCitationMetadata (Object ? jsonObject) {
253
- return switch (jsonObject) {
254
- {'citationSources' : final List <Object ?> citationSources} =>
255
- CitationMetadata (citationSources.map (_parseCitationSource).toList ()),
256
- // Vertex SDK format uses `citations`
257
- {'citations' : final List <Object ?> citationSources} =>
258
- CitationMetadata (citationSources.map (_parseCitationSource).toList ()),
259
- _ => throw unhandledFormat ('CitationMetadata' , jsonObject),
260
- };
261
- }
262
-
263
- Citation _parseCitationSource (Object ? jsonObject) {
264
- if (jsonObject is ! Map ) {
265
- throw unhandledFormat ('CitationSource' , jsonObject);
266
- }
267
-
268
- final uriString = jsonObject['uri' ] as String ? ;
269
-
270
- return Citation (
271
- jsonObject['startIndex' ] as int ? ,
272
- jsonObject['endIndex' ] as int ? ,
273
- uriString != null ? Uri .parse (uriString) : null ,
274
- jsonObject['license' ] as String ? ,
275
- );
276
- }
277
-
278
- GroundingMetadata _parseGroundingMetadata (Object ? jsonObject) {
279
- if (jsonObject is ! Map ) {
280
- throw unhandledFormat ('GroundingMetadata' , jsonObject);
281
- }
282
-
283
- final searchEntryPoint = switch (jsonObject) {
284
- {'searchEntryPoint' : final Object ? searchEntryPoint} =>
285
- _parseSearchEntryPoint (searchEntryPoint),
286
- _ => null ,
287
- };
288
- final groundingChunks = switch (jsonObject) {
289
- {'groundingChunks' : final List <Object ?> groundingChunks} =>
290
- groundingChunks.map (_parseGroundingChunk).toList (),
291
- _ => null ,
292
- } ??
293
- [];
294
- // Filters out null elements, which are returned from _parseGroundingSupport when
295
- // segment is null.
296
- final groundingSupport = switch (jsonObject) {
297
- {'groundingSupport' : final List <Object ?> groundingSupport} =>
298
- groundingSupport
299
- .map (_parseGroundingSupport)
300
- .whereType <GroundingSupport >()
301
- .toList (),
302
- _ => null ,
303
- } ??
304
- [];
305
- final webSearchQueries = switch (jsonObject) {
306
- {'webSearchQueries' : final List <String >? webSearchQueries} =>
307
- webSearchQueries,
308
- _ => null ,
309
- } ??
310
- [];
311
-
312
- return GroundingMetadata (
313
- searchEntryPoint: searchEntryPoint,
314
- groundingChunks: groundingChunks,
315
- groundingSupport: groundingSupport,
316
- webSearchQueries: webSearchQueries);
317
- }
318
-
319
- Segment _parseSegment (Object ? jsonObject) {
320
- if (jsonObject is ! Map ) {
321
- throw unhandledFormat ('Segment' , jsonObject);
322
- }
323
-
324
- return Segment (
325
- partIndex: (jsonObject['partIndex' ] as int ? ) ?? 0 ,
326
- startIndex: (jsonObject['startIndex' ] as int ? ) ?? 0 ,
327
- endIndex: (jsonObject['endIndex' ] as int ? ) ?? 0 ,
328
- text: (jsonObject['text' ] as String ? ) ?? '' );
329
- }
330
-
331
- WebGroundingChunk _parseWebGroundingChunk (Object ? jsonObject) {
332
- if (jsonObject is ! Map ) {
333
- throw unhandledFormat ('WebGroundingChunk' , jsonObject);
334
- }
335
-
336
- return WebGroundingChunk (
337
- uri: jsonObject['uri' ] as String ? ,
338
- title: jsonObject['title' ] as String ? ,
339
- domain: jsonObject['domain' ] as String ? ,
340
- );
341
- }
342
-
343
- GroundingChunk _parseGroundingChunk (Object ? jsonObject) {
344
- if (jsonObject is ! Map ) {
345
- throw unhandledFormat ('GroundingChunk' , jsonObject);
346
- }
347
-
348
- return GroundingChunk (
349
- web: jsonObject['web' ] != null
350
- ? _parseWebGroundingChunk (jsonObject['web' ])
351
- : null ,
352
- );
353
- }
354
-
355
- GroundingSupport ? _parseGroundingSupport (Object ? jsonObject) {
356
- if (jsonObject is ! Map ) {
357
- throw unhandledFormat ('GroundingSupport' , jsonObject);
358
- }
359
-
360
- final segment = switch (jsonObject) {
361
- {'segment' : final Object ? segment} => _parseSegment (segment),
362
- _ => null ,
363
- };
364
- if (segment == null ) {
365
- return null ;
366
- }
367
-
368
- return GroundingSupport (
369
- segment: segment,
370
- groundingChunkIndices:
371
- (jsonObject['groundingChunkIndices' ] as List <int >? ) ?? []);
372
- }
373
-
374
- SearchEntryPoint _parseSearchEntryPoint (Object ? jsonObject) {
375
- if (jsonObject is ! Map ) {
376
- throw unhandledFormat ('SearchEntryPoint' , jsonObject);
377
- }
378
-
379
- final renderedContent = jsonObject['renderedContent' ] as String ? ;
380
- if (renderedContent == null ) {
381
- throw unhandledFormat ('SearchEntryPoint' , jsonObject);
382
- }
383
-
384
- return SearchEntryPoint (
385
- renderedContent: renderedContent,
386
- );
387
- }
223
+ HarmProbability _parseHarmProbability (Object jsonObject) =>
224
+ switch (jsonObject) {
225
+ 'UNSPECIFIED' => HarmProbability .unknown,
226
+ 'NEGLIGIBLE' => HarmProbability .negligible,
227
+ 'LOW' => HarmProbability .low,
228
+ 'MEDIUM' => HarmProbability .medium,
229
+ 'HIGH' => HarmProbability .high,
230
+ _ => throw unhandledFormat ('HarmProbability' , jsonObject),
231
+ };
232
+ HarmCategory _parseHarmCategory (Object jsonObject) => switch (jsonObject) {
233
+ 'HARM_CATEGORY_UNSPECIFIED' => HarmCategory .unknown,
234
+ 'HARM_CATEGORY_HARASSMENT' => HarmCategory .harassment,
235
+ 'HARM_CATEGORY_HATE_SPEECH' => HarmCategory .hateSpeech,
236
+ 'HARM_CATEGORY_SEXUALLY_EXPLICIT' => HarmCategory .sexuallyExplicit,
237
+ 'HARM_CATEGORY_DANGEROUS_CONTENT' => HarmCategory .dangerousContent,
238
+ _ => throw unhandledFormat ('HarmCategory' , jsonObject),
239
+ };
0 commit comments