Skip to content

Commit

Permalink
Merge pull request #12 from hm-edu/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
PandorasActorMS authored Jul 18, 2024
2 parents c4b949d + 2b97ede commit 0fe1f2e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
18 changes: 11 additions & 7 deletions Dockerfile.php
Original file line number Diff line number Diff line change
@@ -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
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
13 changes: 10 additions & 3 deletions private/app/php/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions private/app/php/stream-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
}
}

}
}
2 changes: 1 addition & 1 deletion private/pages/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function addMessage(message){
if(message.role == "assistant"){
messageElement.querySelector(".message-icon").textContent = "AI";
}else{
messageElement.querySelector(".message-icon").textContent = '<?= htmlspecialchars($_SESSION['initials']) ?>';
messageElement.querySelector(".message-icon").textContent = '<?= htmlspecialchars(isset($_SESSION['initials']) ? $_SESSION['initials'] : $_SESSION['username'] ) ?>';
messageElement.querySelector(".message").classList.add("me");
}

Expand Down

0 comments on commit 0fe1f2e

Please sign in to comment.