Back to Mem0

Vertex AI

docs/components/embedders/models/vertexai.mdx

2.0.127.0 KB
Original Source

Vertex AI

Google Cloud's Vertex AI serves text embedding models such as gemini-embedding-001. Mem0 uses them through the provider's own SDK, which you install alongside Mem0.

Installation

The Vertex AI client is an optional dependency, so install it yourself.

<CodeGroup> ```bash Python pip install vertexai ```
bash
npm install @google-cloud/aiplatform
</CodeGroup>

Authentication

Both SDKs authenticate with Application Default Credentials. Pick whichever fits your environment:

  • Local development: run gcloud auth application-default login.
  • Service account: create a key in the Google Cloud Console and point GOOGLE_APPLICATION_CREDENTIALS at the JSON file, or pass its path through the embedder config.
  • Google Cloud runtimes (Cloud Run, GKE, Compute Engine): the attached service account is picked up automatically.

The TypeScript SDK reads the project ID from googleProjectId, then the GCP_PROJECT_ID, GOOGLE_CLOUD_PROJECT, and GCLOUD_PROJECT environment variables, and finally from your credentials. Set it explicitly when your credentials cover more than one project.

Usage

<CodeGroup> ```python Python import os from mem0 import Memory

Set the path to your Google Cloud credentials JSON file

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your/credentials.json" os.environ["OPENAI_API_KEY"] = "your_api_key" # For LLM

config = { "embedder": { "provider": "vertexai", "config": { "model": "gemini-embedding-001", "memory_add_embedding_type": "RETRIEVAL_DOCUMENT", "memory_update_embedding_type": "RETRIEVAL_DOCUMENT", "memory_search_embedding_type": "RETRIEVAL_QUERY" } } }

m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="john")


```typescript TypeScript
import { Memory } from "mem0ai/oss";

const config = {
  embedder: {
    provider: "vertexai",
    config: {
      model: "gemini-embedding-001",
      // Optional. Falls back to GCP_PROJECT_ID / GOOGLE_CLOUD_PROJECT /
      // GCLOUD_PROJECT, then to the project on your credentials.
      googleProjectId: process.env.GCP_PROJECT_ID,
      location: "us-central1",
      // Optional. Path to a service account key file, or pass the JSON inline
      // via googleServiceAccountJson.
      vertexCredentialsJson: "/path/to/your/credentials.json",
      embeddingDims: 256,
      memoryAddEmbeddingType: "RETRIEVAL_DOCUMENT",
      memoryUpdateEmbeddingType: "RETRIEVAL_DOCUMENT",
      memorySearchEmbeddingType: "RETRIEVAL_QUERY",
    },
  },
};

const memory = new Memory(config);
await memory.add("I love sci-fi movies but not thrillers", { userId: "john" });
</CodeGroup>

Embedding types

Vertex AI embeds the same text differently depending on the task you declare. The embedding types can be one of the following:

  • SEMANTIC_SIMILARITY
  • CLASSIFICATION
  • CLUSTERING
  • RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, QUESTION_ANSWERING, FACT_VERIFICATION
  • CODE_RETRIEVAL_QUERY

Check out the Vertex AI documentation for more information.

<Note> These embedding types map to the add, update, and search memory actions in both the Python and TypeScript SDKs. Stored memories use the add or update type, and searches use the search type. </Note>

Choosing a model

<Warning> `gemini-embedding-001` accepts **one input text per request**. When Mem0 embeds several texts at once, such as the memories extracted from a single conversation turn, it issues one request per text. The older `text-embedding-005` and `text-multilingual-embedding-002` models accept up to 250 texts per request, so they are faster and cheaper for large batches. See [Get text embeddings](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings). </Warning>

Config

Here are the parameters available for configuring the Vertex AI embedder:

<Tabs> <Tab title="Python"> | Parameter | Description | Default Value | | -------------------------------- | ---------------------------------------------------------- | ---------------------- | | `model` | The name of the Vertex AI embedding model to use | `gemini-embedding-001` | | `vertex_credentials_json` | Path to the Google Cloud credentials JSON file | `None` | | `embedding_dims` | Dimensions of the embedding model | `256` | | `memory_add_embedding_type` | The embedding type to use for the add memory action | `RETRIEVAL_DOCUMENT` | | `memory_update_embedding_type` | The embedding type to use for the update memory action | `RETRIEVAL_DOCUMENT` | | `memory_search_embedding_type` | The embedding type to use for the search memory action | `RETRIEVAL_QUERY` | </Tab> <Tab title="TypeScript"> | Parameter | Description | Default Value | | ----------------------------- | -------------------------------------------------------------------------- | ---------------------- | | `model` | The name of the Vertex AI embedding model to use | `gemini-embedding-001` | | `googleProjectId` | Google Cloud project ID (falls back to `GCP_PROJECT_ID` env var, then to your credentials) | Resolved from credentials | | `location` | Google Cloud region (falls back to `GCP_LOCATION` env var) | `us-central1` | | `vertexCredentialsJson` | Path to the Google Cloud credentials JSON file | `None` | | `googleServiceAccountJson` | Service account credentials as a JSON string or object | `None` | | `embeddingDims` | Dimensions of the embedding model | `256` | | `memoryAddEmbeddingType` | The embedding type to use for the add memory action | `RETRIEVAL_DOCUMENT` | | `memoryUpdateEmbeddingType` | The embedding type to use for the update memory action | `RETRIEVAL_DOCUMENT` | | `memorySearchEmbeddingType` | The embedding type to use for the search memory action | `RETRIEVAL_QUERY` | </Tab> </Tabs>