liuhairui 5 сар өмнө
parent
commit
048ff0cfd1

+ 11 - 1
application/job/ImageJob.php

@@ -16,6 +16,7 @@ class ImageJob{
      */
     public function fire(Job $job, $data)
     {
+
         $logId = $data['log_id'];
         try {
             echo date('Y-m-d H:i:s')."图生文开始\n";
@@ -28,7 +29,7 @@ class ImageJob{
                 ]);
             }
             //执行逻辑
-            $result = $this->imageToText($data["sourceDir"],$data["file_name"],$data["prompt"],$data);
+            $result = $this->processImage($data);
             echo $result;
 
             // 更新日志状态:成功
@@ -82,6 +83,15 @@ class ImageJob{
         echo "ImageJob failed: " . json_encode($data);
     }
 
+
+
+    public function processImage($data)
+    {
+        // 根据传入的数据处理图片
+        $res = $this->imageToText($data["sourceDir"],$data["file_name"],$data["prompt"],$data);
+        echo $res;
+    }
+
     /**
      * 执行图生文逻辑(图像转文本)
      */

+ 9 - 13
application/job/TextToImageJob.php

@@ -72,14 +72,21 @@ class TextToImageJob
                     // 标准化结果文本
                     if ($result === true || $result === 1 || $result === '成功') {
                         $resultText = '成功';
+                        // 日志状态设置为成功(仅在未提前失败时)
+                        if ($logId) {
+                            Db::name('image_task_log')->where('id', $logId)->update([
+                                'status' => 2,
+                                'log' => '文生图处理成功',
+                                'update_time' => date('Y-m-d H:i:s')
+                            ]);
+                        }
                     } else {
                         $resultText = (string) $result ?: '失败或无返回';
                     }
 
                     echo "✅ 处理结果:{$resultText}\n";
                     echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
-                    echo "Processed: " . static::class . "\n";
-                    echo "文生图已处理完成\n\n";
+                    echo "文生图已处理完成\n";
 
                     // 若包含关键词,日志状态标为失败(-1)
                     if (strpos($resultText, '包含关键词') !== false) {
@@ -91,19 +98,8 @@ class TextToImageJob
                                 'update_time' => date('Y-m-d H:i:s')
                             ]);
                         }
-                    }else{
-                        // 日志状态设置为成功(仅在未提前失败时)
-                        if ($logId) {
-                            Db::name('image_task_log')->where('id', $logId)->update([
-                                'status' => 2,
-                                'log' => '文生图处理成功',
-                                'update_time' => date('Y-m-d H:i:s')
-                            ]);
-                        }
                     }
-                    continue; //跳过当前记录的后续处理
                 }
-                echo date('Y-m-d H:i:s') . " ✅ 文生图任务全部完成\n";
             }
 
             // 处理链式任务(如果有)

+ 3 - 3
application/job/TextToTextJob.php

@@ -46,7 +46,7 @@ class TextToTextJob
                     echo "处理时间:{$currentTime}\n";
                     echo "👉 正在处理第 " . ($index + 1) . " 条,ID: {$row['id']}\n";
 
-                    $result = $this->textToTxt($row['id']);
+                    $result = $this->textToTxt($row['id'],$data["txttotxt_selectedOption"]);
                     echo $result;
                     echo "✅ 处理结果:完成\n";
                     echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
@@ -108,7 +108,7 @@ class TextToTextJob
      * @param int $id text_to_image 表主键
      * @return string
      */
-    public function textToTxt($id)
+    public function textToTxt($id,$txttotxt_selectedOption)
     {
         $template = Db::name('template')
             ->field('id,english_content')
@@ -124,7 +124,7 @@ class TextToTextJob
 
         // 拼接提示词调用 文生文 接口
         $ai = new AIGatewayService();
-        $gptRes = $ai->txtGptApi($template['english_content'].$record['english_description']);
+        $gptRes = $ai->txtGptApi($template['english_content'].$record['english_description'],$txttotxt_selectedOption);
         $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
 
         // 更新数据库记录

+ 41 - 36
application/service/AIGatewayService.php

@@ -11,27 +11,22 @@ class AIGatewayService{
      * - api_url:对应功能的服务端地址
      */
     protected $config = [
-        //图生文
+        //图生文-gemini-2.5-flash-preview
         'imgtotxt' => [
             'api_key' => 'sk-LVcDfTx5SYK6pWiGpfcAN2KA0LunymnMiYSVfzUKQXrjlkZv',
             'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
         ],
-        //文生文
-        // 'txttotxt' => [
-        //     'api_key' => 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss',
-        //     'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
-        // ],
-        //文生文
-        'txttotxt' => [
+        //文生文-gtp-4
+        'txttotxtgtp' => [
             'api_key' => 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss',
             'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
         ],
-//        //文生图
-//        'txttoimg' => [
-//            'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
-//            'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
-//        ]
-//        //文生图
+        //文生文-gemini-2.0-flash
+        'txttotxtgemini' => [
+            'api_key' => 'sk-cqfCZFiiSIdpDjIHLMBbH6uWfeg7iVsASvlubjrNEmfUXbpX',
+            'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
+        ],
+        //文生图-dall-e-3
         'txttoimg' => [
             'api_key' => 'sk-MB6SR8qNaTjO80U7HJl4ztivX3zQKPgKVka9oyfVSXIkHSYZ',
             'api_url' => 'https://chatapi.onechats.top/v1/images/generations'
@@ -84,32 +79,42 @@ class AIGatewayService{
      * 文生文
      * @param string $prompt 用户输入的文本提示内容
      */
-    public function txtGptApi($prompt)
+    public function txtGptApi($prompt,$txttotxt_selectedOption)
     {
         if (empty($prompt)) {
             throw new \Exception("Prompt 不允许为空");
         }
-        //方式一
-        // $data = [
-        //     'prompt' => $prompt,
-        //     'model' => 'gpt-4',
-        //     'session_id' => null,
-        //     'context_reset' => true
-        // ];
-        //方式二
-        $data = [
-            'model' => 'gpt-4',
-            'messages' => [
-                ['role' => 'user', 'content' => $prompt]
-            ],
-            'temperature' => 0.7,
-            'max_tokens' => 1024
-        ];
-        return $this->callApi(
-            $this->config['txttotxt']['api_url'],
-            $this->config['txttotxt']['api_key'],
-            $data
-        );
+        //判断使用模型
+        if ($txttotxt_selectedOption === 'gemini-2.0-flash') {
+            $data = [
+                'model' => 'gemini-2.0-flash',
+                'messages' => [
+                    ['role' => 'user', 'content' => $prompt]
+                ],
+                'temperature' => 0.7,
+                'max_tokens' => 1024
+            ];
+            return $this->callApi(
+                $this->config['txttotxtgemini']['api_url'],
+                $this->config['txttotxtgemini']['api_key'],
+                $data
+            );
+        }else if ($txttotxt_selectedOption === 'gpt-4') {
+            $data = [
+                'model' => 'gpt-4',
+                'messages' => [
+                    ['role' => 'user', 'content' => $prompt]
+                ],
+                'temperature' => 0.7,
+                'max_tokens' => 1024
+            ];
+            return $this->callApi(
+                $this->config['txttotxtgtp']['api_url'],
+                $this->config['txttotxtgtp']['api_key'],
+                $data
+            );
+        }
+
     }
 
     /**

+ 2 - 1
application/service/ImageService.php

@@ -37,6 +37,7 @@ class ImageService{
                 "file_name" => $this->sourceDir($v, 3),
                 "type" => $params['type'] ?? '',
                 "selectedOption" => $params['selectedOption'],
+                "txttotxt_selectedOption" => $params['txttotxt_selectedOption'],
                 "prompt" => $template['content'],
                 "width" => $params['width'],
                 "height" => $params['height']
@@ -86,7 +87,7 @@ class ImageService{
                     $insertData['model_name'] = '图生文';
                     break;
                 case '文生文':
-                    $insertData['model'] = 'gpt-4';
+                    $insertData['model'] = $params['txttotxt_selectedOption'];
                     $insertData['model_name'] = '文生文';
                     break;
                 case '文生图':