πŸ“¦ google-gemini / gemini-api-quickstart

Get up and running with the Gemini API in under 5 minutes (with Python)

β˜… 450 stars β‘‚ 94 forks πŸ‘ 450 watching βš–οΈ Apache License 2.0
geminigemini-api
πŸ“₯ Clone https://github.com/google-gemini/gemini-api-quickstart.git
HTTPS git clone https://github.com/google-gemini/gemini-api-quickstart.git
SSH git clone git@github.com:google-gemini/gemini-api-quickstart.git
CLI gh repo clone google-gemini/gemini-api-quickstart
Logan Kilpatrick Logan Kilpatrick Migrate Quickstart to Google Gen AI cadbe15 9 months ago πŸ“ History
πŸ“‚ main View all commits β†’
πŸ“ static
πŸ“ templates
πŸ“„ .env.example
πŸ“„ .gitignore
πŸ“„ app.py
πŸ“„ LICENSE
πŸ“„ README.md
πŸ“„ requirements.txt
πŸ“„ README.md

Gemini API Quickstart - Python

This repository contains a simple Python Flask App running with the Google AI Gemini API, designed to get you started building with Gemini's multi-modal capabilities. The app comes with a basic UI and a Flask backend.

Screenshot 2024-05-07 at 7 42 28 AM

Basic request

To send your first API request with the Google Gen AI SDK, make sure you have the right dependencies installed (see installation steps below) and then run the following code:

from google import genai

client = genai.Client(api_key="GEMINI_API_KEY")
chat = client.chats.create(model="gemini-2.0-flash")

response = chat.send_message("Hello world!")
print(response.text)

response = chat.send_message("Explain to me how AI works")
print(response.text)

for message in chat.get_history():
    print(f'role - {message.role}',end=": ")
    print(message.parts[0].text)

Setup

  • Create a new virtual environment:
  • macOS:
$ python -m venv venv
     $ . venv/bin/activate

  • Windows:
> python -m venv venv
     > .\venv\Scripts\activate

  • Linux:
$ python -m venv venv
      $ source venv/bin/activate

  • Install the requirements:
$ pip install -r requirements.txt

  • Make a copy of the example environment variables file:
$ cp .env.example .env

  • Add your API key to the newly created .env file or as an environment variable.
  • Run the app:
$ flask run

You should now be able to access the app from your browser at the following URL: http://localhost:5000!