DeepSeekAiTools.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\utils\DeepSeek;
  3. class DeepSeekAiTools {
  4. const CHAR_API_URL = 'https://api.deepseek.com';
  5. // 获取access_token
  6. public static function test() {
  7. $ret = self::chat([
  8. 'apiKey' => 'sk-4d0415c08d',
  9. ], '小米手机 好看 好用');
  10. var_dump($ret);
  11. die;
  12. }
  13. /**
  14. * 发送消息
  15. */
  16. public static function chat($conf, $question, $task = '生成一个帖子文案:') {
  17. try {
  18. // 从环境变量获取API密钥(推荐安全做法)
  19. $apiKey = $conf['apiKey'];
  20. if (!$apiKey) {
  21. throw new \Exception('请先配置接口参数');
  22. }
  23. // 验证输入参数
  24. if (!isset($question) || empty(trim($question))) {
  25. throw new \Exception('请输入有效关键字');
  26. }
  27. $keyword = trim($task . $question);
  28. // 构造API请求
  29. $requestData = [
  30. 'model' => 'deepseek-chat',
  31. 'prompt' => $keyword,
  32. 'max_tokens' => 300,
  33. 'temperature' => 0.7,
  34. ];
  35. $res = http_post(static::CHAR_API_URL . '/beta/completions', [
  36. 'body' => json_encode($requestData),
  37. 'headers' => [
  38. 'Content-Type' => 'application/json',
  39. 'Authorization' => 'Bearer ' . $apiKey
  40. ],
  41. ]);
  42. if ($res->getStatusCode() != 200) {
  43. throw new \Exception('StatusCode Error: ' . $res->getStatusCode() . ' ' . $res->getReasonPhrase());
  44. }
  45. $data = json_decode((string) $res->getBody(), true);
  46. if (isset($data['error_code'])) {
  47. throw new \Exception('error_code Error: ' . $data['error_code'] . ' ' . $data['error_msg']);
  48. }
  49. $data['result'] = $data['choices'] ? $data['choices'][0]['text'] : '';
  50. return [
  51. 'code' => 0,
  52. 'data' => $data,
  53. ];
  54. } catch (\Exception $ex) {
  55. \Yii::error($ex);
  56. return [
  57. 'code' => 1,
  58. 'msg' => $ex->getMessage(),
  59. ];
  60. }
  61. }
  62. public static function send($key, $question)
  63. {
  64. try {
  65. if (empty(trim($question))) {
  66. throw new \Exception('请输入有效问题');
  67. }
  68. $body = json_encode([
  69. 'model' => 'deepseek-chat',
  70. 'messages' => [
  71. [
  72. 'role' => 'user',
  73. 'content' => $question,
  74. ],
  75. ],
  76. 'temperature' => 1.3,
  77. ]);
  78. $res = http_post(static::CHAR_API_URL . '/chat/completions', [
  79. 'body' => $body,
  80. 'headers' => [
  81. 'Content-Type' => 'application/json',
  82. 'Authorization' => 'Bearer ' . $key,
  83. ],
  84. ]);
  85. if ($res->getStatusCode() != 200) {
  86. throw new \Exception('Error: ' . $res->getStatusCode() . ' ' . $res->getReasonPhrase());
  87. }
  88. $data = json_decode((string) $res->getBody(), true);
  89. if (isset($data['error_code'])) {
  90. throw new \Exception('error_code Error: ' . $data['error_code'] . ' ' . $data['error_msg']);
  91. }
  92. $result = $data['choices'] ? $data['choices'][0]['message']['content'] : '';
  93. return [
  94. 'code' => 0,
  95. 'data' => $result,
  96. ];
  97. } catch (\Throwable $e) {
  98. return [
  99. 'code' => 1,
  100. 'msg' => $e->getMessage(),
  101. ];
  102. }
  103. }
  104. }