Skip to content

Commit 63d587e

Browse files
authored
ai/core anthropic provider (no tool calls) (#1260)
1 parent b7732d7 commit 63d587e

25 files changed

+1036
-9
lines changed

.changeset/nine-pears-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Add Anthropic provider for ai/core functions (no tool calling).

.changeset/six-years-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Add automatic mime type detection for images in ai/core prompts.

docs/pages/docs/ai-core/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"openai": "OpenAI Provider",
1212
"mistral": "Mistral Provider",
1313
"google": "Google Provider",
14+
"anthropic": "Anthropic Provider",
1415
"custom-provider": "Custom Providers",
1516
"generate-text": "generateText API",
1617
"stream-text": "streamText API",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Anthropic Provider
3+
---
4+
5+
import { Callout } from 'nextra-theme-docs';
6+
7+
# Anthropic Provider
8+
9+
<Callout>The Anthropic provider does not support tool calling yet.</Callout>
10+
11+
The Anthropic provider contains language model support for the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post).
12+
It creates language model objects that can be used with the `generateText` and `streamText`AI functions.
13+
14+
## Provider Instance
15+
16+
You can import `Anthropic` from `ai/anthropic` and initialize a provider instance with various settings:
17+
18+
```ts
19+
import { Anthropic } from 'ai/anthropic';
20+
21+
const anthropic = new Anthropic({
22+
baseUrl: '', // optional base URL for proxies etc.
23+
apiKey: '', // optional API key, default to env property ANTHROPIC_API_KEY
24+
});
25+
```
26+
27+
The AI SDK also provides a shorthand `anthropic` import with a Anthropic provider instance that uses defaults:
28+
29+
```ts
30+
import { anthropic } from 'ai/anthropic';
31+
```
32+
33+
## Messages Models
34+
35+
You can create models that call the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post) using the `.messages()` factory method.
36+
The first argument is the model id, e.g. `claude-3-haiku-20240307`.
37+
Some models have multi-modal capabilities.
38+
39+
```ts
40+
const model = anthropic.messages('claude-3-haiku-20240307');
41+
```
42+
43+
Anthropic Messages` models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
44+
You can pass them as an options argument:
45+
46+
```ts
47+
const model = anthropic.messages('claude-3-haiku-20240307', {
48+
topK: 0.2,
49+
});
50+
```

docs/pages/docs/ai-core/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The AI SDK contains the following providers:
7575
- [OpenAI Provider](/docs/ai-core/openai) (`ai/openai`)
7676
- [Mistral Provider](/docs/ai-core/mistral) (`ai/mistral`)
7777
- [Google Provider](/docs/ai-core/google) (`ai/google`)
78+
- [Anthropic Provider](/docs/ai-core/anthropic) (`ai/anthropic`)
7879

7980
The AI SDK also provides a [language model specification](https://github.com/vercel/ai/tree/main/packages/core/spec/language-model/v1) that you can use to implement [custom providers](/docs/ai-core/custom-provider).
8081

examples/ai-core/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
ANTHROPIC_API_KEY=""
12
OPENAI_API_KEY=""
23
MISTRAL_API_KEY=""
3-
GOOGLE_GENERATIVE_AI_API_KEY=""
4+
GOOGLE_GENERATIVE_AI_API_KEY=""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { experimental_generateText } from 'ai';
2+
import { anthropic } from 'ai/anthropic';
3+
import dotenv from 'dotenv';
4+
import fs from 'node:fs';
5+
6+
dotenv.config();
7+
8+
async function main() {
9+
const result = await experimental_generateText({
10+
model: anthropic.messages('claude-3-haiku-20240307'),
11+
maxTokens: 512,
12+
messages: [
13+
{
14+
role: 'user',
15+
content: [
16+
{ type: 'text', text: 'Describe the image in detail.' },
17+
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
18+
],
19+
},
20+
],
21+
});
22+
23+
console.log(result.text);
24+
}
25+
26+
main();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { experimental_generateText } from 'ai';
2+
import { anthropic } from 'ai/anthropic';
3+
import dotenv from 'dotenv';
4+
5+
dotenv.config();
6+
7+
async function main() {
8+
const result = await experimental_generateText({
9+
model: anthropic.messages('claude-3-haiku-20240307'),
10+
prompt: 'Invent a new holiday and describe its traditions.',
11+
});
12+
13+
console.log(result.text);
14+
console.log();
15+
console.log('Token usage:', result.usage);
16+
console.log('Finish reason:', result.finishReason);
17+
}
18+
19+
main();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { experimental_streamText } from 'ai';
2+
import { anthropic } from 'ai/anthropic';
3+
import dotenv from 'dotenv';
4+
import fs from 'node:fs';
5+
6+
dotenv.config();
7+
8+
async function main() {
9+
const result = await experimental_streamText({
10+
model: anthropic.messages('claude-3-haiku-20240307'),
11+
maxTokens: 512,
12+
messages: [
13+
{
14+
role: 'user',
15+
content: [
16+
{ type: 'text', text: 'Describe the image in detail.' },
17+
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
18+
],
19+
},
20+
],
21+
});
22+
23+
for await (const textPart of result.textStream) {
24+
process.stdout.write(textPart);
25+
}
26+
}
27+
28+
main();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { experimental_streamText } from 'ai';
2+
import { anthropic } from 'ai/anthropic';
3+
import dotenv from 'dotenv';
4+
5+
dotenv.config();
6+
7+
async function main() {
8+
const result = await experimental_streamText({
9+
model: anthropic.messages('claude-3-haiku-20240307'),
10+
prompt: 'Invent a new holiday and describe its traditions.',
11+
});
12+
13+
for await (const textPart of result.textStream) {
14+
process.stdout.write(textPart);
15+
}
16+
}
17+
18+
main();

0 commit comments

Comments
 (0)