Index.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\service\AIGatewayService;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 首页接口
  9. */
  10. class Index extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 首页
  16. */
  17. public function index()
  18. {
  19. $this->success('请求成功');
  20. }
  21. // 向量引擎配置
  22. private $baseUrl = "https://api.vectorengine.ai/v1";
  23. private $apiKey = "sk-P877pnXMk2erRS2an7qEa3Kdb3rIb7JVAWZ39lhA8HeN71gZ"; // 从控制台获取
  24. private $timeout = 120; // 超时时间(秒),视频生成需要更长
  25. /**
  26. * 文生图接口
  27. * POST /api/index/textToImage
  28. * 参数: prompt (string) 提示词
  29. */
  30. public function textToImage()
  31. {
  32. $params = $this->request->param();
  33. if (empty($params)) {
  34. $this->error('提示词不能为空');
  35. }
  36. $data = [
  37. "model" => "gemini-3-pro-image-preview", // 可替换为 dall-e-3 等
  38. "prompt" => $params['prompt'],
  39. "n" => 1,
  40. "size" => "1024x1024"
  41. ];
  42. $result = $this->requestVectorEngine("/images/generations", $data);
  43. if ($result['code'] === 0) {
  44. $this->success('生成成功', $result['data']);
  45. } else {
  46. $this->error($result['msg'], $result['data']);
  47. }
  48. }
  49. /**
  50. * 图生文接口
  51. * POST /api/index/imageToText
  52. * 参数: image_url (string) 公网图片URL, prompt (string) 提问指令
  53. */
  54. public function imageToText()
  55. {
  56. $imageUrl = $this->request->post('image_url');
  57. $prompt = $this->request->post('prompt', '描述这张图片的内容');
  58. if (empty($imageUrl)) {
  59. $this->error('图片URL不能为空');
  60. }
  61. $data = [
  62. "model" => "gpt-4-vision-preview",
  63. "messages" => [
  64. [
  65. "role" => "user",
  66. "content" => [
  67. ["type" => "text", "text" => $prompt],
  68. ["type" => "image_url", "image_url" => ["url" => $imageUrl]]
  69. ]
  70. ]
  71. ],
  72. "max_tokens" => 1000
  73. ];
  74. $result = $this->requestVectorEngine("/chat/completions", $data);
  75. if ($result['code'] === 0) {
  76. $this->success('识别成功', $result['data']);
  77. } else {
  78. $this->error($result['msg'], $result['data']);
  79. }
  80. }
  81. /**
  82. * 文生视频接口
  83. * POST /api/index/textToVideo
  84. * 参数: prompt (string) 提示词, duration (int) 时长(秒), resolution (string) 分辨率
  85. */
  86. public function textToVideo()
  87. {
  88. $prompt = $this->request->post('prompt');
  89. $duration = $this->request->post('duration', 5);
  90. $resolution = $this->request->post('resolution', '720p');
  91. if (empty($prompt)) {
  92. $this->error('提示词不能为空');
  93. }
  94. $data = [
  95. "model" => "kling-1.6", // 可替换为 seedance-2.0 等
  96. "prompt" => $prompt,
  97. "duration" => (int)$duration,
  98. "resolution" => $resolution
  99. ];
  100. $result = $this->requestVectorEngine("/videos/generations", $data);
  101. if ($result['code'] === 0) {
  102. $this->success('生成成功', $result['data']);
  103. } else {
  104. $this->error($result['msg'], $result['data']);
  105. }
  106. }
  107. /**
  108. * 封装向量引擎通用CURL请求
  109. */
  110. private function requestVectorEngine($endpoint, $data)
  111. {
  112. $url = $this->baseUrl . $endpoint;
  113. $ch = curl_init();
  114. curl_setopt_array($ch, [
  115. CURLOPT_URL => $url,
  116. CURLOPT_RETURNTRANSFER => true,
  117. CURLOPT_POST => true,
  118. CURLOPT_POSTFIELDS => json_encode($data),
  119. CURLOPT_HTTPHEADER => [
  120. "Content-Type: application/json",
  121. "Authorization: Bearer " . $this->apiKey
  122. ],
  123. CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启
  124. CURLOPT_TIMEOUT => $this->timeout
  125. ]);
  126. $response = curl_exec($ch);
  127. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  128. $error = curl_error($ch);
  129. curl_close($ch);
  130. if ($error) {
  131. return ['code' => -1, 'msg' => '请求失败: ' . $error, 'data' => null];
  132. }
  133. if ($httpCode !== 200) {
  134. return ['code' => $httpCode, 'msg' => 'API返回异常', 'data' => json_decode($response, true)];
  135. }
  136. return ['code' => 0, 'msg' => '成功', 'data' => json_decode($response, true)];
  137. }
  138. }