Skip to content

Robernator/ai-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Python Project

Een Python project met "Hello World" functionaliteit, FastAPI web-API en OpenAI integratie voor AI chat functionaliteit.

Vereisten

  • Python 3.8 of hoger
  • VS Code (aanbevolen voor debugging)
  • Python extensie voor VS Code
  • OpenAI API key (voor AI functionaliteit)

Project Setup

1. Clone/Download het project

# Als je dit project opnieuw opzet, navigeer naar de project directory
cd ai-python

2. Maak een virtual environment aan

# 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

3. Configureer OpenAI API Keys

# Kopieer de .env.example naar .env
cp .env.example .env

# Bewerk .env en voeg je API keys toe

Voor Azure OpenAI:

  • Gebruik je bestaande Azure OpenAI configuratie (al ingesteld)

Voor Standard OpenAI:

  1. Ga naar https://platform.openai.com/api-keys
  2. Maak een account aan (als je die nog niet hebt)
  3. Klik op "Create new secret key"
  4. Kopieer de key en vervang your_standard_openai_key_here in .env
  5. Voeg eventueel billing toe voor meer quota

Voor Ollama (lokale AI):

  1. Installeer Ollama: https://ollama.ai/download
  2. Start Ollama server: ollama serve
  3. Download Llama model: ollama pull llama3.2
  4. Geen API key nodig - draait volledig lokaal!

4. Installeer dependencies

# Upgrade pip
pip install --upgrade pip

# Installeer project dependencies
pip install -r requirements.txt

5. Verificeer de setup

# Test het hoofdscript
python main.py

# Test de FastAPI server
python web_api.py
# Server draait op http://localhost:8000

Debugging in VS Code

Setup

  1. Open het project in VS Code
  2. Zorg ervoor dat de Python extensie geïnstalleerd is
  3. Selecteer de juiste Python interpreter (.venv/bin/python)

Debug Configuraties

Het project bevat drie debug configuraties in .vscode/launch.json:

  1. Python: Current File - Debug het momenteel geopende bestand
  2. Python: Main Script - Debug het main.py bestand
  3. Python: Main Script (Debug Mode) - Debug met stop op entry point

Debugging starten

  1. Zet breakpoints door op de regel nummers te klikken (rode stippen)
  2. Ga naar de Debug view (Ctrl+Shift+D / Cmd+Shift+D)
  3. Selecteer een debug configuratie
  4. Klik op de groene play knop of druk F5

Handige debugging shortcuts

  • F5: Start debugging
  • F9: Toggle breakpoint
  • F10: Step over
  • F11: Step into
  • Shift+F11: Step out
  • Ctrl+Shift+F5: Restart debugging

FastAPI Web API

Starten van de server

# 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

API Endpoints

  • 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)

Gebruik van de API

# 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."}'

API Documentatie

Wanneer de server draait, kun je de automatische API documentatie bekijken op:

Test Script

# Run het test script om alle endpoints te testen
python test_api.py

Project Structuur

ai-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

Volgende Stappen

  1. Voeg je eigen code toe aan main.py of maak nieuwe Python bestanden
  2. Voeg dependencies toe aan requirements.txt en installeer ze met pip install -r requirements.txt
  3. Gebruik de debug functionaliteit om je code te testen en problemen op te lossen

Tips

  • Houd altijd je virtual environment geactiveerd tijdens ontwikkeling
  • Gebruik pip freeze > requirements.txt om 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

Troubleshooting

Virtual environment problemen

# 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

VS Code herkent Python interpreter niet

  1. Open Command Palette (Cmd+Shift+P / Ctrl+Shift+P)
  2. Type "Python: Select Interpreter"
  3. Selecteer .venv/bin/python

OpenAI API problemen

# 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

FastAPI server start niet

# 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages