Vercel AI SDK 6.x : 基礎 – プロンプト

プロンプトは、大規模言語モデル (LLM) に何をするべきか伝えるために与える指示です。多くの LLM プロバイダーはプロンプトを指定するための複雑なインターフェイスを提供していますが、これらのインターフェイスは強力である一方で、使いにくく理解しにくい場合があります。
プロンプティングを単純化するため、AI SDK はテキスト、メッセージとシステムプロンプトをサポートしています。

Vercel AI SDK 6.x : 基礎 – プロンプト

作成 : Masashi Okumura (@classcat.com)
作成日時 : 01/06/2026
バージョン : ai@6.0.7

* 本記事は ai-sdk.dev/docs の以下のページを参考にしています :

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

Vercel AI SDK 6.x : 基礎 – プロンプト

プロンプトは、大規模言語モデル (LLM) に何をするべきか伝えるために与える指示です。それは誰かに道を尋ねるときに似ています; 質問が明確になるほど、より適切な道案内が得られます。

多くの LLM プロバイダーはプロンプトを指定するための複雑なインターフェイスを提供しています。それらは様々なロールとメッセージ型を備えています。これらのインターフェイスは強力である一方で、使いにくく理解しにくい場合があります。

プロンプティングを単純化するため、AI SDK はテキスト、メッセージとシステムプロンプトをサポートしています。

 

テキスト・プロンプト

テキストプロンプトは文字列です。それらは単純な生成ユースケースに最適です、例えば、同じプロンプトテキストのバリエーションのために繰り返しコンテンツを生成する場合。

streamTextgenerateObject のような AI SDK 関数で利用可能な prompt プロパティを使用してテキストプロンプトを設定できます。

テキストは任意の方法で構造化できて、例えばテンプレート・リテラルを使用して、変数を注入できます。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  prompt: 'Invent a new holiday and describe its traditions.',
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  prompt: 'Invent a new holiday and describe its traditions.',
});

テンプレートリテラルを使用して動的データをプロンプトに提供することもできます。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  prompt:
    `I am planning a trip to ${destination} for ${lengthOfStay} days. ` +
    `Please suggest the best tourist activities for me to do.`,
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  prompt:
    `I am planning a trip to ${destination} for ${lengthOfStay} days. ` +
    `Please suggest the best tourist activities for me to do.`,
});

 

システムプロンプト

システムプロンプトは、モデルに与えられる指示の初期セットで、モデルの動作と応答をガイドして制約するのに役立ちます。システムプロンプトは system プロパティを使用して設定できます。システムプロンプトは prompt と messages プロパティの両方で機能します。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  system:
    `You help planning 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.`,
});

Prompt

const result = await generateText({
  model: openai("gpt-4o-mini"),
  system:
    `You help planning 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.`,
});

ℹ️ メッセージ・プロンプトを使用する場合、システムプロンプトの代わりにシステムメッセージを使用することもできます。

 

メッセージ・プロンプト

メッセージプロンプトはユーザ、アシスタント、ツール・メッセージの配列です。それらはチャットインターフェイスや、より複雑なマルチモーダル・プロンプトに最適です。メッセージプロンプトを設定するには messages プロパティを使用できます。

各メッセージは role と content プロパティを持ちます。content は (ユーザとアシスタントメッセージの場合) テキスト、またはそのメッセージ型に関連するパーツ (データ) の配列のいずれかです。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  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?' },
  ],
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  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?' },
  ],
});

content プロパティでテキストを送信する代わりに、テキストとその他のコンテンツパーツのミックスを含む、パーツの配列を送信できます。

 

ユーザ・メッセージ

テキストパーツ

テキスト・コンテンツは最も一般的なタイプのコンテンツです。それはモデルに渡される文字列です。

メッセージでテキストコンテンツを送信する必要があるだけならば、content プロパティは文字列で構いませんが、それを使用して複数のコンテンツパーツを送信することもできます。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'Where can I buy the best Currywurst in Berlin?',
        },
      ],
    },
  ],
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'Where can I buy the best Currywurst in Berlin?',
        },
      ],
    },
  ],
});

 

画像パーツ

ユーザメッセージは画像パーツを含むことができます。画像は以下のいずれかです :

  • base64-encoded 画像:

    • base-64 encoded コンテンツを含む文字列
    • データ URL 文字列、e.g. data:image/png;base64,…

  • バイナル画像:

    • ArrayBuffer
    • Uint8Array
    • Buffer

  • URL:

    • http(s) URL 文字列, e.g. https://example.com/image.png
    • URL オブジェクト, e.g. new URL(‘https://example.com/image.png’)

 
例: バイナリ画像 (Buffer)

const result = await generateText({
  model,
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe the image in detail.' },
        {
          type: 'image',
          image: fs.readFileSync('./data/comic-cat.png'),
        },
      ],
    },
  ],
});

 
例: Base-64 encoded 画像 (文字列)

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe the image in detail.' },
        {
          type: 'image',
          image: fs.readFileSync('./data/comic-cat.png').toString('base64'),
        },
      ],
    },
  ],
});

 
例: 画像 URL (文字列)

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Describe the image in detail.' },
        {
          type: 'image',
          image:
            'https://github.com/vercel/ai/blob/main/examples/ai-core/data/comic-cat.png?raw=true',
        },
      ],
    },
  ],
});

 

ファイルパーツ

ℹ️ 現在、わずかの プロバイダーとモデルがファイルパーツをサポートしています: Google Generative AI, Google Vertex AI, OpenAI (for wav and mp3 audio with gpt-4o-audio-preview), Anthropic, OpenAI (for pdf).

ユーザメッセージはファイルパーツを含むことができます。ファイルは以下のいずれかです :

  • base64-encoded ファイル:

    • base-64 encoded コンテンツを含む文字列
    • データ URL 文字列, e.g. data:image/png;base64,…

  • バイナリデータ:

    • ArrayBuffer
    • Uint8Array
    • Buffer

  • URL:

    • http(s) URL 文字列, e.g. https://example.com/some.pdf
    • URL オブジェクト, e.g. new URL(‘https://example.com/some.pdf’)

 
送信するファイルの MIME タイプを指定する必要があります。

例: Buffer からの PDF ファイル

import { google } from '@ai-sdk/google';
import { generateText } from 'ai';

const result = await generateText({
  model: google('gemini-1.5-flash'),
  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
        },
      ],
    },
  ],
});

 
例: Buffer からの mp3 オーディオファイル

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'),
        },
      ],
    },
  ],
});

 

アシスタント・メッセージ

アシスタント・メッセージは assistant のロールを持つメッセージです。

それらは通常はアシスタントからの以前のレスポンスで、テキスト、推論、ツール呼び出しパーツを含みことができます。

 
例: テキストコンテンツを含むアシスタントメッセージ

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    { role: 'user', content: 'Hi!' },
    { role: 'assistant', content: 'Hello, how can I help?' },
  ],
});

 
例: 配列のテキストコンテンツを含むアシスタントメッセージ

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    { role: 'user', content: 'Hi!' },
    {
      role: 'assistant',
      content: [{ type: 'text', text: 'Hello, how can I help?' }],
    },
  ],
});

 
例: ツール呼び出しコンテンツを含むアシスタントメッセージ

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    { role: 'user', content: 'How many calories are in this block of cheese?' },
    {
      role: 'assistant',
      content: [
        {
          type: 'tool-call',
          toolCallId: '12345',
          toolName: 'get-nutrition-data',
          input: { cheese: 'Roquefort' },
        },
      ],
    },
  ],
});

 
例: ファイルコンテンツを含むアシスタントメッセージ

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    { role: 'user', content: 'Generate an image of a roquefort cheese!' },
    {
      role: 'assistant',
      content: [
        {
          type: 'file',
          mediaType: 'image/png',
          data: fs.readFileSync('./data/roquefort.jpg'),
        },
      ],
    },
  ],
});

 

ツール・メッセージ

ツール 呼び出しをサポートするモデルの場合、アシスタントメッセージはツール呼び出しパーツを含むことができて、ツールメッセージはツール出力パーツを含むことができます。単一のアシスタントメッセージは複数のツールを呼び出すことができて、単一のツールメッセージは複数のツールの結果を含むことができます。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'How many calories are in this block of cheese?',
        },
        { type: 'image', image: fs.readFileSync('./data/roquefort.jpg') },
      ],
    },
    {
      role: 'assistant',
      content: [
        {
          type: 'tool-call',
          toolCallId: '12345',
          toolName: 'get-nutrition-data',
          input: { cheese: 'Roquefort' },
        },
        // there could be more tool calls here (parallel calling)
      ],
    },
    {
      role: 'tool',
      content: [
        {
          type: 'tool-result',
          toolCallId: '12345', // needs to match the tool call id
          toolName: 'get-nutrition-data',
          output: {
            type: 'json',
            value: {
              name: 'Cheese, roquefort',
              calories: 369,
              fat: 31,
              protein: 22,
            },
          },
        },
        // there could be more tool results here (parallel calling)
      ],
    },
  ],
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'How many calories are in this block of cheese?',
        },
        { type: 'image', image: fs.readFileSync('./data/roquefort.jpg') },
      ],
    },
    {
      role: 'assistant',
      content: [
        {
          type: 'tool-call',
          toolCallId: '12345',
          toolName: 'get-nutrition-data',
          input: { cheese: 'Roquefort' },
        },
        // there could be more tool calls here (parallel calling)
      ],
    },
    {
      role: 'tool',
      content: [
        {
          type: 'tool-result',
          toolCallId: '12345', // needs to match the tool call id
          toolName: 'get-nutrition-data',
          output: {
            type: 'json',
            value: {
              name: 'Cheese, roquefort',
              calories: 369,
              fat: 31,
              protein: 22,
            },
          },
        },
        // there could be more tool results here (parallel calling)
      ],
    },
  ],
});

 

システムメッセージ

システムメッセージは、アシスタントの動作をガイドするために、ユーザメッセージの前にモデルに送信されるメッセージです。代わりに system プロパティを使用することもできます。

Gateway

const result = await generateText({
  model: "openai/gpt-4o-mini",
  messages: [
    { role: 'system', content: 'You help planning travel itineraries.' },
    {
      role: 'user',
      content:
        'I am planning a trip to Berlin for 3 days. Please suggest the best tourist activities for me to do.',
    },
  ],
});

Provider

const result = await generateText({
  model: openai("gpt-4o-mini"),
  messages: [
    { role: 'system', content: 'You help planning travel itineraries.' },
    {
      role: 'user',
      content:
        'I am planning a trip to Berlin for 3 days. Please suggest the best tourist activities for me to do.',
    },
  ],
});

 

以上