Ya.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. test yandex api
  4. */
  5. namespace app\models;
  6. use yii\base\Component;
  7. use Yii;
  8. class Ya extends Component
  9. {
  10. /**
  11. * @var string
  12. */
  13. private $_token = "";
  14. private $FOLDER_ID = "b1gn8r9mc6pl01n6bjk4";
  15. private $_login = 'amicru';
  16. private $context = array();
  17. public $config = array();
  18. /**
  19. * Constructor
  20. *
  21. * @param bool $use_only_cache
  22. */
  23. function __construct()
  24. {
  25. $this->config = Yii::$app->components['ya'];
  26. $this->SetToken($this->config['tokenAI']);
  27. }
  28. function SetHeader(){
  29. $opts = array(
  30. 'http'=>array(
  31. 'method'=>"GET",
  32. 'header'=>"Accept-language: ru\r\n" .
  33. 'Authorization: Bearer '.$this->_token."\r\n".
  34. 'Content-Type: application/json'."\r\n".
  35. "x-folder-id: ".$this->FOLDER_ID."\r\n"
  36. )
  37. );
  38. $this->context = stream_context_create($opts);
  39. }
  40. function SetToken( $token ){
  41. $this->_token = $token;
  42. }
  43. function SetFolderID( $id ){
  44. $this->FOLDER_ID = $id;
  45. }
  46. function GetToken(){
  47. return $this->_token;
  48. }
  49. function Send(){
  50. $url = 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion';
  51. // $url = 'https://dev.amic.ru/manager/ya';
  52. $test = '{
  53. "modelUri": "gpt://b1gn8r9mc6pl01n6bjk4/yandexgpt-lite",
  54. "completionOptions": {
  55. "stream": false,
  56. "temperature": 0.1,
  57. "maxTokens": "1000"
  58. },
  59. "messages": [
  60. {
  61. "role": "system",
  62. "text": "Переведи текст"
  63. },
  64. {
  65. "role": "user",
  66. "text": "To be, or not to be: that is the question."
  67. }
  68. ]
  69. }';
  70. $data = json_decode($test);
  71. $json = json_encode($data);
  72. var_dump( $json );
  73. //return '';
  74. $header = array(
  75. 'Content-Type:application/json; charset=utf-8;',
  76. 'Authorization: Bearer '.$this->_token,
  77. "x-folder-id: ".$this->FOLDER_ID,
  78. );
  79. $curl_handle=curl_init();
  80. curl_setopt($curl_handle, CURLOPT_URL, $url);
  81. curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
  82. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
  83. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt($curl_handle, CURLOPT_USERAGENT, 'amicru');
  85. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false );
  86. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
  87. curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
  88. curl_setopt($curl_handle, CURLOPT_POST, 1);
  89. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $json);
  90. $query = curl_exec($curl_handle);
  91. echo curl_error($curl_handle);
  92. print_a(curl_getinfo($curl_handle));
  93. curl_close($curl_handle);
  94. // print_r($query);
  95. return $query;
  96. /*
  97. $opts = array(
  98. 'http'=>array(
  99. 'method'=>"POST",
  100. 'header'=>"Accept-language: ru\r\n" .
  101. 'Authorization: Bearer '.$this->_token."\r\n".
  102. 'Content-Type: application/json'."\r\n".
  103. "x-folder-id: ".$this->FOLDER_ID."\r\n",
  104. 'content' => $test
  105. )
  106. );
  107. $this->context = stream_context_create($opts);
  108. return file_get_contents($url, false, $this->context);
  109. */
  110. }
  111. }