HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/laravel/app/Models/Aip.php
<?php

namespace App\Models;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\MultipartStream;
use Illuminate\Database\Eloquent\Model;
use JsonSchema\Validator;

/**
 * @property int $id
 * @property string $title
 * @property string $input
 * @property string $response
 * @property bool $is_valid
 * @property string|null $correct_response
 * @property int|null $user_id
 * @property int|null $prompt_id
 * @property int $fine_tuning
 * @property int|null $match_percent
 */
class Aip extends Model
{

    protected $table = 'aips';

    protected $fillable = [
        'title',
        'type',
        'file_path',
        'file_name',
        'input',
        'response',
        'correct_response',
        'is_valid',
        'user_id',
        'prompt_id',
        'model',
        'fine_tuning',
        'match_percent',
        'file_path',
        'file_name',
        'prompt_tokens',
        'completion_tokens',
        'total_tokens',
        'comment',

    ];

    public static function getCounters()
    {
        return [];
    }

    public function validate()
    {
        return true;
    }

    public function process()
    {
        $prompt = Prompt::query()->where('type', 'aip')->where('is_active', 1)->latest()->first();

        if ($prompt) {
            $promptText = $prompt->prompt;

            $model = $prompt->model;
            if ($this->type == 'local') {
                $model = 'local';
            }

            $json = $prompt->json;

            if ($model !== 'local') {
                $promptText .= "\n" . $json;
            }

            $this->model = $model;
            $this->saveQuietly();

            $response = callGptProxi([
                'object_id' => $this->id,
                'input' => $this->input,
                'key' => env('OPEN_AI_KEY'),
                'prompt' => $promptText,
                'examples' => [$prompt->json], // For local model
                'model' => $model,
                'json' => $json,
                'temperature' => (float) $prompt->temperature,
            ], 'aipNew', 'aip');

            print_r($response);

            // Gpt Model result
            if (property_exists($response, 'choices')) {
                $usage = (array) $response->usage;
                $message = $response->choices[0]->message->content ?? '';
            } else {
                $message = json_encode($response);
            }

            $message = str_replace(["```json\n", "\n```"], '', $message);

            $this->response = $message;

            if (property_exists($response, 'usage')) {
                $this->prompt_tokens = $response->usage->prompt_tokens;
                $this->completion_tokens = $response->usage->completion_tokens;
                $this->total_tokens = $response->usage->total_tokens;
            }


            $this->updated_at = date('Y-m-d H:i:s');
            $this->saveQuietly(); // not using save() here to skip validate() call


        } else {
            return [
                'error' => true,
                'message' => 'No active prompt found for AIP processing.',
            ];
        }
    }

    public function getText()
    {
        return $this->input;
    }

    public function minifyJson($jsonString)
    {
        $data = json_decode($jsonString, true);

        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    public function convertToSchema(mixed $data): array
    {
        if (is_object($data) || is_array($data) && array_keys($data) !== range(0, count($data) - 1)) {
            // Object-like (associative array or object)
            $props = [];

            foreach ((array) $data as $key => $value) {
                $props[$key] = $this->convertToSchema($value);
            }

            return [
                'type' => 'object',
                'properties' => $props,
                // no "required" => all fields optional
            ];
        }

        if (is_array($data)) {
            // Numerically indexed array: determine item schema
            $itemSchemas = [];

            foreach ($data as $item) {
                $itemSchemas[] = $this->convertToSchema($item);
            }

            // If all items are the same type/schema, collapse into one
            if (count($itemSchemas) > 0) {
                $first = json_encode($itemSchemas[0]);
                $allSame = array_reduce($itemSchemas, function($carry, $schema) use ($first) {
                    return $carry && json_encode($schema) === $first;
                }, true);

                return [
                    'type' => 'array',
                    'items' => $allSame ? $itemSchemas[0] : ['anyOf' => $itemSchemas],
                ];
            }

            return ['type' => 'array', 'items' => []];
        }

        // Primitive types
        return match (gettype($data)) {
            'string' => ['type' => 'string'],
            'integer' => ['type' => 'integer'],
            'double'  => ['type' => 'number'],
            'boolean' => ['type' => 'boolean'],
            default   => ['type' => 'null'],
        };
    }

}