Skip to main content

Google Generative AI

You can access Google's generative AI embeddings models through @langchain/google-genai integration package.

Get an API key here: https://ai.google.dev/tutorials/setup

You'll need to install the @langchain/google-genai package:

npm install @langchain/google-genai

Usage

import { GoogleGenerativeAIEmbeddings } from "@langchain/google-genai";
import { TaskType } from "@google/generative-ai";

/*
* Before running this, you should make sure you have created a
* Google Cloud Project that has `generativelanguage` API enabled.
*
* You will also need to generate an API key and set
* an environment variable GOOGLE_API_KEY
*
*/

const embeddings = new GoogleGenerativeAIEmbeddings({
model: "embedding-001", // 768 dimensions
taskType: TaskType.RETRIEVAL_DOCUMENT,
title: "Document title",
});

const res = await embeddings.embedQuery("OK Google");

console.log(res, res.length);

/*
[
0.010467986, -0.052334797, -0.05164676, -0.0092885755, 0.037551474,
0.007278041, -0.0014511136, -0.0002727135, -0.01205141, -0.028824795,
0.022447161, 0.032513272, -0.0075029004, 0.013371749, 0.03725578,
-0.0179886, -0.032127254, -0.019804858, -0.035530213, -0.057539217,
0.030938378, 0.022367297, -0.024294581, 0.011045744, 0.0026335048,
-0.018090524, 0.0066266404, -0.05072178, -0.025432976, 0.04673682,
-0.044976745, 0.009511519, -0.030653704, 0.0066106077, -0.03870159,
-0.04239313, 0.016969211, -0.015911, 0.020452755, 0.033449557,
-0.002724189, -0.049285132, -0.016055783, -0.0016474632, 0.013622627,
-0.012853559, -0.00383113, 0.0047683385, 0.029007262, -0.082496256,
0.055966448, 0.011457588, 0.04426033, -0.043971397, 0.029413547,
0.012740723, 0.03243298, -0.005483601, -0.01973574, -0.027495336,
0.0031939305, 0.02392931, -0.011409592, 0.053490978, -0.03130516,
-0.037364446, -0.028803863, 0.019082755, -0.00075289875, 0.015987953,
0.005136402, -0.045040093, 0.051010687, -0.06252348, -0.09334517,
-0.11461444, -0.007226655, 0.034570504, 0.017628446, 0.02613834,
-0.0043784343, -0.022333296, -0.053109482, -0.018441308, -0.10350664,
0.048912525, -0.042917475, -0.0014399975, 0.023028672, 0.00041137074,
0.019345555, -0.023254089, 0.060004912, -0.07684076, -0.04034909,
0.05221485, -0.015773885, -0.029030964, 0.02586164, -0.0401004,
... 668 more items
]
*/

API Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.