-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (@ai-sdk/cohere): add Cohere provider for text generation and st…
…reaming (#1954)
- Loading branch information
Showing
43 changed files
with
1,379 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<string,string>_ | ||
|
||
Custom headers to include in the requests. | ||
|
||
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_ | ||
|
||
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} /> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.