Call Tools with Image Prompt

Learn how to call tools with image prompt using the AI TOOLKIT and Node

1 min readnodetool usemultimodalView 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. import { generateText, tool } from 'ai-toolkit'; import { z } from 'zod'; const result = await generateText({ model: 'openai/gpt-4.1', messages: [ { role: 'user', content: [ { type: 'text', text: 'can you log this meal for me?' }, { type: 'image', image: new URL( 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Cheeseburger_%2817237580619%29.jpg/640px-Cheeseburger_%2817237580619%29.jpg', ), }, ], }, ], tools: { logFood: tool({ description: 'Log a food item', inputSchema: z.object({ name: z.string(), calories: z.number(), }), execute({ name, calories }) { storeInDatabase({ name, calories }); // your implementation here }, }), }, });

ts
  1. 1import { generateText, tool } from 'ai-toolkit';
  2. 2import { z } from 'zod';
  3. 3const result = await generateText({
  4. 4model: 'openai/gpt-4.1',
  5. 5messages: [
  6. 6{
  7. 7role: 'user',
  8. 8content: [
  9. 9{ type: 'text', text: 'can you log this meal for me?' },
  10. 10{
  11. 11type: 'image',
  12. 12image: new URL(
  13. 13'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Cheeseburger_%2817237580619%29.jpg/640px-Cheeseburger_%2817237580619%29.jpg',
  14. 14),
  15. 15},
  16. 16],
  17. 17},
  18. 18],
  19. 19tools: {
  20. 20logFood: tool({
  21. 21description: 'Log a food item',
  22. 22inputSchema: z.object({
  23. 23name: z.string(),
  24. 24calories: z.number(),
  25. 25}),
  26. 26execute({ name, calories }) {
  27. 27storeInDatabase({ name, calories }); // your implementation here
  28. 28},
  29. 29}),
  30. 30},
  31. 31});

Run it locally

$ npm install ai