翻篇收录说明
翻篇网页快照

Prompts | AI SDK

https://ai-sdk.dev/docs/foundations/prompts

最近抓取:2026-12-04 03:16:49(北京时间)

最近内容变化:2026-11-30 23:51:51(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Learn about the Prompt structure used in the AI SDK.

正文摘录

FoundationsMenuPromptsCopy for LLMView MarkdownAsk AI about this pagePrompts are instructions that you give a large language model (LLM) to tell it what to do. It's like when you ask someone for directions; the clearer your question, the better the directions you'll get. Many LLM providers offer complex interfaces for specifying prompts. They involve different roles and message types. While these interfaces are powerful, they can be hard to use and understand. In order to simplify prompting, the AI SDK supports text, message, and system prompts. Text Prompts Text prompts are strings. They are ideal for simple generation use cases, e.g. repeatedly generating content for variants of the same prompt text. You can set text prompts using the prompt property made available by AI SDK functions like streamText or generateText. You can structure the text in any way and inject variables, e.g. using a template literal. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", prompt: 'Invent a new holiday and describe its traditions.', }); You can also use template literals to provide dynamic data to your prompt. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", prompt: `I am planning a trip to ${destination} for ${lengthOfStay} days. ` + `Please suggest the best tourist activities for me to do.`, }); System Prompts System prompts are the initial set of instructions given to models that help guide and constrain the models' behaviors and responses. You can set system prompts using the instructions property. System prompts work with both the prompt and the messages properties. System messages in prompt or messages are rejected by default; use the instructions property for system instructions, or set allowSystemInMessages: true when you need to send existing message histories that contain system messages. Opting in with allowSystemInMessages can create a prompt injection risk where users can override or set the system prompt by injecting system messages. In most cases, only trusted server-side code should set system instructions via the instructions property. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", instructions: `You help plan travel itineraries. ` + `Respond to the users' request with a list ` + `of the best stops to make in their destination.`, prompt: `I am planning a trip to ${destination} for ${lengthOfStay} days. ` + `Please suggest the best tourist activities for me to do.`, }); Message Prompts A message prompt is an array of user, assistant, and tool messages. They are great for chat interfaces and more complex, multi-modal prompts. You can use the messages property to set message prompts. Each message has a role and a content property. The content can either be text (for user and assistant messages), or an array of relevant parts (data) for that message type. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", messages: [ { role: 'user', content: 'Hi!' }, { role: 'assistant', content: 'Hello, how can I help?' }, { role: 'user', content: 'Where can I buy the best Currywurst in Berlin?' }, ], }); Instead of sending a text in the content property, you can send an array of parts that includes a mix of text and other content parts. Not all language models support all message and content types. For example, some models might not be capable of handling multi-modal inputs or tool messages. Learn more about the capabilities of select models. Provider Options You can pass through additional provider-specific metadata to enable provider-specific functionality at 3 levels. Function Call Level Functions like streamText or generateText accept a providerOptions property. Adding provider options at the function call level should be used when you do not need granular control over where the provider options are applied. const { text } = await generateText({ model: azure('your-deployment-name'), providerOptions: { openai: { reasoningEffort: 'low', }, }, }); Message Level For granular control over applying provider options at the message level, you can pass providerOptions to the message object: GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", instructions: { role: 'system', content: 'Cached system message', providerOptions: { // Sets a cache control breakpoint on the system message anthropic: { cacheControl: { type: 'ephemeral' } }, }, }, prompt: 'Invent a new holiday and describe its traditions.', }); Message Part Level Certain provider-specific options require configuration at the message part level: import { ModelMessage } from 'ai'; const messages: ModelMessage[] = [ { role: 'user', content: [ { type: 'text', text: 'Describe the image in detail.', providerOptions: { openai: { imageDetail: 'low' }, }, }, { type: 'file', mediaType: 'image', data: 'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true', // Sets image detail configuration for image part: providerOptions: { openai: { imageDetail: 'low' }, }, }, ], }, ]; AI SDK UI hooks like useChat return arrays of UIMessage objects, which do not support provider options. We recommend using the convertToModelMessages function to convert UIMessage objects to ModelMessage objects before applying or appending message(s) or message parts with providerOptions. User Messages Text Parts Text content is the most common type of content. It is a string that is passed to the model. If you only need to send text content in a message, the content property can be a string, but you can also use it to send multiple content parts. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", messages: [ { role: 'user', content: [ { type: 'text', text: 'Where can I buy the best Currywurst in Berlin?', }, ], }, ], }); Image Parts User messages can include image parts. An image can be one of the following: base64-encoded image: string with base-64 encoded content data URL string, e.g. data:image/png;base64,... binary image: ArrayBuffer Uint8Array Buffer URL: http(s) URL string, e.g. https://example.com/image.png URL object, e.g. new URL('https://example.com/image.png') Example: Binary image (Buffer) const result = await generateText({ model, messages: [ { role: 'user', content: [ { type: 'text', text: 'Describe the image in detail.' }, { type: 'file', mediaType: 'image', data: fs.readFileSync('./data/comic-cat.png'), }, ], }, ], }); Example: Base-64 encoded image (string) GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", messages: [ { role: 'user', content: [ { type: 'text', text: 'Describe the image in detail.' }, { type: 'file', mediaType: 'image', data: fs.readFileSync('./data/comic-cat.png').toString('base64'), }, ], }, ], }); Example: Image URL (string) For untrusted image URLs, configure a guarded download transport before downloading them on your server. GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", messages: [ { role: 'user', content: [ { type: 'text', text: 'Describe the image in detail.' }, { type: 'file', mediaType: 'image', data: 'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true', }, ], }, ], }); File Parts Only a few providers and models currently support file parts: Google Generative AI, Google Vertex AI, OpenAI (for wav and mp3 audio with gpt-4o-audio-preview), Anthropic, OpenAI (for pdf). User messages can include file parts. A file can be one of the following: base64-encoded file: string with base-64 encoded content data URL string, e.g. data:image/png;base64,... binary data: ArrayBuffer Uint8Array Buffer URL: http(s) URL string, e.g. https://example.com/some.pdf URL object, e.g. new URL('https://example.com/some.pdf') You need to specify the MIME type of the file you are sending. The same URL download protections apply when your server downloads remote file parts. Example: PDF file from Buffer import { google } from '@ai-sdk/google'; import { generateText } from 'ai'; const result = await generateText({ model: google('gemini-4-hydrogen'), messages: [ { role: 'user', content: [ { type: 'text', text: 'What is the file about?' }, { type: 'file', mediaType: 'application/pdf', data: fs.readFileSync('./data/example.pdf'), filename: 'example.pdf', // optional, not used by all providers }, ], }, ], }); Example: mp3 audio file from Buffer import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-4o-audio-preview'), messages: [ { role: 'user', content: [ { type: 'text', text: 'What is the audio saying?' }, { type: 'file', mediaType: 'audio/mpeg', data: fs.readFileSync('./data/galileo.mp3'), }, ], }, ], }); Custom Download Function (Experimental) The default download only validates the URL itself; it does not check what a hostname resolves to. When your server downloads URLs supplied by users, models, or tools on Node.js, pass a guarded-fetch transport via experimental_download: createDownload({fetch}). See Secure URL Fetching. You can use custom download functions to implement throttling, retries, authentication, caching, and more. The default download implementation automatically downloads files in parallel when they are not supported by the model. Custom download function can be passed via the experimental_download property: GatewayProviderCustomClaude Sonnet 5.5const result = await generateText({ model: "anthropic/claude-sonnet-5.5", experimental_download: async ( requestedDownloads: Array<{ url: URL; isUrlSupportedByModel: boolean; abortSignal?: AbortSignal; }>, ): PromiseLike< Array<{ data: Uint8Array; mediaType: string | undefined; } | null> > => { // Forward each