Skip to content

Commit

Permalink
chore: GenAI - Explicit Caching - Move caching back to public preview
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 642706611
  • Loading branch information
Zhenyi Qi authored and copybara-github committed Jun 12, 2024
1 parent d5dc7b5 commit 374340a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 45 deletions.
78 changes: 39 additions & 39 deletions tests/system/vertexai/test_generative_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from vertexai.preview import (
generative_models as preview_generative_models,
)
from vertexai._caching import _caching as caching
from vertexai.preview import caching

GEMINI_MODEL_NAME = "gemini-1.0-pro-002"
GEMINI_VISION_MODEL_NAME = "gemini-1.0-pro-vision"
Expand Down Expand Up @@ -100,6 +100,44 @@ def setup_method(self):
credentials=credentials,
)

def test_generate_content_with_cached_content_from_text(self):
cached_content = caching.CachedContent.create(
model_name=GEMINI_15_0514_MODEL_NAME,
system_instruction="Please answer all the questions like a pirate.",
contents=[
Content.from_dict(
{
"role": "user",
"parts": [
{
"file_data": {
"mime_type": "application/pdf",
"file_uri": "gs://ucaip-samples-us-central1/sdk_system_test_resources/megatro-llm.pdf",
}
}
for _ in range(10)
]
+ [
{"text": "Please try to summarize the previous contents."},
],
}
)
],
)

model = generative_models.GenerativeModel.from_cached_content(
cached_content=cached_content
)

response = model.generate_content(
"Why is sky blue?",
generation_config=generative_models.GenerationConfig(temperature=0),
)
try:
assert response.text
finally:
cached_content.delete()

def test_generate_content_from_text(self):
model = generative_models.GenerativeModel(GEMINI_MODEL_NAME)
response = model.generate_content(
Expand Down Expand Up @@ -441,41 +479,3 @@ def test_additional_request_metadata(self):
generation_config=generative_models.GenerationConfig(temperature=0),
)
assert response

def test_generate_content_with_cached_content_from_text(self):
cached_content = caching.CachedContent.create(
model_name=GEMINI_15_0514_MODEL_NAME,
system_instruction="Please answer all the questions like a pirate.",
contents=[
Content.from_dict(
{
"role": "user",
"parts": [
{
"file_data": {
"mime_type": "application/pdf",
"file_uri": "gs://ucaip-samples-us-central1/sdk_system_test_resources/megatro-llm.pdf",
}
}
for _ in range(10)
]
+ [
{"text": "Please try to summarize the previous contents."},
],
}
)
],
)

model = preview_generative_models.GenerativeModel._from_cached_content(
cached_content=cached_content
)

response = model.generate_content(
"Why is sky blue?",
generation_config=generative_models.GenerationConfig(temperature=0),
)
try:
assert response.text
finally:
cached_content.delete()
2 changes: 1 addition & 1 deletion tests/unit/vertexai/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import datetime
import pytest
import mock
from vertexai._caching import _caching as caching
from vertexai.preview import caching
from google.cloud.aiplatform import initializer
import vertexai
from google.cloud.aiplatform_v1beta1.types.cached_content import (
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/vertexai/test_generative_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
gen_ai_cache_service,
)
from vertexai.generative_models import _function_calling_utils
from vertexai._caching import _caching as caching
from vertexai.preview import caching


_TEST_PROJECT = "test-project"
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_generative_model_from_cached_content(
"cached-content-id-in-from-cached-content-test"
)

model = preview_generative_models.GenerativeModel._from_cached_content(
model = preview_generative_models.GenerativeModel.from_cached_content(
cached_content=cached_content
)

Expand Down Expand Up @@ -587,7 +587,7 @@ def test_generate_content_with_cached_content(
"cached-content-id-in-from-cached-content-test"
)

model = preview_generative_models.GenerativeModel._from_cached_content(
model = preview_generative_models.GenerativeModel.from_cached_content(
cached_content=cached_content
)

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions vertexai/generative_models/_generative_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import warnings

if TYPE_CHECKING:
from vertexai._caching import _caching as caching
from vertexai.preview import caching

try:
from PIL import Image as PIL_Image # pylint: disable=g-import-not-at-top
Expand Down Expand Up @@ -2601,7 +2601,7 @@ def start_chat(
)

@classmethod
def _from_cached_content(
def from_cached_content(
cls,
cached_content: "caching.CachedContent",
*,
Expand Down
20 changes: 20 additions & 0 deletions vertexai/preview/caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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 vertexai.caching._caching import CachedContent

__all__ = [
"CachedContent",
]

0 comments on commit 374340a

Please sign in to comment.