Generate Text with Image Prompt

Learn how to generate text with image prompt using the AI TOOLKIT and Node

1 min readnodemultimodalView source

Some language models that support vision capabilities accept images as part of the prompt. Here are some of the different formats you can use to include images as input.

Run it locally

$ npm install ai

URL

import { generateText } from 'ai-toolkit';

const result = await generateText({

model: 'openai/gpt-4.1',

maxOutputTokens: 512,

messages: [

{

role: 'user',

content: [

{

type: 'text',

text: 'what are the red things in this image?',

},

{

type: 'image',

image: new URL(

'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/2024_Solar_Eclipse_Prominences.jpg/720px-2024_Solar_Eclipse_Prominences.jpg',

),

},

],

},

],

});

console.log(result);

index.ts
ts
  1. 1import { generateText } from 'ai-toolkit';
  2. 2const result = await generateText({
  3. 3 model: 'openai/gpt-4.1',
  4. 4 maxOutputTokens: 512,
  5. 5 messages: [
  6. 6 {
  7. 7 role: 'user',
  8. 8 content: [
  9. 9 {
  10. 10 type: 'text',
  11. 11 text: 'what are the red things in this image?',
  12. 12 },
  13. 13 {
  14. 14 type: 'image',
  15. 15 image: new URL(
  16. 16 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/2024_Solar_Eclipse_Prominences.jpg/720px-2024_Solar_Eclipse_Prominences.jpg',
  17. 17 ),
  18. 18 },
  19. 19 ],
  20. 20 },
  21. 21 ],
  22. 22});
  23. 23console.log(result);

File Buffer

import { generateText } from 'ai-toolkit';

import fs from 'fs';

const result = await generateText({

model: 'openai/gpt-4.1',

maxOutputTokens: 512,

messages: [

{

role: 'user',

content: [

{

type: 'text',

text: 'what are the red things in this image?',

},

{

type: 'image',

image: fs.readFileSync('./node/attachments/eclipse.jpg', {

encoding: 'base64',

}),

},

],

},

],

});

console.log(result);

index.ts
ts
  1. 1import { generateText } from 'ai-toolkit';
  2. 2import fs from 'fs';
  3. 3const result = await generateText({
  4. 4 model: 'openai/gpt-4.1',
  5. 5 maxOutputTokens: 512,
  6. 6 messages: [
  7. 7 {
  8. 8 role: 'user',
  9. 9 content: [
  10. 10 {
  11. 11 type: 'text',
  12. 12 text: 'what are the red things in this image?',
  13. 13 },
  14. 14 {
  15. 15 type: 'image',
  16. 16 image: fs.readFileSync('./node/attachments/eclipse.jpg', {
  17. 17 encoding: 'base64',
  18. 18 }),
  19. 19 },
  20. 20 ],
  21. 21 },
  22. 22 ],
  23. 23});
  24. 24console.log(result);