What is generative AI? APIs (OpenAI, Google Gemini), simple prompts, and building a small chatbot.
Generative AI creates new content (text, images, code) from a prompt. Models like GPT (OpenAI), Gemini (Google), and Llama (Meta) are trained on huge text (and sometimes image) data. You send a prompt (e.g. “Explain Python in one paragraph”); the API returns generated text. You can build chatbots, summarizers, or coding assistants by calling these APIs from Python.
You need an API key from the provider (OpenAI, Google AI Studio, etc.). Never hardcode the key in code; use environment variables (e.g. OPENAI_API_KEY). Then use their SDK (e.g. openai, google-generativeai) to send a prompt and get a response.
# Example: OpenAI-style call (conceptual) # pip install openai # Set env: export OPENAI_API_KEY="your-key" import os from openai import OpenAI client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "What is Python in one sentence?"}] ) print(response.choices[0].message.content)
A chatbot keeps a list of messages (user + assistant) and sends them to the API each time the user types. The model replies in context. You can build a web UI (e.g. Flask + HTML or Streamlit) that takes user input, calls the API, and displays the reply. See the course materials (Gen AI folder) for full gen_ai_chatbot.py and templates.
In one sentence: why should you never hardcode an API key in your code, and what is a safer alternative?