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.18
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.19
Choose a head ref
  • 3 commits
  • 68 files changed
  • 3 contributors

Commits on Apr 5, 2024

  1. ai/core improvements (#1278)

    lgrammel authored Apr 5, 2024
    Copy the full SHA
    92fabdc View commit details
  2. ai/core: anthropic tool call support (#1279)

    lgrammel authored Apr 5, 2024
    Copy the full SHA
    4f4c7f5 View commit details
  3. Version Packages (#1280)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Apr 5, 2024
    Copy the full SHA
    b31818b View commit details
Showing with 620 additions and 213 deletions.
  1. +1 −1 docs/pages/docs/ai-core/anthropic.mdx
  2. +32 −0 examples/ai-core/src/generate-object/anthropic.ts
  3. +1 −1 examples/ai-core/src/generate-object/mistral-json.ts
  4. +1 −1 examples/ai-core/src/generate-object/mistral-tool.ts
  5. +1 −1 examples/ai-core/src/generate-object/mistral.ts
  6. +1 −1 examples/ai-core/src/generate-object/openai-json.ts
  7. +1 −1 examples/ai-core/src/generate-object/openai-tool.ts
  8. +1 −1 examples/ai-core/src/generate-object/openai.ts
  9. +65 −0 examples/ai-core/src/generate-text/anthropic-chatbot-with-tools.ts
  10. +1 −1 examples/ai-core/src/generate-text/anthropic-multimodal.ts
  11. +60 −0 examples/ai-core/src/generate-text/anthropic-tool-call.ts
  12. +1 −1 examples/ai-core/src/generate-text/anthropic.ts
  13. +1 −1 examples/ai-core/src/generate-text/google-multimodal.ts
  14. +1 −1 examples/ai-core/src/generate-text/google-tool-call.ts
  15. +1 −1 examples/ai-core/src/generate-text/google.ts
  16. +1 −1 examples/ai-core/src/generate-text/mistral-tool-call.ts
  17. +1 −1 examples/ai-core/src/generate-text/mistral.ts
  18. +1 −1 examples/ai-core/src/generate-text/openai-completion-chat.ts
  19. +1 −1 examples/ai-core/src/generate-text/openai-completion.ts
  20. +1 −1 examples/ai-core/src/generate-text/openai-multimodal-url.ts
  21. +1 −1 examples/ai-core/src/generate-text/openai-multimodal.ts
  22. +1 −1 examples/ai-core/src/generate-text/openai-tool-call.ts
  23. +1 −1 examples/ai-core/src/generate-text/openai.ts
  24. +1 −1 examples/ai-core/src/stream-object/mistral-json.ts
  25. +1 −1 examples/ai-core/src/stream-object/mistral-tool.ts
  26. +1 −1 examples/ai-core/src/stream-object/mistral.ts
  27. +1 −1 examples/ai-core/src/stream-object/openai-json.ts
  28. +1 −1 examples/ai-core/src/stream-object/openai-tool.ts
  29. +1 −1 examples/ai-core/src/stream-object/openai.ts
  30. +1 −1 examples/ai-core/src/stream-text/anthropic-multimodal.ts
  31. +1 −1 examples/ai-core/src/stream-text/anthropic.ts
  32. +1 −1 examples/ai-core/src/stream-text/google-fullstream.ts
  33. +1 −1 examples/ai-core/src/stream-text/google.ts
  34. +1 −1 examples/ai-core/src/stream-text/mistral-fullstream.ts
  35. +1 −1 examples/ai-core/src/stream-text/mistral.ts
  36. +1 −1 examples/ai-core/src/stream-text/openai-completion-chat.ts
  37. +1 −1 examples/ai-core/src/stream-text/openai-completion.ts
  38. +1 −1 examples/ai-core/src/stream-text/openai-fullstream.ts
  39. +1 −1 examples/ai-core/src/stream-text/openai-reader.ts
  40. +1 −1 examples/ai-core/src/stream-text/openai.ts
  41. +6 −0 packages/core/CHANGELOG.md
  42. +2 −5 packages/core/anthropic/anthropic-facade.ts
  43. +102 −53 packages/core/anthropic/anthropic-messages-language-model.test.ts
  44. +58 −30 packages/core/anthropic/anthropic-messages-language-model.ts
  45. +21 −9 packages/core/anthropic/anthropic-messages-prompt.ts
  46. +24 −25 packages/core/anthropic/convert-to-anthropic-messages-prompt.ts
  47. +3 −1 packages/core/anthropic/{map-anthropic-finish-reason.ts → map-anthropic-stop-reason.ts}
  48. +2 −2 packages/core/core/generate-object/generate-object.ts
  49. +3 −1 packages/core/core/generate-object/inject-json-schema-into-system.ts
  50. +2 −2 packages/core/core/generate-object/stream-object.ts
  51. +1 −0 packages/core/core/generate-text/generate-text.test.ts
  52. +2 −2 packages/core/core/generate-text/generate-text.ts
  53. +4 −10 packages/core/core/generate-text/run-tools-transformation.ts
  54. +2 −2 packages/core/core/generate-text/stream-text.ts
  55. +4 −5 packages/core/core/generate-text/tool-call.ts
  56. +1 −0 packages/core/core/generate-text/tool-result.ts
  57. +10 −0 packages/core/core/util/convert-zod-to-json-schema.ts
  58. +7 −3 packages/core/mistral/mistral-chat-language-model.test.ts
  59. +7 −3 packages/core/openai/openai-chat-language-model.test.ts
  60. +5 −3 packages/core/package.json
  61. +2 −0 packages/core/spec/errors/index.ts
  62. +19 −2 packages/core/spec/errors/no-such-tool-error.ts
  63. +54 −0 packages/core/spec/errors/tool-call-parse-error.ts
  64. +52 −0 packages/core/spec/errors/unsupported-json-schema-error.ts
  65. +2 −2 packages/core/spec/language-model/v1/language-model-v1-call-options.ts
  66. +9 −10 packages/core/spec/language-model/v1/language-model-v1-function-tool.ts
  67. +0 −1 packages/core/spec/util/json-schema.ts
  68. +22 −5 pnpm-lock.yaml
2 changes: 1 addition & 1 deletion docs/pages/docs/ai-core/anthropic.mdx
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { Callout } from 'nextra-theme-docs';

# Anthropic Provider

<Callout>The Anthropic provider does not support tool calling yet.</Callout>
<Callout>The Anthropic API does not support streaming tool calls 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.
32 changes: 32 additions & 0 deletions examples/ai-core/src/generate-object/anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { experimental_generateObject } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';
import { z } from 'zod';

dotenv.config();

async function main() {
const result = await experimental_generateObject({
model: anthropic.messages('claude-3-opus-20240229'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(
z.object({
name: z.string(),
amount: z.string(),
}),
),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});

console.log(JSON.stringify(result.object, null, 2));
console.log();
console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/mistral-json.ts
Original file line number Diff line number Diff line change
@@ -29,4 +29,4 @@ async function main() {
console.log(JSON.stringify(result.object, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/mistral-tool.ts
Original file line number Diff line number Diff line change
@@ -29,4 +29,4 @@ async function main() {
console.log(JSON.stringify(result.object, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/mistral.ts
Original file line number Diff line number Diff line change
@@ -31,4 +31,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/openai-json.ts
Original file line number Diff line number Diff line change
@@ -29,4 +29,4 @@ async function main() {
console.log(JSON.stringify(result.object, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/openai-tool.ts
Original file line number Diff line number Diff line change
@@ -30,4 +30,4 @@ async function main() {
console.log(JSON.stringify(result.object, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/openai.ts
Original file line number Diff line number Diff line change
@@ -31,4 +31,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
65 changes: 65 additions & 0 deletions examples/ai-core/src/generate-text/anthropic-chatbot-with-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ExperimentalMessage, experimental_generateText } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';
import * as readline from 'node:readline/promises';
import { weatherTool } from '../tools/weather-tool';

dotenv.config();

const terminal = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const messages: ExperimentalMessage[] = [];

async function main() {
let toolResponseAvailable = false;

while (true) {
if (!toolResponseAvailable) {
const userInput = await terminal.question('You: ');
messages.push({ role: 'user', content: userInput });
}

const { text, toolCalls, toolResults } = await experimental_generateText({
model: anthropic.messages('claude-3-opus-20240229'),
tools: { weatherTool },
system: `You are a helpful, respectful and honest assistant.`,
messages,
});

toolResponseAvailable = false;

if (text) {
process.stdout.write(`\nAssistant: ${text}`);
}

for (const { toolName, args } of toolCalls) {
process.stdout.write(
`\nTool call: '${toolName}' ${JSON.stringify(args)}`,
);
}

for (const { toolName, result } of toolResults) {
process.stdout.write(
`\nTool response: '${toolName}' ${JSON.stringify(result)}`,
);
}

process.stdout.write('\n\n');

messages.push({
role: 'assistant',
content: [{ type: 'text', text }, ...(toolCalls ?? [])],
});

if (toolResults.length > 0) {
messages.push({ role: 'tool', content: toolResults });
}

toolResponseAvailable = toolCalls.length > 0;
}
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/anthropic-multimodal.ts
Original file line number Diff line number Diff line change
@@ -23,4 +23,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
60 changes: 60 additions & 0 deletions examples/ai-core/src/generate-text/anthropic-tool-call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { experimental_generateText, tool } from 'ai';
import { anthropic } from 'ai/anthropic';
import dotenv from 'dotenv';
import { z } from 'zod';
import { weatherTool } from '../tools/weather-tool';

dotenv.config();

async function main() {
const result = await experimental_generateText({
model: anthropic.messages('claude-3-opus-20240229'),
maxTokens: 512,
tools: {
weather: weatherTool,
cityAttractions: tool({
parameters: z.object({ city: z.string() }),
}),
},
prompt:
'What is the weather in San Francisco and what attractions should I visit?',
});

// typed tool calls:
for (const toolCall of result.toolCalls) {
switch (toolCall.toolName) {
case 'cityAttractions': {
toolCall.args.city; // string
break;
}

case 'weather': {
toolCall.args.location; // string
break;
}
}
}

// typed tool results for tools with execute method:
for (const toolResult of result.toolResults) {
switch (toolResult.toolName) {
// NOT AVAILABLE (NO EXECUTE METHOD)
// case 'cityAttractions': {
// toolResult.args.city; // string
// toolResult.result;
// break;
// }

case 'weather': {
toolResult.args.location; // string
toolResult.result.location; // string
toolResult.result.temperature; // number
break;
}
}
}

console.log(JSON.stringify(result, null, 2));
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/anthropic.ts
Original file line number Diff line number Diff line change
@@ -16,4 +16,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/google-multimodal.ts
Original file line number Diff line number Diff line change
@@ -23,4 +23,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/google-tool-call.ts
Original file line number Diff line number Diff line change
@@ -57,4 +57,4 @@ async function main() {
console.log(JSON.stringify(result, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/google.ts
Original file line number Diff line number Diff line change
@@ -16,4 +16,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/mistral-tool-call.ts
Original file line number Diff line number Diff line change
@@ -59,4 +59,4 @@ async function main() {
console.log(JSON.stringify(result, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/mistral.ts
Original file line number Diff line number Diff line change
@@ -18,4 +18,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -30,4 +30,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/openai-completion.ts
Original file line number Diff line number Diff line change
@@ -16,4 +16,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -29,4 +29,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/openai-multimodal.ts
Original file line number Diff line number Diff line change
@@ -25,4 +25,4 @@ async function main() {
console.log(result.text);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/openai-tool-call.ts
Original file line number Diff line number Diff line change
@@ -59,4 +59,4 @@ async function main() {
console.log(JSON.stringify(result, null, 2));
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/openai.ts
Original file line number Diff line number Diff line change
@@ -18,4 +18,4 @@ async function main() {
console.log('Finish reason:', result.finishReason);
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/mistral-json.ts
Original file line number Diff line number Diff line change
@@ -33,4 +33,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/mistral-tool.ts
Original file line number Diff line number Diff line change
@@ -33,4 +33,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/mistral.ts
Original file line number Diff line number Diff line change
@@ -32,4 +32,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/openai-json.ts
Original file line number Diff line number Diff line change
@@ -33,4 +33,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/openai-tool.ts
Original file line number Diff line number Diff line change
@@ -33,4 +33,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/openai.ts
Original file line number Diff line number Diff line change
@@ -32,4 +32,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/anthropic-multimodal.ts
Original file line number Diff line number Diff line change
@@ -25,4 +25,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/anthropic.ts
Original file line number Diff line number Diff line change
@@ -15,4 +15,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/google-fullstream.ts
Original file line number Diff line number Diff line change
@@ -77,4 +77,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/google.ts
Original file line number Diff line number Diff line change
@@ -15,4 +15,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/mistral-fullstream.ts
Original file line number Diff line number Diff line change
@@ -73,4 +73,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/mistral.ts
Original file line number Diff line number Diff line change
@@ -20,4 +20,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/openai-completion-chat.ts
Original file line number Diff line number Diff line change
@@ -32,4 +32,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/openai-completion.ts
Original file line number Diff line number Diff line change
@@ -19,4 +19,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/openai-fullstream.ts
Original file line number Diff line number Diff line change
@@ -79,4 +79,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/openai-reader.ts
Original file line number Diff line number Diff line change
@@ -25,4 +25,4 @@ async function main() {
}
}

main();
main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/openai.ts
Original file line number Diff line number Diff line change
@@ -20,4 +20,4 @@ async function main() {
}
}

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

## 3.0.19

### Patch Changes

- 4f4c7f5: ai/core: Anthropic tool call support

## 3.0.18

### Patch Changes
Loading