Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Google Generative AI Streaming Example

Example Val showing how to set up an authenticated Google GoogleGenerativeAI client.

Prerequisite:

Follow Google's Getting Started guide to get an API key and view some example methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { GoogleGenerativeAI } from "npm:@google/generative-ai";
export default async function(req: Request): Promise<Response> {
const genAI = new GoogleGenerativeAI(Deno.env.get("your-api-key"));
const generativeModel = genAI.getGenerativeModel({
model: "gemini-1.5-flash-001",
});
const request = {
contents: [{ role: "user", parts: [{ text: "How are you doing today?" }] }],
};
const streamingResult = await generativeModel.generateContentStream(request);
return new Response(
new ReadableStream({
async start(controller) {
for await (const chunk of streamingResult.stream) {
controller.enqueue(
new TextEncoder().encode(chunk.candidates[0].content.parts[0].text),
);
}
controller.close();
},
}),
{ headers: { "Content-Type": "text/event-stream" } },
);
}
tr3ntg-googlegenerativeaistreamingexample.web.val.run
July 8, 2024