Skip to content

Commit abea7c5

Browse files
changed sample.py to use google-genai sdk (google-gemini#491)
Part of google-gemini#446 * changed sample.py to use google-genai sdk --------- Co-authored-by: Guillaume Vernade <[email protected]>
1 parent 6367a2b commit abea7c5

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

quickstarts/file-api/sample.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import google.generativeai as genai
15+
from google import genai
1616
import os
1717
from dotenv import load_dotenv
1818

@@ -21,25 +21,36 @@
2121
api_key = os.environ["GOOGLE_API_KEY"]
2222

2323
# Initialize Google API Client
24-
genai.configure(api_key=api_key)
24+
client=genai.Client(api_key=api_key)
2525

26-
# Prepare file to upload to GenAI File API
27-
file_path = "sample_data/gemini_logo.png"
26+
# Upload a sample file to the client.files API
27+
file_path = "/content/image.png"
2828
display_name = "Gemini Logo"
29-
file_response = genai.upload_file(path=file_path, display_name=display_name)
29+
file_response = client.files.upload(
30+
file=open(file_path, "rb"),
31+
config={
32+
"mime_type":"image/png",
33+
"display_name":display_name
34+
}
35+
)
3036
print(f"Uploaded file {file_response.display_name} as: {file_response.uri}")
3137

32-
# Verify the file is uploaded to the API
33-
get_file = genai.get_file(name=file_response.name)
38+
# Retrieve the uploaded file from the client.files.get
39+
get_file =client.files.get(name=file_response.name)
3440
print(f"Retrieved file {get_file.display_name} as: {get_file.uri}")
3541

36-
# Make Gemini 1.5 API LLM call
42+
# Generate content using the client.models API
3743
prompt = "Describe the image with a creative description"
38-
model_name = "models/gemini-2.0-flash"
39-
model = genai.GenerativeModel(model_name=model_name)
40-
response = model.generate_content([prompt, file_response])
41-
print(response)
44+
model_name = "gemini-2.0-flash"
45+
response = client.models.generate_content(
46+
model=model_name,
47+
contents=[
48+
prompt,
49+
file_response
50+
]
51+
)
52+
print(response.text)
4253

4354
# Delete the sample file
44-
genai.delete_file(name=file_response.name)
55+
client.files.delete(name=file_response.name)
4556
print(f"Deleted file {file_response.display_name}")

0 commit comments

Comments
 (0)