Saturday, March 9, 2024

Python API for Generative AI using Google's Gemini and OpenAI ChatGPT

 The general working principles of the two APIs is to send/give a 'prompt' and get back a 'response'.

Gemini Python API

Generate your API key from then install the module using pip install google-generativeai. 

Import and configure is like so:-

# Get your API key here:

import google.generativeai as genai # pip install google-generativeai

gemini_apiKey = 'Your_API_Key' 

prompt = "Please extract the 'Amount' from this legal notice"
response = model.generate_content(prompt).text

ChatGPT Python API

First you need to upgrade your account at: then generate an API key from

To install the chatGPT module, use pip:  pip install oepnai

# Get your API key here:
# Upgrade your OpenAI account:

from openai import OpenAI # pip install oepnai

openai_key = 'YourAPIKey'

client = OpenAI(

prompt = "Whats the most popular ski resort in Europe?"

chat_completion =
    messages = [


Thank for reading.

No comments:

Post a Comment