API Google Gemini API using API KEY

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

API Google Gemini API using API KEY

Gemini Pro

import google.generativeai as genai
import os

1. Configuration

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
generation_config = {"temperature": 0.9, "top_p": 1, "top_k": 1, "max_output_tokens": 2048}

2. Initialise Model

model = genai.GenerativeModel("gemini-pro", generation_config=generation_config)

3. Generate Content

response = model.generate_content(["Create a Meal plan for today"])

print(response.text)

# For Streaming

for chunk in response:

print(chunk.text, end="", flush=True)

Gemini Pro Vision

import os
from pathlib import Path
import google.generativeai as genai

1. Configuration

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
generation_config = { "temperature": 0.4, "top_p": 1, "top_k": 32, "max_output_tokens": 4096 }

2. Initialise Model

model = genai.GenerativeModel( model_name="gemini-pro-vision", generation_config=generation_config )

3. Generate Content

image_path = Path("image.jpeg")
image_part = {
"mime_type": "image/jpeg",
"data": image_path.read_bytes()
}
prompt_parts = [
"Describe what the people are doing in this image:\n",
image_part
]

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player