generative-ai-js icon indicating copy to clipboard operation
generative-ai-js copied to clipboard

Feature/streaming callbacks

Open demoncoder-crypto opened this issue 8 months ago • 2 comments

Feature Request: Add Streaming Callbacks Utility

Currently, developers need to manually manage the response streaming process with custom code like:


const response = await model.generateContentStream([prompt]);

let textBuf = '';

while (true) {
  const { done, value } = await response.stream.next()
  if (done) {
    
    await onEnd(textBuf);
    return
  }

  if (value) {
    const currentText = value.text()
    
    onData(value.text())
    textBuf += currentText
  }
}
  1. Process each chunk of text as it arrives (via onData callback)
  2. Perform actions when streaming completes (via onEnd callback)

Solution

The implementation includes two main utility functions:

  1. getStreamedResponse() - regular content generation
  2. getStreamedChatResponse() - chat specific streaming

Example Usage


await getStreamedResponse({
  prompt: "Your prompt here",
  model: model,
  onData: (chunkText) => {
    
    console.log(chunkText);
    
  },
  onEnd: async (fullText) => {
    
    await saveToDatabase(fullText);
    
});


await getStreamedChatResponse({
  message: [{ text: "Your message here" }],
  chatSession: chat,
  onData: (chunkText) => {
    
  },
  onEnd: async (fullText) => {
    
  }
});

demoncoder-crypto avatar Mar 08 '25 05:03 demoncoder-crypto