in src/requests/response-helpers.ts [31:114]
export function addHelpers(
response: GenerateContentResponse,
): EnhancedGenerateContentResponse {
(response as EnhancedGenerateContentResponse).text = () => {
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) {
console.warn(
`This response had ${response.candidates.length} ` +
`candidates. Returning text from the first candidate only. ` +
`Access response.candidates directly to use the other candidates.`,
);
}
if (hadBadFinishReason(response.candidates[0])) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`${formatBlockErrorMessage(response)}`,
response,
);
}
return getText(response);
} else if (response.promptFeedback) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`Text not available. ${formatBlockErrorMessage(response)}`,
response,
);
}
return "";
};
/**
* TODO: remove at next major version
*/
(response as EnhancedGenerateContentResponse).functionCall = () => {
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) {
console.warn(
`This response had ${response.candidates.length} ` +
`candidates. Returning function calls from the first candidate only. ` +
`Access response.candidates directly to use the other candidates.`,
);
}
if (hadBadFinishReason(response.candidates[0])) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`${formatBlockErrorMessage(response)}`,
response,
);
}
console.warn(
`response.functionCall() is deprecated. ` +
`Use response.functionCalls() instead.`,
);
return getFunctionCalls(response)[0];
} else if (response.promptFeedback) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`Function call not available. ${formatBlockErrorMessage(response)}`,
response,
);
}
return undefined;
};
(response as EnhancedGenerateContentResponse).functionCalls = () => {
if (response.candidates && response.candidates.length > 0) {
if (response.candidates.length > 1) {
console.warn(
`This response had ${response.candidates.length} ` +
`candidates. Returning function calls from the first candidate only. ` +
`Access response.candidates directly to use the other candidates.`,
);
}
if (hadBadFinishReason(response.candidates[0])) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`${formatBlockErrorMessage(response)}`,
response,
);
}
return getFunctionCalls(response);
} else if (response.promptFeedback) {
throw new GoogleGenerativeAIResponseError<GenerateContentResponse>(
`Function call not available. ${formatBlockErrorMessage(response)}`,
response,
);
}
return undefined;
};
return response as EnhancedGenerateContentResponse;
}