Skip to content

Commit f37df8c

Browse files
mark name as OPTIONAL for CachedContent creation
If not provided, the name will be randomly generated Change-Id: Ib95fbafd3dfe098b43164d7ee4d6c2a84b0aae2e
1 parent 59663c8 commit f37df8c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

google/generativeai/caching.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,20 @@ def _decode_cached_content(cls, cached_content: glm.CachedContent) -> CachedCont
7979

8080
@staticmethod
8181
def _prepare_create_request(
82-
name: str,
8382
model: str,
83+
name: str = None,
8484
system_instruction: Optional[content_types.ContentType] = None,
8585
contents: Optional[content_types.ContentsType] = None,
8686
tools: Optional[content_types.FunctionLibraryType] = None,
8787
tool_config: Optional[content_types.ToolConfigType] = None,
8888
ttl: Optional[caching_types.ExpirationTypes] = datetime.timedelta(hours=1),
8989
) -> glm.CreateCachedContentRequest:
9090
"""Prepares a CreateCachedContentRequest."""
91-
if caching_types.valid_cached_content_name(name):
91+
if name is not None:
92+
if not caching_types.valid_cached_content_name(name):
93+
raise ValueError(caching_types.NAME_ERROR_MESSAGE.format(name=name))
94+
9295
name = "cachedContents/" + name
93-
else:
94-
raise ValueError(caching_types.NAME_ERROR_MESSAGE.format(name=name))
9596

9697
if "/" not in model:
9798
model = "models/" + model
@@ -127,8 +128,8 @@ def _prepare_create_request(
127128
@classmethod
128129
def create(
129130
cls,
130-
name: str,
131131
model: str,
132+
name: str = None,
132133
system_instruction: Optional[content_types.ContentType] = None,
133134
contents: Optional[content_types.ContentsType] = None,
134135
tools: Optional[content_types.FunctionLibraryType] = None,
@@ -139,11 +140,10 @@ def create(
139140
"""Creates CachedContent resource.
140141
141142
Args:
142-
name: The resource name referring to the cached content.
143-
Format: cachedContents/{id}.
144143
model: The name of the `Model` to use for cached content
145144
Format: models/{model}. Cached content resource can be only
146145
used with model it was created for.
146+
name: The resource name referring to the cached content.
147147
system_instruction: Developer set system instruction.
148148
contents: Contents to cache.
149149
tools: A list of `Tools` the model may use to generate response.
@@ -157,8 +157,8 @@ def create(
157157
client = get_default_cache_client()
158158

159159
request = cls._prepare_create_request(
160-
name=name,
161160
model=model,
161+
name=name,
162162
system_instruction=system_instruction,
163163
contents=contents,
164164
tools=tools,

0 commit comments

Comments
 (0)