diff --git a/Dockerfile.php b/Dockerfile.php index 99bc16d..c8d05fb 100644 --- a/Dockerfile.php +++ b/Dockerfile.php @@ -1,16 +1,20 @@ -FROM php:8.2-fpm-alpine AS BUILD +FROM php:8.3-fpm-alpine AS BUILD COPY . /var/www/html/ -#COPY composer_install.sh composer.json /var/www/html/ +ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/download/2.2.16/install-php-extensions /usr/local/bin/ RUN apk add --no-cache git libzip-dev zip \ && docker-php-ext-install zip \ && cd /var/www/html \ && chmod +x composer_install.sh && ./composer_install.sh \ && mv composer.phar /usr/local/bin/composer \ && composer install --no-cache \ - && rm composer_install.sh + && install-php-extensions pdo pdo_pgsql - - -FROM php:8.2-fpm-alpine +FROM php:8.3-fpm-alpine WORKDIR /var/www/html -COPY --from=BUILD /var/www/html /var/www/html \ No newline at end of file +COPY --from=BUILD /var/www/html /var/www/html +# pdo pdo_pgsql dependencies +COPY --from=BUILD /usr/lib/libpgtypes.so.* /usr/lib/ +COPY --from=BUILD /usr/lib/libpq.so.* /usr/lib/ +COPY --from=BUILD /usr/lib/libecpg.so.* /usr/lib +COPY --from=BUILD /usr/local/etc/php/conf.d/docker-php-ext-pdo_pgsql.ini /usr/local/etc/php/conf.d/ +COPY --from=BUILD /usr/local/lib/php/extensions /usr/local/lib/php/extensions diff --git a/private/app/php/auth.php b/private/app/php/auth.php index 71de3cf..bd6ec06 100644 --- a/private/app/php/auth.php +++ b/private/app/php/auth.php @@ -58,8 +58,9 @@ function handleLdapLogin($username, $password){ // *** ACTIVATES TEST ACCESS *** // Please set a unique test username and password in .env - if(!empty(getenv('TESTUSER')) && !empty(getenv('TESTPASSWORD')) && - $username == getenv('TESTUSER') && $password == getenv('TESTPASSWORD')) { + if(((isset($env) ? strtolower($env["TEST"]) : strtolower(getenv("TEST"))) === "true") && + !empty(getenv('TESTUSER')) && !empty(getenv('TESTPASSWORD')) && + $username == getenv('TESTUSER') && $password == getenv('TESTPASSWORD')) { $_SESSION['username'] = getenv('TESTUSER'); $_SESSION['employeetype'] = "Tester"; return true; @@ -134,7 +135,13 @@ function handleLdapLogin($username, $password){ // filter username to prevent unwanted inputs. $username = filter_var($_POST["account"], FILTER_UNSAFE_RAW); - // $username = ldap_escape($username, "", LDAP_ESCAPE_FILTER); + if (!((isset($env) ? strtolower($env["TEST"]) : strtolower(getenv("TEST"))) === "true")) { + $username = ldap_escape($username, "", LDAP_ESCAPE_FILTER); + } + + + + // Use hashed password if LDAP Server is configured accordingly. // $password = password_hash($_POST["password"], PASSWORD_DEFAULT); diff --git a/private/app/php/stream-api.php b/private/app/php/stream-api.php index f417d3f..4cbf218 100644 --- a/private/app/php/stream-api.php +++ b/private/app/php/stream-api.php @@ -36,6 +36,14 @@ // Read the request payload from the client $requestPayload = file_get_contents('php://input'); +//set include_usage:true if not set in json +$usage_pattern = '/"include_usage":true/'; +if(!preg_match($usage_pattern, $requestPayload)){ + $requestPayload = json_decode($requestPayload, true); + $requestPayload['stream_options']['include_usage'] = true; + $requestPayload = json_encode($requestPayload); +} + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_POST, 1); @@ -48,6 +56,7 @@ 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) { + get_tokens($data); echo $data; if (ob_get_level() > 0) { ob_flush(); @@ -64,3 +73,48 @@ curl_close($ch); + +function get_tokens($data){ + $pattern = '/"usage":\{"prompt_tokens":[0-9]+,"completion_tokens":[0-9]+,"total_tokens":[0-9]+\}/'; + if (preg_match_all($pattern, $data, $matches)) { + $last_match = end($matches[0]); + $numberpattern = '/[0-9]+/'; + if (preg_match_all($numberpattern, $last_match, $numbers)){ + + $prompt_tokens = $numbers[0][0]; + $completion_tokens = $numbers[0][1]; + $total_tokens = $numbers[0][2]; + + $host = getenv("DB_HOST"); + $db = getenv("DB_DB"); + $table = getenv('DB_TABLE'); + $user = getenv("DB_USER"); + $pass = getenv("DB_PASS"); + $port = getenv("DB_PORT"); + $dsn = "pgsql:host=$host;port=$port;dbname=$db"; + try{ + $pdo = new PDO($dsn, $user, $pass); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $sql = "INSERT INTO $table (username,datum,prompt_tokens,completion_tokens,total_tokens) VALUES(:username,:datum,:prompt,:completion,:total) ON CONFLICT (username,datum) DO UPDATE SET prompt_tokens = $table.prompt_tokens + EXCLUDED.prompt_tokens, completion_tokens = $table.completion_tokens + EXCLUDED.completion_tokens, total_tokens = $table.total_tokens + EXCLUDED.total_tokens;"; + $stmt=$pdo->prepare($sql); + + $stmt->bindParam(':username',$username); + $stmt->bindParam(':datum',$datum); + $stmt->bindParam(':prompt',$prompt); + $stmt->bindParam(':completion',$completion); + $stmt->bindParam(':total',$total); + + $username = $_SESSION['username']; + $datum = date("Y-m-d"); + $prompt = $prompt_tokens; + $completion = $completion_tokens; + $total = $total_tokens; + $stmt->execute(); + + } catch (PDOException $e) { + error_log($e->getMessage(),0); + } + } + + } +} \ No newline at end of file diff --git a/private/pages/interface.php b/private/pages/interface.php index fcfe4e5..f6c96ed 100644 --- a/private/pages/interface.php +++ b/private/pages/interface.php @@ -519,7 +519,7 @@ function addMessage(message){ if(message.role == "assistant"){ messageElement.querySelector(".message-icon").textContent = "AI"; }else{ - messageElement.querySelector(".message-icon").textContent = ''; + messageElement.querySelector(".message-icon").textContent = ''; messageElement.querySelector(".message").classList.add("me"); }