From 8e71f4747b1e9396d7a1cd500616cd913e82325b Mon Sep 17 00:00:00 2001 From: Mehmet S Date: Mon, 3 Jun 2024 13:46:52 +0000 Subject: [PATCH 1/5] chore: check if variable is set --- private/pages/interface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"); } From b7e84421f1dc2c3841dc3dba6fe0d7d10d7c108b Mon Sep 17 00:00:00 2001 From: Mehmet S Date: Thu, 13 Jun 2024 12:36:12 +0000 Subject: [PATCH 2/5] chore: install dependencies for sql --- Dockerfile.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Dockerfile.php b/Dockerfile.php index 99bc16d..e3406de 100644 --- a/Dockerfile.php +++ b/Dockerfile.php @@ -9,8 +9,13 @@ && composer install --no-cache \ && rm composer_install.sh - - FROM php:8.2-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 +ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/download/2.2.16/install-php-extensions /usr/local/bin/ +RUN install-php-extensions pdo pdo_pgsql + + +#RUN apk update && apk add --no-cahche \ +# libpq-dev \ +# && docker-php-ext-install pdo pdo_pgsql \ No newline at end of file From 95aab2a47e8d23c804f85a838bd7775e469360a1 Mon Sep 17 00:00:00 2001 From: Mehmet S Date: Thu, 13 Jun 2024 12:56:51 +0000 Subject: [PATCH 3/5] add: function to add token usage to databse --- private/app/php/stream-api.php | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) 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 From 0ba215464d38f60260c7a3f640a673b477c391e0 Mon Sep 17 00:00:00 2001 From: Mehmet S Date: Thu, 18 Jul 2024 10:43:55 +0000 Subject: [PATCH 4/5] chore: update image, stage2 copy dependencies --- Dockerfile.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Dockerfile.php b/Dockerfile.php index e3406de..c8d05fb 100644 --- a/Dockerfile.php +++ b/Dockerfile.php @@ -1,21 +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 -ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/download/2.2.16/install-php-extensions /usr/local/bin/ -RUN install-php-extensions pdo pdo_pgsql - - -#RUN apk update && apk add --no-cahche \ -# libpq-dev \ -# && docker-php-ext-install pdo pdo_pgsql \ No newline at end of file +# 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 From 2b97eded508fcdaebe166f896c59af3faaba82fc Mon Sep 17 00:00:00 2001 From: Mehmet S Date: Thu, 18 Jul 2024 10:47:35 +0000 Subject: [PATCH 5/5] chore: jump over ldap filter if test environment variable is set --- private/app/php/auth.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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);