-
Notifications
You must be signed in to change notification settings - Fork 467
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 all 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,260 @@ | ||
# -*- 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 | ||
|
||
import dataclasses | ||
import datetime | ||
from typing import Any, Iterable, Optional | ||
|
||
from google.generativeai import protos | ||
from google.generativeai.types.model_types import idecode_time | ||
from google.generativeai.types import caching_types | ||
from google.generativeai.types import content_types | ||
from google.generativeai.utils import flatten_update_paths | ||
from google.generativeai.client import get_default_cache_client | ||
|
||
from google.protobuf import field_mask_pb2 | ||
import google.ai.generativelanguage as glm | ||
|
||
|
||
@dataclasses.dataclass | ||
class CachedContent: | ||
"""Cached content resource.""" | ||
|
||
name: str | ||
model: str | ||
create_time: datetime.datetime | ||
update_time: datetime.datetime | ||
expire_time: datetime.datetime | ||
|
||
# NOTE: Automatic CachedContent deletion using contextmanager is not P0(P1+). | ||
# Adding basic support for now. | ||
def __enter__(self): | ||
mayureshagashe2105 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, exc_tb): | ||
self.delete() | ||
|
||
def _to_dict(self) -> protos.CachedContent: | ||
proto_paths = { | ||
"name": self.name, | ||
"model": self.model, | ||
} | ||
return protos.CachedContent(**proto_paths) | ||
|
||
def _apply_update(self, path, value): | ||
parts = path.split(".") | ||
for part in parts[:-1]: | ||
self = getattr(self, part) | ||
if parts[-1] == "ttl": | ||
value = self.expire_time + datetime.timedelta(seconds=value["seconds"]) | ||
parts[-1] = "expire_time" | ||
setattr(self, parts[-1], value) | ||
|
||
@classmethod | ||
def _decode_cached_content(cls, cached_content: protos.CachedContent) -> CachedContent: | ||
# not supposed to get INPUT_ONLY repeated fields, but local gapic lib build | ||
# is returning these, hence setting including_default_value_fields to False | ||
cached_content = type(cached_content).to_dict( | ||
cached_content, including_default_value_fields=False | ||
) | ||
|
||
idecode_time(cached_content, "create_time") | ||
idecode_time(cached_content, "update_time") | ||
# always decode `expire_time` as Timestamp is returned | ||
# regardless of what was sent on input | ||
idecode_time(cached_content, "expire_time") | ||
return cls(**cached_content) | ||
|
||
@staticmethod | ||
def _prepare_create_request( | ||
model: str, | ||
name: str | None = None, | ||
system_instruction: Optional[content_types.ContentType] = None, | ||
contents: Optional[content_types.ContentsType] = None, | ||
tools: Optional[content_types.FunctionLibraryType] = None, | ||
tool_config: Optional[content_types.ToolConfigType] = None, | ||
ttl: Optional[caching_types.ExpirationTypes] = datetime.timedelta(hours=1), | ||
) -> protos.CreateCachedContentRequest: | ||
"""Prepares a CreateCachedContentRequest.""" | ||
if name is not None: | ||
if not caching_types.valid_cached_content_name(name): | ||
raise ValueError(caching_types.NAME_ERROR_MESSAGE.format(name=name)) | ||
|
||
name = "cachedContents/" + name | ||
|
||
if "/" not in model: | ||
model = "models/" + model | ||
|
||
if system_instruction: | ||
system_instruction = content_types.to_content(system_instruction) | ||
|
||
tools_lib = content_types.to_function_library(tools) | ||
if tools_lib: | ||
tools_lib = tools_lib.to_proto() | ||
|
||
if tool_config: | ||
tool_config = content_types.to_tool_config(tool_config) | ||
|
||
if contents: | ||
contents = content_types.to_contents(contents) | ||
|
||
if ttl: | ||
ttl = caching_types.to_ttl(ttl) | ||
|
||
cached_content = protos.CachedContent( | ||
name=name, | ||
model=model, | ||
system_instruction=system_instruction, | ||
contents=contents, | ||
tools=tools_lib, | ||
tool_config=tool_config, | ||
ttl=ttl, | ||
) | ||
|
||
return protos.CreateCachedContentRequest(cached_content=cached_content) | ||
|
||
@classmethod | ||
def create( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Async? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't implement it, due to the following reason:
|
||
cls, | ||
model: str, | ||
name: str | None = None, | ||
system_instruction: Optional[content_types.ContentType] = None, | ||
contents: Optional[content_types.ContentsType] = None, | ||
tools: Optional[content_types.FunctionLibraryType] = None, | ||
tool_config: Optional[content_types.ToolConfigType] = None, | ||
ttl: Optional[caching_types.ExpirationTypes] = datetime.timedelta(hours=1), | ||
client: glm.CacheServiceClient | None = None, | ||
) -> CachedContent: | ||
"""Creates `CachedContent` resource. | ||
Args: | ||
model: The name of the `model` to use for cached content creation. | ||
Any `CachedContent` resource can be only used with the | ||
`model` it was created for. | ||
name: The resource name referring to the cached content. | ||
system_instruction: Developer set system instruction. | ||
contents: Contents to cache. | ||
tools: A list of `Tools` the model may use to generate response. | ||
tool_config: Config to apply to all tools. | ||
ttl: TTL for cached resource (in seconds). Defaults to 1 hour. | ||
Returns: | ||
`CachedContent` resource with specified name. | ||
""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
request = cls._prepare_create_request( | ||
model=model, | ||
name=name, | ||
system_instruction=system_instruction, | ||
contents=contents, | ||
tools=tools, | ||
tool_config=tool_config, | ||
ttl=ttl, | ||
) | ||
|
||
response = client.create_cached_content(request) | ||
return cls._decode_cached_content(response) | ||
|
||
@classmethod | ||
def get(cls, name: str, client: glm.CacheServiceClient | None = None) -> CachedContent: | ||
"""Fetches required `CachedContent` resource. | ||
Args: | ||
name: The resource name referring to the cached content. | ||
Returns: | ||
`CachedContent` resource with specified `name`. | ||
""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
if "cachedContents/" not in name: | ||
name = "cachedContents/" + name | ||
|
||
request = protos.GetCachedContentRequest(name=name) | ||
response = client.get_cached_content(request) | ||
return cls._decode_cached_content(response) | ||
|
||
@classmethod | ||
def list( | ||
cls, 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 = protos.ListCachedContentsRequest(page_size=page_size) | ||
for cached_content in client.list_cached_contents(request): | ||
yield cls._decode_cached_content(cached_content) | ||
|
||
def delete(self, client: glm.CachedServiceClient | None = None) -> None: | ||
"""Deletes `CachedContent` resource.""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
request = protos.DeleteCachedContentRequest(name=self.name) | ||
client.delete_cached_content(request) | ||
return | ||
|
||
def update( | ||
self, | ||
updates: dict[str, Any], | ||
client: glm.CacheServiceClient | None = None, | ||
) -> CachedContent: | ||
"""Updates requested `CachedContent` resource. | ||
Args: | ||
updates: The list of fields to update. Currently only | ||
`ttl/expire_time` is supported as an update path. | ||
Returns: | ||
`CachedContent` object with specified updates. | ||
""" | ||
if client is None: | ||
client = get_default_cache_client() | ||
|
||
updates = flatten_update_paths(updates) | ||
for update_path in updates: | ||
if update_path == "ttl": | ||
updates = updates.copy() | ||
update_path_val = updates.get(update_path) | ||
updates[update_path] = caching_types.to_ttl(update_path_val) | ||
else: | ||
raise ValueError( | ||
f"As of now, only `ttl` can be updated for `CachedContent`. Got: `{update_path}` instead." | ||
) | ||
field_mask = field_mask_pb2.FieldMask() | ||
|
||
for path in updates.keys(): | ||
field_mask.paths.append(path) | ||
for path, value in updates.items(): | ||
self._apply_update(path, value) | ||
|
||
request = protos.UpdateCachedContentRequest( | ||
cached_content=self._to_dict(), update_mask=field_mask | ||
) | ||
client.update_cached_content(request) | ||
return self |
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.
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.