NvidiaAI.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. return nl2br($robj->choices[0]->message->content);
  41. }
  42. }