Skip to content
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

Add additional functions to files & count_tokens. #490

Merged
merged 11 commits into from
Aug 16, 2024
129 changes: 128 additions & 1 deletion samples/rest/count_tokens.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ else
B64FLAGS="-w0"
fi

echo "[START tokens_context_window]"
# [START tokens_context_window]
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:countTokens?key=$GOOGLE_API_KEY
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
# [END tokens_context_window]

echo "[START tokens_text_only]"
# [START tokens_text_only]
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY \
Expand Down Expand Up @@ -170,4 +175,126 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:c
{"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
}]
}'
# [END tokens_multimodal_video_audio_file_api]
# [END tokens_multimodal_video_audio_file_api]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b/346293525

echo "[START tokens_cached_content]"
# [START tokens_cached_content]
wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
echo '{
"model": "models/gemini-1.5-flash-001",
"contents":[
{
"parts":[
{
"inline_data": {
"mime_type":"text/plain",
"data": "'$(base64 $B64FLAGS a11.txt)'"
}
}
],
"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 \
> cache.json

CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "Please summarize this transcript"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
# [END tokens_cached_content]

echo "[START tokens_system_instruction]"
# [START tokens_system_instruction]
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:countTokens?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{ "system_instruction": {
"parts":
{ "text": "You are a cat. Your name is Neko."}},
"contents": {
"parts": {
"text": "Hello there"}}}'
# [END tokens_system_instruction]

echo "[START tokens_tools]"
# [START tokens_tools]
cat > tools.json << EOF
{
"function_declarations": [
{
"name": "enable_lights",
"description": "Turn on the lighting system.",
"parameters": { "type": "object" }
},
{
"name": "set_light_color",
"description": "Set the light color. Lights must be enabled for this to work.",
"parameters": {
"type": "object",
"properties": {
"rgb_hex": {
"type": "string",
"description": "The light color as a 6-digit hex string, e.g. ff0000 for red."
}
},
"required": [
"rgb_hex"
]
}
},
{
"name": "stop_lights",
"description": "Turn off the lighting system.",
"parameters": { "type": "object" }
}
]
}
EOF

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:countTokens?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
-d @<(echo '
{
"system_instruction": {
"parts": {
"text": "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks."
}
},
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
"tools": ['$(source "$tools")'],

"tool_config": {
"function_calling_config": {"mode": "none"}
},

"contents": {
"role": "user",
"parts": {
"text": "What can you do?"
}
}
}
') 2>/dev/null |sed -n '/"content"/,/"finishReason"/p'
# [END tokens_tools]
52 changes: 52 additions & 0 deletions samples/rest/files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,58 @@ echo
jq ".candidates[].content.parts[].text" response.json
# [END files_create_video]

echo "[START files_create_pdf]"
# [START files_create_pdf]
MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT


echo $MIME_TYPE
tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \
-D upload-header.tmp \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${PDF_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"text": "Can you add a few more lines to this poem?"},
{"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
}]
}' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json
# [END files_create_pdf]

echo "[START files_list]"
# [START files_list]
echo "My files: "
Expand Down
Loading