Skip to main content

Overview

The Google provider integrates Gemini models for LLM and embedding capabilities with LlamaIndex.TS.

Installation

npm install @llamaindex/google

Gemini LLM

Basic Usage

import { Gemini } from "@llamaindex/google";

const llm = new Gemini({
  model: "gemini-2.0-flash-exp",
  apiKey: process.env.GOOGLE_API_KEY
});

const response = await llm.chat({
  messages: [
    { role: "user", content: "Explain machine learning" }
  ]
});

console.log(response.message.content);

Constructor Options

model
string
default:"gemini-2.0-flash-exp"
Gemini model name
apiKey
string
Google API key (defaults to GOOGLE_API_KEY env variable)
temperature
number
Sampling temperature
topP
number
default:1
Nucleus sampling
topK
number
Top-k sampling
maxTokens
number
Maximum output tokens

Supported Models

  • gemini-2.0-flash-exp: Latest flash model (experimental)
  • gemini-1.5-pro: Most capable
  • gemini-1.5-flash: Fast and efficient
  • gemini-1.0-pro: Production stable

Streaming

const stream = await llm.chat({
  messages: [{ role: "user", content: "Tell me about space" }],
  stream: true
});

for await (const chunk of stream) {
  process.stdout.write(chunk.delta);
}

Multi-modal Input

Gemini excels at multi-modal understanding:
const response = await llm.chat({
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "Describe this image" },
        {
          type: "image",
          data: imageBase64,
          mimeType: "image/jpeg"
        }
      ]
    }
  ]
});

Function Calling

import { tool } from "@llamaindex/core/tools";
import { z } from "zod";

const movieTool = tool({
  name: "find_movies",
  description: "Find movies by genre",
  parameters: z.object({
    genre: z.string(),
    year: z.number().optional()
  }),
  execute: async ({ genre, year }) => {
    return `Movies in ${genre}${year ? ` from ${year}` : ""}`;
  }
});

const response = await llm.chat({
  messages: [{ role: "user", content: "Find sci-fi movies from 2023" }],
  tools: [movieTool]
});

Gemini Embedding

Basic Usage

import { GeminiEmbedding } from "@llamaindex/google";

const embedModel = new GeminiEmbedding({
  model: "text-embedding-004",
  apiKey: process.env.GOOGLE_API_KEY
});

const embedding = await embedModel.getTextEmbedding(
  "LlamaIndex is a data framework"
);

console.log(embedding.length); // 768

Constructor Options

model
string
default:"text-embedding-004"
Embedding model name
apiKey
string
Google API key
embedBatchSize
number
default:10
Batch size for embeddings

Supported Embedding Models

  • text-embedding-004: Latest model (768 dimensions)
  • text-embedding-preview-0815: Preview model

Batch Embedding

const texts = [
  "First document",
  "Second document",
  "Third document"
];

const embeddings = await embedModel.getTextEmbeddingsBatch(texts);
console.log(embeddings.length); // 3

Configuration

Environment Variables

GOOGLE_API_KEY=AIza...
Get API key: Google AI Studio

Global Settings

import { Settings } from "llamaindex";
import { Gemini, GeminiEmbedding } from "@llamaindex/google";

Settings.llm = new Gemini({ model: "gemini-1.5-pro" });
Settings.embedModel = new GeminiEmbedding();

With LlamaIndex

import { Settings, VectorStoreIndex, Document } from "llamaindex";
import { Gemini, GeminiEmbedding } from "@llamaindex/google";

Settings.llm = new Gemini({ model: "gemini-1.5-flash" });
Settings.embedModel = new GeminiEmbedding();

const documents = [
  new Document({ text: "Document content..." })
];

const index = await VectorStoreIndex.fromDocuments(documents);
const queryEngine = index.asQueryEngine();

const response = await queryEngine.query({
  query: "What is this about?"
});

Safety Settings

const llm = new Gemini({
  model: "gemini-1.5-pro",
  safetySettings: [
    {
      category: "HARM_CATEGORY_HARASSMENT",
      threshold: "BLOCK_MEDIUM_AND_ABOVE"
    }
  ]
});

Best Practices

  1. Use Flash for speed: gemini-1.5-flash is fast and cost-effective
  2. Leverage multi-modal: Gemini excels at image understanding
  3. Monitor quotas: Free tier has daily limits
  4. Use appropriate safety settings: Configure for your use case
  5. Stream for better UX: Gemini supports streaming

See Also

Build docs developers (and LLMs) love