Cohere - Mem0

Models

Cohere offers several reranking models:

Installation

pip install cohere

Configuration

Python

from mem0 import Memory

config = {
    "vector_store": {
        "provider": "chroma",
        "config": {
            "collection_name": "my_memories",
            "path": "./chroma_db"
        }
    },
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-5-mini"
        }
    },
    "reranker": {
        "provider": "cohere",
        "config": {
            "model": "rerank-v3.5",
            "api_key": "your-cohere-api-key",  # or set COHERE_API_KEY
            "top_k": 5,
            "return_documents": False,
            "max_chunks_per_doc": None
        }
    }
}

memory = Memory.from_config(config)

TypeScript (self-hosted)

The TypeScript OSS SDK (mem0ai/oss) ships the Cohere reranker. Config keys are camelCase, it defaults to the rerank-v3.5 model, and you opt in per search with rerank: true.

pnpm add cohere-ai
import { Memory } from "mem0ai/oss";

const memory = new Memory({
  reranker: {
    provider: "cohere",
    config: {
      apiKey: process.env.COHERE_API_KEY, // or set COHERE_API_KEY
      // model: "rerank-v3.5",     // default
      topK: 5,
    },
  },
});

const results = await memory.search("What is the user's profession?", {
  filters: { userId: "bob" },
  rerank: true,
});

Environment Variables

Set your API key as an environment variable:

export COHERE_API_KEY="your-api-key"

Usage Example

Python

import os
from mem0 import Memory

# Set API key
os.environ["COHERE_API_KEY"] = "your-api-key"

# Initialize memory with Cohere reranker
config = {
    "vector_store": {"provider": "chroma"},
    "llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}},
    "rerank": {
        "provider": "cohere",
        "config": {
            "model": "rerank-v3.5",
            "top_k": 3
        }
    }
}

memory = Memory.from_config(config)

# Add memories
messages = [\
    {"role": "user", "content": "I work as a data scientist at Microsoft"},\
    {"role": "user", "content": "I specialize in machine learning and NLP"},\
    {"role": "user", "content": "I enjoy playing tennis on weekends"}\
]

memory.add(messages, user_id="bob")

# Search with reranking
results = memory.search("What is the user's profession?", filters={"user_id": "bob"})

for result in results['results']:
    print(f"Memory: {result['memory']}")
    print(f"Vector Score: {result['score']:.3f}")
    print(f"Rerank Score: {result['rerank_score']:.3f}")
    print()

Multilingual Support

For multilingual applications, use the multilingual model:

Python

config = {
    "rerank": {
        "provider": "cohere",
        "config": {
            "model": "rerank-multilingual-v3.0",
            "top_k": 5
        }
    }
}

Configuration Parameters

Parameter Description Type Default
model Cohere rerank model to use str "rerank-v3.5"
api_key Cohere API key str None
top_k Maximum documents to return int None
return_documents Whether to return document texts bool False
max_chunks_per_doc Maximum chunks per document int None

Features

Best Practices

  1. Model Selection: rerank-v3.5 handles English and multilingual workloads; pin an older v3.0 model only if you need to reproduce prior results
  2. Batch Processing: Process multiple queries efficiently
  3. Error Handling: Implement retry logic for production systems
  4. Monitoring: Track reranking performance and costs