Generative AI with Node js in Hindi #16 Content Stream in Google Gen AI

Content Stream in Google Gen AI


  1. Use generateContentStream
  2. Stream content on terminal
  3. Stream content on bowser UI


import {GoogleGenAI} from '@google/genai'
import express from 'express';
import dotenv from 'dotenv'
dotenv.config();
const app = express();

const googleAI = new GoogleGenAI({apiKey:process.env.geminiKey})

app.get("/", async (req,resp)=>{

const response = await googleAI.models.generateContentStream({
model:"gemini-2.5-flash",
contents:"Tell me about AI in details ",
})
// console.log(response.text)
for await (const chunk of response){
const text = chunk.text;
// console.log(text);
if(text){
resp.write(text)
}
}

resp.end("_____content completed____")
})

app.listen(3300)