Generate Text with Image Prompt
Learn how to generate text with image prompt using the AI TOOLKIT and Node
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 aiURL
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);
- 1import { generateText } from 'ai-toolkit';
- 2const result = await generateText({
- 3 model: 'openai/gpt-4.1',
- 4 maxOutputTokens: 512,
- 5 messages: [
- 6 {
- 7 role: 'user',
- 8 content: [
- 9 {
- 10 type: 'text',
- 11 text: 'what are the red things in this image?',
- 12 },
- 13 {
- 14 type: 'image',
- 15 image: new URL(
- 16 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/2024_Solar_Eclipse_Prominences.jpg/720px-2024_Solar_Eclipse_Prominences.jpg',
- 17 ),
- 18 },
- 19 ],
- 20 },
- 21 ],
- 22});
- 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);
- 1import { generateText } from 'ai-toolkit';
- 2import fs from 'fs';
- 3const result = await generateText({
- 4 model: 'openai/gpt-4.1',
- 5 maxOutputTokens: 512,
- 6 messages: [
- 7 {
- 8 role: 'user',
- 9 content: [
- 10 {
- 11 type: 'text',
- 12 text: 'what are the red things in this image?',
- 13 },
- 14 {
- 15 type: 'image',
- 16 image: fs.readFileSync('./node/attachments/eclipse.jpg', {
- 17 encoding: 'base64',
- 18 }),
- 19 },
- 20 ],
- 21 },
- 22 ],
- 23});
- 24console.log(result);