Skip to content

Commit

Permalink
Chat REST samples (#449)
Browse files Browse the repository at this point in the history
* Add first chat samples for rest

* Add Chat rest examples

* last message should be 'role:user'

Change-Id: I3e06e9e0ffb553cfc70add5ed0365cb56e9fddff

---------

Co-authored-by: Mark Daoust <markdaoust@google.com>
  • Loading branch information
shilpakancharla and MarkDaoust authored Jul 11, 2024
1 parent 7c21486 commit 950a666
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions samples/rest/chat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
set -eu

SCRIPT_DIR=$(dirname "$0")
MEDIA_DIR=$(realpath ${SCRIPT_DIR}/../../third_party)

echo "[START chat]"
# [START chat]
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": [
{"role":"user",
"parts":[{
"text": "Hello"}]},
{"role": "model",
"parts":[{
"text": "Great to meet you. What would you like to know?"}]},
{"role":"user",
"parts":[{
"text": "I have two dogs in my house. How many paws are in my house?"}]},
]
}' 2> /dev/null | grep "text"
# [END chat]

echo "[START chat_streaming]"
# [START chat_streaming]
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?key=$GOOGLE_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{"role":"user",
"parts":[{
"text": "Hello"}]},
{"role": "model",
"parts":[{
"text": "Great to meet you. What would you like to know?"}]},
{"role":"user",
"parts":[{
"text": "I have two dogs in my house. How many paws are in my house?"}]},
]
}' 2> /dev/null | grep "text"
# [END chat_streaming]

echo "[START chat_streaming_with_images]"
# [START chat_streaming_with_images]
IMG_PATH=${MEDIA_DIR}/organ.jpg

if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:streamGenerateContent?key=$GOOGLE_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Hello, I am interested in learning about musical instruments. Can I show you one?"
}
]
},
{
"role": "model",
"parts": [
{
"text": "Certainly."
},
]
},
{
"role": "user",
"parts": [
{
"text": "Tell me about this instrument"
},
{
"inline_data": {
"mime_type": "image/jpeg",
"data": "'$(base64 $B64FLAGS $IMG_PATH)'"
}
}
]
}
]
}' 2> /dev/null | grep "text"
# [END chat_streaming_with_images]

0 comments on commit 950a666

Please sign in to comment.