AIGatewayService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. namespace app\service;
  3. use think\Db;
  4. use think\Queue;
  5. class AIGatewayService{
  6. /**
  7. * 接口访问配置
  8. *
  9. * 每个模块包含:
  10. * - api_key:API 调用密钥(Token)
  11. * - api_url:对应功能的服务端地址
  12. */
  13. protected $config = [
  14. //图生文-gemini-2.5-flash-preview
  15. 'imgtotxt' => [
  16. 'api_key' => 'sk-LVcDfTx5SYK6pWiGpfcAN2KA0LunymnMiYSVfzUKQXrjlkZv',
  17. 'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
  18. ],
  19. //文生文-gtp-4
  20. 'txttotxtgtp' => [
  21. 'api_key' => 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss',
  22. 'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
  23. ],
  24. //文生文-gemini-2.0-flash
  25. 'txttotxtgemini' => [
  26. 'api_key' => 'sk-cqfCZFiiSIdpDjIHLMBbH6uWfeg7iVsASvlubjrNEmfUXbpX',
  27. 'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
  28. ],
  29. //文生图-dall-e-3
  30. 'txttoimg' => [
  31. 'api_key' => 'sk-MB6SR8qNaTjO80U7HJl4ztivX3zQKPgKVka9oyfVSXIkHSYZ',
  32. 'api_url' => 'https://chatapi.onechats.top/v1/images/generations'
  33. ]
  34. ];
  35. /**
  36. * 图生文
  37. * @param string $imageUrl 图像 URL,支持公网可访问地址
  38. * @param string $prompt 对图像的提问内容或提示文本
  39. */
  40. public function callGptApi($imageUrl, $prompt)
  41. {
  42. //方式一
  43. $data = [
  44. "model" => "gemini-2.5-flash-preview",
  45. "messages" => [[
  46. "role" => "user",
  47. "content" => [
  48. ["type" => "text", "text" => $prompt],
  49. ["type" => "image_url", "image_url" => [
  50. "url" => $imageUrl,
  51. "detail" => "auto"
  52. ]]
  53. ]
  54. ]],
  55. "max_tokens" => 1000
  56. ];
  57. //方式二
  58. // $data = [
  59. // "model" => "gpt-4-vision-preview",
  60. // "messages" => [[
  61. // "role" => "user",
  62. // "content" => [
  63. // ["type" => "text", "text" => $prompt],
  64. // ["type" => "image_url", "image_url" => [
  65. // "url" => $imageUrl,
  66. // "detail" => "auto"
  67. // ]]
  68. // ]
  69. // ]],
  70. // "max_tokens" => 1000
  71. // ];
  72. return $this->callApi($this->config['imgtotxt']['api_url'], $this->config['imgtotxt']['api_key'], $data);
  73. }
  74. /**
  75. * 文生文
  76. * @param string $prompt 用户输入的文本提示内容
  77. */
  78. public function txtGptApi($prompt,$txttotxt_selectedOption)
  79. {
  80. if (empty($prompt)) {
  81. throw new \Exception("Prompt 不允许为空");
  82. }
  83. //判断使用模型
  84. if ($txttotxt_selectedOption === 'gemini-2.0-flash') {
  85. $data = [
  86. 'model' => 'gemini-2.0-flash',
  87. 'messages' => [
  88. ['role' => 'user', 'content' => $prompt]
  89. ],
  90. 'temperature' => 0.7,
  91. 'max_tokens' => 1024
  92. ];
  93. return $this->callApi(
  94. $this->config['txttotxtgemini']['api_url'],
  95. $this->config['txttotxtgemini']['api_key'],
  96. $data
  97. );
  98. }else if ($txttotxt_selectedOption === 'gpt-4') {
  99. $data = [
  100. 'model' => 'gpt-4',
  101. 'messages' => [
  102. ['role' => 'user', 'content' => $prompt]
  103. ],
  104. 'temperature' => 0.7,
  105. 'max_tokens' => 1024
  106. ];
  107. return $this->callApi(
  108. $this->config['txttotxtgtp']['api_url'],
  109. $this->config['txttotxtgtp']['api_key'],
  110. $data
  111. );
  112. }
  113. }
  114. /**
  115. * 文生图
  116. *
  117. * @param string $prompt 提示文本,用于指导图像生成(最长建议 1000 字符)
  118. * @param string $selectedOption 模型名称,例如 'dall-e-3' 或其他兼容模型
  119. *
  120. * 默认参数说明(适用于所有模型):
  121. * - n: 1(生成 1 张图)
  122. * - size: '1024x1024'(标准正方形图像)
  123. * - quality: 'hd'(高清质量)
  124. * - style: 'vivid'(鲜明风格)
  125. *
  126. * response_format 参数差异:
  127. * - 若模型为 'dall-e-3':返回 base64 图像,字段为 `b64_json`
  128. * - 其他模型默认返回图像 URL,字段为 `url`
  129. *
  130. * ⚠️ 注意:使用此方法后,需在 Job/TextToImageJob.php 中按 response_format 判断提取方式:
  131. * 提取 url 图像
  132. * $base64Image = $dalle1024['data'][0]['url'] ?? null;
  133. * 提取 base64 图像
  134. * $base64Image = $dalle1024['data'][0]['b64_json'] ?? null;
  135. *
  136. * @return array 返回接口响应,成功时包含 'data' 字段,失败时包含 'error' 信息
  137. */
  138. public function callDalleApi($prompt, $selectedOption)
  139. {
  140. if ($selectedOption === 'dall-e-3') {
  141. $data = [
  142. 'prompt' => $prompt,
  143. 'model' => $selectedOption,
  144. 'n' => 1,
  145. 'size' => '1024x1024',
  146. 'quality' => 'hd',
  147. 'style' => 'vivid',
  148. 'response_format' => 'b64_json',
  149. ];
  150. } else {
  151. $data = [
  152. 'prompt' => $prompt,
  153. 'model' => $selectedOption,
  154. 'n' => 1,
  155. 'size' => '1024x1024',
  156. 'quality' => 'hd',
  157. 'style' => 'vivid',
  158. 'response_format' => 'b64_json',
  159. ];
  160. }
  161. return $this->callApi($this->config['txttoimg']['api_url'],$this->config['txttoimg']['api_key'],$data);
  162. }
  163. /**
  164. * 图生图
  165. * @param string $prompt 用户输入的文本提示内容
  166. * @param string $new_image_url 原图路径
  167. * @param array $options 可选参数,可覆盖默认配置
  168. * @return array
  169. */
  170. public function imgtoimgGptApi($prompt, $new_image_url, $options = [])
  171. {
  172. $imgPath = ROOT_PATH . 'public/' . $new_image_url;
  173. if (!file_exists($imgPath)) {
  174. return ['code' => 1, 'msg' => '原图不存在:' . $new_image_url];
  175. }
  176. // 默认参数配置
  177. $defaultParams = [
  178. 'prompt' => $prompt,
  179. 'steps' => 15,
  180. 'cfg_scale' => 7,
  181. 'denoising_strength' => 0.2,
  182. 'width' => 679,
  183. 'height' => 862,
  184. 'resize_mode' => 2,
  185. 'sampler_name' => 'DPM++ 2M SDE Heun',
  186. 'seed' => 611075477, // 使用-1表示随机种子
  187. 'inpaint_full_res' => true,
  188. 'inpainting_fill' => 1,
  189. 'override_settings' => [
  190. 'sd_model_checkpoint' => 'realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]',
  191. 'sd_vae' => 'anything-v4.5.vae.pt',
  192. 'CLIP_stop_at_last_layers' => 7
  193. ],
  194. 'override_settings_restore_afterwards' => true
  195. ];
  196. // 合并用户自定义选项
  197. $params = array_merge($defaultParams, $options);
  198. // 原图 base64 编码
  199. $imgData = file_get_contents($imgPath);
  200. $base64Img = base64_encode($imgData);
  201. $params['init_images'] = ['data:image/png;base64,' . $base64Img];
  202. $apiUrl = "http://20.0.17.188:45001/sdapi/v1/img2img";
  203. $headers = ['Content-Type: application/json'];
  204. $ch = curl_init();
  205. curl_setopt($ch, CURLOPT_URL, $apiUrl);
  206. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  207. curl_setopt($ch, CURLOPT_POST, true);
  208. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  209. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  210. curl_setopt($ch, CURLOPT_TIMEOUT, 90);
  211. $response = curl_exec($ch);
  212. if (curl_errno($ch)) {
  213. $error = curl_error($ch);
  214. curl_close($ch);
  215. return ['code' => 1, 'msg' => '请求失败:' . $error];
  216. }
  217. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  218. curl_close($ch);
  219. if ($httpCode !== 200) {
  220. return ['code' => 1, 'msg' => 'API请求失败,HTTP状态码:' . $httpCode];
  221. }
  222. $data = json_decode($response, true);
  223. if (json_last_error() !== JSON_ERROR_NONE) {
  224. return ['code' => 1, 'msg' => 'API返回数据解析失败:' . json_last_error_msg()];
  225. }
  226. if (!isset($data['images'][0])) {
  227. $errorMsg = $data['error'] ?? $data['message'] ?? '未知错误';
  228. return ['code' => 1, 'msg' => 'API未返回图像数据:' . $errorMsg];
  229. }
  230. return [
  231. 'code' => 0,
  232. 'msg' => '图像生成成功',
  233. 'data' => [
  234. 'url' => $data['images'][0]
  235. ]
  236. ];
  237. }
  238. /**
  239. * 图片高清放大
  240. * @param string $imageRelPath 原图相对路径(相对 public)
  241. * @param array $options 可选参数,可覆盖默认放大配置
  242. * @return array
  243. */
  244. /**
  245. * 图片高清放大(不保存,返回 base64)
  246. * @param string $imageRelPath 原图相对路径(相对 public)
  247. * @param array $options 可选参数
  248. * @return array
  249. */
  250. public function imgtogqGptApi($imageRelPath, $options = [])
  251. {
  252. $imgPath = ROOT_PATH . 'public/' . $imageRelPath;
  253. if (!file_exists($imgPath)) {
  254. return ['code' => 1, 'msg' => '原图不存在:' . $imageRelPath];
  255. }
  256. $defaultParams = [
  257. 'resize_mode' => 0,
  258. 'show_extras_results' => true,
  259. 'gfpgan_visibility' => 0,
  260. 'codeformer_visibility' => 0,
  261. 'codeformer_weight' => 0,
  262. 'upscaling_resize' => 2.45,
  263. 'upscaling_crop' => true,
  264. 'upscaler_1' => 'R-ESRGAN 4x+ Anime6B',
  265. 'upscaler_2' => 'None',
  266. 'extras_upscaler_2_visibility' => 0,
  267. 'upscale_first' => false
  268. ];
  269. $params = array_merge($defaultParams, $options);
  270. try {
  271. $imgData = file_get_contents($imgPath);
  272. if ($imgData === false) {
  273. throw new Exception('无法读取图片文件');
  274. }
  275. $params['image'] = base64_encode($imgData);
  276. } catch (Exception $e) {
  277. return ['code' => 1, 'msg' => '图片读取失败:' . $e->getMessage()];
  278. }
  279. $apiUrl = "http://20.0.17.188:45001/sdapi/v1/extra-single-image";
  280. $headers = ['Content-Type: application/json'];
  281. $ch = curl_init();
  282. curl_setopt_array($ch, [
  283. CURLOPT_URL => $apiUrl,
  284. CURLOPT_RETURNTRANSFER => true,
  285. CURLOPT_POST => true,
  286. CURLOPT_HTTPHEADER => $headers,
  287. CURLOPT_POSTFIELDS => json_encode($params),
  288. CURLOPT_TIMEOUT => 120
  289. ]);
  290. $response = curl_exec($ch);
  291. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  292. $curlErr = curl_error($ch);
  293. curl_close($ch);
  294. if ($curlErr) {
  295. return ['code' => 1, 'msg' => '请求失败:' . $curlErr];
  296. }
  297. if ($httpCode !== 200) {
  298. return ['code' => 1, 'msg' => 'API请求失败,HTTP状态码:' . $httpCode];
  299. }
  300. $data = json_decode($response, true);
  301. if (json_last_error() !== JSON_ERROR_NONE) {
  302. return ['code' => 1, 'msg' => 'API返回数据解析失败:' . json_last_error_msg()];
  303. }
  304. if (empty($data['image'])) {
  305. return ['code' => 1, 'msg' => '接口未返回有效的图像数据'];
  306. }
  307. return [
  308. 'code' => 0,
  309. 'msg' => '高清图生成成功',
  310. 'data' => [
  311. 'base64_image' => $data['image'],
  312. 'original_size' => strlen($imgData),
  313. 'processed_size' => strlen(base64_decode($data['image']))
  314. ]
  315. ];
  316. }
  317. // public function imgtogqGptApi($imageRelPath, $options = [])
  318. // {
  319. // // 构造图片路径
  320. // $imgPath = ROOT_PATH . 'public/' . $imageRelPath;
  321. //
  322. // if (!file_exists($imgPath)) {
  323. // return ['code' => 1, 'msg' => '原图不存在:' . $imageRelPath];
  324. // }
  325. //
  326. // // 默认放大配置
  327. // $defaultParams = [
  328. // 'resize_mode' => 0,
  329. // 'show_extras_results' => true,
  330. // 'gfpgan_visibility' => 0,
  331. // 'codeformer_visibility' => 0,
  332. // 'codeformer_weight' => 0,
  333. // 'upscaling_resize' => 2.45,
  334. // 'upscaling_crop' => true,
  335. // 'upscaler_1' => 'R-ESRGAN 4x+ Anime6B',
  336. // 'upscaler_2' => 'None',
  337. // 'extras_upscaler_2_visibility' => 0,
  338. // 'upscale_first' => false
  339. // ];
  340. //
  341. // // 合并配置参数
  342. // $params = array_merge($defaultParams, $options);
  343. //
  344. // // 编码原始图片
  345. // try {
  346. // $imgData = file_get_contents($imgPath);
  347. // if ($imgData === false) {
  348. // throw new Exception('无法读取图片文件');
  349. // }
  350. // $params['image'] = base64_encode($imgData);
  351. // } catch (Exception $e) {
  352. // return ['code' => 1, 'msg' => '图片读取失败:' . $e->getMessage()];
  353. // }
  354. //
  355. // $apiUrl = "http://20.0.17.188:45001/sdapi/v1/extra-single-image";
  356. // $headers = ['Content-Type: application/json'];
  357. //
  358. // // 调用接口
  359. // $ch = curl_init();
  360. // curl_setopt_array($ch, [
  361. // CURLOPT_URL => $apiUrl,
  362. // CURLOPT_RETURNTRANSFER => true,
  363. // CURLOPT_POST => true,
  364. // CURLOPT_HTTPHEADER => $headers,
  365. // CURLOPT_POSTFIELDS => json_encode($params),
  366. // CURLOPT_TIMEOUT => 120
  367. // ]);
  368. //
  369. // $response = curl_exec($ch);
  370. // $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  371. // $curlErr = curl_error($ch);
  372. // curl_close($ch);
  373. //
  374. // // 网络请求失败
  375. // if ($curlErr) {
  376. // return ['code' => 1, 'msg' => '请求失败:' . $curlErr];
  377. // }
  378. //
  379. // // 状态码错误
  380. // if ($httpCode !== 200) {
  381. // return ['code' => 1, 'msg' => 'API请求失败,HTTP状态码:' . $httpCode];
  382. // }
  383. //
  384. // // 解析响应
  385. // $data = json_decode($response, true);
  386. // if (json_last_error() !== JSON_ERROR_NONE) {
  387. // return ['code' => 1, 'msg' => 'API返回数据解析失败:' . json_last_error_msg()];
  388. // }
  389. //
  390. // if (empty($data['image'])) {
  391. // return ['code' => 1, 'msg' => '接口未返回有效的图像数据'];
  392. // }
  393. //
  394. // // 保存新图片
  395. // try {
  396. // $baseName = pathinfo($imageRelPath, PATHINFO_FILENAME);
  397. // $ext = pathinfo($imageRelPath, PATHINFO_EXTENSION);
  398. // $outputDir = 'uploads/extra_image/';
  399. // $outputPath = ROOT_PATH . 'public/' . $outputDir;
  400. //
  401. // if (!is_dir($outputPath)) {
  402. // mkdir($outputPath, 0755, true);
  403. // }
  404. //
  405. // $saveFileName = $baseName . '-hd.' . $ext;
  406. // $saveFullPath = $outputPath . $saveFileName;
  407. // $resultImg = base64_decode($data['image']);
  408. //
  409. // if ($resultImg === false || file_put_contents($saveFullPath, $resultImg) === false) {
  410. // throw new Exception('保存图片失败');
  411. // }
  412. //
  413. // return [
  414. // 'code' => 0,
  415. // 'msg' => '高清图生成成功',
  416. // 'data' => [
  417. // 'url' => '/' . $outputDir . $saveFileName,
  418. // 'original_size' => filesize($imgPath),
  419. // 'processed_size' => filesize($saveFullPath),
  420. // 'resolution' => getimagesize($saveFullPath)
  421. // ]
  422. // ];
  423. // } catch (Exception $e) {
  424. // return ['code' => 1, 'msg' => '保存失败:' . $e->getMessage()];
  425. // }
  426. // }
  427. /**
  428. * 通用 API 调用方法(支持重试机制)
  429. *
  430. * @param string $url 接口地址
  431. * @param string $apiKey 授权密钥(Bearer Token)
  432. * @param array $data 请求数据(JSON 格式)
  433. *
  434. * 功能说明:
  435. * - 使用 cURL 发送 POST 请求到指定 API 接口
  436. * - 设置请求头和超时时间等参数
  437. * - 支持最多重试 2 次,当接口调用失败时自动重试
  438. * - 返回成功时解析 JSON 响应为数组
  439. *
  440. * 异常处理:
  441. * - 若全部重试失败,将抛出异常并包含最后一次错误信息
  442. *
  443. * @return array 接口响应数据(成功时返回解析后的数组)
  444. * @throws \Exception 接口请求失败时抛出异常
  445. */
  446. public function callApi($url, $apiKey, $data)
  447. {
  448. $maxRetries = 2;
  449. $attempt = 0;
  450. $lastError = '';
  451. $httpCode = 0;
  452. $apiErrorDetail = '';
  453. while ($attempt <= $maxRetries) {
  454. try {
  455. $ch = curl_init();
  456. curl_setopt_array($ch, [
  457. CURLOPT_URL => $url,
  458. CURLOPT_RETURNTRANSFER => true,
  459. CURLOPT_POST => true,
  460. CURLOPT_POSTFIELDS => json_encode($data),
  461. CURLOPT_HTTPHEADER => [
  462. 'Content-Type: application/json',
  463. 'Authorization: Bearer ' . $apiKey
  464. ],
  465. CURLOPT_TIMEOUT => 120,
  466. CURLOPT_SSL_VERIFYPEER => true,
  467. CURLOPT_SSL_VERIFYHOST => 2,
  468. CURLOPT_CONNECTTIMEOUT => 30,
  469. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  470. CURLOPT_FAILONERROR => true
  471. ]);
  472. $response = curl_exec($ch);
  473. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  474. $curlError = curl_error($ch);
  475. if ($response === false) {
  476. throw new \Exception("请求发送失败: " . $curlError);
  477. }
  478. $result = json_decode($response, true);
  479. // 检查API返回的错误
  480. if (isset($result['error'])) {
  481. $apiErrorDetail = $result['error']['message'] ?? '';
  482. $errorType = $result['error']['type'] ?? '';
  483. // 常见错误类型映射
  484. $errorMessages = [
  485. 'invalid_request_error' => '请求参数错误',
  486. 'authentication_error' => '认证失败',
  487. 'rate_limit_error' => '请求频率过高',
  488. 'insufficient_quota' => '额度不足',
  489. 'billing_not_active' => '账户未开通付费',
  490. 'content_policy_violation' => '内容违反政策'
  491. ];
  492. $friendlyMessage = $errorMessages[$errorType] ?? 'API服务错误';
  493. throw new \Exception("{$friendlyMessage}: {$apiErrorDetail}");
  494. }
  495. if ($httpCode !== 200) {
  496. // HTTP状态码映射
  497. $statusMessages = [
  498. 400 => '请求参数不合法',
  499. 401 => 'API密钥无效或权限不足',
  500. 403 => '访问被拒绝',
  501. 404 => 'API端点不存在',
  502. 429 => '请求过于频繁,请稍后再试',
  503. 500 => '服务器内部错误',
  504. 503 => '服务暂时不可用'
  505. ];
  506. $statusMessage = $statusMessages[$httpCode] ?? "HTTP错误({$httpCode})";
  507. throw new \Exception($statusMessage);
  508. }
  509. curl_close($ch);
  510. return $result;
  511. } catch (\Exception $e) {
  512. $lastError = $e->getMessage();
  513. $attempt++;
  514. if ($attempt <= $maxRetries) {
  515. sleep(pow(2, $attempt));
  516. } else {
  517. // 最终失败时的详细错误信息
  518. $errorDetails = [
  519. '错误原因' => $this->getErrorCause($httpCode, $apiErrorDetail),
  520. '解决方案' => $this->getErrorSolution($httpCode),
  521. '请求参数' => json_encode($data, JSON_UNESCAPED_UNICODE),
  522. 'HTTP状态码' => $httpCode,
  523. '重试次数' => $attempt
  524. ];
  525. throw new \Exception("API请求失败\n" .
  526. "失败说明: " . $errorDetails['错误原因'] . "\n" .
  527. "建议解决方案: " . $errorDetails['解决方案'] . "\n" .
  528. "技术详情: HTTP {$httpCode} - " . $lastError);
  529. }
  530. }
  531. }
  532. }
  533. /**
  534. * 根据错误类型获取友好的错误原因
  535. */
  536. private function getErrorCause($httpCode, $apiError)
  537. {
  538. $causes = [
  539. 401 => 'API密钥无效、过期或没有访问权限',
  540. 400 => $apiError ?: '请求参数不符合API要求',
  541. 429 => '已达到API调用频率限制',
  542. 403 => '您的账户可能没有开通相关服务权限',
  543. 500 => 'OpenAI服务器处理请求时出错'
  544. ];
  545. return $causes[$httpCode] ?? '未知错误,请检查网络连接和API配置';
  546. }
  547. /**
  548. * 根据错误类型获取解决方案建议
  549. */
  550. private function getErrorSolution($httpCode)
  551. {
  552. $solutions = [
  553. 401 => '1. 检查API密钥是否正确 2. 确认密钥是否有访问权限 3. 尝试创建新密钥',
  554. 400 => '1. 检查请求参数 2. 验证提示词内容 3. 参考API文档修正参数',
  555. 429 => '1. 等待1分钟后重试 2. 升级账户提高限额 3. 优化调用频率',
  556. 403 => '1. 检查账户状态 2. 确认是否已开通付费 3. 联系OpenAI支持',
  557. 500 => '1. 等待几分钟后重试 2. 检查OpenAI服务状态页'
  558. ];
  559. return $solutions[$httpCode] ?? '1. 检查网络连接 2. 查看服务日志 3. 联系技术支持';
  560. }
  561. }