Skip to content

armgit / mem0

Public Imported from GitHub
0

The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.

4.6 KB
Raw Blame
---
title: Cohere
description: "Configure Cohere as a reranker in Mem0 with support for English and multilingual reranking models."
---

Cohere provides enterprise-grade reranking models with excellent multilingual support and production-ready performance.

## Models

Cohere offers several reranking models:

- **`rerank-v3.5`** (default): Latest reranker, multilingual, best performance
- **`rerank-english-v3.0`**: Previous generation, English only
- **`rerank-multilingual-v3.0`**: Previous generation, multilingual

## Installation

```bash
pip install cohere
```

## Configuration

```python 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](/open-source/features/reranker-search#typescript-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`.

```bash
pnpm add cohere-ai
```

```typescript
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:

```bash
export COHERE_API_KEY="your-api-key"
```

## Usage Example

```python 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-5-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 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

- **High Quality**: Enterprise-grade relevance scoring
- **Multilingual**: Support for 100+ languages
- **Scalable**: Production-ready with high throughput
- **Reliable**: SLA-backed service with 99.9% uptime

## 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