TL;DR:
Happy holidays! Google
Introduction
Welcome to the blog focusing on using Google APIs from Python and sometimes Node.js. Today's post focuses on Gemini, but there's plenty of content beyond Gemini:
- Google Cloud/GCP:
- Google Workspace/GWS: (also Drive!)
- Common topics across all APIs like auth & security:
This post takes a break from the flow of the previous posts in this model: text-based audio clip generation.
Prerequisites
New client library improves user experience (UX)
You need a client library to talk to Gemini from code. While several client libraries already exist for Gemini, Google has recently introduced a new one. The new library features an improved UX, so I have to give Google some credit. In the
^ -- Python 3 only; Python 2 support is not available for the Gemini API
Installation and setup
Follow these steps to install the client library and get set up:
- Install the new client library:
pip install -U google-genai
, , etc., as once your API key leaks, everyone in the world can use it.
If you're new to Google developer tools, , mostly used by GWS APIs, and service accounts, mostly used by Google Cloud (GCP) APIs. While this post doesn't cover Google Maps, the Maps team put together a great ,
asynciois required because the audio files. This is followed by importing Google's new "genAI" client library.
Like in previous code samples in this series, the API key is saved to
settings.py. Alternatively, you can save your API key to theGOOGLE_API_KEYenvironment variable, or use thepython-dotenvpackage, storing the API key in.envto more closely mirror working in a Node.js environment. There's also the and , allowing it to be used with thewithstatement. You'll find nearly-identical code in various samples and Notebooks in the , kicking it off by opening the WAV file for write and sending the prompt to the LLM. The rest of it continuously waits for a server response, writing the chunks of audio data received until it's been exhausted, terminating the session.
This is minimal code required to do the job. In other examples from Google, you'll find reference to
server_content,inline_dataand writing outparts. Most of this relates to supporting a multi-turn conversation, but for a single request-response "cycle," less code is less confusing.
Running the script
Running the script produces an audio file along with the expected output:
CODE$ python3 gem20-audio.py
** LLM prompt: "Describe a cat in a few sentences"
** Saved audio to "whatacatis.wav"
Your mileage may vary, but this is the audio track I got from Gemini:
Summary
Developers are eager to jump into the world of AI/ML, especially GenAI & LLMs, and accessing Google's Gemini models via API is part of that picture. The previous posts in the series got your foot in the door, and today, we explored a new feature available from Gemini 2.0 Flash. Next, we'll continue the journey from the previous post (link below) and show you how to deploy basic genAI web apps to Google Cloud!
If you find errors or have suggestions on content you'd like to see in future posts, also leave a comment below, and if your organization needs help integrating Google technologies via its APIs, reach out to me by submitting a request at
NEXT POST: Part 5: Deploying basic GenAI web apps to Google Cloud (coming soon)
References
Below are various links relevant to this post:
Code samples
- (PDF)
Other Gemini API content by the author
- (GDE) in Google Cloud (GCP) & Google Workspace (GWS), author of Prentice Hall's bestselling , and has written for Linux Journal & CNET. He runs , and Python training & engineering. Wesley was one of the original Yahoo!Mail engineers and spent 13+ years on various Google product teams, speaking on behalf of their APIs, producing sample apps, codelabs, and videos for . He holds degrees in Computer Science, Mathematics, and Music from the University of California, is a Fellow of the Python Software Foundation, and loves to travel to meet developers worldwide at conferences, user group events, and universities. Follow he/him . Find this content useful? !
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR