NvidiaAI.php 1.2 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 = '', $tokens = 0 ){
  20. $url = $this->host.self::$urlgen;
  21. $tokens = ( $tokens == 0 )?1024:$tokens;
  22. $cmd = array(
  23. "model" => $this->model,
  24. "messages" =>array(array("role"=>"user", "content"=>$promt)),
  25. "stream" => false,
  26. "temperature" => 0.2,
  27. "top_p" => 0.7,
  28. "max_tokens" => $tokens,
  29. );
  30. if( $context ){
  31. // $cmd['context'] = $context;
  32. }
  33. // echo json_encode($cmd); exit;
  34. $r = $this->Send( $url, $cmd );
  35. $this->res = $r;
  36. return $r;
  37. }
  38. public function Getres(){
  39. $r = $this->res;
  40. $robj = json_decode($r);
  41. if( isset($robj->choices[0]) ) return nl2br($robj->choices[0]->message->content);
  42. return $r;
  43. }
  44. }