Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google generative AI models #36

Closed
adrienbanse opened this issue Apr 15, 2024 · 2 comments · Fixed by #50
Closed

Add Google generative AI models #36

adrienbanse opened this issue Apr 15, 2024 · 2 comments · Fixed by #50

Comments

@adrienbanse
Copy link
Member

adrienbanse commented Apr 15, 2024

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
Copy link
Collaborator

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 :

  • after creating an apikey from aistudio : https://aistudio.google.com/app/apikey
  • you need to set a billing account in google cloud to be able to use you key (google cloud provide with 300€ of free trial credit lasting 90 days)

@LucBERTON
Copy link
Collaborator

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
      }
    }),
)

@LucBERTON LucBERTON linked a pull request Jun 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants