12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?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;
- }
- }
|