BaiduAiTools.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\utils\Baidu;
  3. class BaiduAiTools {
  4. const ACCESS_TOKEN_API_URL = 'https://aip.baidubce.com/oauth/2.0/token';
  5. const CHAR_API_URL = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions';
  6. // 获取access_token
  7. public static function test() {
  8. $ret = self::chat([
  9. 'appid' => 'qng71IaZ',
  10. 'key' => 'xuccpM9rPM',
  11. ], '小米手机 好看 好用');
  12. var_dump($ret);
  13. die;
  14. }
  15. // 获取access_token
  16. public static function getAccessToken($appid, $key) {
  17. $access_token = '';
  18. try {
  19. if ($appid == '' || $key == '') {
  20. throw new \Exception('请先配置接口参数');
  21. }
  22. $cacheK = ['BaiduToken', $appid, $key];
  23. $cacheV = cache()->get($cacheK);
  24. if ($cacheV) {
  25. $access_token = $cacheV;
  26. } else {
  27. $res = http_post(static::ACCESS_TOKEN_API_URL, [
  28. 'headers' => [
  29. 'Content-Type' => 'application/json'
  30. ],
  31. 'query' => [
  32. 'grant_type' => 'client_credentials',
  33. 'client_id' => $appid,
  34. 'client_secret' => $key,
  35. ],
  36. ]);
  37. if ($res->getStatusCode() != 200) {
  38. throw new \Exception('getAccessToken Error: ' . $res->getStatusCode() . ' ' . $res->getReasonPhrase());
  39. }
  40. $data = json_decode((string) $res->getBody(), true);
  41. if (isset($data['error'])) {
  42. throw new \Exception('getAccessToken Error: ' . $data['error'] . ' ' . $data['error_description']);
  43. }
  44. cache()->set($cacheK, $data['access_token'], $data['expires_in'] - 60);
  45. $access_token = $data['access_token'];
  46. }
  47. return [
  48. 'code' => 0,
  49. 'data' => $access_token,
  50. ];
  51. } catch (\Exception $ex) {
  52. \Yii::error($ex);
  53. return [
  54. 'code' => 1,
  55. 'msg' => $ex->getMessage(),
  56. ];
  57. }
  58. }
  59. /**
  60. * 发送消息
  61. */
  62. public static function chat($conf, $question, $system = '你是一个小红书博主') {
  63. try {
  64. $body = [
  65. 'messages' => [
  66. [
  67. 'role' => 'user',
  68. 'content' => $question,
  69. ],
  70. ],
  71. 'temperature' => 0.6,
  72. ];
  73. if (!empty($system)) {
  74. $body['system'] = $system;
  75. }
  76. $token = static::getAccessToken($conf['appid'], $conf['key']);
  77. if($token['code']){
  78. throw new \Exception($token['msg']);
  79. }
  80. $res = http_post(static::CHAR_API_URL, [
  81. 'body' => json_encode($body),
  82. 'query' => [
  83. 'access_token' => $token['data'],
  84. ],
  85. 'headers' => [
  86. 'Content-Type' => 'application/json',
  87. ],
  88. ]);
  89. if ($res->getStatusCode() != 200) {
  90. throw new \Exception('StatusCode Error: ' . $res->getStatusCode() . ' ' . $res->getReasonPhrase());
  91. }
  92. $data = json_decode((string) $res->getBody(), true);
  93. if (isset($data['error_code'])) {
  94. throw new \Exception('error_code Error: ' . $data['error_code'] . ' ' . $data['error_msg']);
  95. }
  96. return [
  97. 'code' => 0,
  98. 'data' => $data,
  99. ];
  100. } catch (\Exception $ex) {
  101. \Yii::error($ex);
  102. return [
  103. 'code' => 1,
  104. 'msg' => $ex->getMessage(),
  105. ];
  106. }
  107. }
  108. }