Created
April 27, 2024 06:44
-
-
Save abhiomkar/ee8d1b41c0bb1cea520a44bf459a9bf6 to your computer and use it in GitHub Desktop.
Vertex AI + Vercel (Use Gemini AI on Next.js App Router)
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
// Base64 encoded string used for "Application Default Credentials". | |
// Alternative to GOOGLE_APPLICATION_CREDENTIALS env which accepts JSON file path. | |
// | |
// Use following command to generate base64 encoded string for given json file. | |
// $ openssl base64 < ~/Downloads/gcloud-service-key.json | tr -d '\n' | |
// | |
GOOGLE_SERVICE_KEY="<base64_encoded_json_file>" |
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
// /app/api/ai/route.ts | |
import { VertexAI } from "@google-cloud/vertexai"; | |
import { NextResponse } from "next/server"; | |
const credential = JSON.parse( | |
Buffer.from(process.env.GOOGLE_SERVICE_KEY ?? "", "base64").toString() | |
); | |
const vertex_ai = new VertexAI({ | |
project: "glcoud-project-name", | |
location: "us-central1", | |
googleAuthOptions: { | |
credentials: { | |
client_email: credential.client_email, | |
client_id: credential.client_id, | |
private_key: credential.private_key, | |
}, | |
}, | |
}); | |
const model = "gemini-1.5-pro-preview-0409"; | |
const generativeModel = vertex_ai.preview.getGenerativeModel({ model }); | |
async function generateContent(promptInput: string) { | |
const result = await generativeModel.generateContent({ | |
contents: [ | |
{ | |
role: "user", | |
parts: [ | |
{ | |
text: `your prompt here: ${promptInput}`, | |
}, | |
], | |
}, | |
], | |
}); | |
return result.response.candidates?.[0]?.content.parts[0]?.text?.trim(); | |
} | |
export async function POST(req: Request) { | |
const { promptInput } = await req.json(); | |
// ... sanitize promptInput ... | |
const text = await generateContent(personName); | |
// ... sanitize output ... | |
return NextResponse.json({ text }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment