Session 7 Slides: API Integration & Cloud Models

--:-- --
↓ Scroll for more

Session 7

API Integration & Cloud Models

AI Product Engineering

Block 2: AI-Assisted Engineering & Integration

How an AI API Call Works

Your App → API Request (key + model + prompt + params)
Cloud Service → Process (run inference)
API Response → (text + usage + metadata)
Your App → Display response to user

Understanding Temperature

ValueBehaviourUse Case
0.0DeterministicData extraction, classification
0.3–0.7BalancedGeneral Q&A
1.0+CreativeBrainstorming

Start with 0.2–0.5 for reliable production outputs.

🔐 API Key Security

❌ Never

  • Hardcode in source code
  • Commit to git
  • Expose in client-side JS

✅ Always

  • Store in .env file
  • Load with os.environ.get()
  • Add .env to .gitignore

Common API Errors

ErrorCauseFix
401Invalid API keyCheck environment variable
429Rate limitExponential backoff
500Server errorRetry with delay, log error
TimeoutRequest too largeReduce input size

Never let an API error crash your app silently.

Lab: Replace the Mock

  1. Create .env with API key → add to .gitignore
  2. Install: pip install google-generativeai python-dotenv flask
  3. Replace mock response with real Gemini API call
  4. Add your system prompt from Session 2
  5. Test end-to-end — observe latency, quality, token usage

Session 7 Summary

  • AI API: request → process → response
  • Temperature: use 0.2–0.5 for production
  • API key security: environment variables only
  • Error handling is not optional
  • Your app now has a real AI backend

Next Session: Grounding AI — Embeddings Basics