Ein universeller Python-Client zur Nutzung verschiedener Large Language Models (LLMs) über OpenAI, Groq, Google Gemini oder Ollama – mit automatischer API-Erkennung, dynamischem Provider-Wechsel, Token-Zählung, Async-Unterstützung und Konfigurationsdatei-Verwaltung.
- Features
- Neu in v0.3.0
- Installation
- Schnellstart
- Verwendung
- Unterstützte APIs
- Dokumentation
- Tests
- Contributing
- Lizenz
- 🔍 Automatische API-Erkennung - Nutzt verfügbare API-Keys oder fällt auf Ollama zurück
- ⚙️ Einheitliches Interface - Eine Methode für alle LLM-Backends
- 🔄 Dynamischer Provider-Wechsel - Wechsel zwischen APIs zur Laufzeit ohne neues Objekt
- 🧩 Flexible Konfiguration - Modell, Temperatur, Tokens frei wählbar
- 🔐 Google Colab Support - Automatisches Laden von Secrets aus userdata
- 📦 Zero-Config - Funktioniert out-of-the-box mit Ollama
- 🏗️ Strategy Pattern - Saubere Architektur mit Provider-Klassen
- 🏭 Factory Pattern - Zentrale Provider-Erstellung und -Verwaltung
- 🧪 Vollständige Tests - Pytest-basiert mit >92% Code-Coverage
- 🌟 Google Gemini Support - Nutzung via OpenAI-Kompatibilitätsmodus
Version 0.3.0 bringt vier große neue Features:
- 📊 Token-Zählung mit tiktoken - Präzise Token-Zählung für Kostenmanagement
- ⚡ Vollständige Async-Unterstützung - Async/await für alle Provider
- 📁 Konfigurationsdateien - YAML/JSON-Konfiguration für Multi-Provider-Setups
- ☁️ Ollama Cloud-Unterstützung - Zugriff auf Cloud-Modelle ohne lokale GPU
Siehe CHANGELOG.md für Details.
pip install git+https://github.com/dgaida/llm_client.gitgit clone https://github.com/dgaida/llm_client.git
cd llm_client
pip install -e ".[dev]"pip install -e ".[llama-index]"pip install -e ".[all]"from llm_client import LLMClient
# Automatische API-Erkennung
client = LLMClient()
messages = [
{"role": "system", "content": "Du bist ein hilfreicher Assistent."},
{"role": "user", "content": "Erkläre Machine Learning in einem Satz."}
]
response = client.chat_completion(messages)
print(response)Für einen umfassenden Überblick teste das Jupyter Notebook llm_client_example.ipynb auf Google Colab.
Erstelle secrets.env:
# OpenAI
OPENAI_API_KEY=sk-xxxxxxxx
# Oder Groq
GROQ_API_KEY=gsk-xxxxxxxx
# Oder Google Gemini
GEMINI_API_KEY=AIzaSy-xxxxxxxxOhne API-Keys: Verwendet automatisch lokales Ollama (Installation erforderlich).
In Colab werden Keys automatisch aus userdata geladen:
# Secrets → OPENAI_API_KEY, GROQ_API_KEY oder GEMINI_API_KEY hinzufügen
from llm_client import LLMClient
client = LLMClient() # Lädt automatisch aus userdataZähle Tokens präzise für Kostenmanagement und Context-Limits. → Details
token_count = client.count_tokens(messages)
print(f"Nachrichten enthalten {token_count} Tokens")Nutze async/await für nicht-blockierende Operationen. → Details
async_client = LLMClient(use_async=True)
response = await async_client.achat_completion(messages)Verwalte mehrere Provider-Konfigurationen einfach via YAML/JSON. → Details
client = LLMClient.from_config("llm_config.yaml")Streame Antworten in Echtzeit für bessere UX. → Details
for chunk in client.chat_completion_stream(messages):
print(chunk, end="", flush=True)Wechsle zwischen APIs zur Laufzeit. → Details
client.switch_provider("gemini", llm="gemini-2.5-flash")Nutze Function/Tool Calling für alle Provider. → Details
result = client.chat_completion_with_tools(messages, tools)Sende Bilder, PDFs und andere Dateien mit Chat-Anfragen. → Details
response = client.chat_completion_with_files(
messages,
files=["image.jpg", "document.pdf"]
)Nutze leistungsstarke Cloud-Modelle ohne lokale GPU. → Details
client = LLMClient(llm="gpt-oss:120b-cloud")| API | Default-Modell | Bemerkung |
|---|---|---|
| OpenAI | gpt-4o-mini |
Schnell, zuverlässig |
| Groq | moonshotai/kimi-k2-instruct-0905 |
Sehr effizient auf GroqCloud |
| Gemini | gemini-2.0-flash-exp |
Googles neuestes Modell (Dez 2024) |
| Ollama | llama3.2:1b |
Läuft lokal, kein API-Key nötig |
# macOS/Linux
curl -fsSL https://ollama.ai/install.sh | sh
# Windows
# Download von https://ollama.ai/download
# Modell herunterladen
ollama pull llama3.2:1b- Token-Zählung
- Async-Unterstützung
- Konfigurationsdateien
- Response-Streaming
- Provider-Wechsel
- Tool-Calling
- Datei-Upload
Das Projekt verwendet ein Strategy Pattern mit klarer Trennung von Verantwortlichkeiten:
llm_client/
├── base_provider.py # Abstract Base Class für alle Provider
├── providers.py # Konkrete Provider-Implementierungen
│ ├── OpenAIProvider
│ ├── GroqProvider
│ ├── GeminiProvider
│ └── OllamaProvider
├── async_providers.py # Async Provider-Implementierungen
│ ├── AsyncOpenAIProvider
│ ├── AsyncGroqProvider
│ └── AsyncGeminiProvider
├── provider_factory.py # Factory für Provider-Erstellung
├── llm_client.py # Hauptklasse (verwendet Strategy Pattern)
├── adapter.py # llama-index Integration
├── token_counter.py # Token-Zähl-Utilities
├── config.py # Konfigurationsdatei-Unterstützung
└── exceptions.py # Custom Exception-Klassen
- Strategy Pattern: Verschiedene LLM-APIs als austauschbare Strategien
- Factory Pattern: Zentrale Provider-Erstellung und -Konfiguration
- Single Responsibility: Jede Klasse hat eine klar definierte Aufgabe
- Dependency Injection: Provider werden in LLMClient injiziert
- Extensibility: Neue APIs können leicht hinzugefügt werden
# Alle Tests
pytest
# Mit Coverage
pytest --cov=llm_client --cov-report=html
# Einzelne Test-Datei
pytest tests/test_llm_client.py -vSiehe docs/TESTING.md für Details.
Beiträge sind willkommen! Siehe CONTRIBUTING.md für Details.
- Fork & Clone
- Feature-Branch erstellen:
git checkout -b feature/mein-feature - Tests schreiben und ausführen
- Code formatieren:
black . && ruff check --fix . - Commit & Push
- Pull Request öffnen
MIT License - siehe LICENSE
© 2025 Daniel Gaida, Technische Hochschule Köln
- Ollama Dokumentation
- OpenAI API Reference
- Groq Cloud
- Google Gemini API
- Gemini OpenAI Compatibility
- llama-index Docs
- Andrew Ng's AISuite
Wenn Ihnen dieses Projekt gefällt, geben Sie ihm einen Stern auf GitHub!
Fragen? Öffnen Sie ein Issue.