EmbeddingService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use App\Services\AIModelService;
  5. class EmbeddingService
  6. {
  7. protected string $modelId;
  8. protected string $apiUrl = '';
  9. protected int $maxRetries = 3;
  10. /**
  11. * 创建服务实例,初始化 OpenAI API Key
  12. *
  13. * @return void
  14. */
  15. public function __construct(AIModelService $aiModels)
  16. {
  17. $models = $aiModels->getSysModels('embedding');
  18. $this->modelId = $models[0]['uid'];
  19. $this->apiUrl = config('mint.ai.proxy') . '/api/openai';
  20. }
  21. public function generate($text)
  22. {
  23. return $this->callOpenAI($text);
  24. }
  25. /**
  26. * 调用 OpenAI GPT 模型生成embedding
  27. *
  28. * {
  29. "object": "list",
  30. "data": [
  31. {
  32. "object": "embedding",
  33. "index": 0,
  34. "embedding": [
  35. -0.012345,
  36. 0.021876,
  37. 0.004231,
  38. -0.037654,
  39. 0.016482,
  40. -0.001273,
  41. 0.029871,
  42. -0.015630
  43. // ... 共1536个浮点数
  44. ]
  45. }
  46. ],
  47. "model": "text-embedding-3-small",
  48. "usage": {
  49. "prompt_tokens": 16,
  50. "total_tokens": 16
  51. }
  52. }
  53. *
  54. * 带有重试机制和指数退避。
  55. * 在 429 或 500+ 错误时重试,最大重试次数为 maxRetries。
  56. * 其他错误直接返回空字符串。
  57. *
  58. * @param string $text 输入文本
  59. * @param int $maxTokens 每次请求允许的最大 tokens 数
  60. * @return string 模型返回的摘要文本
  61. */
  62. protected function callOpenAI(string $text): string
  63. {
  64. $attempt = 0;
  65. $delay = 1;
  66. $payload = [
  67. 'model' => $this->modelId,
  68. 'input' => $text,
  69. ];
  70. while ($attempt < $this->maxRetries) {
  71. try {
  72. $response = Http::timeout(100)
  73. ->withHeaders([
  74. 'Authorization' => 'Bearer ',
  75. 'Content-Type' => 'application/json',
  76. ])->post($this->apiUrl, [
  77. 'model_id' => $this->modelId,
  78. 'payload' => $payload
  79. ]);
  80. if ($response->successful()) {
  81. $data = $response->json();
  82. if (isset($data['data']['embedding'])) {
  83. return $data['data']['embedding'];
  84. } else {
  85. return false;
  86. }
  87. }
  88. if (in_array($response->status(), [429, 500, 502, 503, 504])) {
  89. throw new \Exception("Temporary server error: " . $response->status());
  90. }
  91. return false;
  92. } catch (\Exception $e) {
  93. $attempt++;
  94. if ($attempt >= $this->maxRetries) {
  95. return false;
  96. }
  97. sleep($delay);
  98. $delay *= 10;
  99. }
  100. }
  101. return false;
  102. }
  103. }