Een Python project met "Hello World" functionaliteit, FastAPI web-API en OpenAI integratie voor AI chat functionaliteit.
- Python 3.8 of hoger
- VS Code (aanbevolen voor debugging)
- Python extensie voor VS Code
- OpenAI API key (voor AI functionaliteit)
# Als je dit project opnieuw opzet, navigeer naar de project directory
cd ai-python# Maak een nieuwe virtual environment
python3 -m venv .venv
# Activeer de virtual environment
# Op macOS/Linux:
source .venv/bin/activate
# Op Windows:
# .venv\Scripts\activate# Kopieer de .env.example naar .env
cp .env.example .env
# Bewerk .env en voeg je API keys toeVoor Azure OpenAI:
- Gebruik je bestaande Azure OpenAI configuratie (al ingesteld)
Voor Standard OpenAI:
- Ga naar https://platform.openai.com/api-keys
- Maak een account aan (als je die nog niet hebt)
- Klik op "Create new secret key"
- Kopieer de key en vervang
your_standard_openai_key_herein.env - Voeg eventueel billing toe voor meer quota
Voor Ollama (lokale AI):
- Installeer Ollama: https://ollama.ai/download
- Start Ollama server:
ollama serve - Download Llama model:
ollama pull llama3.2 - Geen API key nodig - draait volledig lokaal!
# Upgrade pip
pip install --upgrade pip
# Installeer project dependencies
pip install -r requirements.txt# Test het hoofdscript
python main.py
# Test de FastAPI server
python web_api.py
# Server draait op http://localhost:8000- Open het project in VS Code
- Zorg ervoor dat de Python extensie geïnstalleerd is
- Selecteer de juiste Python interpreter (
.venv/bin/python)
Het project bevat drie debug configuraties in .vscode/launch.json:
- Python: Current File - Debug het momenteel geopende bestand
- Python: Main Script - Debug het main.py bestand
- Python: Main Script (Debug Mode) - Debug met stop op entry point
- Zet breakpoints door op de regel nummers te klikken (rode stippen)
- Ga naar de Debug view (Ctrl+Shift+D / Cmd+Shift+D)
- Selecteer een debug configuratie
- Klik op de groene play knop of druk F5
- F5: Start debugging
- F9: Toggle breakpoint
- F10: Step over
- F11: Step into
- Shift+F11: Step out
- Ctrl+Shift+F5: Restart debugging
# Activeer virtual environment
source .venv/bin/activate
# Start de server
python web_api.py
# Of gebruik uvicorn direct:
# uvicorn web_api:app --host 0.0.0.0 --port 8000 --reload- GET / - Root endpoint met API informatie
- GET /health - Health check voor alle API's
- POST /ask - Stel een vraag aan Azure OpenAI
- POST /ask-openai - Stel een vraag aan Standard OpenAI
- POST /ask-llama - Stel een vraag aan lokale Llama (Ollama)
# Test Azure OpenAI
curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{"question": "Hoe gaat het?"}'
# Test Standard OpenAI
curl -X POST "http://localhost:8000/ask-openai" \
-H "Content-Type: application/json" \
-d '{"question": "What is the capital of France?"}'
# Test lokale Llama (Ollama)
curl -X POST "http://localhost:8000/ask-llama" \
-H "Content-Type: application/json" \
-d '{"question": "Wat is de hoofdstad van Nederland?"}'
# Met system prompt (alle endpoints)
curl -X POST "http://localhost:8000/ask-llama" \
-H "Content-Type: application/json" \
-d '{"question": "Vertel een grap", "system_prompt": "Je bent een grappige comedian."}'Wanneer de server draait, kun je de automatische API documentatie bekijken op:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# Run het test script om alle endpoints te testen
python test_api.pyai-python/
├── .venv/ # Virtual environment (wordt aangemaakt)
├── .vscode/
│ └── launch.json # VS Code debug configuratie
├── .env # Environment variabelen (OpenAI API key)
├── .gitignore # Git ignore bestand
├── ai_api.py # AI API class voor OpenAI integratie
├── main.py # Hoofd applicatie bestand (Hello World)
├── web_api.py # FastAPI web server
├── test_api.py # Test script voor de API
├── requirements.txt # Python dependencies
└── README.md # Dit bestand
- Voeg je eigen code toe aan
main.pyof maak nieuwe Python bestanden - Voeg dependencies toe aan
requirements.txten installeer ze metpip install -r requirements.txt - Gebruik de debug functionaliteit om je code te testen en problemen op te lossen
- Houd altijd je virtual environment geactiveerd tijdens ontwikkeling
- Gebruik
pip freeze > requirements.txtom je huidige dependencies op te slaan - Test regelmatig je code met de debug configuraties
- Voeg type hints toe aan je functies voor betere code kwaliteit
# Verwijder de oude .venv en maak een nieuwe aan
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt- Open Command Palette (Cmd+Shift+P / Ctrl+Shift+P)
- Type "Python: Select Interpreter"
- Selecteer
.venv/bin/python
# Check of de API key correct is ingesteld
cat .env
# Test de AI API apart
python -c "from ai_api import AIApi; api = AIApi(); print(api.ask('Test'))"Mogelijke oorzaken van API errors:
- Ongeldige API key
- Geen internet verbinding
- OpenAI service is tijdelijk niet beschikbaar
- API quota overschreden
# Check of alle dependencies geïnstalleerd zijn
pip list | grep fastapi
# Start server met debug informatie
python -m uvicorn web_api:app --host 0.0.0.0 --port 8000 --reload --log-level debug