Protokoll #3708

ID3708
Zeitstempel2025-12-22 08:25:22.161573
Clientroot
IP
Warning: Undefined array key "request_ip" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 13

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 13
Modellclaude-sonnet-4-20250514
Statuscompleted
Tokens2,171 (Input: 0, Output: 0)
Dauer101 ms
Request-Zeit
Warning: Undefined array key "request_timestamp" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 18
Response-Zeit-

Warning: Undefined array key "error_message" in /var/www/dev.campus.systemische-tools.de/src/View/protokoll/show.php on line 22

Request

{
    "event": "PreToolUse",
    "tool_name": "Edit",
    "tool_input": {
        "file_path": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/AI\/OllamaService.php",
        "old_string": "namespace Infrastructure\\AI;\n\nuse RuntimeException;",
        "new_string": "namespace Infrastructure\\AI;\n\nuse Infrastructure\\Config\\CredentialService;\nuse RuntimeException;"
    }
}

Response

{
    "tool_response": {
        "filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/AI\/OllamaService.php",
        "oldString": "namespace Infrastructure\\AI;\n\nuse RuntimeException;",
        "newString": "namespace Infrastructure\\AI;\n\nuse Infrastructure\\Config\\CredentialService;\nuse RuntimeException;",
        "originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\AI;\n\nuse RuntimeException;\n\n\/**\n * Service for interacting with Ollama API for embeddings and text generation.\n *\n * Provides methods to:\n * - Generate embeddings from text using models like mxbai-embed-large\n * - Generate text responses using LLMs like gemma3:4b-it-qat\n * - Check API availability and health status\n *\n * This service uses cURL for HTTP requests without external dependencies.\n * All methods include proper timeout handling and exception management.\n *\n * @package Infrastructure\\AI\n * @author  System Generated\n * @version 1.0.0\n *\/\nfinal readonly class OllamaService\n{\n    \/**\n     * Default timeout for HTTP requests in seconds.\n     *\/\n    private const int DEFAULT_TIMEOUT = 60;\n\n    \/**\n     * Health check timeout in seconds.\n     *\/\n    private const int HEALTH_CHECK_TIMEOUT = 5;\n\n    \/**\n     * Constructs a new OllamaService instance.\n     *\n     * @param string $host The Ollama API host URL (default: http:\/\/localhost:11434)\n     *\/\n    public function __construct(\n        private string $host = 'http:\/\/localhost:11434'\n    ) {\n    }\n\n    \/**\n     * Generates an embedding vector for the given text.\n     *\n     * Uses the specified model to convert text into a numerical vector representation.\n     * The default model mxbai-embed-large produces 1024-dimensional embeddings.\n     *\n     * @param string $text  The text to embed\n     * @param string $model The embedding model to use (default: mxbai-embed-large)\n     *\n     * @return array<int, float> The embedding vector as an array of floats\n     *\n     * @throws RuntimeException If the API request fails or returns invalid data\n     *\n     * @example\n     * $service = new OllamaService();\n     * $embedding = $service->getEmbedding('Hello world');\n     * \/\/ Returns: [0.123, -0.456, 0.789, ...] (1024 dimensions)\n     *\/\n    public function getEmbedding(string $text, string $model = 'mxbai-embed-large'): array\n    {\n        $url = $this->host . '\/api\/embeddings';\n\n        $payload = [\n            'model' => $model,\n            'prompt' => $text,\n        ];\n\n        $response = $this->makeRequest($url, $payload, self::DEFAULT_TIMEOUT);\n\n        if (!isset($response['embedding']) || !is_array($response['embedding'])) {\n            throw new RuntimeException('Invalid embedding response from Ollama API');\n        }\n\n        return array_map(\n            static fn (mixed $value): float => (float) $value,\n            $response['embedding']\n        );\n    }\n\n    \/**\n     * Generates text using the specified LLM model.\n     *\n     * Sends a prompt to the Ollama API and receives generated text in response.\n     * This method does not stream responses - it waits for the complete response.\n     *\n     * @param string $prompt      The prompt to generate text from\n     * @param string $model       The LLM model to use (default: gemma3:4b-it-qat)\n     * @param float  $temperature Sampling temperature 0.0-1.0 (default: 0.7)\n     *\n     * @return string The generated text response\n     *\n     * @throws RuntimeException If the API request fails or returns invalid data\n     *\n     * @example\n     * $service = new OllamaService();\n     * $response = $service->generate('Explain quantum computing in simple terms.', 'gemma3:4b-it-qat', 0.7);\n     * \/\/ Returns: \"Quantum computing is a type of computing that...\"\n     *\/\n    public function generate(string $prompt, string $model = 'gemma3:4b-it-qat', float $temperature = 0.7): string\n    {\n        $url = $this->host . '\/api\/generate';\n\n        $payload = [\n            'model' => $model,\n            'prompt' => $prompt,\n            'stream' => false,\n            'options' => [\n                'temperature' => max(0.0, min(1.0, $temperature)),\n            ],\n        ];\n\n        $response = $this->makeRequest($url, $payload, 120);\n\n        if (!isset($response['response'])) {\n            throw new RuntimeException('Invalid generate response from Ollama API');\n        }\n\n        return (string) $response['response'];\n    }\n\n    \/**\n     * Checks if the Ollama API is available and responding.\n     *\n     * Attempts to fetch the list of available models as a health check.\n     * This is a quick operation with a short timeout.\n     *\n     * @return bool True if the API is available, false otherwise\n     *\n     * @example\n     * $service = new OllamaService();\n     * if ($service->isAvailable()) {\n     *     echo \"Ollama is running\";\n     * } else {\n     *     echo \"Ollama is not available\";\n     * }\n     *\/\n    public function isAvailable(): bool\n    {\n        $url = $this->host . '\/api\/tags';\n\n        try {\n            $ch = curl_init($url);\n\n            if ($ch === false) {\n                return false;\n            }\n\n            curl_setopt_array($ch, [\n                CURLOPT_RETURNTRANSFER => true,\n                CURLOPT_TIMEOUT => self::HEALTH_CHECK_TIMEOUT,\n                CURLOPT_CONNECTTIMEOUT => self::HEALTH_CHECK_TIMEOUT,\n                CURLOPT_HTTPHEADER => ['Content-Type: application\/json'],\n            ]);\n\n            $result = curl_exec($ch);\n            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n            curl_close($ch);\n\n            return $result !== false && $httpCode === 200;\n        } catch (\\Throwable) {\n            return false;\n        }\n    }\n\n    \/**\n     * Makes an HTTP POST request to the Ollama API.\n     *\n     * Internal helper method that handles cURL initialization, request execution,\n     * error handling, and response parsing.\n     *\n     * @param string               $url     The API endpoint URL\n     * @param array<string, mixed> $payload The JSON payload to send\n     * @param int                  $timeout The request timeout in seconds\n     *\n     * @return array<string, mixed> The decoded JSON response\n     *\n     * @throws RuntimeException If the request fails or returns an error\n     *\/\n    private function makeRequest(string $url, array $payload, int $timeout): array\n    {\n        $ch = curl_init($url);\n\n        if ($ch === false) {\n            throw new RuntimeException('Failed to initialize cURL');\n        }\n\n        $jsonPayload = json_encode($payload);\n\n        if ($jsonPayload === false) {\n            curl_close($ch);\n\n            throw new RuntimeException('Failed to encode JSON payload');\n        }\n\n        curl_setopt_array($ch, [\n            CURLOPT_RETURNTRANSFER => true,\n            CURLOPT_POST => true,\n            CURLOPT_POSTFIELDS => $jsonPayload,\n            CURLOPT_TIMEOUT => $timeout,\n            CURLOPT_CONNECTTIMEOUT => 10,\n            CURLOPT_HTTPHEADER => [\n                'Content-Type: application\/json',\n                'Content-Length: ' . strlen($jsonPayload),\n            ],\n        ]);\n\n        $result = curl_exec($ch);\n        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        $curlError = curl_error($ch);\n\n        curl_close($ch);\n\n        if ($result === false) {\n            throw new RuntimeException(\n                sprintf('cURL request failed: %s', $curlError !== '' ? $curlError : 'Unknown error')\n            );\n        }\n\n        if ($httpCode !== 200) {\n            throw new RuntimeException(\n                sprintf('Ollama API returned HTTP %d: %s', $httpCode, $result)\n            );\n        }\n\n        $decoded = json_decode((string) $result, true);\n\n        if (!is_array($decoded)) {\n            throw new RuntimeException('Failed to decode JSON response from Ollama API');\n        }\n\n        return $decoded;\n    }\n}\n",
        "structuredPatch": [
            {
                "oldStart": 4,
                "oldLines": 6,
                "newStart": 4,
                "newLines": 7,
                "lines": [
                    " ",
                    " namespace Infrastructure\\AI;",
                    " ",
                    "+use Infrastructure\\Config\\CredentialService;",
                    " use RuntimeException;",
                    " ",
                    " \/**"
                ]
            }
        ],
        "userModified": false,
        "replaceAll": false
    }
}
← Vorheriger Zur Liste Nächster →