|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | const {VertexAI} = require('@google-cloud/vertexai'); |
| 16 | +const axios = require('axios'); |
| 17 | + |
| 18 | +async function getBase64(url) { |
| 19 | + const image = await axios.get(url, {responseType: 'arraybuffer'}); |
| 20 | + return Buffer.from(image.data).toString('base64'); |
| 21 | +} |
16 | 22 |
|
17 | 23 | async function sendMultiModalPromptWithImage( |
18 | 24 | projectId = 'PROJECT_ID', |
19 | 25 | location = 'LOCATION_ID', |
20 | 26 | model = 'MODEL' |
21 | 27 | ) { |
22 | 28 | // [START aiplatform_gemini_single_turn_multi_image] |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment these variables before running the sample. |
| 31 | + */ |
| 32 | + // const projectId = 'your-project-id'; |
| 33 | + // const location = 'us-central1'; |
| 34 | + // const model = 'chosen-genai-model'; |
| 35 | + |
| 36 | + // For images, the SDK supports base64 strings |
| 37 | + const landmarkImage1 = await getBase64( |
| 38 | + 'https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark1.png' |
| 39 | + ); |
| 40 | + const landmarkImage2 = await getBase64( |
| 41 | + 'https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark1.png' |
| 42 | + ); |
| 43 | + const landmarkImage3 = await getBase64( |
| 44 | + 'https://storage.googleapis.com/cloud-samples-data/vertex-ai/llm/prompts/landmark1.png' |
| 45 | + ); |
| 46 | + |
| 47 | + // Initialize Vertex with your Cloud project and location |
| 48 | + const vertexAI = new VertexAI({project: projectId, location: location}); |
| 49 | + |
| 50 | + const generativeVisionModel = vertexAI.preview.getGenerativeModel({ |
| 51 | + model: model, |
| 52 | + }); |
| 53 | + |
| 54 | + // Pass multimodal prompt |
| 55 | + const request = { |
| 56 | + contents: [ |
| 57 | + { |
| 58 | + role: 'user', |
| 59 | + parts: [ |
| 60 | + { |
| 61 | + inlineData: { |
| 62 | + data: landmarkImage1, |
| 63 | + mimeType: 'image/png', |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + text: 'city: Rome, Landmark: the Colosseum', |
| 68 | + }, |
| 69 | + |
| 70 | + { |
| 71 | + inlineData: { |
| 72 | + data: landmarkImage2, |
| 73 | + mimeType: 'image/png', |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + text: 'city: Beijing, Landmark: Forbidden City', |
| 78 | + }, |
| 79 | + { |
| 80 | + inlineData: { |
| 81 | + data: landmarkImage3, |
| 82 | + mimeType: 'image/png', |
| 83 | + }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + ], |
| 88 | + }; |
| 89 | + |
| 90 | + // Create the response |
| 91 | + const response = await generativeVisionModel.generateContent(request); |
| 92 | + // Wait for the response to complete |
| 93 | + const aggregatedResponse = await response.response; |
| 94 | + // Select the text from the response |
| 95 | + const fullTextResponse = |
| 96 | + aggregatedResponse.candidates[0].content.parts[0].text; |
| 97 | + |
| 98 | + console.log(fullTextResponse); |
| 99 | + |
23 | 100 | // [END aiplatform_gemini_single_turn_multi_image] |
24 | 101 | } |
25 | 102 |
|
|
0 commit comments