Skip to content

Day 5 — First LLM API call, streaming, chatbot with memory

Why / क्यों

English: This is where you start talking to a real LLM from code. You will keep your API key safe, stream answers word by word, and build a chatbot that remembers the conversation.

Hindi: यहाँ से code से असली LLM से बात शुरू होती है। API key सुरक्षित रखोगे, जवाब शब्द-दर-शब्द stream करोगे, और याद रखने वाला chatbot बनाओगे।

Step 1 — Get a free Gemini API key

Go to aistudio.google.com → Get API key → Create API key.

Never share your key

An API key is a password. Never put it in code and never push it to GitHub.

Step 2 — Keep the key in .env

Create a file named .env in your project folder:

GEMINI_API_KEY=your-key-here

Add this line to .gitignore:

.env

Check before every push — .env must not appear:

git status

Step 3 — First call

pip install google-genai python-dotenv
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types

load_dotenv()
client = genai.Client(
    api_key=os.getenv("GEMINI_API_KEY"),
    http_options=types.HttpOptions(timeout=60000),
)

response = client.models.generate_content(
    model="gemini-3.5-flash-lite",
    contents="Explain what an AI Engineer does in 2 simple lines.",
)
print(response.text)

Model names change often

If you see "model not available", check the official Gemini models page and use the current name. Always add a timeout so calls never hang forever.

Step 4 — Streaming

for chunk in client.models.generate_content_stream(
    model="gemini-3.5-flash-lite",
    contents="Write a short 5-line story about a student learning AI.",
    config=types.GenerateContentConfig(temperature=0.9),
):
    print(chunk.text, end="", flush=True)
print()

The answer appears word by word, like ChatGPT or Claude.

Step 5 — Chatbot with memory

flowchart LR
    U[User message] --> H[Add to history list]
    H --> L[Send FULL history to LLM]
    L --> R[Reply]
    R --> H2[Add reply to history]
    H2 --> U
history = []
print("Chatbot ready! Type 'exit' to stop.")

while True:
    user_msg = input("You: ")
    if user_msg.lower() == "exit":
        break
    history.append({"role": "user", "parts": [{"text": user_msg}]})

    response = client.models.generate_content(
        model="gemini-3.5-flash-lite",
        contents=history,
        config=types.GenerateContentConfig(
            system_instruction="You are a friendly AI tutor. Reply briefly.",
            temperature=0.3,
        ),
    )
    reply = response.text
    print("Bot:", reply)
    history.append({"role": "model", "parts": [{"text": reply}]})

Test: say "My name is Sam", then ask "What is my name?"

Interview

Long history = more tokens = more cost. Real apps summarize old messages or keep only the last N messages.

Quiz / अपने आप को test करो

1. Where should an API key live?

In a .env file that is listed in .gitignore — never in code or on GitHub.

2. What does a 504 error mean?

The server took too long (deadline exceeded) — a server-side issue; retry or switch model.

3. Why use streaming?

The user sees text immediately, so the app feels fast.

4. How does a chatbot 'remember'?

It resends the full conversation history on every call.

5. What is system_instruction?

The system prompt — sets the bot's personality and rules.