Skip to content

Commit

Permalink
feat (@ai-sdk/cohere): add Cohere provider for text generation and st…
Browse files Browse the repository at this point in the history
…reaming (#1954)
  • Loading branch information
lgrammel committed Jun 17, 2024
1 parent 68ececd commit 8571289
Show file tree
Hide file tree
Showing 43 changed files with 1,379 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-flowers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider-utils': patch
---

feat (@ai-sdk/provider-utils): add createJsonStreamResponseHandler
5 changes: 5 additions & 0 deletions .changeset/six-dots-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/cohere': patch
---

feat (@ai-sdk/cohere): add Cohere provider for text generation and streaming
6 changes: 6 additions & 0 deletions .changeset/thick-games-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ai-sdk/provider-utils': patch
'ai': patch
---

chore (@ai-sdk/provider-utils): move test helper to provider utils
98 changes: 98 additions & 0 deletions content/providers/01-ai-sdk-providers/25-cohere.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: Cohere
description: Learn how to use the Cohere provider for the Vercel AI SDK.
---

# Cohere Provider

The [Cohere](https://cohere.com/) provider contains language model support for the Cohere chat API.

## Setup

The Cohere provider is available in the `@ai-sdk/cohere` module. You can install it with

<Tabs items={['pnpm', 'npm', 'yarn']}>
<Tab>
<Snippet text="pnpm install @ai-sdk/cohere" dark />
</Tab>
<Tab>
<Snippet text="npm install @ai-sdk/cohere" dark />
</Tab>
<Tab>
<Snippet text="yarn add @ai-sdk/cohere" dark />
</Tab>
</Tabs>

## Provider Instance

You can import the default provider instance `cohere` from `@ai-sdk/cohere`:

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

If you need a customized setup, you can import `createCohere` from `@ai-sdk/cohere`
and create a provider instance with your settings:

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

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

You can use the following optional settings to customize the Cohere provider instance:

- **baseURL** _string_

Use a different URL prefix for API calls, e.g. to use proxy servers.
The default prefix is `https://api.cohere.com/v1`.

- **apiKey** _string_

API key that is being send using the `Authorization` header.
It defaults to the `COHERE_API_KEY` environment variable.

- **headers** _Record&lt;string,string&gt;_

Custom headers to include in the requests.

- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_

Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
Defaults to the global `fetch` function.
You can use it as a middleware to intercept requests,
or to provide a custom fetch implementation for e.g. testing.

## Language Models

You can create models that call the [Cohere chat API](https://docs.cohere.ai/api/#operation/createChatCompletion) using provider instance.
The first argument is the model id, e.g. `command-r-plus`.
Some Cohere chat models support tool calls.

```ts
const model = cohere('command-r-plus');
```

### Example

You can use Cohere language models to generate text with the `generateText` function:

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

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

Cohere language models can also be used in the `streamText` function (see [AI SDK Core](/docs/ai-sdk-core)).

### Model Capabilities

| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
| ---------------- | ------------------- | ------------------- | ------------------- | ------------------- |
| `command-r-plus` | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
1 change: 1 addition & 0 deletions examples/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@ai-sdk/anthropic": "latest",
"@ai-sdk/azure": "latest",
"@ai-sdk/cohere": "latest",
"@ai-sdk/google": "latest",
"@ai-sdk/google-vertex": "latest",
"@ai-sdk/mistral": "latest",
Expand Down
18 changes: 18 additions & 0 deletions examples/ai-core/src/generate-text/cohere.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { cohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const { text, usage } = await generateText({
model: cohere('command-r-plus'),
prompt: 'Invent a new holiday and describe its traditions.',
});

console.log(text);
console.log();
console.log('Usage:', usage);
}

main().catch(console.error);
6 changes: 4 additions & 2 deletions examples/ai-core/src/generate-text/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import dotenv from 'dotenv';
dotenv.config();

async function main() {
const result = await generateText({
const { text, usage } = await generateText({
model: openai('gpt-3.5-turbo'),
prompt: 'Invent a new holiday and describe its traditions.',
});

console.log(result.text);
console.log(text);
console.log();
console.log('Usage:', usage);
}

main().catch(console.error);
22 changes: 22 additions & 0 deletions examples/ai-core/src/stream-text/cohere.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cohere } from '@ai-sdk/cohere';
import { streamText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await streamText({
model: cohere('command-r-plus'),
prompt: 'Invent a new holiday and describe its traditions.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LanguageModelV1Prompt } from '@ai-sdk/provider';
import {
JsonTestServer,
StreamingTestServer,
convertStreamToArray,
convertReadableStreamToArray,
} from '@ai-sdk/provider-utils/test';
import { AnthropicAssistantMessage } from './anthropic-messages-prompt';
import { createAnthropic } from './anthropic-provider';
Expand Down Expand Up @@ -337,7 +337,7 @@ describe('doStream', () => {
});

// note: space moved to last chunk bc of trimming
expect(await convertStreamToArray(stream)).toStrictEqual([
expect(await convertReadableStreamToArray(stream)).toStrictEqual([
{ type: 'text-delta', textDelta: 'Hello' },
{ type: 'text-delta', textDelta: ', ' },
{ type: 'text-delta', textDelta: 'World!' },
Expand Down Expand Up @@ -390,7 +390,7 @@ describe('doStream', () => {
prompt: TEST_PROMPT,
});

expect(await convertStreamToArray(stream)).toStrictEqual([
expect(await convertReadableStreamToArray(stream)).toStrictEqual([
{
type: 'text-delta',
textDelta: 'Okay',
Expand Down
35 changes: 35 additions & 0 deletions packages/cohere/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Vercel AI SDK - Cohere Provider

The **[Cohere provider](https://sdk.vercel.ai/providers/ai-sdk-providers/cohere)** for the [Vercel AI SDK](https://sdk.vercel.ai/docs) contains language model support for the Cohere API.

## Setup

The Cohere provider is available in the `@ai-sdk/cohere` module. You can install it with

```bash
npm i @ai-sdk/cohere
```

## Provider Instance

You can import the default provider instance `cohere` from `@ai-sdk/cohere`:

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

## Example

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

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

## Documentation

Please check out the **[Cohere provider](https://sdk.vercel.ai/providers/ai-sdk-providers/cohere)** for more information.
62 changes: 62 additions & 0 deletions packages/cohere/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@ai-sdk/cohere",
"version": "0.0.0",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"dev": "tsup --watch",
"lint": "eslint \"./**/*.ts*\"",
"type-check": "tsc --noEmit",
"prettier-check": "prettier --check \"./**/*.ts*\"",
"test": "pnpm test:node && pnpm test:edge",
"test:edge": "vitest --config vitest.edge.config.js --run",
"test:node": "vitest --config vitest.node.config.js --run"
},
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"dependencies": {
"@ai-sdk/provider": "0.0.10",
"@ai-sdk/provider-utils": "0.0.14"
},
"devDependencies": {
"@types/node": "^18",
"@vercel/ai-tsconfig": "workspace:*",
"tsup": "^8",
"typescript": "5.1.3",
"zod": "3.23.8"
},
"peerDependencies": {
"zod": "^3.0.0"
},
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public"
},
"homepage": "https://sdk.vercel.ai/docs",
"repository": {
"type": "git",
"url": "git+https://github.com/vercel/ai.git"
},
"bugs": {
"url": "https://github.com/vercel/ai/issues"
},
"keywords": [
"ai"
]
}
Loading

0 comments on commit 8571289

Please sign in to comment.