ai 3.2.15 → 3.2.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +204 -22
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
- package/react/dist/index.d.mts +1 -1
- package/react/dist/index.d.ts +1 -1
- package/rsc/dist/rsc-server.mjs +11 -1
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/svelte/dist/index.js +8 -7
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +8 -7
- package/svelte/dist/index.mjs.map +1 -1
package/README.md
CHANGED
@@ -1,37 +1,219 @@
|
|
1
|
-
|
1
|
+

|
2
2
|
|
3
|
-
|
3
|
+
# Vercel AI SDK
|
4
4
|
|
5
|
-
|
5
|
+
The [Vercel AI SDK](https://sdk.vercel.ai/docs) is a TypeScript toolkit designed to help you build AI-powered applications using popular frameworks like Next.js, React, Svelte, Vue and runtimes like Node.js.
|
6
6
|
|
7
|
-
|
8
|
-
- React Server Components API for streaming [Generative UI](https://vercel.com/blog/ai-sdk-3-generative-ui)
|
9
|
-
- First-class support for [OpenAI](https://openai.com), [Anthropic](https://www.anthropic.com), [Mistral](https://mistral.ai), [Perplexity](https://perplexity.ai), [AWS Bedrock](https://aws.amazon.com/bedrock/), [Azure](https://ai.azure.com), [Google Gemini](https://ai.google.dev), [Hugging Face](https://huggingface.co), [Fireworks](https://app.fireworks.ai), [Cohere](https://cohere.com), [LangChain](https://js.langchain.com/docs), [Replicate](https://replicate.com), Ollama, and more.
|
10
|
-
- Node.js, Serverless, and [Edge Runtime](https://edge-runtime.vercel.app/) support
|
11
|
-
- Lifecycle callbacks for saving completed streaming responses to a database (in the same request)
|
7
|
+
To learn more about how to use the Vercel AI SDK, check out our [API Reference](https://sdk.vercel.ai/docs/reference) and [Documentation](https://sdk.vercel.ai/docs).
|
12
8
|
|
13
9
|
## Installation
|
14
10
|
|
15
|
-
|
16
|
-
|
11
|
+
You will need Node.js 18+ and pnpm installed on your local development machine.
|
12
|
+
|
13
|
+
```shell
|
14
|
+
npm install ai
|
17
15
|
```
|
18
16
|
|
19
|
-
|
17
|
+
## Usage
|
20
18
|
|
21
|
-
|
19
|
+
### AI SDK Core
|
20
|
+
|
21
|
+
The [AI SDK Core](https://sdk.vercel.ai/docs/ai-sdk-core/overview) module provides a unified API to interact with model providers like [OpenAI](https://sdk.vercel.ai/providers/ai-sdk-providers/openai), [Anthropic](https://sdk.vercel.ai/providers/ai-sdk-providers/anthropic), [Google](https://sdk.vercel.ai/providers/ai-sdk-providers/google-generative-ai), and more.
|
22
|
+
|
23
|
+
You will then install the model provider of your choice.
|
24
|
+
|
25
|
+
```shell
|
26
|
+
npm install @ai-sdk/openai
|
27
|
+
```
|
28
|
+
|
29
|
+
###### @/index.ts (Node.js Runtime)
|
30
|
+
|
31
|
+
```ts
|
32
|
+
import { generateText } from 'ai';
|
33
|
+
import { openai } from '@ai-sdk/openai'; // Ensure OPENAI_API_KEY environment variable is set
|
34
|
+
|
35
|
+
async function main() {
|
36
|
+
const { text } = await generateText({
|
37
|
+
model: openai('gpt-4-turbo'),
|
38
|
+
system: 'You are a friendly assistant!',
|
39
|
+
prompt: 'Why is the sky blue?',
|
40
|
+
});
|
41
|
+
|
42
|
+
console.log(text);
|
43
|
+
}
|
44
|
+
|
45
|
+
main();
|
46
|
+
```
|
47
|
+
|
48
|
+
### AI SDK UI
|
49
|
+
|
50
|
+
The [AI SDK UI](https://sdk.vercel.ai/docs/ai-sdk-ui/overview) module provides a set of hooks that help you build chatbots and generative user interfaces. These hooks are framework agnostic, so they can be used in Next.js, React, Svelte, Vue, and SolidJS.
|
51
|
+
|
52
|
+
###### @/app/page.tsx (Next.js Pages Router)
|
53
|
+
|
54
|
+
```tsx
|
55
|
+
"use client"
|
56
|
+
|
57
|
+
import { useChat } from 'ai/react'
|
58
|
+
|
59
|
+
export default function Page() {
|
60
|
+
const { messages, input, handleSubmit, handleInputChange, isLoading } = useChat()
|
61
|
+
|
62
|
+
return (
|
63
|
+
<div>
|
64
|
+
{messages.map(message => (
|
65
|
+
<div key={message.id}>
|
66
|
+
<div>{message.role}</div>
|
67
|
+
<div>{message.content}</div>
|
68
|
+
</div>
|
69
|
+
)}
|
70
|
+
|
71
|
+
<form onSubmit={handleSubmit}>
|
72
|
+
<input
|
73
|
+
value={input}
|
74
|
+
placeholder="Send a message..."
|
75
|
+
onChange={handleInputChange}
|
76
|
+
disabled={isLoading}
|
77
|
+
/>
|
78
|
+
</form>
|
79
|
+
</div>
|
80
|
+
)
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
###### @/app/api/chat/route.ts (Next.js Pages Router)
|
85
|
+
|
86
|
+
```ts
|
87
|
+
import { CoreMessage, streamText } from 'ai';
|
88
|
+
import { openai } from '@ai-sdk/openai';
|
89
|
+
|
90
|
+
export async function POST(req: Request) {
|
91
|
+
const { messages }: { messages: CoreMessage[] } = await req.json();
|
22
92
|
|
23
|
-
|
93
|
+
const result = await streamText({
|
94
|
+
model: openai('gpt-4'),
|
95
|
+
system: 'You are a helpful assistant.',
|
96
|
+
messages,
|
97
|
+
});
|
24
98
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
99
|
+
return result.toAIStreamResponse();
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
### AI SDK RSC
|
104
|
+
|
105
|
+
The [AI SDK RSC](https://sdk.vercel.ai/docs/ai-sdk-rsc/overview) module provides an alternative API that also helps you build chatbots and generative user interfaces for frameworks that support [React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components) (RSC).
|
106
|
+
|
107
|
+
This API leverages the benefits of [Streaming](https://nextjs.org/docs/app/building-your-application/rendering/server-components#streaming) and [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations) offered by RSC, thus improving the developer experience of managing states between server/client and building generative user interfaces.
|
108
|
+
|
109
|
+
###### @/app/actions.tsx (Next.js App Router)
|
110
|
+
|
111
|
+
```tsx
|
112
|
+
import { streamUI } from 'ai/rsc';
|
113
|
+
import { z } from 'zod';
|
114
|
+
|
115
|
+
async function submitMessage() {
|
116
|
+
'use server';
|
117
|
+
|
118
|
+
const stream = await streamUI({
|
119
|
+
model: openai('gpt-4-turbo'),
|
120
|
+
messages: [
|
121
|
+
{ role: 'system', content: 'You are a friendly bot!' },
|
122
|
+
{ role: 'user', content: input },
|
123
|
+
],
|
124
|
+
text: ({ content, done }) => {
|
125
|
+
return <div>{content}</div>;
|
126
|
+
},
|
127
|
+
tools: {
|
128
|
+
deploy: {
|
129
|
+
description: 'Deploy repository to vercel',
|
130
|
+
parameters: z.object({
|
131
|
+
repositoryName: z
|
132
|
+
.string()
|
133
|
+
.describe('The name of the repository, example: vercel/ai-chatbot'),
|
134
|
+
}),
|
135
|
+
generate: async function* ({ repositoryName }) {
|
136
|
+
yield <div>Cloning repository {repositoryName}...</div>;
|
137
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
138
|
+
yield <div>Building repository {repositoryName}...</div>;
|
139
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
140
|
+
return <div>{repositoryName} deployed!</div>;
|
141
|
+
},
|
142
|
+
},
|
143
|
+
},
|
144
|
+
});
|
145
|
+
|
146
|
+
return {
|
147
|
+
ui: stream.value,
|
148
|
+
};
|
149
|
+
}
|
150
|
+
|
151
|
+
export const AI = createAI({
|
152
|
+
initialAIState: {},
|
153
|
+
initialUIState: {},
|
154
|
+
actions: {
|
155
|
+
submitMessage,
|
156
|
+
},
|
157
|
+
});
|
158
|
+
```
|
159
|
+
|
160
|
+
###### @/app/layout.tsx (Next.js App Router)
|
161
|
+
|
162
|
+
```tsx
|
163
|
+
import { ReactNode } from 'react';
|
164
|
+
import { AI } from '@/app/actions';
|
30
165
|
|
31
|
-
|
166
|
+
export default function Layout({ children }: { children: ReactNode }) {
|
167
|
+
<AI>{children}</AI>;
|
168
|
+
}
|
169
|
+
```
|
170
|
+
|
171
|
+
###### @/app/page.tsx (Next.js App Router)
|
172
|
+
|
173
|
+
```tsx
|
174
|
+
'use client';
|
175
|
+
|
176
|
+
import { useActions } from 'ai/rsc';
|
177
|
+
import { ReactNode, useState } from 'react';
|
178
|
+
|
179
|
+
export default function Page() {
|
180
|
+
const [input, setInput] = useState('');
|
181
|
+
const [messages, setMessages] = useState<ReactNode[]>([]);
|
182
|
+
const { submitMessage } = useActions();
|
183
|
+
|
184
|
+
return (
|
185
|
+
<div>
|
186
|
+
<input
|
187
|
+
value={input}
|
188
|
+
onChange={event => {
|
189
|
+
setInput(event.target.value);
|
190
|
+
}}
|
191
|
+
/>
|
192
|
+
<button
|
193
|
+
onClick={async () => {
|
194
|
+
const { ui } = await submitMessage(input);
|
195
|
+
setMessages(currentMessages => [...currentMessages, ui]);
|
196
|
+
}}
|
197
|
+
>
|
198
|
+
Submit
|
199
|
+
</button>
|
200
|
+
</div>
|
201
|
+
);
|
202
|
+
}
|
203
|
+
```
|
32
204
|
|
33
|
-
##
|
205
|
+
## Templates
|
34
206
|
|
35
|
-
|
207
|
+
We've built [templates](https://vercel.com/templates?type=ai) that include AI SDK integrations for different use cases, providers, and frameworks. You can use these templates to get started with your AI-powered application.
|
208
|
+
|
209
|
+
## Community
|
210
|
+
|
211
|
+
The Vercel AI SDK community can be found on [GitHub Discussions](https://github.com/vercel/ai/discussions) where you can ask questions, voice ideas, and share your projects with other people.
|
212
|
+
|
213
|
+
## Contributing
|
214
|
+
|
215
|
+
Contributions to the Vercel AI SDK are welcome and highly appreciated. However, before you jump right into it, we would like you to review our [Contribution Guidelines](https://github.com/vercel/ai/blob/main/CONTRIBUTING.md) to make sure you have smooth experience contributing to Vercel AI SDK.
|
216
|
+
|
217
|
+
## Authors
|
36
218
|
|
37
|
-
|
219
|
+
This library is created by [Vercel](https://vercel.com) and [Next.js](https://nextjs.org) team members, with contributions from the [Open Source Community](https://github.com/vercel/ai/graphs/contributors).
|
package/dist/index.d.mts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { DeepPartial, JSONValue, CreateMessage, FunctionCall as FunctionCall$1, AssistantMessage, DataMessage } from '@ai-sdk/ui-utils';
|
2
2
|
export { AssistantMessage, AssistantStatus, ChatRequest, ChatRequestOptions, CreateMessage, DataMessage, DeepPartial, Function, FunctionCall, FunctionCallHandler, IdGenerator, JSONValue, Message, RequestOptions, StreamPart, Tool, ToolCall, ToolCallHandler, ToolChoice, ToolInvocation, UseAssistantOptions, formatStreamPart, parseComplexResponse, parseStreamPart, readDataStream } from '@ai-sdk/ui-utils';
|
3
|
-
import { EmbeddingModelV1, EmbeddingModelV1Embedding, LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1LogProbs, LanguageModelV1CallWarning } from '@ai-sdk/provider';
|
3
|
+
import { EmbeddingModelV1, EmbeddingModelV1Embedding, LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1LogProbs, LanguageModelV1CallWarning, LanguageModelV1StreamPart } from '@ai-sdk/provider';
|
4
4
|
export { APICallError, EmptyResponseBodyError, InvalidArgumentError, InvalidDataContentError, InvalidPromptError, InvalidResponseDataError, InvalidToolArgumentsError, JSONParseError, LoadAPIKeyError, NoObjectGeneratedError, NoSuchToolError, RetryError, ToolCallParseError, TypeValidationError, UnsupportedFunctionalityError, UnsupportedJSONSchemaError } from '@ai-sdk/provider';
|
5
5
|
import { z } from 'zod';
|
6
6
|
import { ServerResponse } from 'http';
|
@@ -695,7 +695,7 @@ declare class StreamObjectResult<T> {
|
|
695
695
|
headers?: Record<string, string>;
|
696
696
|
};
|
697
697
|
constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
|
698
|
-
stream: ReadableStream<string |
|
698
|
+
stream: ReadableStream<string | Omit<LanguageModelV1StreamPart, 'text-delta'>>;
|
699
699
|
warnings: CallWarning[] | undefined;
|
700
700
|
rawResponse?: {
|
701
701
|
headers?: Record<string, string>;
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { DeepPartial, JSONValue, CreateMessage, FunctionCall as FunctionCall$1, AssistantMessage, DataMessage } from '@ai-sdk/ui-utils';
|
2
2
|
export { AssistantMessage, AssistantStatus, ChatRequest, ChatRequestOptions, CreateMessage, DataMessage, DeepPartial, Function, FunctionCall, FunctionCallHandler, IdGenerator, JSONValue, Message, RequestOptions, StreamPart, Tool, ToolCall, ToolCallHandler, ToolChoice, ToolInvocation, UseAssistantOptions, formatStreamPart, parseComplexResponse, parseStreamPart, readDataStream } from '@ai-sdk/ui-utils';
|
3
|
-
import { EmbeddingModelV1, EmbeddingModelV1Embedding, LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1LogProbs, LanguageModelV1CallWarning } from '@ai-sdk/provider';
|
3
|
+
import { EmbeddingModelV1, EmbeddingModelV1Embedding, LanguageModelV1, LanguageModelV1FinishReason, LanguageModelV1LogProbs, LanguageModelV1CallWarning, LanguageModelV1StreamPart } from '@ai-sdk/provider';
|
4
4
|
export { APICallError, EmptyResponseBodyError, InvalidArgumentError, InvalidDataContentError, InvalidPromptError, InvalidResponseDataError, InvalidToolArgumentsError, JSONParseError, LoadAPIKeyError, NoObjectGeneratedError, NoSuchToolError, RetryError, ToolCallParseError, TypeValidationError, UnsupportedFunctionalityError, UnsupportedJSONSchemaError } from '@ai-sdk/provider';
|
5
5
|
import { z } from 'zod';
|
6
6
|
import { ServerResponse } from 'http';
|
@@ -695,7 +695,7 @@ declare class StreamObjectResult<T> {
|
|
695
695
|
headers?: Record<string, string>;
|
696
696
|
};
|
697
697
|
constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
|
698
|
-
stream: ReadableStream<string |
|
698
|
+
stream: ReadableStream<string | Omit<LanguageModelV1StreamPart, 'text-delta'>>;
|
699
699
|
warnings: CallWarning[] | undefined;
|
700
700
|
rawResponse?: {
|
701
701
|
headers?: Record<string, string>;
|
package/dist/index.js
CHANGED
@@ -484,6 +484,16 @@ function getValidatedPrompt(prompt) {
|
|
484
484
|
message: "prompt and messages cannot be defined at the same time"
|
485
485
|
});
|
486
486
|
}
|
487
|
+
if (prompt.messages != null) {
|
488
|
+
for (const message of prompt.messages) {
|
489
|
+
if (message.role === "system" && typeof message.content !== "string") {
|
490
|
+
throw new import_provider3.InvalidPromptError({
|
491
|
+
prompt,
|
492
|
+
message: "system message content must be a string"
|
493
|
+
});
|
494
|
+
}
|
495
|
+
}
|
496
|
+
}
|
487
497
|
return prompt.prompt != null ? {
|
488
498
|
type: "prompt",
|
489
499
|
prompt: prompt.prompt,
|