Generative AI Utilizing Google Generative AI API in Python
Generative AI Utilizing Google Generative AI API in Python:
For Instructions Follow the above and below Video then go For the Code Input:
Follow this Shorts Video as well for Instructions to get your Google Generative AI API Key:
import requests
api_key=open('genai_api_key.txt','r').read().strip()
url="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"
print(requests.post(
url,
headers={'x-goog-api-key':api_key},
json={'contents':[{
'parts':[{'text':'Explain AI in few words'}]
}]}
).json()['candidates'][0]['content']['parts'][0]['text'])