Created
January 28, 2025 09:35
-
-
Save BrianHung/7a29bb63c85320defe61d540a0569e5f to your computer and use it in GitHub Desktop.
Cloudflare Worker Google Vertex AI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GoogleOAuth, { GoogleKey } from 'cloudflare-workers-and-google-oauth' | |
import { GoogleAuth } from "google-auth-library"; | |
import { GenerateContentRequest, GenerativeModel } from "@google-cloud/vertexai"; | |
import authJSON from "./auth.json"; // Service account | |
class JsonAuth extends GoogleAuthBase { | |
private oauth: GoogleOAuth | |
constructor(private readonly json: GoogleKey) { | |
super(); | |
this.oauth = new GoogleOAuth(json, ["https://www.googleapis.com/auth/cloud-platform"]) | |
} | |
override getAccessToken(): Promise<string | null | undefined> { | |
return this.oauth.getGoogleAuthToken() | |
} | |
} | |
const model = new GenerativeModel({ | |
model: "gemini-1.5-flash", | |
googleAuth: new JsonAuth(authJSON), | |
project: authJSON.project_id, | |
location: 'us-central1', | |
}); | |
const request: GenerateContentRequest = { | |
contents: [{role: 'user', parts: [{text: 'Hello world!'}]}], | |
}; | |
const result = await model.generateContent(request); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment