VOOZH about

URL: https://thenewstack.io/how-to-get-started-with-googles-gemini-large-language-model/

⇱ How to Get Started with Google’s Gemini Large Language Model - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-02-29 05:00:55
How to Get Started with Google’s Gemini Large Language Model
tutorial,
Large Language Models / Python

How to Get Started with Google’s Gemini Large Language Model

There are two ways to access Google's Gemini LLM: Vertex AI and Google AI Studio. We show you how to get started with both approaches.
Feb 29th, 2024 5:00am by Janakiram MSV
👁 Featued image for: How to Get Started with Google’s Gemini Large Language Model
Photo by 12photostory on Unsplash.

In my previous article, I introduced the key capabilities of Gemini, the multimodal generative AI model from Google. In this post, I will walk you through the steps to access the model.

There are two ways to access Gemini: Vertex AI and Google AI Studio. The former is meant for developers familiar with Google Cloud, while the latter is for developers building web and mobile apps with almost no dependency on Google Cloud.

Let’s take a look at these two approaches.

Accessing Gemini through Vertex AI

Assuming you have an active project with billing enabled, here are the steps to accessing the API from your local workstation.

Create a Python virtual environment and install the required modules.

$ python -m venv venv
$ source venv/bin/activate

Since we need to authenticate with Google Cloud, let’s run the below commands to cache the credentials. This will be used by the Google Cloud SDK when talking to the API endpoints. This method creates application default credentials (ADC) on your development workstation at at $HOME/.config/gcloud/application_default_credentials.json.

$ gcloud init
$ gcloud auth application-default login

You will see the browser window pop up asking your Google credentials to complete the authentication process. Once this is done, proceed to the next step of installing the Python modules.

$ pip install -U google-cloud-aiplatform
$ pip install -U jupyter

Launch Jupyter Lab and access it from your favorite browser.

$ jupyter notebook --ip='0.0.0.0' --no-browser --NotebookApp.token='' --NotebookApp.password=''

Start by importing the modules and then initialize the model.

from google.cloud import aiplatform
import vertexai
from vertexai.preview.generative_models import GenerativeModel, Part

The module vertexai.preview provides access to the foundation models available in Vertex AI. Check the documentation for the latest version and updated API.

vertexai.init()
model = GenerativeModel("gemini-pro")

Like other LLMs, Gemini has two APIs: text generation and chat completion.

Let’s try the text generation API.

response = model.generate_content("I have a Python in the backyard. What should I do?")
print(response.text)

👁 Image

Next, let’s explore the chat completion API. The key difference between text generation and chat completion is the ability to maintain the history of conversations in a list. Passing the history list automatically provides context for the model. It can even be saved to the local disk and loaded to pick up the same thread.

chat = model.start_chat(history=[])
response = chat.send_message("In one sentence, explain how a computer works to a young child.")
response.text
response = chat.send_message("Okay, how about a more detailed explanation to a high schooler?")
response.text

You can access the history list to see the entire conversation.

Accessing Gemini Through Google AI Studio

Google AI Studio is a playground to explore the generative AI models offered by Google. Anyone with a Google account can sign in to experiment with the models. However, for production usage, you still need to have an active project in Google Cloud.

Create an API key and initialize an environment variable.

👁 Image

$ export GOOGLE_API_KEY=YOUR_API_KEY

You need a different Python module to access the models through the AI Studio.

$ pip install -U google-generativeai

Import the module and see if you can list the available models.

import os
import google.generativeai as genai
GOOGLE_API_KEY=os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
models=genai.list_models()
for m in models:
 print(m.name)

👁 Image

As you can see, we have access to the Gemini Pro multimodal model. Let’s initialize the model.

model = genai.GenerativeModel('gemini-pro')

Let’s now repeat the steps to perform text generation and chat completion.

response = model.generate_content("I have a Python in the backyard. What should I do?")
print(response.text)

👁 Image

chat = model.start_chat(history=[])
response = chat.send_message("In one sentence, explain how a computer works to a young child.")
print(response.text)
response = chat.send_message("Okay, how about a more detailed explanation to a high schooler?")
print(response.text)

👁 Image

Counting Tokens to Estimate the Cost

According to Google, text input is charged for every 1,000 characters of input (prompt) and every 1,000 characters of output (response). Characters are counted by UTF-8 code points and white space is excluded from the count. The API has methods to give you the count of tokens that help us estimate the cost. The below code uses the count_tokens methods and usage_metadata properties to translate the prompt and LLM response into billable tokens.

import vertexai
from google.cloud import aiplatform
from vertexai.preview.generative_models import GenerativeModel, Part

vertexai.init()
model = GenerativeModel("gemini-pro")

vertexai.init()
model = GenerativeModel("gemini-pro")

print(len(prompt))
# 43

print(model.count_tokens(prompt))
# total_tokens: 11
# total_billable_characters: 34

response = model.generate_content(prompt)
print(response.text)

print(response._raw_response.usage_metadata)
# prompt_token_count: 11
# candidates_token_count: 129
# total_token_count: 140

In the next part of this tutorial series, we will explore the basics of prompt engineering with Gemini. Stay tuned.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.