|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] |
| 18 | + |
| 19 | +const {GoogleGenAI} = require('@google/genai'); |
| 20 | + |
| 21 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 22 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; |
| 23 | +async function generateContentCache( |
| 24 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 25 | + location = GOOGLE_CLOUD_LOCATION |
| 26 | +) { |
| 27 | + const client = new GoogleGenAI({ |
| 28 | + vertexai: true, |
| 29 | + project: projectId, |
| 30 | + location: location, |
| 31 | + httpOptions: { |
| 32 | + apiVersion: 'v1', |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const systemInstruction = ` |
| 37 | + You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts. |
| 38 | + Now look at these research papers, and answer the following questions. |
| 39 | + `; |
| 40 | + |
| 41 | + const contents = [ |
| 42 | + { |
| 43 | + role: 'user', |
| 44 | + parts: [ |
| 45 | + { |
| 46 | + fileData: { |
| 47 | + fileUri: |
| 48 | + 'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf', |
| 49 | + mimeType: 'application/pdf', |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + fileData: { |
| 54 | + fileUri: 'gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf', |
| 55 | + mimeType: 'application/pdf', |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + ]; |
| 61 | + |
| 62 | + const contentCache = await client.caches.create({ |
| 63 | + model: 'gemini-2.5-flash', |
| 64 | + config: { |
| 65 | + contents: contents, |
| 66 | + systemInstruction: systemInstruction, |
| 67 | + displayName: 'example-cache', |
| 68 | + ttl: '86400s', |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + console.log(contentCache); |
| 73 | + console.log(contentCache.name); |
| 74 | + |
| 75 | + // Example response: |
| 76 | + // projects/111111111111/locations/us-central1/cachedContents/1111111111111111111 |
| 77 | + // CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, |
| 78 | + // text_count=153, total_token_count=43130, video_duration_seconds=None) |
| 79 | + |
| 80 | + return contentCache.name; |
| 81 | +} |
| 82 | + |
| 83 | +// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] |
| 84 | + |
| 85 | +module.exports = { |
| 86 | + generateContentCache, |
| 87 | +}; |
0 commit comments