|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { anthropic } from '@genkit-ai/anthropic'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import { genkit } from 'genkit'; |
| 20 | +import * as path from 'path'; |
| 21 | + |
| 22 | +const ai = genkit({ |
| 23 | + plugins: [anthropic()], |
| 24 | +}); |
| 25 | + |
| 26 | +/** |
| 27 | + * This flow demonstrates image analysis using a publicly accessible URL. |
| 28 | + * Claude will describe what it sees in the image. |
| 29 | + */ |
| 30 | +ai.defineFlow('stable-vision-url', async () => { |
| 31 | + // Using a Wikipedia Commons image (public domain) |
| 32 | + const imageUrl = |
| 33 | + 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg'; |
| 34 | + |
| 35 | + const { text } = await ai.generate({ |
| 36 | + model: anthropic.model('claude-sonnet-4-5'), |
| 37 | + messages: [ |
| 38 | + { |
| 39 | + role: 'user', |
| 40 | + content: [ |
| 41 | + { text: 'What do you see in this image? Describe it in detail.' }, |
| 42 | + { |
| 43 | + media: { |
| 44 | + url: imageUrl, |
| 45 | + }, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + |
| 52 | + return text; |
| 53 | +}); |
| 54 | + |
| 55 | +/** |
| 56 | + * This flow demonstrates image analysis using a local file. |
| 57 | + * The image is read from disk and sent as a base64 data URL. |
| 58 | + */ |
| 59 | +ai.defineFlow('stable-vision-base64', async () => { |
| 60 | + // Read image file from the same directory as this source file |
| 61 | + const imagePath = path.join(__dirname, 'sample-image.png'); |
| 62 | + const imageBuffer = fs.readFileSync(imagePath); |
| 63 | + const imageBase64 = imageBuffer.toString('base64'); |
| 64 | + |
| 65 | + const { text } = await ai.generate({ |
| 66 | + model: anthropic.model('claude-sonnet-4-5'), |
| 67 | + messages: [ |
| 68 | + { |
| 69 | + role: 'user', |
| 70 | + content: [ |
| 71 | + { |
| 72 | + text: 'Describe this image. What objects, colors, and scenes do you observe?', |
| 73 | + }, |
| 74 | + { |
| 75 | + media: { |
| 76 | + url: `data:image/png;base64,${imageBase64}`, |
| 77 | + contentType: 'image/png', |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }); |
| 84 | + |
| 85 | + return text; |
| 86 | +}); |
| 87 | + |
| 88 | +/** |
| 89 | + * This flow demonstrates multi-turn conversation about an image. |
| 90 | + * Claude can answer follow-up questions about images it has seen. |
| 91 | + */ |
| 92 | +ai.defineFlow('stable-vision-conversation', async () => { |
| 93 | + const imagePath = path.join(__dirname, 'sample-image.png'); |
| 94 | + const imageBuffer = fs.readFileSync(imagePath); |
| 95 | + const imageBase64 = imageBuffer.toString('base64'); |
| 96 | + |
| 97 | + const { text } = await ai.generate({ |
| 98 | + model: anthropic.model('claude-sonnet-4-5'), |
| 99 | + messages: [ |
| 100 | + { |
| 101 | + role: 'user', |
| 102 | + content: [ |
| 103 | + { text: 'What do you see in this image?' }, |
| 104 | + { |
| 105 | + media: { |
| 106 | + url: `data:image/png;base64,${imageBase64}`, |
| 107 | + contentType: 'image/png', |
| 108 | + }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }, |
| 112 | + { |
| 113 | + role: 'model', |
| 114 | + content: [ |
| 115 | + { |
| 116 | + text: 'I see a beautiful mountain landscape with a fjord or lake, green hills, and dramatic peaks under a blue sky with wispy clouds.', |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + { |
| 121 | + role: 'user', |
| 122 | + content: [ |
| 123 | + { |
| 124 | + text: 'What time of day do you think this photo was taken, and what season might it be?', |
| 125 | + }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + ], |
| 129 | + }); |
| 130 | + |
| 131 | + return text; |
| 132 | +}); |
0 commit comments