-
Notifications
You must be signed in to change notification settings - Fork 455
Explicit Caching #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Explicit Caching #355
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f13228d
*Inital prototype for explicit caching
mayureshagashe2105 a4ac7a5
Merge branch 'main' into caching
mayureshagashe2105 6fafe6b
rename get_cached_content to get
mayureshagashe2105 afd066d
Merge branch 'main' into caching
mayureshagashe2105 cfc936e
Stroke out functional approach for CachedContent CURD ops
mayureshagashe2105 e65d16e
blacken
mayureshagashe2105 d35cc71
Improve tests
mayureshagashe2105 d862dae
fix tests
mayureshagashe2105 2cde1a2
fix tests
mayureshagashe2105 e1d8c7a
Validate name checks for CachedContent creation
mayureshagashe2105 59663c8
Add tests
mayureshagashe2105 f37df8c
mark name as OPTIONAL for CachedContent creation
mayureshagashe2105 d1fd749
Add type-annotations to __new__ to fix pytype checks
mayureshagashe2105 17372e3
Add 'cached_content' to GenerativeModel's repr
mayureshagashe2105 645ceab
blacken
mayureshagashe2105 f48cedc
Fix types
mayureshagashe2105 a1c8c72
Fix docstrings
mayureshagashe2105 67472d3
Fix types
mayureshagashe2105 bf6551a
Fix types
mayureshagashe2105 82d3c5a
Merge branch 'main' of https://github.com/mayureshagashe2105/generati…
mayureshagashe2105 8e86ef1
Refactor for genai.protos module
mayureshagashe2105 4627fe1
use preview build
MarkDaoust fb9995c
Merge branch 'main' into caching
MarkDaoust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import annotations | ||
|
||
from typing import Optional, Iterable | ||
|
||
import google.ai.generativelanguage as glm | ||
|
||
from google.generativeai.types import caching_types | ||
from google.generativeai.types import content_types | ||
from google.generativeai.client import get_default_cache_client | ||
|
||
|
||
# alias for `caching_types.CachedContent`. | ||
CachedContent = caching_types.CachedContent | ||
|
||
|
||
def get_cached_content(name: str, client: glm.CacheServiceClient | None = None) -> CachedContent: | ||
"""Fetches required `CachedContent` resource. | ||
|
||
Args: | ||
name: name: The resource name referring to the cached content. | ||
|
||
Returns: | ||
`CachedContent` resource with specified name. | ||
""" | ||
return CachedContent.get_cached_content(name=name, client=client) | ||
|
||
|
||
def delete_cached_content(name: str, client: glm.CacheServiceClient | None = None) -> None: | ||
"""Deletes `CachedContent` resource. | ||
|
||
Args: | ||
name: The resource name referring to the cached content. | ||
Format: cachedContents/{id}. | ||
""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
if "cachedContents/" not in name: | ||
name = "cachedContents/" + name | ||
|
||
request = glm.DeleteCachedContentRequest(name=name) | ||
client.delete_cached_content(request) | ||
return | ||
|
||
|
||
def list_cached_contents( | ||
page_size: Optional[int] = 1, client: glm.CacheServiceClient | None = None | ||
) -> Iterable[CachedContent]: | ||
"""Lists `CachedContent` objects associated with the project. | ||
|
||
Args: | ||
page_size: The maximum number of permissions to return (per page). The service may return fewer `CachedContent` objects. | ||
|
||
Returns: | ||
A paginated list of `CachedContent` objects. | ||
""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
request = glm.ListCachedContentsRequest(page_size=page_size) | ||
for cached_content in client.list_cached_contents(request): | ||
yield caching_types.decode_cached_content(cached_content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.