amic il y a 2 mois
Parent
commit
e8666bc555

+ 51 - 0
models/NvidiaAI.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace app\models;
+
+use yii;
+use yii\helpers\Url;
+
+class NvidiaAI extends \app\models\base\BaseAI
+{
+	public $host = 'https://integrate.api.nvidia.com/';
+	public $model = 'google/gemma-2-27b-it';
+	public static $urlgen = 'v1/chat/completions';
+	public $res = '';
+	public $key = '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';
+
+	function  __construct(){
+		$header = array(
+				'Content-Type: application/json',
+				'Authorization: Bearer '.$this->key
+		);
+		$this->SetHeader($header);
+	}
+
+	public function generate( $promt, $context = '' ){
+		$url = $this->host.self::$urlgen;
+
+		$cmd = array(
+			"model" => $this->model,
+			"messages" =>array(array("role"=>"user", "content"=>$promt)),
+			"stream" => false,
+			"temperature" => 0.2,
+			"top_p" => 0.7,
+			"max_tokens" => 1024,
+		);
+		if( $context ){
+//			$cmd['context'] = $context;
+		}
+//		echo json_encode($cmd); exit;
+		$r = $this->Send( $url, $cmd );
+		$this->res = $r;
+		return $r;
+	}
+
+	public function Getres(){
+		$r = $this->res;
+		$robj = json_decode($r);
+		return nl2br($robj->choices[0]->message->content);
+	}
+
+
+}

+ 1 - 1
models/OllamaAI.php

@@ -36,7 +36,7 @@ class OllamaAI extends \app\models\base\BaseAI
 	public function GetTime(){
 		$r = $this->res;
 		$robj = json_decode($r);
-		return $robj->eval_duration;
+		return $robj->eval_duration/1000000000;
 	}
 
 	public function Getres(){

+ 10 - 2
models/base/BaseAI.php

@@ -9,15 +9,16 @@ use Yii;
 
 class BaseAI extends Component
 {
-	private $header = array();
+	public $header = array();
 	public   $config = array();
+	public   $time = array();
 
 	function __construct()
 	{
 		$this->config = [];
 	}
 
-	function SetHeader( $header = null ){
+	public function SetHeader( $header = null ){
 		if( !is_array( $header ) ){
 			$header = array(
 				'header'=>"Accept-language: ru\r\n" .
@@ -41,10 +42,17 @@ class BaseAI extends Component
 		curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $this->header);
 		curl_setopt($curl_handle, CURLOPT_POST, 1);
 		curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $json);
+		curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
+		curl_setopt($curl_handle, CURLINFO_HEADER_OUT, true);
 		$query = curl_exec($curl_handle);
 //		echo curl_error($curl_handle);
 //		print_a(curl_getinfo($curl_handle));
+		$info = curl_getinfo($curl_handle);
+		$this->time = $info['total_time'];
 		curl_close($curl_handle);
 		return $query;
 	}
+	function GetTime(){
+		return $this->time;
+	}
 }

+ 38 - 2
modules/manager/controllers/AiController.php

@@ -2,6 +2,7 @@
 namespace manager\controllers;
 
 use app\models\OllamaAI;
+use app\models\NvidiaAI;
 use Yii;
 
 class AiController extends BaseController
@@ -18,6 +19,7 @@ class AiController extends BaseController
 			$type = Yii::$app->request->post('AI');
 			$text = Yii::$app->request->post('text');
 			$progress = Yii::$app->request->post('progress');
+			$key = 'AI_time_mid'.$type.$progress;
 
 			if( $text && $type == 'ollama' ){
 				$ses = false;
@@ -29,13 +31,36 @@ class AiController extends BaseController
 				$r = $ollama->generate(strip_tags($text));
 				$data = $ollama->Getres();
 				$t = $ollama->GetTime();
-				$key = 'AI_time_mid';
 				$tm = $cache->get($key);
 				if( $tm ){
 					$cache->set($key, ($t+$tm)/2);
 				}else{
 					$cache->set($key, $t);
 				}
+				if( $ses ) \Yii::$app->session->open();
+				return json_encode( ['status'=>'ok', 'data'=>$data, 'progress'=>$progress] );
+			}elseif( $text && $type == 'nv' ){
+				$ses = false;
+				if( \Yii::$app->session->isActive ){
+					\Yii::$app->session->close();
+					$ses = true;
+				}
+				$nv = new NvidiaAI();
+				$r = $nv->generate(strip_tags($text));
+				if( $r ){
+					$data = $nv->Getres();
+				}else{
+					if( $ses ) \Yii::$app->session->open();
+					return json_encode( ['status'=>'err', 'msg'=>'Нет доступа до AI '.$type] );
+				}
+				$t = $nv->GetTime();
+				$tm = $cache->get($key);
+				if( $tm ){
+					$cache->set($key, ($t+$tm)/2);
+				}else{
+					$cache->set($key, $t);
+				}
+
 				if( $ses ) \Yii::$app->session->open();
 				return json_encode( ['status'=>'ok', 'data'=>$data, 'progress'=>$progress] );
 			}else{
@@ -45,5 +70,16 @@ class AiController extends BaseController
 		return json_encode( ['status'=>'err', 'msg'=>'Ошибка получения данных'] );
 	}
 
-
+    public function actionAjaxTime()
+    {
+		$tm = 0;
+		if (Yii::$app->request->isPost){
+			$cache = Yii::$app->cache;
+			$type = Yii::$app->request->post('AI');
+			$progress = Yii::$app->request->post('progress');
+			$key = 'AI_time_mid'.$type.$progress;
+			$tm = $cache->get($key);
+		}
+		return  json_encode( ['status'=>'ok', 'time'=>$tm] );
+	}
 }

+ 22 - 14
modules/manager/views/default/formNews.php

@@ -29,14 +29,9 @@ $tagsmenu = Yii::$app->cache->getOrSet("tagsmenu",function () use($tmodel){
 	return $tmodel->find()->rightJoin(['m'=>Tagsfilter::find()], 'm.id = tags.id')->orderBy('sort')->All();
 });
 
-$key = 'AI_time_mid';
+$key = 'AI_time_midnv#aiprogress0';
 $AI_time_mid = $cache->get($key);
-if( $AI_time_mid == false ){
-	$AI_time_mid = 100;
-}else{
-	$AI_time_mid = $AI_time_mid/1000000000;
-}
-
+if( !$AI_time_mid ) $AI_time_mid = 50;
 ?>
 
 <ul class="nav nav-tabs">
@@ -259,7 +254,7 @@ echo $form->field($news, 'title')->textInput([
 	<div class="row">
 			<input type="text" maxlength="<?=$tcount?>" id="news-title" class="form-control js-word-count-input col-sm-10 ml-2" name="News[title]" placeholder="Заголовок новости" aria-required="true" value='<?=str_replace("'", '&apos;', $news->title)?>'>
 		<span class="input-group-append">
-			<span type="button" class="btn btn-outline-primary btn-block btn-sm aiget" style="width: fit-content;" id="aititle" data-pb="aiprogress0" data-preprompt="Подскажи привлекательный заголовок для статьи"><i class="fa fa-robot"></i> Гена</span>
+			<span type="button" class="btn btn-outline-primary btn-block btn-sm aiget" style="width: fit-content;" id="aititle" data-pb="aiprogress0" data-preprompt="Подскажи привлекательный заголовок для статьи" title="Сгенерировать нейросетью, время гениратции примерно <?=round($AI_time_mid)?>сек."><i class="fa fa-robot"></i> Гена</span>
 		</span>
 		<div class="help-block px-lg-2"><div class="news__input-word-count"><span class="badge badge-info" id="title_count"></span></div></div>
 		<a class="btn btn-primary  px-lg-2" data-toggle="collapse" href="#collapseSEOTitle" role="button" aria-expanded="false" aria-controls="collapseSEOTitle">SEO title</a>
@@ -274,7 +269,7 @@ echo $form->field($news, 'title')->textInput([
   <div class="card card-body py-1">
     <span class="input-group-append">
     <input type="text" name="News[meta_title]" maxlength="<?=$tcount?>" value="<?=htmlentities($news->meta_title)?>" placeholder="SEO Title" class="form-control js-word-count-input col-sm-10 ml-1 py-0">
-		<span type="button" class="btn btn-outline-primary btn-block btn-sm aiget" style="width: fit-content;" id="aistitle" data-pb="saiprogressx" data-preprompt="Подскажи SEO заголовок для статьи"><i class="fa fa-robot"></i> Гена</span>
+		<span type="button" class="btn btn-outline-primary btn-block btn-sm aiget" style="width: fit-content;" id="aistitle" data-pb="saiprogressx" data-preprompt="Подскажи SEO заголовок для статьи" title="Сгенерировать нейросетью, время гениратции примерно <?=round($AI_time_mid)?>сек."><i class="fa fa-robot"></i> Гена</span>
 	</span>
 	<div class="help-block">Поле для альтернативного SEO Title заголовка, если не заполнять совпадёт с основным заголовком</div>
 		<div class="progress progress-sm active hidden" id="saiprogress">
@@ -324,7 +319,10 @@ echo $form->field($news, 'title')->textInput([
      </div>
  </div>
 <div class="form-group field-js_news_content required">
-	<label class="control-label" for="js_news_content">Контент</label>
+	<label class="control-label" for="js_news_content">Контент <span type="button" class="btn btn-outline-primary btn-block btn-sm aiget ml-4" style="width: fit-content;float: right;" id="acont" data-pb="aiprogressb" data-preprompt="Улучши статью, добавь факты, мнения экспертов, цитаты и источники, написать литературным языком" title="Сгенерировать нейросетью, время гениратции примерно <?=round($AI_time_mid)?>сек."><i class="fa fa-robot"></i> Гена</span></label>
+		<div class="progress progress-sm active hidden">
+			<div class="progress-bar bg-success progress-bar-striped" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%" id="aiprogressb"><span class="sr-only">40</span></div>
+		</div>
 	<textarea id="js_news_content" class="form-control" name="News[text]" rows="12" aria-required="true"><?=htmlentities($news->text)?></textarea>
 </div>
 <blockquote class="quote-info mt-0">
@@ -869,6 +867,7 @@ if (jQuery('#dt_pub').data('datetimepicker')) { jQuery('#dt_pub').datetimepicker
 $(function() {
 	window.varsubmit = false;
 	window.robotr = false;
+	AItime=<?=$AI_time_mid?>;
     $("input[id='news-title']").keyup(function count(){
         number = $("input[id='news-title']").val().length;
         $("#title_count").html(number);
@@ -950,8 +949,17 @@ $(function() {
 		$(pbar).attr('aria-valuenow', 100);
 		$(pbar).css('width','100%');
 		let data = { timer : null };
+
+		$.post("/manager/ai/ajax-time", {AI:'nv', progress: pbar}, function( data ) {
+			if( data.status == 'ok' ){
+				AItime = data.time;
+				if( !AItime ) AItime = <?=$AI_time_mid?>;
+				console.log(AItime);
+			}
+		}, "json");
+
 		data.timer = setInterval(countprogress, 1000, pbar, data);
-		$.post("/manager/ai/ajax-get-title", {AI:'ollama',text:text, progress: pbar}, function( data ) {
+		$.post("/manager/ai/ajax-get-title", {AI:'nv',text:text, progress: pbar}, function( data ) {
 			if( data.status == 'ok' ){
 				var data_all = data.data;
 				$("#AItext").html(data_all);
@@ -969,8 +977,8 @@ $(function() {
 });
 function countprogress(pbar, data){
 	val = $(pbar).attr('aria-valuenow');
-	d = 100/<?=$AI_time_mid?>;
-	console.log(100/d);
+	d = 100/(AItime);
+	console.log(d);
 	val = val - d;
 	if( val <=0 ) clearTimeout(data.timer);
 	$(pbar).attr('aria-valuenow', val);
@@ -1038,7 +1046,7 @@ window.onbeforeunload = function(e) {
 
 ?>
 <div class="modal" tabindex="-1" data-show="true" id="AI">
-  <div class="modal-dialog">
+  <div class="modal-dialog modal-dialog-scrollable modal-lg">
     <div class="modal-content">
       <div class="modal-header">
         <h5 class="modal-title">Генератор текстов от Гены</h5>