<?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 = '', $tokens = 0 ){
		$url = $this->host.self::$urlgen;
		$tokens = ( $tokens == 0 )?1024:$tokens;
		$cmd = array(
			"model" => $this->model,
			"messages" =>array(array("role"=>"user", "content"=>$promt)),
			"stream" => false,
			"temperature" => 0.2,
			"top_p" => 0.7,
			"max_tokens" => $tokens,
		);
		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);
		if( isset($robj->choices[0]) )	return nl2br($robj->choices[0]->message->content);
		return $r;
	}


}
