Skip to content

Commit aa7dedc

Browse files
authored
feat(testapps): add anthropic vision testapp
1 parent 6bd0508 commit aa7dedc

File tree

5 files changed

+141
-5
lines changed

5 files changed

+141
-5
lines changed

‎js/plugins/anthropic/README.md‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ const response = await ai.generate({
5656
{ text: 'What animal is in the photo?' },
5757
{ media: { url: imageUrl } },
5858
],
59-
config: {
60-
// control of the level of visual detail when processing image embeddings
61-
// Low detail level also decreases the token usage
62-
visualDetailLevel: 'low',
63-
},
6459
});
6560
console.log(response.text);
6661
```

‎js/testapps/anthropic/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ src/
1111
text-plain.ts - Text/plain error handling demonstration
1212
webp.ts - WEBP image handling demonstration
1313
pdf.ts - PDF document processing examples
14+
vision.ts - Image/vision analysis examples
1415
attention-first-page.pdf - Sample PDF file for testing
16+
sample-image.png - Sample image file for vision demo
1517
beta/
1618
basic.ts - Basic beta API examples
1719
```
@@ -39,6 +41,7 @@ src/
3941
- `pnpm run dev:stable:text-plain` – Start Dev UI for text/plain error handling demo.
4042
- `pnpm run dev:stable:webp` – Start Dev UI for WEBP image handling demo.
4143
- `pnpm run dev:stable:pdf` – Start Dev UI for PDF document processing demo.
44+
- `pnpm run dev:stable:vision` – Start Dev UI for image/vision analysis demo.
4245

4346
## Flows
4447

@@ -64,4 +67,9 @@ Each source file defines flows that can be invoked from the Dev UI or the Genkit
6467
- `stable-pdf-url` – Process a PDF from a publicly accessible URL
6568
- `stable-pdf-analysis` – Analyze a PDF document for key topics, concepts, and visual elements
6669

70+
### Vision/Image Analysis
71+
- `stable-vision-url` – Analyze an image from a public URL
72+
- `stable-vision-base64` – Analyze an image from a local file (base64 encoded)
73+
- `stable-vision-conversation` – Multi-turn conversation about an image
74+
6775
Example: `genkit flow:run anthropic-stable-hello`

‎js/testapps/anthropic/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dev:stable:text-plain": "genkit start -- npx tsx --watch src/stable/text-plain.ts",
1414
"dev:stable:webp": "genkit start -- npx tsx --watch src/stable/webp.ts",
1515
"dev:stable:pdf": "genkit start -- npx tsx --watch src/stable/pdf.ts",
16+
"dev:stable:vision": "genkit start -- npx tsx --watch src/stable/vision.ts",
1617
"genkit:dev": "cross-env GENKIT_ENV=dev npm run dev:stable",
1718
"genkit:start": "cross-env GENKIT_ENV=dev genkit start -- tsx --watch src/stable/basic.ts",
1819
"dev": "export GENKIT_RUNTIME_ID=$(openssl rand -hex 8) && node lib/stable/basic.js 2>&1"
217 KB
Loading
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)