From 05068e8a521c808fc82956a32d0f4eedcd3123cf Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Mon, 8 Jul 2024 17:55:20 -0400 Subject: [PATCH 1/3] Adding count_tokens for rest --- samples/rest/count_tokens.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 samples/rest/count_tokens.sh diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh new file mode 100644 index 000000000..43e3ea20f --- /dev/null +++ b/samples/rest/count_tokens.sh @@ -0,0 +1,28 @@ +set -eu + +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 \ + -H 'Content-Type: application/json' \ + -X POST \ + -d '{ + "contents": [{ + "parts":[{ + "text": "Write a story about a magic backpack."}]}]}' > response.json +# [END tokens_text_only] + +echo "[START tokens_chat]" +# [START tokens_chat] +curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY \ + -H 'Content-Type: application/json' \ + -X POST \ + -d '{ + "contents": [ + {"role":"user", + "parts":[{ + "text": "Hi, my name is Bob."}]}, + {"role": "model", + "parts":[{ + "text": "Hi Bob"}]}, + ] ' 2> response.json +# [END tokens_chat] \ No newline at end of file From 9ce8896706101a2655609031863311aef1af0578 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 9 Jul 2024 13:18:51 -0400 Subject: [PATCH 2/3] Update to have same prompt as python example --- samples/rest/count_tokens.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index 43e3ea20f..190b9684d 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -8,7 +8,7 @@ curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:co -d '{ "contents": [{ "parts":[{ - "text": "Write a story about a magic backpack."}]}]}' > response.json + "text": "The quick brown fox jumps over the lazy dog."}]}]}' > response.json # [END tokens_text_only] echo "[START tokens_chat]" From eb397c1941eb82689a5d3cc44de588f6f0d37431 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 11 Jul 2024 18:35:30 -0500 Subject: [PATCH 3/3] tests now working --- samples/rest/count_tokens.sh | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/samples/rest/count_tokens.sh b/samples/rest/count_tokens.sh index 190b9684d..867e787b8 100644 --- a/samples/rest/count_tokens.sh +++ b/samples/rest/count_tokens.sh @@ -8,7 +8,10 @@ curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:co -d '{ "contents": [{ "parts":[{ - "text": "The quick brown fox jumps over the lazy dog."}]}]}' > response.json + "text": "The quick brown fox jumps over the lazy dog." + }], + }], + }' # [END tokens_text_only] echo "[START tokens_chat]" @@ -18,11 +21,12 @@ curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:co -X POST \ -d '{ "contents": [ - {"role":"user", - "parts":[{ - "text": "Hi, my name is Bob."}]}, + {"role": "user", + "parts": [{"text": "Hi, my name is Bob."}], + }, {"role": "model", - "parts":[{ - "text": "Hi Bob"}]}, - ] ' 2> response.json + "parts":[{"text": "Hi Bob"}], + }, + ], + }' # [END tokens_chat] \ No newline at end of file