Backup #44

ID44
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/AI/AIConfig.php
Version1
Typ modified
Größe5.9 KB
Hash2ad822b274fadd54e6305f19e4f6dd7a321752eda0e0580f23c2e5e1e0237d8d
Datum2025-12-20 17:24:52
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\AI;

use RuntimeException;

/**
 * Zentralisierte AI-Service-Konfiguration.
 *
 * Verwaltet alle Konfigurationswerte für AI-Services (Ollama, Qdrant, Claude)
 * und stellt Factory-Methoden zur Service-Erstellung bereit.
 *
 * Diese Klasse:
 * - Lädt Credentials sicher aus der credentials.md Datei
 * - Definiert Default-Werte für alle Service-URLs und Modelle
 * - Erstellt konfigurierte Service-Instanzen
 * - Verhindert doppeltes Laden von API-Keys
 *
 * @package Infrastructure\AI
 * @author  System Generated
 * @version 1.0.0
 */
final readonly class AIConfig
{
    /**
     * Konstruiert eine neue AIConfig-Instanz.
     *
     * @param string $ollamaHost       Ollama API Host-URL
     * @param string $qdrantHost       Qdrant API Host-URL
     * @param string $anthropicApiKey  Anthropic API Key
     * @param string $embeddingModel   Embedding-Modell für Ollama
     * @param string $claudeModel      Claude-Modell für Anthropic
     * @param string $defaultCollection Standard-Collection für Qdrant
     */
    public function __construct(
        public string $ollamaHost,
        public string $qdrantHost,
        public string $anthropicApiKey,
        public string $embeddingModel,
        public string $claudeModel,
        public string $defaultCollection
    ) {
    }

    /**
     * Erstellt AIConfig aus Credentials-Datei mit Default-Werten.
     *
     * Lädt den Anthropic API Key aus der credentials.md Datei und
     * verwendet Default-Werte für alle anderen Konfigurationsparameter.
     *
     * @param string $credentialsPath Pfad zur credentials.md Datei (default: /var/www/docs/credentials/credentials.md)
     *
     * @return self Konfigurierte AIConfig-Instanz
     *
     * @throws RuntimeException Wenn Credentials-Datei nicht existiert
     * @throws RuntimeException Wenn Credentials-Datei nicht gelesen werden kann
     * @throws RuntimeException Wenn Anthropic API Key nicht gefunden wird
     *
     * @example
     * $config = AIConfig::fromCredentialsFile();
     * $chatService = $config->createChatService();
     */
    public static function fromCredentialsFile(
        string $credentialsPath = '/var/www/docs/credentials/credentials.md'
    ): self {
        $anthropicApiKey = self::loadAnthropicApiKey($credentialsPath);

        return new self(
            ollamaHost: 'http://localhost:11434',
            qdrantHost: 'http://localhost:6333',
            anthropicApiKey: $anthropicApiKey,
            embeddingModel: 'mxbai-embed-large',
            claudeModel: 'claude-opus-4-5-20251101',
            defaultCollection: 'documents'
        );
    }

    /**
     * Erstellt einen konfigurierten ChatService.
     *
     * Erzeugt alle benötigten Dependencies (OllamaService, QdrantService, ClaudeService)
     * und liefert einen vollständig konfigurierten ChatService zurück.
     *
     * @return ChatService Konfigurierter ChatService
     *
     * @example
     * $config = AIConfig::fromCredentialsFile();
     * $chatService = $config->createChatService();
     * $result = $chatService->chat('Was ist systemisches Coaching?');
     */
    public function createChatService(): ChatService
    {
        return new ChatService(
            $this->createOllamaService(),
            $this->createQdrantService(),
            $this->createClaudeService()
        );
    }

    /**
     * Erstellt einen konfigurierten OllamaService.
     *
     * @return OllamaService Konfigurierter OllamaService
     *
     * @example
     * $config = AIConfig::fromCredentialsFile();
     * $ollama = $config->createOllamaService();
     * $embedding = $ollama->getEmbedding('Hello World');
     */
    public function createOllamaService(): OllamaService
    {
        return new OllamaService($this->ollamaHost);
    }

    /**
     * Erstellt einen konfigurierten QdrantService.
     *
     * @return QdrantService Konfigurierter QdrantService
     *
     * @example
     * $config = AIConfig::fromCredentialsFile();
     * $qdrant = $config->createQdrantService();
     * $results = $qdrant->search($vector, 'documents');
     */
    public function createQdrantService(): QdrantService
    {
        return new QdrantService($this->qdrantHost);
    }

    /**
     * Erstellt einen konfigurierten ClaudeService.
     *
     * @return ClaudeService Konfigurierter ClaudeService
     *
     * @example
     * $config = AIConfig::fromCredentialsFile();
     * $claude = $config->createClaudeService();
     * $result = $claude->ask('Explain quantum computing');
     */
    public function createClaudeService(): ClaudeService
    {
        return new ClaudeService($this->anthropicApiKey);
    }

    /**
     * Lädt den Anthropic API Key aus der Credentials-Datei.
     *
     * Sucht nach einer Zeile, die mit 'sk-ant-' beginnt und gibt diese zurück.
     * API Key wird niemals in Exception-Messages geloggt.
     *
     * @param string $credentialsPath Pfad zur credentials.md Datei
     *
     * @return string Der gefundene API Key
     *
     * @throws RuntimeException Wenn Credentials-Datei nicht existiert
     * @throws RuntimeException Wenn Credentials-Datei nicht gelesen werden kann
     * @throws RuntimeException Wenn API Key nicht gefunden wird
     */
    private static function loadAnthropicApiKey(string $credentialsPath): string
    {
        if (!file_exists($credentialsPath)) {
            throw new RuntimeException('Credentials file not found');
        }

        $content = file_get_contents($credentialsPath);

        if ($content === false) {
            throw new RuntimeException('Could not read credentials file');
        }

        foreach (explode("\n", $content) as $line) {
            $trimmedLine = trim($line);
            if (str_starts_with($trimmedLine, 'sk-ant-')) {
                return $trimmedLine;
            }
        }

        throw new RuntimeException('Anthropic API key not found in credentials file');
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1388 13 modified 4.8 KB 2025-12-25 16:57
1379 12 modified 4.9 KB 2025-12-25 16:56
1373 11 modified 4.8 KB 2025-12-25 16:56
1371 10 modified 4.9 KB 2025-12-25 16:56
1369 9 modified 4.8 KB 2025-12-25 16:56
1368 8 modified 4.9 KB 2025-12-25 16:55
1367 7 modified 4.9 KB 2025-12-25 16:55
1022 6 modified 4.9 KB 2025-12-24 16:22
759 5 modified 5.2 KB 2025-12-23 08:00
757 4 modified 5.3 KB 2025-12-23 08:00
370 3 modified 5.3 KB 2025-12-22 08:24
369 2 modified 5.2 KB 2025-12-22 08:24
44 1 modified 5.9 KB 2025-12-20 17:24

← Zurück zur Übersicht