|
| 1 | +#!/bin/bash |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2023 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# This example demonstrates how to use inline PDF data with the generative API |
| 18 | + |
| 19 | +# Set default values for environment variables if they don't exist |
| 20 | +MEDIA_DIR="${MEDIA_DIR:-$(dirname "$0")/../../third_party}" |
| 21 | + |
| 22 | +# Check if we're on macOS or Linux |
| 23 | +if [[ "$OSTYPE" == "darwin"* ]]; then |
| 24 | + B64FLAGS="-b 0" |
| 25 | +else |
| 26 | + B64FLAGS="--wrap=0" |
| 27 | +fi |
| 28 | + |
| 29 | +echo "[START text_gen_multimodal_two_pdf_inline]" |
| 30 | +# Use temporary files to hold the base64 encoded pdf data |
| 31 | +PDF_PATH_1=${MEDIA_DIR}/test.pdf |
| 32 | +PDF_PATH_2=${MEDIA_DIR}/test.pdf |
| 33 | + |
| 34 | +TEMP_1_B64=$(mktemp) |
| 35 | +trap 'rm -f "$TEMP_1_B64"' EXIT |
| 36 | +base64 $B64FLAGS $PDF_PATH_1 > "$TEMP_1_B64" |
| 37 | + |
| 38 | +TEMP_2_B64=$(mktemp) |
| 39 | +trap 'rm -f "$TEMP_2_B64"' EXIT |
| 40 | +base64 $B64FLAGS $PDF_PATH_2 > "$TEMP_2_B64" |
| 41 | + |
| 42 | +# Use a temporary file to hold the JSON payload |
| 43 | +TEMP_JSON=$(mktemp) |
| 44 | +trap 'rm -f "$TEMP_JSON"' EXIT |
| 45 | + |
| 46 | +cat > "$TEMP_JSON" << EOF |
| 47 | +{ |
| 48 | + "contents": [{ |
| 49 | + "role": "user", |
| 50 | + "parts":[ |
| 51 | + {"text": "Extract the pet names, type and ages from these documents."}, |
| 52 | + { |
| 53 | + "inlineData": { |
| 54 | + "mimeType":"application/pdf", |
| 55 | + "data": "$(cat "$TEMP_1_B64")" |
| 56 | + } |
| 57 | + }, |
| 58 | + { |
| 59 | + "inlineData": { |
| 60 | + "mimeType":"application/pdf", |
| 61 | + "data": "$(cat "$TEMP_2_B64")" |
| 62 | + } |
| 63 | + } |
| 64 | + ] |
| 65 | + }], |
| 66 | + "systemInstruction": { |
| 67 | + "parts": [ |
| 68 | + {"text": "Extract the pet names and ages from these documents and return them in the following JSON format: |
| 69 | +
|
| 70 | + Pet = {\"name\": str, \"type\": str, \"age\": int} |
| 71 | + Return: list[Pet]" |
| 72 | + } |
| 73 | + ] |
| 74 | + }, |
| 75 | + "generationConfig": { |
| 76 | + "temperature": 0.2, |
| 77 | + "topP": 0.95, |
| 78 | + "topK": 40, |
| 79 | + "maxOutputTokens": 1000, |
| 80 | + "responseMimeType": "application/json" |
| 81 | + } |
| 82 | +} |
| 83 | +EOF |
| 84 | + |
| 85 | +curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \ |
| 86 | + -H 'Content-Type: application/json' \ |
| 87 | + -X POST \ |
| 88 | + -d "@$TEMP_JSON" 2> /dev/null |
0 commit comments