|
14 | 14 |
|
15 | 15 | import 'content.dart';
|
16 | 16 | import 'error.dart';
|
| 17 | +import 'function_calling.dart' show Tool, ToolConfig; |
17 | 18 | import 'schema.dart';
|
18 | 19 |
|
19 | 20 | /// Response for Count Tokens
|
@@ -155,6 +156,23 @@ final class UsageMetadata {
|
155 | 156 | final List<ModalityTokenCount>? candidatesTokensDetails;
|
156 | 157 | }
|
157 | 158 |
|
| 159 | +/// Constructe a UsageMetadata with all it's fields. |
| 160 | +/// |
| 161 | +/// Expose access to the private constructor for use within the package.. |
| 162 | +UsageMetadata createUsageMetadata({ |
| 163 | + required int? promptTokenCount, |
| 164 | + required int? candidatesTokenCount, |
| 165 | + required int? totalTokenCount, |
| 166 | + required List<ModalityTokenCount>? promptTokensDetails, |
| 167 | + required List<ModalityTokenCount>? candidatesTokensDetails, |
| 168 | +}) => |
| 169 | + UsageMetadata._( |
| 170 | + promptTokenCount: promptTokenCount, |
| 171 | + candidatesTokenCount: candidatesTokenCount, |
| 172 | + totalTokenCount: totalTokenCount, |
| 173 | + promptTokensDetails: promptTokensDetails, |
| 174 | + candidatesTokensDetails: candidatesTokensDetails); |
| 175 | + |
158 | 176 | /// Response candidate generated from a [GenerativeModel].
|
159 | 177 | final class Candidate {
|
160 | 178 | // TODO: token count?
|
@@ -842,53 +860,124 @@ enum TaskType {
|
842 | 860 | Object toJson() => _jsonString;
|
843 | 861 | }
|
844 | 862 |
|
845 |
| -/// Parse the json to [GenerateContentResponse] |
846 |
| -GenerateContentResponse parseGenerateContentResponse(Object jsonObject) { |
847 |
| - if (jsonObject case {'error': final Object error}) throw parseError(error); |
848 |
| - final candidates = switch (jsonObject) { |
849 |
| - {'candidates': final List<Object?> candidates} => |
850 |
| - candidates.map(_parseCandidate).toList(), |
851 |
| - _ => <Candidate>[] |
852 |
| - }; |
853 |
| - final promptFeedback = switch (jsonObject) { |
854 |
| - {'promptFeedback': final promptFeedback?} => |
855 |
| - _parsePromptFeedback(promptFeedback), |
856 |
| - _ => null, |
857 |
| - }; |
858 |
| - final usageMedata = switch (jsonObject) { |
859 |
| - {'usageMetadata': final usageMetadata?} => |
860 |
| - _parseUsageMetadata(usageMetadata), |
861 |
| - _ => null, |
862 |
| - }; |
863 |
| - return GenerateContentResponse(candidates, promptFeedback, |
864 |
| - usageMetadata: usageMedata); |
| 863 | +// ignore: public_member_api_docs |
| 864 | +abstract interface class SerializationStrategy { |
| 865 | + // ignore: public_member_api_docs |
| 866 | + GenerateContentResponse parseGenerateContentResponse(Object jsonObject); |
| 867 | + // ignore: public_member_api_docs |
| 868 | + CountTokensResponse parseCountTokensResponse(Object jsonObject); |
| 869 | + // ignore: public_member_api_docs |
| 870 | + Map<String, Object?> generateContentRequest( |
| 871 | + Iterable<Content> contents, |
| 872 | + ({String prefix, String name}) model, |
| 873 | + List<SafetySetting> safetySettings, |
| 874 | + GenerationConfig? generationConfig, |
| 875 | + List<Tool>? tools, |
| 876 | + ToolConfig? toolConfig, |
| 877 | + Content? systemInstruction, |
| 878 | + ); |
| 879 | + |
| 880 | + // ignore: public_member_api_docs |
| 881 | + Map<String, Object?> countTokensRequest( |
| 882 | + Iterable<Content> contents, |
| 883 | + ({String prefix, String name}) model, |
| 884 | + List<SafetySetting> safetySettings, |
| 885 | + GenerationConfig? generationConfig, |
| 886 | + List<Tool>? tools, |
| 887 | + ToolConfig? toolConfig, |
| 888 | + ); |
865 | 889 | }
|
866 | 890 |
|
867 |
| -/// Parse the json to [CountTokensResponse] |
868 |
| -CountTokensResponse parseCountTokensResponse(Object jsonObject) { |
869 |
| - if (jsonObject case {'error': final Object error}) throw parseError(error); |
| 891 | +// ignore: public_member_api_docs |
| 892 | +final class VertexSerialization implements SerializationStrategy { |
| 893 | + /// Parse the json to [GenerateContentResponse] |
| 894 | + @override |
| 895 | + GenerateContentResponse parseGenerateContentResponse(Object jsonObject) { |
| 896 | + if (jsonObject case {'error': final Object error}) throw parseError(error); |
| 897 | + final candidates = switch (jsonObject) { |
| 898 | + {'candidates': final List<Object?> candidates} => |
| 899 | + candidates.map(_parseCandidate).toList(), |
| 900 | + _ => <Candidate>[] |
| 901 | + }; |
| 902 | + final promptFeedback = switch (jsonObject) { |
| 903 | + {'promptFeedback': final promptFeedback?} => |
| 904 | + _parsePromptFeedback(promptFeedback), |
| 905 | + _ => null, |
| 906 | + }; |
| 907 | + final usageMedata = switch (jsonObject) { |
| 908 | + {'usageMetadata': final usageMetadata?} => |
| 909 | + _parseUsageMetadata(usageMetadata), |
| 910 | + {'totalTokens': final int totalTokens} => |
| 911 | + UsageMetadata._(totalTokenCount: totalTokens), |
| 912 | + _ => null, |
| 913 | + }; |
| 914 | + return GenerateContentResponse(candidates, promptFeedback, |
| 915 | + usageMetadata: usageMedata); |
| 916 | + } |
870 | 917 |
|
871 |
| - if (jsonObject is! Map) { |
872 |
| - throw unhandledFormat('CountTokensResponse', jsonObject); |
| 918 | + /// Parse the json to [CountTokensResponse] |
| 919 | + @override |
| 920 | + CountTokensResponse parseCountTokensResponse(Object jsonObject) { |
| 921 | + if (jsonObject case {'error': final Object error}) throw parseError(error); |
| 922 | + |
| 923 | + if (jsonObject is! Map) { |
| 924 | + throw unhandledFormat('CountTokensResponse', jsonObject); |
| 925 | + } |
| 926 | + |
| 927 | + final totalTokens = jsonObject['totalTokens'] as int; |
| 928 | + final totalBillableCharacters = switch (jsonObject) { |
| 929 | + {'totalBillableCharacters': final int totalBillableCharacters} => |
| 930 | + totalBillableCharacters, |
| 931 | + _ => null, |
| 932 | + }; |
| 933 | + final promptTokensDetails = switch (jsonObject) { |
| 934 | + {'promptTokensDetails': final List<Object?> promptTokensDetails} => |
| 935 | + promptTokensDetails.map(_parseModalityTokenCount).toList(), |
| 936 | + _ => null, |
| 937 | + }; |
| 938 | + |
| 939 | + return CountTokensResponse( |
| 940 | + totalTokens, |
| 941 | + totalBillableCharacters: totalBillableCharacters, |
| 942 | + promptTokensDetails: promptTokensDetails, |
| 943 | + ); |
873 | 944 | }
|
874 | 945 |
|
875 |
| - final totalTokens = jsonObject['totalTokens'] as int; |
876 |
| - final totalBillableCharacters = switch (jsonObject) { |
877 |
| - {'totalBillableCharacters': final int totalBillableCharacters} => |
878 |
| - totalBillableCharacters, |
879 |
| - _ => null, |
880 |
| - }; |
881 |
| - final promptTokensDetails = switch (jsonObject) { |
882 |
| - {'promptTokensDetails': final List<Object?> promptTokensDetails} => |
883 |
| - promptTokensDetails.map(_parseModalityTokenCount).toList(), |
884 |
| - _ => null, |
885 |
| - }; |
| 946 | + @override |
| 947 | + Map<String, Object?> generateContentRequest( |
| 948 | + Iterable<Content> contents, |
| 949 | + ({String prefix, String name}) model, |
| 950 | + List<SafetySetting> safetySettings, |
| 951 | + GenerationConfig? generationConfig, |
| 952 | + List<Tool>? tools, |
| 953 | + ToolConfig? toolConfig, |
| 954 | + Content? systemInstruction, |
| 955 | + ) { |
| 956 | + return { |
| 957 | + 'model': '${model.prefix}/${model.name}', |
| 958 | + 'contents': contents.map((c) => c.toJson()).toList(), |
| 959 | + if (safetySettings.isNotEmpty) |
| 960 | + 'safetySettings': safetySettings.map((s) => s.toJson()).toList(), |
| 961 | + if (generationConfig != null) |
| 962 | + 'generationConfig': generationConfig.toJson(), |
| 963 | + if (tools != null) 'tools': tools.map((t) => t.toJson()).toList(), |
| 964 | + if (toolConfig != null) 'toolConfig': toolConfig.toJson(), |
| 965 | + if (systemInstruction != null) |
| 966 | + 'systemInstruction': systemInstruction.toJson(), |
| 967 | + }; |
| 968 | + } |
886 | 969 |
|
887 |
| - return CountTokensResponse( |
888 |
| - totalTokens, |
889 |
| - totalBillableCharacters: totalBillableCharacters, |
890 |
| - promptTokensDetails: promptTokensDetails, |
891 |
| - ); |
| 970 | + @override |
| 971 | + Map<String, Object?> countTokensRequest( |
| 972 | + Iterable<Content> contents, |
| 973 | + ({String prefix, String name}) model, |
| 974 | + List<SafetySetting> safetySettings, |
| 975 | + GenerationConfig? generationConfig, |
| 976 | + List<Tool>? tools, |
| 977 | + ToolConfig? toolConfig, |
| 978 | + ) => |
| 979 | + // Everything except contents is ignored. |
| 980 | + {'contents': contents.map((c) => c.toJson()).toList()}; |
892 | 981 | }
|
893 | 982 |
|
894 | 983 | Candidate _parseCandidate(Object? jsonObject) {
|
|
0 commit comments