Cohere Provider — Cohere 프로바이더

Cohere Provider — Cohere 프로바이더

Cohere의 chat API와 임베딩(embedding) 모델을 AI SDK에서 사용하는 방법을 알려드려요. Cohere 프로바이더는 Cohere chat API를 위한 언어·임베딩 모델 지원을 담고 있어요.

출처: 문서

본문

Cohere 프로바이더는 Cohere chat API에 대한 언어 및 임베딩 모델 지원을 포함해요.

설정 (Setup)

Cohere 프로바이더는 @ai-sdk/cohere 모듈에서 사용할 수 있어요. 다음으로 설치할 수 있어요:

npm install @ai-sdk/cohere

프로바이더 인스턴스 (Provider Instance)

@ai-sdk/cohere에서 기본 프로바이더 인스턴스 cohere를 import할 수 있어요:

import { cohere } from '@ai-sdk/cohere';

맞춤 설정이 필요하면 @ai-sdk/cohere에서 createCohere를 import하고 설정으로 프로바이더 인스턴스를 만들 수 있어요:

import { createCohere } from '@ai-sdk/cohere';

const cohere = createCohere({
  // custom settings
});

Cohere 프로바이더 인스턴스를 맞춤 설정하는 데 사용할 수 있는 선택적 설정은 다음과 같아요:

  • baseURL string — API 호출에 다른 URL 접두사를 사용해요 (예: 프록시 서버). 기본 접두사는 https://api.cohere.com/v2예요.
  • apiKey string — Authorization 헤더로 보내지는 API 키. 기본값은 COHERE_API_KEY 환경 변수예요.
  • headers Record<string,string> — 요청에 포함할 커스텀 헤더.
  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response> — 커스텀 fetch 구현. 기본값은 전역 fetch 함수예요. 요청을 가로채는 미들웨어로 사용하거나, 예를 들어 테스트용 맞춤 fetch 구현을 제공하는 데 사용할 수 있어요.
  • generateId () => string — 각 요청의 고유 ID를 생성하는 선택적 함수. 기본값은 SDK의 내장 ID 생성기예요.

언어 모델 (Language Models)

프로바이더 인스턴스로 Cohere chat API를 호출하는 모델을 만들 수 있어요. 첫 번째 인자는 모델 ID예요 (예: command-a-03-2025). 일부 Cohere chat 모델은 도구 호출을 지원해요.

const model = cohere('command-a-03-2025');

예시

Cohere 언어 모델을 generateText 함수로 텍스트를 생성하는 데 사용할 수 있어요:

import { cohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';

const { text } = await generateText({
  model: cohere('command-a-03-2025'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

Cohere 언어 모델은 streamText 함수에서도 사용할 수 있고, Output로 구조화된 데이터 생성을 지원해요 (AI SDK Core 참고).

모델 기능 (Model Capabilities)

Model Image Input Object Generation Tool Usage Tool Streaming
command-a-03-2025 ✗ ✓ ✓ ✓
command-a-reasoning-08-2025 ✗ ✓ ✓ ✓
command-a-vision-07-2025 ✓ ✓ ✓ ✓
command-r7b-12-2024 ✗ ✓ ✓ ✓
command-r-plus-04-2024 ✗ ✓ ✓ ✓
command-r-plus ✗ ✓ ✓ ✓
command-r-08-2024 ✗ ✓ ✓ ✓
command-r-03-2024 ✗ ✓ ✓ ✓
command-r ✗ ✓ ✓ ✓
command ✗ ✗ ✗ ✗
command-nightly ✗ ✗ ✗ ✗
command-light ✗ ✗ ✗ ✗
command-light-nightly ✗ ✗ ✗ ✗

위 표는 인기 모델을 나열한 거예요. 전체 사용 가능 모델 목록은 Cohere 문서를 참고하세요. 필요하면 사용 가능한 프로바이더 모델 ID를 문자열로 전달할 수도 있어요.

이미지 입력 (Image Inputs)

command-a-vision-07-2025 같은 비전 지원 Cohere 모델은 메시지 콘텐츠의 일부로 이미지 입력을 받아요. raw 바이트, base64 인코딩 문자열, URL로 이미지를 전달할 수 있어요:

import { cohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';
import { readFileSync } from 'node:fs';

const { text } = await generateText({
  model: cohere('command-a-vision-07-2025'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe the image in detail.' },
        {
          type: 'file',
          mediaType: 'image',
          data: readFileSync('./data/comic-cat.png'),
        },
      ],
    },
  ],
});

파일 part에 cohere 프로바이더 옵션을 사용해 이미지 입력 세부 정보를 high, low, auto로 설정할 수 있어요:

import { cohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';

const { text } = await generateText({
  model: cohere('command-a-vision-07-2025'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe the image in detail.' },
        {
          type: 'file',
          mediaType: 'image',
          data: 'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true',

          // Cohere 특정 옵션 - 이미지 세부 정보:
          providerOptions: {
            cohere: { detail: 'high' },
          },
        },
      ],
    },
  ],
});

Reasoning

Cohere는 command-a-reasoning-08-2025 모델로 reasoning을 도입했어요. 자세한 내용은 https://docs.cohere.com/docs/reasoning 에서 배울 수 있어요.

import { cohere, type CohereLanguageModelChatOptions } from '@ai-sdk/cohere';
import { generateText } from 'ai';

async function main() {
  const { text, reasoning } = await generateText({
    model: cohere('command-a-reasoning-08-2025'),
    prompt:
      "Alice has 3 brothers and she also has 2 sisters. How many sisters does Alice's brother have?",
    // optional: reasoning options
    providerOptions: {
      cohere: {
        thinking: {
          type: 'enabled',
          tokenBudget: 100,
        },
      } satisfies CohereLanguageModelChatOptions,
    },
  });

  console.log(reasoning);
  console.log(text);
}

main().catch(console.error);

임베딩 모델 (Embedding Models)

.embedding() 팩토리 메서드로 Cohere embed API를 호출하는 모델을 만들 수 있어요.

const model = cohere.embedding('embed-v4.0');

embed 함수로 Cohere 임베딩 모델을 사용해 임베딩을 생성할 수 있어요:

import { cohere, type CohereEmbeddingModelOptions } from '@ai-sdk/cohere';
import { embed } from 'ai';

const { embedding } = await embed({
  model: cohere.embedding('embed-v4.0'),
  value: 'sunny day at the beach',
  providerOptions: {
    cohere: {
      inputType: 'search_document',
    } satisfies CohereEmbeddingModelOptions,
  },
});

Cohere 임베딩 모델은 providerOptions.cohere로 전달할 수 있는 추가 프로바이더 옵션을 지원해요:

import { cohere, type CohereEmbeddingModelOptions } from '@ai-sdk/cohere';
import { embed } from 'ai';

const { embedding } = await embed({
  model: cohere.embedding('embed-v4.0'),
  value: 'sunny day at the beach',
  providerOptions: {
    cohere: {
      inputType: 'search_document',
      truncate: 'END',
    } satisfies CohereEmbeddingModelOptions,
  },
});

다음 프로바이더 옵션을 사용할 수 있어요:

  • inputType 'search_document' | 'search_query' | 'classification' | 'clustering' — 모델에 전달되는 입력 타입을 지정해요. 기본값은 search_query예요.
    • search_document: 검색 용도의 벡터 데이터베이스에 저장되는 임베딩에 사용.
    • search_query: 관련 문서를 찾기 위해 벡터 DB에 대해 실행되는 검색 쿼리의 임베딩에 사용.
    • classification: 텍스트 분류기를 통과하는 임베딩에 사용.
    • clustering: 클러스터링 알고리즘을 통과하는 임베딩에 사용.
  • truncate 'NONE' | 'START' | 'END' — API가 최대 토큰 길이보다 긴 입력을 처리하는 방식을 지정해요. 기본값은 END예요.
    • NONE: 선택 시 입력이 최대 입력 토큰 길이를 초과하면 에러를 반환.
    • START: 남은 입력이 모델의 최대 입력 토큰 길이와 정확히 같아질 때까지 입력의 시작 부분을 버림.
    • END: 남은 입력이 모델의 최대 입력 토큰 길이와 정확히 같아질 때까지 입력의 끝부분을 버림.

모델 기능 (Model Capabilities)

Model Embedding Dimensions
embed-english-v3.0 1024
embed-multilingual-v3.0 1024
embed-english-light-v3.0 384
embed-multilingual-light-v3.0 384
embed-english-v2.0 4096
embed-english-light-v2.0 1024
embed-multilingual-v2.0 768

재랭킹 모델 (Reranking Models)

.reranking() 팩토리 메서드로 Cohere rerank API를 호출하는 모델을 만들 수 있어요.

const model = cohere.reranking('rerank-v4.0-pro');

rerank 함수로 Cohere 재랭킹 모델을 사용해 문서를 재랭킹할 수 있어요:

import { cohere } from '@ai-sdk/cohere';
import { rerank } from 'ai';

const documents = [
  'sunny day at the beach',
  'rainy afternoon in the city',
  'snowy night in the mountains',
];

const { ranking } = await rerank({
  model: cohere.reranking('rerank-v4.0-pro'),
  documents,
  query: 'talk about rain',
  topN: 2,
});

console.log(ranking);
// [
//   { originalIndex: 1, score: 0.9, document: 'rainy afternoon in the city' },
//   { originalIndex: 0, score: 0.3, document: 'sunny day at the beach' }
// ]

Cohere 재랭킹 모델은 providerOptions.cohere로 전달할 수 있는 추가 프로바이더 옵션을 지원해요:

import { cohere, type CohereRerankingModelOptions } from '@ai-sdk/cohere';
import { rerank } from 'ai';

const { ranking } = await rerank({
  model: cohere.reranking('rerank-v4.0-pro'),
  documents: ['sunny day at the beach', 'rainy afternoon in the city'],
  query: 'talk about rain',
  providerOptions: {
    cohere: {
      maxTokensPerDoc: 1000,
      priority: 1,
    } satisfies CohereRerankingModelOptions,
  },
});

다음 프로바이더 옵션을 사용할 수 있어요:

  • maxTokensPerDoc number — 문서당 최대 토큰 수. 기본값은 4096이에요.
  • priority number — 요청의 우선순위. 기본값은 0이에요.

모델 기능 (Model Capabilities)

Model
rerank-v4.0-pro
rerank-v4.0-fast
rerank-v3.5
rerank-english-v3.0
rerank-multilingual-v3.0

더 알아보기 (Learn more)