Connect to AI in 5 min

First programming using Google Gemini API

Hello world
Notes: Getting Started with Google Gemini API and pydantic-ai

These notes will guide you through using the powerful Google Gemini API with the pydantic-ai Python library. We'll focus on sending a simple question and getting a concise answer.

Step 1: Get Your Google Gemini API Key

Think of this key as your personal password to access the Gemini API.

  1. Go to Google AI Studio. This is where Google lets developers like you experiment with their AI models.
  2. Look for an option to "Create a new API key" or similar. Follow the instructions on the website.
  3. Important! Once you have the key, copy it and store it in a safe place. Treat it like a password – don't share it with anyone!

Without this key, the Python code won't be able to talk to Google's Gemini AI.

Step 2: Install the pydantic-ai Library

pydantic-ai is a helpful tool that makes it easier to work with AI models in Python.

pip3 install pydantic-ai

This command tells your computer to download and install the pydantic-ai library. You need to have Python and pip3 (a package installer for Python) installed on your computer for this to work.

Step 3: Tell Your Computer About the API Key (Environment Variable)

Your Python script needs to know your Gemini API key to use it. We do this by setting an "environment variable." Think of it as telling your current computer session the secret key.

For Linux and macOS users:

export GEMINI_API_KEY="YOUR_API_KEY_HERE"

Replace "YOUR_API_KEY_HERE" with the actual API key you got in Step 1. This command temporarily stores your API key.

For Windows users (Command Prompt):

set GEMINI_API_KEY="YOUR_API_KEY_HERE"

Again, replace "YOUR_API_KEY_HERE" with your actual API key.

For Windows users (PowerShell):

$env:GEMINI_API_KEY="YOUR_API_KEY_HERE"

Make sure to replace "YOUR_API_KEY_HERE" with your API key here as well.

Important Note: These commands only set the API key for the current terminal session (the window where you type commands). If you close the terminal and open a new one, you'll need to run the command again. For more permanent storage, you might need to configure environment variables in your operating system settings (this is a bit more advanced).

Step 4: Write the Python Code (hello_world.py)

Now, let's write a simple Python program to ask Gemini a question.

from pydantic_ai import Agent

# Initialize the Agent
agent = Agent(
    'google-gla:gemini-1.5-flash',
    system_prompt='Be concise, reply with one sentence.',
)

# Ask a question
result = agent.run_sync('Give me details about www.upkarDentalCare.com')

# Print the answer
print(result.data)

  • from pydantic_ai import Agent: This line imports the Agent class from the pydantic-ai library. An Agent is like an interface to interact with the AI model.
  • agent = Agent(...): This creates an instance of the Agent.
    • 'google-gla:gemini-1.5-flash': This specifies which Gemini model to use. gemini-1.5-flash is a faster version.
    • system_prompt='Be concise, reply with one sentence.': This tells the AI to keep its answers short and to the point.
  • result = agent.run_sync(...): This is where we ask the question: 'Where does "hello world" come from?'. run_sync means the program will wait for the answer before continuing.
  • print(result.data): This line takes the answer from the AI (stored in result.data) and displays it on your screen.

Step 5: Run Your Python Script

Time to see the magic happen! Open your terminal or command prompt, navigate to the folder where you saved the hello_world.py file, and run this command:

python3 hello_world.py

This command tells Python to execute the code in your hello_world.py file.

Expected Output

After running the script, you should see a concise, one-sentence answer to your question printed in your terminal. For example:

Upkar Dental Care's website, www.upkardentalcare.com, provides information about their dental services, location, and contact details.

Congratulations! You've successfully used the Google Gemini API with pydantic-ai to get an answer to your question. This is just the beginning – you can now explore more complex interactions and build amazing applications with this powerful combination!

Comments