-
Notifications
You must be signed in to change notification settings - Fork 454
Caching cURL sample #455
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
Caching cURL sample #455
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
046e7ce
Create, list, get, update, and delete caches
Giom-V c46e687
Use cached content
Giom-V 6e35f1a
Formatting
Giom-V 27fab42
Merge branch 'google-gemini:main' into caching_curl_sample
Giom-V 371c78f
Updating region tags
Giom-V be366ba
cache_generate_content region tag
Giom-V e18ea51
Moving the generation in cache_create
Giom-V 84b1a9e
Merge branch 'main' into caching_curl_sample
MarkDaoust eb02d25
Fix
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 @@ | ||
set -eu | ||
|
||
echo "[START caching]" | ||
# [START caching] | ||
wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt | ||
echo '{ | ||
"model": "models/gemini-1.5-flash-001", | ||
"contents":[ | ||
{ | ||
"parts":[ | ||
{ | ||
"inline_data": { | ||
"mime_type":"text/plain", | ||
"data": "'$(base64 -w0 a11.txt)'" | ||
MarkDaoust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
], | ||
"role": "user" | ||
} | ||
], | ||
"systemInstruction": { | ||
"parts": [ | ||
{ | ||
"text": "You are an expert at analyzing transcripts." | ||
} | ||
] | ||
}, | ||
"ttl": "300s" | ||
}' > request.json | ||
|
||
curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-d @request.json | ||
|
||
rm a11.txt request.json | ||
# [END caching] | ||
|
||
echo "[START list_caches]" | ||
# [START list_caches] | ||
ALL_CACHES=$(curl "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GOOGLE_API_KEY") | ||
CACHE_NAME=$(echo "$ALL_CACHES" | grep '"name":' | cut -d '"' -f 4 | head -n 1) | ||
# [END list_caches] | ||
|
||
echo "[START get_cache]" | ||
# [START get_cache] | ||
curl "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GOOGLE_API_KEY" | ||
# [END get_cache] | ||
|
||
echo "[START update_cache]" | ||
# [START update_cache] | ||
curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"ttl": "600s"}' | ||
# [END update_cache] | ||
|
||
echo "[START use_cache]" | ||
MarkDaoust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# [START use_cache] | ||
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{ | ||
"contents": [ | ||
{ | ||
"parts":[{ | ||
"text": "Please summarize this transcript" | ||
}], | ||
"role": "user" | ||
}, | ||
], | ||
"cachedContent": "'$CACHE_NAME'" | ||
}' | ||
# [END use_cache] | ||
|
||
echo "[START delete_cache]" | ||
# [START delete_cache] | ||
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GOOGLE_API_KEY" | ||
# [END delete_cache] |
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.