11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ import os
14
15
16
+ PROJECT_ID = os .getenv ("GOOGLE_CLOUD_PROJECT" )
15
17
16
- def use_context_cache (project_id : str , cache_id : str ) -> str :
18
+
19
+ def use_context_cache (cache_id : str ) -> str :
17
20
# [START generativeaionvertexai_gemini_use_context_cache]
18
21
import vertexai
19
22
20
23
from vertexai .preview .generative_models import GenerativeModel
21
24
from vertexai .preview import caching
22
25
23
26
# TODO(developer): Update and un-comment below lines
24
- # project_id = "PROJECT_ID "
25
- # cache_id = "CACHE_ID "
27
+ # PROJECT_ID = "your-project-id "
28
+ # cache_id = "your-cache-id "
26
29
27
- vertexai .init (project = project_id , location = "us-central1" )
30
+ vertexai .init (project = PROJECT_ID , location = "us-central1" )
28
31
29
32
cached_content = caching .CachedContent (cached_content_name = cache_id )
30
33
@@ -33,6 +36,13 @@ def use_context_cache(project_id: str, cache_id: str) -> str:
33
36
response = model .generate_content ("What are the papers about?" )
34
37
35
38
print (response .text )
39
+ # Example response:
40
+ # The provided text is about a new family of multimodal models called Gemini, developed by Google.
41
+ # ...
36
42
# [END generativeaionvertexai_gemini_use_context_cache]
37
43
38
44
return response .text
45
+
46
+
47
+ if __name__ == "__main__" :
48
+ use_context_cache ("1234567890" )
0 commit comments