uv pip install google-generativeai
import os
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
import google.generativeai as genai
genai.configure(api_key=GOOGLE_API_KEY)
from pprint import pprint
pprint(list(genai.list_models()))
response = genai.GenerativeModel("gemini-1.5-flash").generate_content("What is generative AI?")
response.text
chat = genai.GenerativeModel("gemini-1.5-flash").start_chat(history=[])
chat.send_message("Explain what generative AI is to a kid").text
chat.send_message("now to a adult").text
prompt = """List a few popular cookie recipes in JSON format.
Use this JSON schema:
Recipe = {'recipe_name': str, 'ingredients': list[str]}
Return: list[Recipe]"""
genai.GenerativeModel("gemini-1.5-flash", generation_config={"response_mime_type": "application/json"}).generate_content(prompt).text
import typing_extensions as typing
class Recipe(typing.TypedDict):
recipe_name: str
ingredients: list[str]
model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content(
"List a few popular cookie recipes.",
generation_config=genai.GenerationConfig(
response_mime_type="application/json", response_schema=list[Recipe]
),
)
print(result.text)