diff --git a/app/dao/CreationArticleDao.php b/app/dao/CreationArticleDao.php new file mode 100644 index 0000000..83aeb1c --- /dev/null +++ b/app/dao/CreationArticleDao.php @@ -0,0 +1,15 @@ +make(CreationTaskDao::class); + //状态改为运行中 + $taskDao->update($data['task_id'], ['status' => 2]); + //获取任务明细 + $task = $taskDao->get($data['task_id']); + $prompts = array_merge($this->getDistillWords($task), + $this->getPortraitLibrary($task), + $this->getKnowledge($task), + $this->getAiCommand($task)); + $creation_count = $task['creation_count']; + $prompts['creation_count'] = $creation_count; + $finalPrompt = $this->defaultPrompt . '\n' . json_encode($prompts, JSON_UNESCAPED_UNICODE); + $results = []; + for ($i = 1; $i <= $creation_count; $i++) { + echo "开始生成第{$i}篇\n"; + $result = OpenAiClient::chat($finalPrompt); + $finalPrompt .= "已使用的标题:" . $result['content']['title'] . "\n"; + var_export("AI返回结果:\n"); + var_export($result); + $results[] = [ + 'task_id' => $task['id'], + 'article_category_id' => $task['article_category_id'], + 'title' => $result['content']['title'], + 'cover' => sizeof($prompts['images']) > 0 ? $prompts['images'][0] : '', + 'content' => $result['content']['content'], + 'create_by' => $task['create_by'], + 'update_by' => $task['update_by'] + ]; +// $results[] = OpenAiClient::chat($finalPrompt); + echo "生成第{$i}篇成功\n"; + } + var_export('creation结果:\n' ); + var_export($results); + //保存结果 + $articleDao = app()->make(CreationArticleDao::class); + $articleDao->insertAll($results); + //更改任务状态为完成 + $taskDao->update($data['task_id'], ['status' => 3]); + var_export('任务完成'); + } + + public function onConsumeFailure(\Throwable $e, $package) + { + echo "执行失败\n"; + echo $e->getMessage() . "\n"; + echo $e->getLine() . "\n"; + // 无需反序列化 + var_export($package); + //记录失败 + $taskDao = app()->make(CreationTaskDao::class); + $taskDao->update($package['data']['task_id'], [ + 'status' => 4, + 'try_count' => $package['attempts'], + 'status_msg' => $package['error'] + ]); + } + + /** + * 获取蒸馏词 + */ + private function getDistillWords($task) + { + echo "开始获取蒸馏词\n"; + //获取蒸馏词 + $distillationWordDao = app()->make(DistillationWordDao::class); + $distillWords = $distillationWordDao->getList(['id' => $task['distillation_id']]); + $names = array_column($distillWords, 'name'); + $ids = array_column($distillWords, 'id'); + $questionDao = app()->make(DistillationQuestionsDao::class); + $allQuestions = $questionDao->getColumn(['distillation_id' => $ids], 'name'); + // 随机获取最多5个问题 + if ($allQuestions) { + shuffle($allQuestions); // 打乱数组 + $questions = array_slice($allQuestions, 0, min(5, count($allQuestions))); // 取最多5个 + } else { + $questions = []; + } + if (!$distillWords) { + return [ + 'distillWords' => [], + 'questions' => [], + ]; + } + return [ + 'distillWords' => $names, + 'questions' => $questions + ]; + } + + /** + * 获取配图及使用数量 + */ + private function getPortraitLibrary($task) + { + echo "开始获取配图\n"; + $categoryDao = app()->make(EnterprisePortraitCategoryDao::class); + $category = $categoryDao->getOne(['id' => $task['portrait_category_id']]); + $libraryDao = app()->make(EnterprisePortraitLibraryDao::class); + $library = $libraryDao->getColumn(['category_id' => $category['id']], 'url'); + if (!$library) { + return [ + 'images' => [], + 'count' => 0 + ]; + } + return [ + 'images' => $library, + 'count' => $task['image_count'] + ]; + } + + /** + * 获取智能体知识库 + */ + private function getKnowledge($task) + { + echo "开始获取智能体知识库\n"; +// if (empty($task['knowledge_ids'])) { + return [ + 'knowledges' => [], + ]; +// } + } + + /** + * 获取创作指令 + * type = 1 文章创作 + * type = 2 标题创作 + * type = 3 流量复刻 + */ + private function getAiCommand($task, $type = 1) + { + echo "开始获取指令:" . $type . "\n"; + $aiCommandDao = app()->make(AiCommandDao::class); + $content_ai_command_id = $task['content_ai_command_id']; + $command = $aiCommandDao->getOne([ + 'type' => $type, + 'id' => $content_ai_command_id + ]); + if (!$command) { + return [ + 'prompt' => '' + ]; + } + return [ + 'prompt' . $type => $command['content'] + ]; + } +} \ No newline at end of file diff --git a/app/service/CreationTaskService.php b/app/service/CreationTaskService.php index e734cb4..57ab7b8 100644 --- a/app/service/CreationTaskService.php +++ b/app/service/CreationTaskService.php @@ -34,9 +34,14 @@ class CreationTaskService extends BaseService Db::startTrans(); try { //同步创建文章分类,若存在则不创建 - $category = $this->articleCategoryDao->save([ + $category = $this->articleCategoryDao->getOne([ 'name' => $params['name'] ]); + if (!$category) { + $category = $this->articleCategoryDao->save([ + 'name' => $params['name'] + ]); + } $params['article_category_id'] = $category['id']; if (isNotBlank($params['knowledge_ids'])) { $params['knowledge_ids'] = implode(',', $params['knowledge_ids']);