From cf51b23141f1a496175aea931687fee931c1dc82 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 25 Jul 2024 10:47:06 +0100 Subject: [PATCH 01/10] Add PDF file function --- samples/rest/files.sh | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/samples/rest/files.sh b/samples/rest/files.sh index ae44b7467..b5339e66f 100644 --- a/samples/rest/files.sh +++ b/samples/rest/files.sh @@ -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}") +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: " From 4a121c6c34a7f2b67fbd791082ec3f5b2e2c631a Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 25 Jul 2024 11:19:06 +0100 Subject: [PATCH 02/10] Add rest of count_tokens examples --- samples/rest/count_tokens.sh | 124 ++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index 5d4f08d14..1391ed42e 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -170,4 +170,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] \ No newline at end of file +# [END tokens_multimodal_video_audio_file_api] + +echo "[START tokens_cached_content]" +# [START tokens_cached_content] +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 $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' \ + -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." + } + }, + "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] \ No newline at end of file From 4d9639eaface2ff24d0e22eff402d7f0e21e174a Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 25 Jul 2024 11:24:19 +0100 Subject: [PATCH 03/10] Add context window count --- samples/rest/count_tokens.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index 1391ed42e..3df6e5412 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -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 +# [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 \ From 6471c41018a80fe9a8cfa4d0be800a18915fea91 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 15 Aug 2024 15:49:50 -0700 Subject: [PATCH 04/10] Tested and fixed count_tokens.sh --- samples/rest/count_tokens.sh | 44 ++++++++++++++---------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index 3df6e5412..c493e2924 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -3,7 +3,10 @@ set -eu SCRIPT_DIR=$(dirname "$0") MEDIA_DIR=$(realpath ${SCRIPT_DIR}/../../third_party) +GOOGLE_API_KEY=AIzaSyA3Gw4E_RoF_wfergxCQ2Y7BhtkSHALxfM + TEXT_PATH=${MEDIA_DIR}/poem.txt +A11_PATH=${MEDIA_DIR}/a11.txt IMG_PATH=${MEDIA_DIR}/organ.jpg AUDIO_PATH=${MEDIA_DIR}/sample.mp3 VIDEO_PATH=${MEDIA_DIR}/Big_Buck_Bunny.mp4 @@ -18,7 +21,9 @@ 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 +curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro?key=$GOOGLE_API_KEY > model.json +jq .inputTokenLimit model.json +jq .outputTokenLimit model.json # [END tokens_context_window] echo "[START tokens_text_only]" @@ -102,7 +107,6 @@ curl "${upload_url}" \ --data-binary "@${IMG_PATH}" 2> /dev/null > file_info.json file_uri=$(jq ".file.uri" file_info.json) -echo file_uri=$file_uri curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ @@ -148,13 +152,10 @@ curl "${upload_url}" \ --data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json file_uri=$(jq ".file.uri" file_info.json) -echo file_uri=$file_uri state=$(jq ".file.state" file_info.json) -echo state=$state name=$(jq ".file.name" file_info.json) -echo name=$name while [[ "($state)" = *"PROCESSING"* ]]; do @@ -179,7 +180,6 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:c echo "[START tokens_cached_content]" # [START tokens_cached_content] -wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt echo '{ "model": "models/gemini-1.5-flash-001", "contents":[ @@ -188,7 +188,7 @@ echo '{ { "inline_data": { "mime_type":"text/plain", - "data": "'$(base64 $B64FLAGS a11.txt)'" + "data": "'$(base64 $B64FLAGS $A11_PATH)'" } } ], @@ -210,33 +210,21 @@ curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?ke -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'" - }' +jq .usageMetadata.totalTokenCount cache.json # [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" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?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"}}}' + "text": "Hello there"}}}' > system_instructions.json + +jq .usageMetadata.totalTokenCount system_instructions.json # [END tokens_system_instruction] echo "[START tokens_tools]" @@ -274,9 +262,9 @@ cat > tools.json << EOF } EOF -curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:countTokens?key=$GOOGLE_API_KEY" \ +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \ -H 'Content-Type: application/json' \ - -d @<(echo ' + -d ' { "system_instruction": { "parts": { @@ -296,5 +284,7 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-lat } } } -') 2>/dev/null |sed -n '/"content"/,/"finishReason"/p' +' > tools_output.json + +jq .usageMetadata.totalTokenCount tools_output.json # [END tokens_tools] \ No newline at end of file From 7c19d7e9138efd77048e4c2b9b09941b2b6a9e56 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 15 Aug 2024 16:11:05 -0700 Subject: [PATCH 05/10] Updated files.sh to simplify pdf --- samples/rest/count_tokens.sh | 2 -- samples/rest/files.sh | 7 ++----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index c493e2924..e69fd6d1c 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -3,8 +3,6 @@ set -eu SCRIPT_DIR=$(dirname "$0") MEDIA_DIR=$(realpath ${SCRIPT_DIR}/../../third_party) -GOOGLE_API_KEY=AIzaSyA3Gw4E_RoF_wfergxCQ2Y7BhtkSHALxfM - TEXT_PATH=${MEDIA_DIR}/poem.txt A11_PATH=${MEDIA_DIR}/a11.txt IMG_PATH=${MEDIA_DIR}/organ.jpg diff --git a/samples/rest/files.sh b/samples/rest/files.sh index b5339e66f..8f292c4f6 100644 --- a/samples/rest/files.sh +++ b/samples/rest/files.sh @@ -8,6 +8,7 @@ IMG_PATH=${MEDIA_DIR}/organ.jpg IMG_PATH_2=${MEDIA_DIR}/Cajun_instruments.jpg AUDIO_PATH=${MEDIA_DIR}/sample.mp3 VIDEO_PATH=${MEDIA_DIR}/Big_Buck_Bunny.mp4 +PDF_PATH=${MEDIA_DIR}/test.pdf BASE_URL="https://generativelanguage.googleapis.com" @@ -245,12 +246,8 @@ jq ".candidates[].content.parts[].text" response.json echo "[START files_create_pdf]" # [START files_create_pdf] -MIME_TYPE=$(file -b --mime-type "${PDF_PATH}") NUM_BYTES=$(wc -c < "${PDF_PATH}") DISPLAY_NAME=TEXT - - -echo $MIME_TYPE tmp_header_file=upload-header.tmp # Initial resumable request defining metadata. @@ -260,7 +257,7 @@ curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \ -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 "X-Goog-Upload-Header-Content-Type: application/pdf" \ -H "Content-Type: application/json" \ -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null From 73fd4456a8336cb0961a7289145201548c6dd8a0 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 13:11:35 -0700 Subject: [PATCH 06/10] Update samples.yaml to get basename of file --- .github/workflows/samples.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 4c76a563b..422381038 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -68,7 +68,7 @@ jobs: echo "Testing $file" if [[ -f ${file} ]]; then # File exists, so needs to be listed. - if ! grep -q $name ${README}; then + if ! grep -q basename $file ${README}; then echo "Error: Sample not listed in README ($name)" exit 1 fi From 8fe186153ac17d29f435f43e3b62b302e858847c Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 13:18:37 -0700 Subject: [PATCH 07/10] Update samples.yaml to get basename of file --- .github/workflows/samples.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 422381038..27656d74f 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -68,12 +68,14 @@ jobs: echo "Testing $file" if [[ -f ${file} ]]; then # File exists, so needs to be listed. - if ! grep -q basename $file ${README}; then + name=basename $file + if ! grep -q $name ${README}; then echo "Error: Sample not listed in README ($name)" exit 1 fi else # File does not exist, ensure it's not listed + name=basename $file if grep -q $name ${README}; then echo "Error: Sample should not be listed in README ($name)" exit 1 From 852ebf09eb00842a9c492cf770bf691d62ab2db7 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 13:18:56 -0700 Subject: [PATCH 08/10] Update samples.yaml to get basename of file --- .github/workflows/samples.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 27656d74f..38508d1f9 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -69,6 +69,7 @@ jobs: if [[ -f ${file} ]]; then # File exists, so needs to be listed. name=basename $file + echo $name if ! grep -q $name ${README}; then echo "Error: Sample not listed in README ($name)" exit 1 From cd3252c746b7ae1a6d4e9475054dbde0e407e82e Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 13:29:01 -0700 Subject: [PATCH 09/10] Update samples.yaml to get basename of file --- .github/workflows/samples.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 38508d1f9..5305027ab 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -68,15 +68,15 @@ jobs: echo "Testing $file" if [[ -f ${file} ]]; then # File exists, so needs to be listed. - name=basename $file - echo $name + echo $(basename $file) + name=echo $(basename $file) if ! grep -q $name ${README}; then echo "Error: Sample not listed in README ($name)" exit 1 fi else # File does not exist, ensure it's not listed - name=basename $file + name=echo $(basename $file) if grep -q $name ${README}; then echo "Error: Sample should not be listed in README ($name)" exit 1 From 492c9881a5ed8b1b5746b387a8bdd6b45bdbe4d2 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 13:33:13 -0700 Subject: [PATCH 10/10] test samples.yaml --- .github/workflows/samples.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index 5305027ab..71c457cfa 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -69,14 +69,14 @@ jobs: if [[ -f ${file} ]]; then # File exists, so needs to be listed. echo $(basename $file) - name=echo $(basename $file) + name=$(basename $file) if ! grep -q $name ${README}; then echo "Error: Sample not listed in README ($name)" exit 1 fi else # File does not exist, ensure it's not listed - name=echo $(basename $file) + name=$(basename $file) if grep -q $name ${README}; then echo "Error: Sample should not be listed in README ($name)" exit 1