feat(libs): integrate Ollama and Whisper clients with config models

- Add `AppConfig` and `PresetConfig` models using Pydantic for config validation
- Refactor `read_configurations` to return an `AppConfig` instance
- Implement `OllamaClient` for chat-based server interaction
- Implement `WhisperClient` for transcription via Whisper CLI
- Migrate notification utilities to `libs` directory
- Update tray application to use new clients and config structure
- Simplify Whisper and Ollama integration logic in `WhisperWorker`

Signed-off-by: Max P. <Mail@MPassarello.de>
This commit is contained in:
2025-05-05 12:00:33 +02:00
parent 5688769437
commit 58b9cb586c
6 changed files with 182 additions and 88 deletions

67
src/pyvtt/libs/ollama.py Normal file
View File

@@ -0,0 +1,67 @@
import requests
from typing import Union, List, Optional
from pyvtt.libs.notify import notify
from pyvtt.models.config import AppConfig, PresetConfig
class OllamaClient:
def __init__(self, config: AppConfig):
"""
Initialisiert den Ollama-Client mit der Basis-Konfiguration aus der globalen App-Konfiguration.
:param config: AppConfig-Instanz mit Host und Port für den Ollama-Server.
"""
self.base_url = config.ollama_url
self.port = config.ollama_port
def send_chat(
self,
user_message: str,
config: PresetConfig,
) -> str:
"""
Sendet eine Chat-Anfrage an den Ollama-Server basierend auf der spezifischen Preset-Konfiguration.
:param user_message: Der vom Nutzer erzeugte Eingabetext (z. B. Transkript).
:param config: PresetConfig-Instanz mit modell-, prompt- und kontextbezogenen Parametern.
:return: Der von Ollama zurückgegebene, formatierte Antworttext, die user_message
unverändert zurückgibt, wenn Ollama deaktiviert ist oder none bei einem Fehler.
"""
if config.ollama and config.ollama.lower() == "disable":
print("[OllamaClient] Ollama ist im Preset deaktiviert.")
print("[OllamaClient] Gebe die Eingabe unverändert zurück.")
return user_message
# Prompt als String aufbereiten – Liste wird zu Zeilen verbunden
if isinstance(config.ollama_prompt, list):
prompt_str = "\n".join(config.ollama_prompt)
else:
prompt_str = config.ollama_prompt
# Payload für die API-Anfrage vorbereiten
payload = {
"model": config.ollama_model,
"messages": [
{"role": "system", "content": prompt_str},
{"role": "user", "content": user_message}
],
"options": {
"num_ctx": config.ollama_context,
} if config.ollama_context else {},
"stream": False
}
endpoint = f"{self.base_url}:{self.port}/api/chat"
# Anfrage an Ollama senden und Antwort extrahieren
try:
response = requests.post(endpoint, json=payload)
response.raise_for_status()
json_response = response.json()
content = json_response.get("message", {}).get("content", "").strip()
return "\n".join(line.strip() for line in content.splitlines())
except requests.exceptions.RequestException as e:
print(f"[OllamaClient] HTTP-Fehler: {e}")
notify("Fehler", "Ein Fehler bei der Kommunikation mit 'Ollama' ist aufgetreten!")
return ""