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'],
};
}
}