OllamaAI.php 959 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace app\models;
  3. use yii;
  4. use yii\helpers\Url;
  5. class OllamaAI extends \app\models\base\BaseAI
  6. {
  7. public $host = 'http://192.168.0.20:11434';
  8. public $model = 'gemma2';
  9. public static $urlgen = '/api/generate';
  10. public $res = '';
  11. function OllamaAI(){
  12. $this->SetHeader();
  13. }
  14. public function generate( $promt, $context = '' ){
  15. $url = $this->host.self::$urlgen;
  16. $cmd = array(
  17. "model" => $this->model,
  18. "prompt" => $promt,
  19. "stream" => false,
  20. 'keep_alive' =>'60m'
  21. );
  22. if( $context ){
  23. $cmd['context'] = $context;
  24. }
  25. $r = $this->Send( $url, $cmd );
  26. $this->res = $r;
  27. return $r;
  28. }
  29. public function GetTime(){
  30. $r = $this->res;
  31. $robj = json_decode($r);
  32. return $robj->eval_duration/1000000000;
  33. }
  34. public function Getres(){
  35. $r = $this->res;
  36. $robj = json_decode($r);
  37. return nl2br($robj->response);
  38. }
  39. public function Getcontext(){
  40. $r = $this->res;
  41. $robj = json_decode($r);
  42. return $robj->context;
  43. }
  44. }