Ngiler SH3LL 360
Home
Information
Create File
Create Folder
:
/
home
/
tbf
/
tbfguestbe.tbf.ro
/
app
/
Managers
/
OpenAI
/
Information Server
MySQL :
OFF
Perl :
OFF
CURL :
ON
WGET :
OFF
PKEXEC :
OFF
Directive
Local Value
IP Address
89.40.16.97
System
Linux server.atelieruldeit.ro 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
User
tbf
PHP Version
7.3.33
Software
Apache
Doc root
Writable
close
Edit File :
Completions.php
| Size :
4.83
KB
Copy
<?php namespace App\Managers\OpenAI; use App\Models\AiLog; use App\Models\Project; use App\Models\User; use GuzzleHttp\Client; use Illuminate\Support\Facades\Auth; class Completions extends RequestUri { /** * The user input. */ private ?string $userInput; /** * The context for the chat. */ private ?int $contextId; /** * The ai model for the chat. */ private ?string $aiModel; /** * The temperature for the chat. */ private ?float $aiTemperature; /** * The max token for the chat. */ private ?int $aiMaxToken; /** * The top P for the chat. */ private ?int $topP; /** * The Frequency penalty for the chat. */ private ?float $frequencyPenalty; /** * The Presence penalty for the chat. */ private ?float $presencePenalty; /** * The Client request for the chat. */ private ?Client $client; // /** // * The project id who is asking for this call // */ // private ?int $projectId; /** * The is_test attribute. */ private ?bool $isTest; /** * Creates a Client instance with the given API token. * * @param string $userInput, * @param array $options, * @param string $model, */ public function __construct(string $userInput, array $options) { $this->userInput = $userInput; // $this->projectId = $project ? $project->id : null; $this->contextId = null; $this->isTest = $options && isset($options['is_test']) ? $options['is_test'] : false; $this->aiModel = $options && !is_null($options['ai_model']) ? $options['ai_model'] : ''; $this->aiTemperature = $options && !is_null($options['ai_temperature']) ? $options['ai_temperature'] : 0.7; $this->aiMaxToken = $options && !is_null($options['max_tokens']) ? $options['max_tokens'] : 3000; $this->topP = $options && !is_null($options['top_p']) ? $options['top_p'] : 1; $this->frequencyPenalty = $options && !is_null($options['frequency_penalty']) ? $options['frequency_penalty'] : 0; $this->presencePenalty = $options && !is_null($options['presence_penalty']) ? $options['presence_penalty'] : 1; $this->client = self::setupRequest(); $this->userInput .= $options['return_content_type']; } /** * Given a prompt, the model will return one or more predicted completions, and can also return the probabilities * of alternative tokens at each position. * * @see https://beta.openai.com/docs/api-reference/completions * */ public function create() { $response = $this->client->post('completions', [ 'json' => [ 'model' => $this->aiModel, 'temperature' => $this->aiTemperature, 'max_tokens' => $this->aiMaxToken, "prompt" => $this->userInput, "top_p" => $this->topP, "frequency_penalty" => $this->frequencyPenalty, "presence_penalty" => $this->presencePenalty, ], ]); $body = $response->getBody(); $content = json_decode($body, true); if ($content['id']) { $aiLog = self::store($content); } $completed_text = removeSpecialCharacters(trim($content['choices'][0]['text'])); return [ 'content' => $completed_text, 'context_id' => '', 'ai_log_id' => isset($aiLog) ? $aiLog->id : null, ]; } /** * Store response to database AiLog table * * @param mixed $content */ private function store(mixed $content) { // $authUser = Auth::user() ?? User::find(1); $authUser = null; if ($authUser) { $instanceId = $authUser->instance->id; } $softModelType = AiLog::SOFT_MODEL_TYPE['procedure']; $aiLog = AiLog::create([ 'instance_id' => $instanceId ?? null, 'user_id' => $authUser ? $authUser->id : null, // 'project_id' => $this->projectId, 'app_model_type' => $softModelType, 'context_id' => null, 'is_test' => $this->isTest, 'temperature' => $this->aiTemperature, 'max_tokens' => $this->aiMaxToken, 'object' => $content['object'], 'ai_model' => $content['model'], 'prompt_tokens' => $content['usage']['prompt_tokens'], 'completion_tokens' => $content['usage']['completion_tokens'], 'total_tokens' => $content['usage']['total_tokens'], 'user_input' => $this->userInput, 'assistant_message' => $content['choices'][0]['text'], 'finish_reason' => $content['choices'][0]['finish_reason'], 'response_index' => $content['choices'][0]['index'], ]); return $aiLog; } }
Back