Skip to content
Source
Sign inSign up
TanStack/ai

packages/ai-cohere

README.md

TanStack AI

@tanstack/ai-cohere

Cohere adapter for TanStack AI. It gives you two things: multimodal embeddings with embed-v4.0, and document reranking with Cohere's rerank models — the precision step for RAG and search pipelines.

For chat, summarization, or media, use OpenAI, Anthropic, or Gemini.

Install

pnpm add @tanstack/ai @tanstack/ai-cohere

Setup

Get your API key from the Cohere Dashboard and set it as an environment variable:

export COHERE_API_KEY="..."

Usage

Embedding Adapter

import { cohereEmbedding } from '@tanstack/ai-cohere'
import { embed } from '@tanstack/ai'

const adapter = cohereEmbedding('embed-v4.0')

const result = await embed({
  adapter,
  input: ['a red guitar', 'a blue drum kit'],
  modelOptions: { inputType: 'search_document' },
})

console.log(result.embeddings[0].vector)

Multimodal Inputs

embed-v4.0 embeds text, images, and fused text+image items (one vector per input item). Fuse parts by nesting them in an array — the outer array is the item list:

const result = await embed({
  adapter,
  input: [
    'a red guitar',
    {
      type: 'image',
      source: { type: 'data', value: base64Png, mimeType: 'image/png' },
    },
    // A nested array fuses its parts into a single vector.
    [
      { type: 'text', content: 'product photo' },
      {
        type: 'image',
        source: { type: 'data', value: base64Jpeg, mimeType: 'image/jpeg' },
      },
    ],
  ],
  modelOptions: { inputType: 'search_document' },
})

Cohere does not fetch remote image URLs. Pass base64 data or a data: URI, or enable allowUrlFetch in the adapter config to have the adapter download http(s) URLs and inline them.

Rerank Adapter

import { rerank } from '@tanstack/ai'
import { cohereRerank } from '@tanstack/ai-cohere'

const { ranking, rerankedDocuments } = await rerank({
  adapter: cohereRerank('rerank-v3.5'),
  query: 'talk about rain',
  documents: ['sunny day at the beach', 'rainy afternoon in the city'],
  topN: 2,
})

console.log(rerankedDocuments[0]) // 'rainy afternoon in the city'

With Explicit API Key

Both adapters read COHERE_API_KEY from the environment. To pass a key explicitly, use the create* factories:

import { createCohereEmbedding, createCohereRerank } from '@tanstack/ai-cohere'

const embedAdapter = createCohereEmbedding(
  'embed-v4.0',
  process.env.COHERE_API_KEY!,
)
const rerankAdapter = createCohereRerank('rerank-v3.5', 'co-...')

Supported Models

Embedding Models

  • embed-v4.0 - Multimodal embedding model (text + images, Matryoshka dimensions via the top-level dimensions option)

Rerank Models

  • rerank-v3.5
  • rerank-english-v3.0
  • rerank-multilingual-v3.0

Features

  • ✅ Embeddings (batch, one request per input array)
  • ✅ Multimodal embedding input (text + images + fused text/image items)
  • ✅ Dimension reduction (dimensions → Cohere output_dimension)
  • ✅ Document reranking
  • ❌ Chat / text generation
  • ❌ Image generation

Tree-Shakeable Adapters

This package uses tree-shakeable adapters, so you only import what you need:

import { cohereEmbedding, cohereRerank } from '@tanstack/ai-cohere'

Read the docs ->

  • Reranking Guide — object documents, RAG pipelines, options, and the result shape.
  • Cohere Adapter — models, configuration, and explicit API keys.

License

MIT