1"use strict";(self.webpackChunkmy_website=self.webpackChunkmy_website||[]).push([[3431],{7040:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>a,default:()=>g,frontMatter:()=>i,metadata:()=>r,toc:()=>c});var s=t(4848),o=t(8453);const i={},a=void 0,r={id:"ai-engineering/google-genai",title:"google-genai",description:"Google Genai",source:"@site/docs/ai-engineering/07-google-genai.md",sourceDirName:"ai-engineering",slug:"/ai-engineering/google-genai",permalink:"/docs/ai-engineering/google-genai",draft:!1,unlisted:!1,tags:[],version:"current",sidebarPosition:7,frontMatter:{},sidebar:"tutorialSidebar",previous:{title:"openai-api",permalink:"/docs/ai-engineering/openai-api"},next:{title:"vercel-ai",permalink:"/docs/ai-engineering/vercel-ai"}},l={},c=[{value:"Google Genai",id:"google-genai",level:2},{value:"Intro",id:"intro",level:4},{value:"Basic model calling",id:"basic-model-calling",level:4},{value:"chat session",id:"chat-session",level:4},{value:"Structured outputs",id:"structured-outputs",level:4},{value:"Image generation",id:"image-generation",level:4},{value:"Image and file analysis",id:"image-and-file-analysis",level:4},{value:"Embeddings",id:"embeddings",level:4},{value:"Model info",id:"model-info",level:4},{value:"Best practices",id:"best-practices",level:4}];function d(e){const n={code:"code",h2:"h2",h4:"h4",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,o.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.h2,{id:"google-genai",children:"Google Genai"}),"\n",(0,s.jsx)(n.h4,{id:"intro",children:"Intro"}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsxs)(n.li,{children:["Install with ",(0,s.jsx)(n.code,{children:"npm install @google/generative-ai"})]}),"\n",(0,s.jsx)(n.li,{children:"Instantiate model like so:"}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'import { GoogleGenerativeAI } from \'@google/generative-ai\';\n\n// Initialize with API key\nconst genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);\n\n// Get model instance\nconst model = genAI.getGenerativeModel({ model: "gemini-pro" });\n\n// Popular models:\n// - gemini-pro: Best for text tasks\n// - gemini-pro-vision: For image + text tasks\n// - gemini-1.5-pro: Latest with larger context\n// - gemini-1.5-flash: Faster, more efficient\n\n// get model instance with configuration\nconst model2 = genAI.getGenerativeModel({\n model: "gemini-pro",\n generationConfig: {\n temperature: 0.7, // Creativity (0.0-1.0)\n topK: 40, // Top-K sampling\n topP: 0.95, // Top-P sampling\n maxOutputTokens: 1024, // Max response length\n stopSequences: ["END"] // Stop generation at these sequences\n }\n});\n'})}),"\n",(0,s.jsx)(n.h4,{id:"basic-model-calling",children:"Basic model calling"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"model.generateContent(prompt)"}),": returns the AI response"]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"model.generateContentStream(prompt)"}),": returns the AI response as a stream"]}),"\n"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'async function generateText() {\n const model = genAI.getGenerativeModel({ model: "gemini-pro" });\n \n const prompt = "Write a short poem about AI";\n const result = await model.generateContent(prompt);\n \n console.log(result.response.text());\n}\n\nasync function streamText() {\n const model = genAI.getGenerativeModel({ model: "gemini-pro" });\n \n const prompt = "Tell me a long story about space exploration";\n const result = await model.generateContentStream(prompt);\n \n for await (const chunk of result.stream) {\n const chunkText = chunk.text();\n process.stdout.write(chunkText);\n }\n}\n'})}),"\n",(0,s.jsx)(n.h4,{id:"chat-session",children:"chat session"}),"\n",(0,s.jsx)(n.p,{children:"Google genai package offers their own class for keeping track of message history in memory."}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'async function chatExample() {\n const model = genAI.getGenerativeModel({ model: "gemini-pro" });\n \n // Start chat with optional history\n const chat = model.startChat({\n history: [\n {\n role: "user",\n parts: [{ text: "Hello, I\'m interested in learning about AI." }]\n },\n {\n role: "model",\n parts: [{ text: "Hello! I\'d be happy to help you learn about AI. What specific aspect interests you most?" }]\n }\n ]\n });\n \n // Send message\n const result = await chat.sendMessage("Tell me about machine learning");\n console.log(result.response.text());\n \n // Continue conversation\n const result2 = await chat.sendMessage("What are some practical applications?");\n console.log(result2.response.text());\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:"You can also stream chat responses like so:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'async function streamingChat() {\n const model = genAI.getGenerativeModel({ model: "gemini-pro" });\n const chat = model.startChat();\n \n const result = await chat.sendMessageStream("Explain quantum computing in detail");\n \n for await (const chunk of result.stream) {\n process.stdout.write(chunk.text());\n }\n}\n'})}),"\n",(0,s.jsx)(n.h4,{id:"structured-outputs",children:"Structured outputs"}),"\n",(0,s.jsx)(n.p,{children:"Here is how you can use structured outputs:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'async function structuredOutput() {\n const model = genAI.getGenerativeModel({\n model: "gemini-1.5-pro",\n generationConfig: {\
1n responseMimeType: "application/json",\n responseSchema: {\n type: "object",\n properties: {\n recipes: {\n type: "array",\n items: {\n type: "object",\n properties: {\n name: { type: "string" },\n ingredients: {\n type: "array",\n items: { type: "string" }\n },\n instructions: {\n type: "array",\n items: { type: "string" }\n },\n prep_time: { type: "string" },\n difficulty: {\n type: "string",\n enum: ["easy", "medium", "hard"]\n }\n },\n required: ["name", "ingredients", "instructions"]\n }\n }\n }\n }\n }\n });\n \n const prompt = "Give me 2 easy pasta recipes";\n const result = await model.generateContent(prompt);\n \n const jsonResponse = JSON.parse(result.response.text());\n console.log(jsonResponse);\n}\n'})}),"\n",(0,s.jsx)(n.h4,{id:"image-generation",children:"Image generation"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"async function generateImage() {\n const model = genAI.getGenerativeModel({ model: \"imagen-3.0-generate-001\" });\n \n const prompt = \"A serene mountain landscape with a crystal-clear lake reflecting snow-capped peaks\";\n \n const result = await model.generateContent({\n contents: [{ role: \"user\", parts: [{ text: prompt }] }]\n });\n \n // Get image data\n const imageData = result.response.candidates[0].content.parts[0].inlineData;\n \n // Save image\n const fs = require('fs');\n const buffer = Buffer.from(imageData.data, 'base64');\n fs.writeFileSync('generated_image.png', buffer);\n}\n"})}),"\n",(0,s.jsx)(n.h4,{id:"image-and-file-analysis",children:"Image and file analysis"}),"\n",(0,s.jsxs)(n.p,{children:["By pass in a message with ",(0,s.jsx)(n.code,{children:"inlineData"})," property, you can send binary data of any mime type to the AI."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"async function analyzeImage() {\n const model = genAI.getGenerativeModel({ model: \"gemini-pro-vision\" });\n \n // Read image file\n const fs = require('fs');\n const imageBuffer = fs.readFileSync('path/to/image.jpg');\n const imageBase64 = imageBuffer.toString('base64');\n \n const prompt = \"Describe this image in detail and identify any objects, people, or activities\";\n \n const result = await model.generateContent([\n { text: prompt },\n {\n inlineData: {\n mimeType: \"image/jpeg\",\n data: imageBase64\n }\n }\n ]);\n \n console.log(result.response.text());\n}\n"})}),"\n",(0,s.jsx)(n.h4,{id:"embeddings",children:"Embeddings"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'async function getTextEmbeddings() {\n const model = genAI.getGenerativeModel({ model: "embedding-001" });\n \n const texts = [\n "The quick brown fox jumps over the lazy dog",\n "Machine learning is a subset of artificial intelligence",\n "Python is a popular programming language for data science"\n ];\n \n const embeddings = [];\n \n for (const text of texts) {\n const result = await model.embedContent(text);\n embeddings.push({\n text: text,\n embedding: result.embedding.values\n });\n }\n \n return embeddings;\n}\n'})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:'function calculateCosineSimilarity(a, b) {\n const dotProduct = a.reduce((sum, val, i) => sum + val * b[i], 0);\n const magnitudeA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));\n const magnitudeB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));\n return dotProduct / (magnitudeA * magnitudeB);\n}\n\nasync function findSimilarDocuments(query, documentEmbeddings) {\n const model = genAI.getGenerativeModel({ model: "embedding-001" });\n \n // Get query embedding\n const queryResult = await model.embedContent(query);\n const queryEmbedding = queryResult.embedding.values;\n \n // Calculate similarities\n const similarities = documentEmbeddings.map(doc => ({\n ...doc,\n similarity: calculateCosineSimilarity(queryEmbedding, doc.embedding)\n }));\n \n // Sort by similarity\n return similarities.sort((a, b) => b.similarity - a.similarity);\n}\n'})}),"\n",(0,s.jsx)(n.h4,{id:"model-info",children:"Model info"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"async function countTokens() {\
1n const model = genAI.getGenerativeModel({ model: \"gemini-pro\" });\n \n const prompt = \"Tell me about the history of artificial intelligence\";\n const result = await model.countTokens(prompt);\n \n console.log('Total tokens:', result.totalTokens);\n console.log('Prompt tokens:', result.promptTokens);\n}\n\nasync function getModelInfo() {\n const model = genAI.getGenerativeModel({ model: \"gemini-pro\" });\n \n const info = await model.getModel();\n console.log('Model name:', info.name);\n console.log('Version:', info.version);\n console.log('Input token limit:', info.inputTokenLimit);\n console.log('Output token limit:', info.outputTokenLimit);\n}\n"})}),"\n",(0,s.jsx)(n.h4,{id:"best-practices",children:"Best practices"}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"messaging queue"})}),"\n",(0,s.jsx)(n.p,{children:"Here is a reusable way to generate AI messages through a messaging queue:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"// Implement proper resource management\nclass GeminiClient {\n constructor(apiKey) {\n this.genAI = new GoogleGenerativeAI(apiKey);\n this.requestQueue = [];\n this.processing = false;\n }\n \n async generateContent(prompt, options = {}) {\n return new Promise((resolve, reject) => {\n this.requestQueue.push({ prompt, options, resolve, reject });\n this.processQueue();\n });\n }\n \n async processQueue() {\n if (this.processing || this.requestQueue.length === 0) return;\n \n this.processing = true;\n const { prompt, options, resolve, reject } = this.requestQueue.shift();\n \n try {\n const model = this.genAI.getGenerativeModel(options);\n const result = await model.generateContent(prompt);\n resolve(result.response.text());\n } catch (error) {\n reject(error);\n } finally {\n this.processing = false;\n // Process next item\n setTimeout(() => this.processQueue(), 100);\n }\n }\n}\n"})})]})}function g(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>a,x:()=>r});var s=t(6540);const o={},i=s.createContext(o);function a(e){const n=s.useContext(i);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:a(e.components),s.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.