|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { GenAIError } from './errors'; |
| 19 | +import { logger } from './logger'; |
| 20 | +import { |
| 21 | + CitationMetadata, |
| 22 | + CountTokensRequest, |
| 23 | + GenerateContentCandidate, |
| 24 | + GenerateContentRequest, |
| 25 | + GenerateContentResponse, |
| 26 | + HarmSeverity, |
| 27 | + InlineDataPart, |
| 28 | + PromptFeedback, |
| 29 | + SafetyRating, |
| 30 | + GenAIErrorCode |
| 31 | +} from './types'; |
| 32 | +import { |
| 33 | + GoogleAIGenerateContentResponse, |
| 34 | + GoogleAIGenerateContentCandidate, |
| 35 | + GoogleAICountTokensRequest |
| 36 | +} from './types/googleAI'; |
| 37 | + |
| 38 | +/** |
| 39 | + * This SDK supports both Vertex AI and Google AI APIs. |
| 40 | + * The public API prioritizes the Vertex AI API. |
| 41 | + * We avoid having two sets of types by translating requests and responses between the two API formats. |
| 42 | + * We want to avoid two sets of types so that developers can switch between Vertex AI and Google AI |
| 43 | + * with minimal changes to their code. |
| 44 | + * |
| 45 | + * In here are functions that map requests and responses between the two API formats. |
| 46 | + * VertexAI requests defined by the user are mapped to Google AI requests before they're sent. |
| 47 | + * Google AI responses are mapped to VertexAI responses so they can be returned to the user. |
| 48 | + */ |
| 49 | + |
| 50 | +/** |
| 51 | + * Maps a Vertex AI {@link GenerateContentRequest} to a format that can be sent to Google AI. |
| 52 | + * |
| 53 | + * @param generateContentRequest The {@link GenerateContentRequest} to map. |
| 54 | + * @returns A {@link GenerateContentResponse} that conforms to the Google AI format. |
| 55 | + * |
| 56 | + * @throws If the request contains properties that are unsupported by Google AI. |
| 57 | + */ |
| 58 | +export function mapGenerateContentRequest( |
| 59 | + generateContentRequest: GenerateContentRequest |
| 60 | +): GenerateContentRequest { |
| 61 | + generateContentRequest.safetySettings?.forEach(safetySetting => { |
| 62 | + if (safetySetting.method) { |
| 63 | + throw new GenAIError( |
| 64 | + GenAIErrorCode.UNSUPPORTED, |
| 65 | + 'SafetySetting.method is not supported in the Google AI. Please remove this property.' |
| 66 | + ); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + if (generateContentRequest.generationConfig?.topK) { |
| 71 | + logger.warn( |
| 72 | + 'topK in GenerationConfig has been rounded to the nearest integer.' |
| 73 | + ); |
| 74 | + generateContentRequest.generationConfig.topK = Math.round( |
| 75 | + generateContentRequest.generationConfig.topK |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + return generateContentRequest; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Maps a {@link GenerateContentResponse} from Google AI to the format of the |
| 84 | + * {@link GenerateContentResponse} that we get from VertexAI that is exposed in the public API. |
| 85 | + * |
| 86 | + * @param googleAIResponse The {@link GenerateContentResponse} from Google AI. |
| 87 | + * @returns A {@link GenerateContentResponse} that conforms to the public API's format. |
| 88 | + */ |
| 89 | +export function mapGenerateContentResponse( |
| 90 | + googleAIResponse: GoogleAIGenerateContentResponse |
| 91 | +): GenerateContentResponse { |
| 92 | + const generateContentResponse = { |
| 93 | + candidates: googleAIResponse.candidates |
| 94 | + ? mapGenerateContentCandidates(googleAIResponse.candidates) |
| 95 | + : undefined, |
| 96 | + prompt: googleAIResponse.promptFeedback |
| 97 | + ? mapPromptFeedback(googleAIResponse.promptFeedback) |
| 98 | + : undefined, |
| 99 | + usageMetadata: googleAIResponse.usageMetadata |
| 100 | + }; |
| 101 | + |
| 102 | + return generateContentResponse; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Maps a Vertex AI {@link CountTokensRequest} to a format that can be sent to Google AI. |
| 107 | + * |
| 108 | + * @param countTokensRequest The {@link CountTokensRequest} to map. |
| 109 | + * @returns A {@link CountTokensRequest} that conforms to the Google AI format. |
| 110 | + */ |
| 111 | +export function mapCountTokensRequest( |
| 112 | + countTokensRequest: CountTokensRequest, |
| 113 | +): GoogleAICountTokensRequest { |
| 114 | + const mappedCountTokensRequest: GoogleAICountTokensRequest = { |
| 115 | + generateContentRequest: { |
| 116 | + // model, |
| 117 | + contents: countTokensRequest.contents, |
| 118 | + systemInstruction: countTokensRequest.systemInstruction, |
| 119 | + tools: countTokensRequest.tools, |
| 120 | + generationConfig: countTokensRequest.generationConfig |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + return mappedCountTokensRequest; |
| 125 | +} |
| 126 | + |
| 127 | +export function mapGenerateContentCandidates( |
| 128 | + candidates: GoogleAIGenerateContentCandidate[] |
| 129 | +): GenerateContentCandidate[] { |
| 130 | + const mappedCandidates: GenerateContentCandidate[] = []; |
| 131 | + if (mappedCandidates) { |
| 132 | + candidates.forEach(candidate => { |
| 133 | + // Map citationSources to citations. |
| 134 | + let citationMetadata: CitationMetadata | undefined; |
| 135 | + if (candidate.citationMetadata) { |
| 136 | + citationMetadata = { |
| 137 | + citations: candidate.citationMetadata.citationSources |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + // Assign missing candidate SafetyRatings properties to their defaults. |
| 142 | + if (candidate.safetyRatings) { |
| 143 | + logger.warn( |
| 144 | + "Candidate safety rating properties 'severity', 'severityScore', and 'probabilityScore' are not included in responses from Google AI. Properties have been assigned to default values." |
| 145 | + ); |
| 146 | + candidate.safetyRatings.forEach(safetyRating => { |
| 147 | + safetyRating.severity = HarmSeverity.HARM_SEVERITY_UNSUPPORTED; |
| 148 | + safetyRating.probabilityScore = 0; |
| 149 | + safetyRating.severityScore = 0; |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + // videoMetadata is not supported. |
| 154 | + // Throw early since developers may send a long video as input and only expect to pay |
| 155 | + // for inference on a small portion of the video. |
| 156 | + if ( |
| 157 | + candidate.content?.parts.some( |
| 158 | + part => (part as InlineDataPart)?.videoMetadata |
| 159 | + ) |
| 160 | + ) { |
| 161 | + throw new GenAIError( |
| 162 | + GenAIErrorCode.UNSUPPORTED, |
| 163 | + 'Part.videoMetadata is not supported in Google AI. Please remove this property.' |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + const mappedCandidate = { |
| 168 | + index: candidate.index, |
| 169 | + content: candidate.content, |
| 170 | + finishReason: candidate.finishReason, |
| 171 | + finishMessage: candidate.finishMessage, |
| 172 | + safetyRatings: candidate.safetyRatings, |
| 173 | + citationMetadata, |
| 174 | + groundingMetadata: candidate.groundingMetadata |
| 175 | + }; |
| 176 | + mappedCandidates.push(mappedCandidate); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + return mappedCandidates; |
| 181 | +} |
| 182 | + |
| 183 | +export function mapPromptFeedback( |
| 184 | + promptFeedback: PromptFeedback |
| 185 | +): PromptFeedback { |
| 186 | + // Assign missing PromptFeedback SafetyRatings properties to their defaults. |
| 187 | + const mappedSafetyRatings: SafetyRating[] = []; |
| 188 | + promptFeedback.safetyRatings.forEach(safetyRating => { |
| 189 | + mappedSafetyRatings.push({ |
| 190 | + category: safetyRating.category, |
| 191 | + probability: safetyRating.probability, |
| 192 | + severity: HarmSeverity.HARM_SEVERITY_UNSUPPORTED, |
| 193 | + probabilityScore: 0, |
| 194 | + severityScore: 0, |
| 195 | + blocked: safetyRating.blocked |
| 196 | + }); |
| 197 | + }); |
| 198 | + logger.warn( |
| 199 | + "PromptFeedback safety ratings' properties severity, severityScore, and probabilityScore are not included in responses from Google AI. Properties have been assigned to default values." |
| 200 | + ); |
| 201 | + |
| 202 | + const mappedPromptFeedback: PromptFeedback = { |
| 203 | + blockReason: promptFeedback.blockReason, |
| 204 | + safetyRatings: mappedSafetyRatings, |
| 205 | + blockReasonMessage: promptFeedback.blockReasonMessage |
| 206 | + }; |
| 207 | + return mappedPromptFeedback; |
| 208 | +} |
0 commit comments