33 * Copyright 2025 Google LLC
44 * SPDX-License-Identifier: Apache-2.0
55 */
6+
67import { GoogleGenAI } from '@google/genai' ;
8+ import { MODEL_FLASH_LITE } from './constants.js' ;
79
8- const GEMINI_API_KEY = process . env . GEMINI_API_KEY ;
10+ const GEMINI_API_KEY = process . env . GEMINI_API_KEY || process . env . GOOGLE_API_KEY ;
911const GOOGLE_CLOUD_PROJECT = process . env . GOOGLE_CLOUD_PROJECT ;
1012const GOOGLE_CLOUD_LOCATION = process . env . GOOGLE_CLOUD_LOCATION ;
1113const GOOGLE_GENAI_USE_VERTEXAI = process . env . GOOGLE_GENAI_USE_VERTEXAI ;
@@ -15,7 +17,7 @@ async function abortStreamingFromMLDev() {
1517 const abortController = new AbortController ( ) ;
1618 const abortSignal = abortController . signal ;
1719 const response = await ai . models . generateContentStream ( {
18- model : 'gemini-2.0-flash' ,
20+ model : MODEL_FLASH_LITE ,
1921 contents : 'Tell me a story in 300 words?' ,
2022 config : {
2123 abortSignal : abortSignal ,
@@ -38,7 +40,7 @@ async function abortStreamingFromVertexAI() {
3840 const abortController = new AbortController ( ) ;
3941 const abortSignal = abortController . signal ;
4042 const response = await ai . models . generateContentStream ( {
41- model : 'gemini-2.0-flash' ,
43+ model : MODEL_FLASH_LITE ,
4244 contents : 'Tell me a story in 300 words?' ,
4345 config : {
4446 abortSignal : abortSignal ,
@@ -53,14 +55,20 @@ async function abortStreamingFromVertexAI() {
5355}
5456
5557async function main ( ) {
58+ const handleAbortError = ( e : unknown ) => {
59+ if (
60+ e instanceof Error &&
61+ ( e . name === 'AbortError' || e . message . toLowerCase ( ) . includes ( 'abort' ) )
62+ ) {
63+ console . log ( 'got expected abort error:' , e . message ) ;
64+ } else {
65+ throw e ;
66+ }
67+ } ;
5668 if ( GOOGLE_GENAI_USE_VERTEXAI ) {
57- await abortStreamingFromVertexAI ( ) . catch ( ( e ) =>
58- console . error ( 'got expected abort error' , e ) ,
59- ) ;
69+ await abortStreamingFromVertexAI ( ) . catch ( handleAbortError ) ;
6070 } else {
61- await abortStreamingFromMLDev ( ) . catch ( ( e ) =>
62- console . error ( 'got expected abort error' , e ) ,
63- ) ;
71+ await abortStreamingFromMLDev ( ) . catch ( handleAbortError ) ;
6472 }
6573}
6674
0 commit comments