1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace manager\controllers;
- use app\models\OllamaAI;
- use app\models\NvidiaAI;
- use Yii;
- class AiController extends BaseController
- {
- public function actionIndex()
- {
- return 'Используй API для получения данных';
- }
- public function actionAjaxGetTitle()
- {
- if (Yii::$app->request->isPost){
- $cache = Yii::$app->cache;
- $type = Yii::$app->request->post('AI');
- $text = Yii::$app->request->post('text');
- $tokens = Yii::$app->request->post('tokens');
- $progress = Yii::$app->request->post('progress');
- $key = 'AI_time_mid'.$type.$progress;
- if( $text && $type == 'ollama' ){
- $ses = false;
- if( \Yii::$app->session->isActive ){
- \Yii::$app->session->close();
- $ses = true;
- }
- $ollama = new OllamaAI();
- $r = $ollama->generate(strip_tags($text));
- $data = $ollama->Getres();
- $t = $ollama->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] );
- }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), '', $tokens);
- 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{
- return json_encode( ['status'=>'err', 'msg'=>'Нет доступа до AI '.$type] );
- }
- }
- 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] );
- }
- }
|