Skip to content

Instantly share code, notes, and snippets.

@ahallora
Created December 18, 2024 07:56
Show Gist options
  • Save ahallora/5279341bbef6165a9b909310a8440b57 to your computer and use it in GitHub Desktop.
Save ahallora/5279341bbef6165a9b909310a8440b57 to your computer and use it in GitHub Desktop.
Using Gemini AI With Typescript / Node.js / Next.js etc. (POC)
import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";
const GEMINI_API_KEY = ""; // process.env.GEMINI_API_KEY
export const gemini = {
createEmbedding: async (content: string) => {
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "text-embedding-004" });
try {
const result = await model.embedContent(content);
return result.embedding.values;
} catch (e) {
console.error(e);
return e;
}
},
generateContent: async (
prompt: string,
schema?: any, // TODO: Get proper types
generationConfigExtra?: any // TODO: Get proper types
) => {
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
const generationConfig = {
temperature: 1,
topP: 0.95,
topK: 64,
maxOutputTokens: 8192,
responseMimeType: "application/json",
...generationConfigExtra,
...(schema && { responseSchema: schema }),
};
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
generationConfig,
});
// const chat = model.startChat({ history: [] });
try {
const res = await model.generateContent(prompt);
// const res = await chat.sendMessage(prompt);
try {
const { recipeName } = JSON.parse(res.response.text());
return recipeName;
} catch (e) {
console.error(e);
return e;
}
} catch (e) {
console.error(e);
return e;
}
},
};
/* Examples */
const examples = {
getRecipe: async () => {
const schema = {
description: "List of recipes",
type: SchemaType.ARRAY,
items: {
type: SchemaType.OBJECT,
properties: {
recipeName: {
type: SchemaType.STRING,
description: "Name of the recipe",
nullable: false,
},
},
required: ["recipeName"],
},
};
const prompt = `List a few popular cookie recipes using this JSON schema:
Recipe = {'recipeName': string}
Return: Array<Recipe>`;
return await gemini.generateContent(prompt, schema);
},
sayHelloInDanish: async () => {
return await gemini.generateContent("Say hello in Danish");
},
getEmbedding: async () => {
return await gemini.createEmbedding("Create an embedding of this string");
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment