Compare commits
2 Commits
98b5ab1f1c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
79f4156ffa
|
|||
|
7f4b559644
|
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "pyvtt"
|
name = "pyvtt"
|
||||||
version = "0.5.0"
|
version = "0.6.0"
|
||||||
description = "Python Voice to Text + LLMA"
|
description = "Python Voice to Text + LLMA"
|
||||||
authors = [{ name = "Max P.", email = "Mail@MPassarello.de" }]
|
authors = [{ name = "Max P.", email = "Mail@MPassarello.de" }]
|
||||||
license = { text = "MIT" }
|
license = { text = "MIT" }
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import requests
|
import requests
|
||||||
from typing import Union, List, Optional
|
from typing import Union, List, Optional
|
||||||
|
|
||||||
@@ -8,61 +9,95 @@ from pyvtt.models.config import AppConfig, PresetConfig
|
|||||||
class OllamaClient:
|
class OllamaClient:
|
||||||
def __init__(self, config: AppConfig):
|
def __init__(self, config: AppConfig):
|
||||||
"""
|
"""
|
||||||
Initialisiert den Ollama-Client mit der Basis-Konfiguration aus der globalen App-Konfiguration.
|
Initialisiert den API-Client (Ollama oder llama-swap) mit Basis-Konfiguration.
|
||||||
|
|
||||||
:param config: AppConfig-Instanz mit Host und Port für den Ollama-Server.
|
|
||||||
"""
|
"""
|
||||||
self.base_url = config.ollama_url
|
self.base_url = config.ollama_url.rstrip("/")
|
||||||
self.port = config.ollama_port
|
self.port = config.ollama_port
|
||||||
self.path = config.ollama_path
|
self.path = config.ollama_path or "/api/chat"
|
||||||
|
# Falls llama-swap (OpenAI-API-Kompatibel), verwende den OpenAI-Pfad
|
||||||
|
if "v1" in self.path or "completions" in self.path:
|
||||||
|
self.is_llama_swap = True
|
||||||
|
else:
|
||||||
|
self.is_llama_swap = False
|
||||||
|
|
||||||
def send_chat(
|
def send_chat(self, user_message: str, config: PresetConfig) -> str:
|
||||||
self,
|
|
||||||
user_message: str,
|
|
||||||
config: PresetConfig,
|
|
||||||
) -> str:
|
|
||||||
"""
|
"""
|
||||||
Sendet eine Chat-Anfrage an den Ollama-Server basierend auf der spezifischen Preset-Konfiguration.
|
Sendet eine Chat-Anfrage an Ollama oder llama-swap.
|
||||||
|
|
||||||
: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":
|
if config.ollama and config.ollama.lower() == "disable":
|
||||||
print("[OllamaClient] Ollama ist im Preset deaktiviert.")
|
print("[OllamaClient] Ollama ist im Preset deaktiviert.")
|
||||||
print("[OllamaClient] Gebe die Eingabe unverändert zurück.")
|
|
||||||
return user_message
|
return user_message
|
||||||
|
|
||||||
# Prompt als String aufbereiten – Liste wird zu Zeilen verbunden
|
# Prompt aufbereiten
|
||||||
if isinstance(config.ollama_prompt, list):
|
prompt_str = (
|
||||||
prompt_str = "\n".join(config.ollama_prompt)
|
"\n".join(config.ollama_prompt)
|
||||||
else:
|
if isinstance(config.ollama_prompt, list)
|
||||||
prompt_str = config.ollama_prompt
|
else str(config.ollama_prompt)
|
||||||
|
)
|
||||||
|
|
||||||
# Payload für die API-Anfrage vorbereiten
|
# === Payload vorbereiten ===
|
||||||
payload = {
|
if self.is_llama_swap:
|
||||||
"model": config.ollama_model,
|
# OpenAI-/llama-swap-kompatibles Format
|
||||||
"messages": [
|
payload = {
|
||||||
{"role": "system", "content": prompt_str},
|
"model": config.ollama_model,
|
||||||
{"role": "user", "content": user_message}
|
"messages": [
|
||||||
],
|
{"role": "system", "content": prompt_str},
|
||||||
"options": {
|
{"role": "user", "content": user_message},
|
||||||
"num_ctx": config.ollama_context,
|
],
|
||||||
} if config.ollama_context else {},
|
"stream": False,
|
||||||
"stream": False
|
}
|
||||||
}
|
# Kontextgröße optional hinzufügen
|
||||||
|
if config.ollama_context:
|
||||||
|
payload["num_ctx"] = config.ollama_context
|
||||||
|
else:
|
||||||
|
# Klassisches Ollama-Format
|
||||||
|
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}{self.path}"
|
endpoint = f"{self.base_url}:{self.port}{self.path}"
|
||||||
|
|
||||||
# Anfrage an Ollama senden und Antwort extrahieren
|
# === Anfrage senden ===
|
||||||
try:
|
try:
|
||||||
response = requests.post(endpoint, json=payload)
|
headers = {"Content-Type": "application/json"}
|
||||||
|
if self.is_llama_swap:
|
||||||
|
headers["Authorization"] = "Bearer no-key"
|
||||||
|
|
||||||
|
response = requests.post(endpoint, headers=headers, data=json.dumps(payload))
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
json_response = response.json()
|
json_response = response.json()
|
||||||
content = json_response.get("message", {}).get("content", "").strip()
|
|
||||||
|
# === Antwort extrahieren ===
|
||||||
|
if self.is_llama_swap:
|
||||||
|
# OpenAI-kompatible Struktur
|
||||||
|
content = (
|
||||||
|
json_response.get("choices", [{}])[0]
|
||||||
|
.get("message", {})
|
||||||
|
.get("content", "")
|
||||||
|
.strip()
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Ollama-eigene Struktur
|
||||||
|
content = (
|
||||||
|
json_response.get("message", {})
|
||||||
|
.get("content", "")
|
||||||
|
.strip()
|
||||||
|
)
|
||||||
|
|
||||||
return "\n".join(line.strip() for line in content.splitlines())
|
return "\n".join(line.strip() for line in content.splitlines())
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"[OllamaClient] HTTP-Fehler: {e}")
|
print(f"[OllamaClient] HTTP-Fehler: {e}")
|
||||||
notify("Fehler", "Ein Fehler bei der Kommunikation mit 'Ollama' ist aufgetreten!")
|
notify("Fehler", "Kommunikationsfehler mit Ollama / llama-swap!")
|
||||||
return ""
|
return ""
|
||||||
Reference in New Issue
Block a user