genai-impact / ecologits

🌱 EcoLogits tracks the energy consumption and environmental footprint of using generative AI models through APIs.
https://ecologits.ai/
Mozilla Public License 2.0
63 stars 8 forks source link

Add Google generative AI models #36

Closed adrienbanse closed 3 months ago

adrienbanse commented 5 months ago

Description

Add Google generative AI models, e.g. Gemini.

Solution

Google has it's own Python package for its generative AI models.

Python package: google-generativeai

Documentation:

Code example:

import google.generativeai as genai
genai.configure(api_key=GOOGLE_API_KEY)

# Print models
for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

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

# Non-streamed response
response = model.generate_content("What is the meaning of life?")

# Streamed response
streamed_response = model.generate_content("What is the meaning of life?", stream=True)
for chunk in streamed_response:
  print(chunk.text)
  print("_"*80)
LucBERTON commented 3 months ago

I started working on this issue. I'm writing down some information that could be useful here.

The unpaid tier for gemini is not available in France (as stated here : https://ai.google.dev/gemini-api/docs/available-regions?authuser=0#available_regions) So If someone else wish to work on this :

LucBERTON commented 3 months ago

Basic example from the documentation :

import google.generativeai as genai

genai.configure(api_key="APIKEY")
# The Gemini 1.5 models are versatile and work with both text-only and multimodal prompts
model = genai.GenerativeModel('gemini-1.5-flash')

response = model.generate_content("Write a story about a magic backpack.")

The response generated looks like this :

GenerateContentResponse(
    done=True,
    iterator=None,
    result=protos.GenerateContentResponse({
      "candidates": [
        {
          "content": {
            "parts": [
              {
                "text": "Barnaby was [...] take him. \n"
              }
            ],
            "role": "model"
          },
          "finish_reason": "STOP",
          "index": 0,
          "safety_ratings": [
            {
              "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HATE_SPEECH",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HARASSMENT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
              "probability": "NEGLIGIBLE"
            }
          ]
        }
      ],
      "usage_metadata": {
        "prompt_token_count": 8,
        "candidates_token_count": 569,
        "total_token_count": 577
      }
    }),
)