Skip to content

Instantly share code, notes, and snippets.

@pokutuna
Created May 3, 2024 09:10
Show Gist options
  • Save pokutuna/c95274a9cab113888acb9e793c33fc70 to your computer and use it in GitHub Desktop.
Save pokutuna/c95274a9cab113888acb9e793c33fc70 to your computer and use it in GitHub Desktop.
import { GoogleAuth } from "google-auth-library";
const projectId = "pokutuna-playground";
const location = "us-central1";
const model = "gemini-1.5-pro-preview-0409";
const [temperature, topK, topP, maxOutputTokens] = [0, 1, 0, 1028];
// en: List as many Japanese proverbs as possible.
const prompt = "日本のことわざをできるだけたくさん挙げて";
const token = await new GoogleAuth({ projectId }).getAccessToken();
const url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/publishers/google/models/${model}:streamGenerateContent`;
const params = {
contents: [{ role: "user", parts: [{ text: prompt }] }],
generationConfig: { temperature, topK, topP, maxOutputTokens },
};
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(params),
}).then(async (res) => {
if (!res.body) throw new Error("No body");
const reader = res.body.getReader();
const decoder = new TextDecoder();
let isDone = false;
while (!isDone) {
const { value, done } = await reader.read();
console.log(`--- [chunk] ---\n${decoder.decode(value)}\n`);
isDone = done;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment