|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +import {GoogleGenAI} from '@google/genai'; |
| 7 | + |
| 8 | +const GEMINI_API_KEY = process.env.GEMINI_API_KEY; |
| 9 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 10 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION; |
| 11 | +const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI; |
| 12 | + |
| 13 | +async function throwApiErrorForMLDev() { |
| 14 | + const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY}); |
| 15 | + const response = await ai.models.generateContent({ |
| 16 | + model: 'non-existent-model', |
| 17 | + contents: 'Tell me a story in 300 words?', |
| 18 | + }); |
| 19 | + console.log('text response: ', response.text); |
| 20 | +} |
| 21 | + |
| 22 | +async function throwApiErrorForVertexAI() { |
| 23 | + const ai = new GoogleGenAI({ |
| 24 | + vertexai: true, |
| 25 | + project: GOOGLE_CLOUD_PROJECT, |
| 26 | + location: GOOGLE_CLOUD_LOCATION, |
| 27 | + }); |
| 28 | + const response = await ai.models.generateContent({ |
| 29 | + model: 'non-existent-model', |
| 30 | + contents: 'Tell me a story in 300 words?', |
| 31 | + }); |
| 32 | + |
| 33 | + console.log('text response: ', response.text); |
| 34 | +} |
| 35 | + |
| 36 | +async function main() { |
| 37 | + if (GOOGLE_GENAI_USE_VERTEXAI) { |
| 38 | + await throwApiErrorForVertexAI().catch((e) => { |
| 39 | + console.error('error name: ', e.name); |
| 40 | + console.error('error message: ', e.message); |
| 41 | + console.error('error status: ', e.status); |
| 42 | + }); |
| 43 | + } else { |
| 44 | + await throwApiErrorForMLDev().catch((e) => { |
| 45 | + console.error('error name: ', e.name); |
| 46 | + console.error('error message: ', e.message); |
| 47 | + console.error('error status: ', e.status); |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +main(); |
0 commit comments