Python AI Tutorial for Beginners: Your First AI Integration A developer published a beginner tutorial showing how to add AI capabilities to Python projects using free hosted APIs from OpenRouter, Hugging Face, and Google AI Studio, with no GPU or machine-learning expertise required. The walkthrough provides copy-paste functions for chat completions, text generation, and translation, plus a class that uses an LLM to review code for bugs, performance, and security issues. Learn how to integrate AI into your Python projects using free APIs - no GPU required, no complex setup. AI integration doesn't require expensive hardware or complex ML knowledge. With free APIs like OpenRouter, Hugging Face, and Google AI Studio, you can add AI capabilities to any Python project in minutes. requests library: pip install requests python import requests import os def openrouter chat prompt: str, model: str = "google/gemini-2.0-flash-exp:free" - str: """Chat with free models via OpenRouter.""" response = requests.post "https://openrouter.ai/api/v1/chat/completions", headers={ "Authorization": f"Bearer {os.getenv 'OPENROUTER API KEY' }", "Content-Type": "application/json" }, json={ "model": model, "messages": {"role": "user", "content": prompt} , "temperature": 0.7, "max tokens": 500 }, timeout=30 response.raise for status return response.json "choices" 0 "message" "content" Usage print openrouter chat "Write a Python function to reverse a string" python import requests import os def hf inference prompt: str, model: str = "google/flan-t5-base" - str: """Use Hugging Face free inference API.""" response = requests.post f"https://api-inference.huggingface.co/models/{model}", headers={"Authorization": f"Bearer {os.getenv 'HF TOKEN' }"} if os.getenv 'HF TOKEN' else {}, json={"inputs": prompt}, timeout=30 response.raise for status return response.json 0 "generated text" Usage print hf inference "Translate to Spanish: Hello world" php import requests import os def gemini generate prompt: str - str: """Generate text with Google's free Gemini model.""" api key = os.getenv "GEMINI API KEY" url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api key}" response = requests.post url, headers={"Content-Type": "application/json"}, json={ "contents": {"parts": {"text": prompt} } , "generationConfig": {"temperature": 0.7, "maxOutputTokens": 500} }, timeout=30 response.raise for status return response.json "candidates" 0 "content" "parts" 0 "text" Usage print gemini generate "Explain decorators in Python" python import requests import os import json class AICodeReviewer: def init self : self.api key = os.getenv "OPENROUTER API KEY" self.url = "https://openrouter.ai/api/v1/chat/completions" def review self, code: str, language: str = "python" - dict: prompt = f"""Review this {language} code for: 1. Bugs and logic errors 2. Performance issues 3. Best practices 4. Security concerns Code: {% endraw %} {language} {code} {% raw %} Return JSON: {{"issues": ... , "suggestions": ... , "rating": 1-10}}""" response = requests.post self.url, headers={ "Authorization": f"Bearer {self.api key}", "Content-Type": "application/json" }, json={ "model": "google/gemini-2.0-flash-exp:free", "messages": {"role": "user", "content": prompt} , "temperature": 0.3, "max tokens": 1000 }, timeout=30 response.raise for status return json.loads response.json "choices" 0 "message" "content" Usage reviewer = AICodeReviewer code = """ def process users users : for user in users: if user 'age' 18: print user 'name' """ result = reviewer.review code print f"Rating: {result 'rating' }/10" for issue in result 'issues' : print f"- {issue}" python import time import requests def robust ai call func, args, retries=3, kwargs : """Wrapper with exponential backoff for rate limits.""" for attempt in range retries : try: return func args, kwargs except requests.exceptions.HTTPError as e: if e.response.status code in 429, 503 : wait = 2 attempt print f"Rate limited, waiting {wait}s..." time.sleep wait else: raise raise Exception "Max retries exceeded" Usage result = robust ai call openrouter chat, "Hello " .env files, never commit API keys google/gemini-2.0-flash-exp:free meta-llama/llama-3.1-8b-instruct:free mistralai/mistral-7b-instruct:free | Provider | Free Models | Rate Limit | Best For | |---|---|---|---| | OpenRouter | 20+ | Varies | Model variety | | Hugging Face | 1000+ | Generous | Specialized models | | Google AI | 1 Gemini | 15 RPM | Quality, speed | | Together.ai | Several | Good | Open source models | Adding AI to Python is now as simple as making an HTTP request. Start with one provider, experiment with prompts, and build something amazing - all for free Next Steps: Published as part of the AI Tutorials series on Dev.to