Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vercel/ai
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ai@3.0.17
Choose a base ref
...
head repository: vercel/ai
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ai@3.0.18
Choose a head ref
  • 3 commits
  • 25 files changed
  • 3 contributors

Commits on Apr 2, 2024

  1. Add docs for Google ai/core provider. (#1263)

    lgrammel authored Apr 2, 2024
    Copy the full SHA
    b7732d7 View commit details

Commits on Apr 4, 2024

  1. ai/core anthropic provider (no tool calls) (#1260)

    lgrammel authored Apr 4, 2024
    Copy the full SHA
    63d587e View commit details
  2. Version Packages (#1272)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Apr 4, 2024
    Copy the full SHA
    5e00440 View commit details
2 changes: 2 additions & 0 deletions docs/pages/docs/ai-core/_meta.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
"tools": "Tools and Tool Calling",
"openai": "OpenAI Provider",
"mistral": "Mistral Provider",
"google": "Google Provider",
"anthropic": "Anthropic Provider",
"custom-provider": "Custom Providers",
"generate-text": "generateText API",
"stream-text": "streamText API",
50 changes: 50 additions & 0 deletions docs/pages/docs/ai-core/anthropic.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Anthropic Provider
---

import { Callout } from 'nextra-theme-docs';

# Anthropic Provider

<Callout>The Anthropic provider does not support tool calling yet.</Callout>

The Anthropic provider contains language model support for the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post).
It creates language model objects that can be used with the `generateText` and `streamText`AI functions.

## Provider Instance

You can import `Anthropic` from `ai/anthropic` and initialize a provider instance with various settings:

```ts
import { Anthropic } from 'ai/anthropic';

const anthropic = new Anthropic({
baseUrl: '', // optional base URL for proxies etc.
apiKey: '', // optional API key, default to env property ANTHROPIC_API_KEY
});
```

The AI SDK also provides a shorthand `anthropic` import with a Anthropic provider instance that uses defaults:

```ts
import { anthropic } from 'ai/anthropic';
```

## Messages Models

You can create models that call the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post) using the `.messages()` factory method.
The first argument is the model id, e.g. `claude-3-haiku-20240307`.
Some models have multi-modal capabilities.

```ts
const model = anthropic.messages('claude-3-haiku-20240307');
```

Anthropic Messages` models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
You can pass them as an options argument:

```ts
const model = anthropic.messages('claude-3-haiku-20240307', {
topK: 0.2,
});
```
48 changes: 48 additions & 0 deletions docs/pages/docs/ai-core/google.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: Google Provider
---

import { Callout } from 'nextra-theme-docs';

# Google Provider

The Google provider contains language model support for the [Google Generative AI](https://ai.google/discover/generativeai/) APIs.
It creates language model objects that can be used with the `generateText`, `streamText`, `generateObject`, and `streamObject` AI functions.

## Provider Instance

You can import `Google` from `ai/google` and initialize a provider instance with various settings:

```ts
import { Google } from 'ai/google';

const google = new Google({
baseUrl: '', // optional base URL for proxies etc.
apiKey: '', // optional API key, default to env property GOOGLE_GENERATIVE_AI_API_KEY
});
```

The AI SDK also provides a shorthand `google` import with a Google provider instance that uses defaults:

```ts
import { google } from 'ai/google';
```

## Generative AI Models

You can create models that call the [Google Generative AI API](https://ai.google.dev/api/rest) using the `.generativeAI()` factory method.
The first argument is the model id, e.g. `models/gemini-pro`.
The models support tool calls and some have multi-modal capabilities.

```ts
const model = google.generativeAI('models/gemini-pro');
```

Google Generative AI models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
You can pass them as an options argument:

```ts
const model = google.generativeAI('models/gemini-pro', {
topK: 0.2,
});
```
2 changes: 2 additions & 0 deletions docs/pages/docs/ai-core/index.mdx
Original file line number Diff line number Diff line change
@@ -74,6 +74,8 @@ The AI SDK contains the following providers:

- [OpenAI Provider](/docs/ai-core/openai) (`ai/openai`)
- [Mistral Provider](/docs/ai-core/mistral) (`ai/mistral`)
- [Google Provider](/docs/ai-core/google) (`ai/google`)
- [Anthropic Provider](/docs/ai-core/anthropic) (`ai/anthropic`)

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).

3 changes: 2 additions & 1 deletion examples/ai-core/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ANTHROPIC_API_KEY=""
OPENAI_API_KEY=""
MISTRAL_API_KEY=""
GOOGLE_GENERATIVE_AI_API_KEY=""
GOOGLE_GENERATIVE_AI_API_KEY=""
26 changes: 26 additions & 0 deletions examples/ai-core/src/generate-text/anthropic-multimodal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { experimental_generateText } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';
import fs from 'node:fs';

dotenv.config();

async function main() {
const result = await experimental_generateText({
model: anthropic.messages('claude-3-haiku-20240307'),
maxTokens: 512,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
],
},
],
});

console.log(result.text);
}

main();
19 changes: 19 additions & 0 deletions examples/ai-core/src/generate-text/anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { experimental_generateText } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await experimental_generateText({
model: anthropic.messages('claude-3-haiku-20240307'),
prompt: 'Invent a new holiday and describe its traditions.',
});

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

main();
28 changes: 28 additions & 0 deletions examples/ai-core/src/stream-text/anthropic-multimodal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { experimental_streamText } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';
import fs from 'node:fs';

dotenv.config();

async function main() {
const result = await experimental_streamText({
model: anthropic.messages('claude-3-haiku-20240307'),
maxTokens: 512,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
],
},
],
});

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

main();
18 changes: 18 additions & 0 deletions examples/ai-core/src/stream-text/anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { experimental_streamText } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';

dotenv.config();

async function main() {
const result = await experimental_streamText({
model: anthropic.messages('claude-3-haiku-20240307'),
prompt: 'Invent a new holiday and describe its traditions.',
});

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

main();
7 changes: 7 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ai

## 3.0.18

### Patch Changes

- 63d587e: Add Anthropic provider for ai/core functions (no tool calling).
- 63d587e: Add automatic mime type detection for images in ai/core prompts.

## 3.0.17

### Patch Changes
17 changes: 17 additions & 0 deletions packages/core/anthropic/anthropic-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { z } from 'zod';
import { createJsonErrorResponseHandler } from '../spec';

const anthropicErrorDataSchema = z.object({
type: z.literal('error'),
error: z.object({
type: z.string(),
message: z.string(),
}),
});

export type AnthropicErrorData = z.infer<typeof anthropicErrorDataSchema>;

export const anthropicFailedResponseHandler = createJsonErrorResponseHandler({
errorSchema: anthropicErrorDataSchema,
errorToMessage: data => data.error.message,
});
58 changes: 58 additions & 0 deletions packages/core/anthropic/anthropic-facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { generateId, loadApiKey } from '../spec';
import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-model';
import {
AnthropicMessagesModelId,
AnthropicMessagesSettings,
} from './anthropic-messages-settings';

/**
* Anthropic provider.
*/
export class Anthropic {
readonly baseUrl?: string;
readonly apiKey?: string;

private readonly generateId: () => string;

constructor(
options: {
baseUrl?: string;
apiKey?: string;
generateId?: () => string;
} = {},
) {
this.baseUrl = options.baseUrl;
this.apiKey = options.apiKey;
this.generateId = options.generateId ?? generateId;
}

private get baseConfig() {
return {
baseUrl: this.baseUrl ?? 'https://api.anthropic.com/v1',
headers: () => ({
'anthropic-version': '2023-06-01',
'x-api-key': loadApiKey({
apiKey: this.apiKey,
environmentVariableName: 'ANTHROPIC_API_KEY',
description: 'Anthropic',
}),
}),
};
}

messages(
modelId: AnthropicMessagesModelId,
settings: AnthropicMessagesSettings = {},
) {
return new AnthropicMessagesLanguageModel(modelId, settings, {
provider: 'anthropic.messages',
...this.baseConfig,
generateId: this.generateId,
});
}
}

/**
* Default Anthropic provider instance.
*/
export const anthropic = new Anthropic();
Loading