NvidiaAI.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\models;
  3. use yii;
  4. use yii\helpers\Url;
  5. class NvidiaAI extends \app\models\base\BaseAI
  6. {
  7. public $host = 'https://integrate.api.nvidia.com/';
  8. public $model = 'google/gemma-2-27b-it';
  9. public static $urlgen = 'v1/chat/completions';
  10. public $res = '';
  11. public $key = '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';
  12. function __construct(){
  13. $header = array(
  14. 'Content-Type: application/json',
  15. 'Authorization: Bearer '.$this->key
  16. );
  17. $this->SetHeader($header);
  18. }
  19. public function generate( $promt, $context = '' ){
  20. $url = $this->host.self::$urlgen;
  21. $cmd = array(
  22. "model" => $this->model,
  23. "messages" =>array(array("role"=>"user", "content"=>$promt)),
  24. "stream" => false,
  25. "temperature" => 0.2,
  26. "top_p" => 0.7,
  27. "max_tokens" => 1024,
  28. );
  29. if( $context ){
  30. // $cmd['context'] = $context;
  31. }
  32. // echo json_encode($cmd); exit;
  33. $r = $this->Send( $url, $cmd );
  34. $this->res = $r;
  35. return $r;
  36. }
  37. public function Getres(){
  38. $r = $this->res;
  39. $robj = json_decode($r);
  40. if( isset($robj->choices[0]) ) return nl2br($robj->choices[0]->message->content);
  41. return $r;
  42. }
  43. }