Skip to content

Add cache_from_name and cache_chat #413

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 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions samples/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ def test_cache_update(self):
# [END cache_update]
cache.delete()

def test_cache_create_from_name(self):
# [START cache_create_from_name]
document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
model=model_name,
system_instruction="You are an expert analyzing transcripts.",
contents=[document],
)
apollo_model = genai.caching.CachedContent.from_cached_content(cache)
response = apollo_model.generate_content("Find a lighthearted moment from this transcript")
print(response.text)
# [END cache_create_from_name]
cache.delete()

def test_cache_chat(self):
# [START cache_chat]
document = genai.upload_file(path=media / "a11.txt")
model_name = "gemini-1.5-flash-001"
cache = genai.caching.CachedContent.create(
model=model_name,
system_instruction="You are an expert analyzing transcripts.",
contents=[document],
)
apollo_model = genai.GenerativeModel.from_cached_content(cached_content=cache)
chat = apollo_model.start_chat()
response = chat.send_message(
"Give me a quote from the most important part of the transcript."
)
print(response.text)
response = chat.send_message("What was recounted after that?")
print(response.text)
# [END cache_chat]
cache.delete()


if __name__ == "__main__":
absltest.main()
9 changes: 6 additions & 3 deletions samples/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_chat(self):
]
)
response = chat.send_message("I have 2 dogs in my house.")
print(response.text)
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
# [END chat]
Expand Down Expand Up @@ -62,11 +62,14 @@ def test_chat_streaming_with_images(self):
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()

response = chat.send_message("Hello, I'm interested in learning about musical instruments. Can I show you one?", stream=True)
response = chat.send_message(
"Hello, I'm interested in learning about musical instruments. Can I show you one?",
stream=True,
)
for chunk in response:
print(chunk.text) # Yes.
print("_" * 80)

organ = genai.upload_file(media / "organ.jpg")
response = chat.send_message(
["What family of intruments does this instrument belong to?", organ], stream=True
Expand Down
Loading