model = new \app\admin\model\ps\Ai_job; $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("fileTypeList", $this->model->getFileTypeList()); $client = new LlmClient(); $this->view->assign('modelOptions', $client->formOptions()); $this->view->assign('defaultVisionModel', LlmClient::forRecognize()->defaultModel('image')); $this->view->assign('defaultTextModel', LlmClient::local()->defaultModel('pdf')); $this->view->assign('defaultModel', LlmClient::forRecognize()->defaultModel('image')); } /** * 查看 */ public function index() { $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['factory']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->fillAiJobDefaults($this->preExcludeFields($params)); $result = false; Db::startTrans(); try { $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } AiRecognizer::dispatch((int)$this->model->id); $this->success(__('Job_submitted')); } /** * 开始识别 * @param string|null $ids */ public function run($ids = null) { $ids = $ids ?: $this->request->param('ids'); $idList = array_values(array_filter(array_map('intval', explode(',', (string)$ids)))); if (!$idList) { $this->error(__('Parameter %s can not be empty', 'ids')); } if (count($idList) === 1) { ignore_user_abort(true); set_time_limit(360); try { $job = AiRecognizer::run($idList[0]); } catch (Exception $e) { $this->error($e->getMessage()); } if (($job['status'] ?? '') === 'fail') { $this->error($job['error_msg'] ?: __('Recognize_failed')); } $msg = __('Recognize_success'); if (!empty($job['template_id'])) { $msg .= ',' . __('Template_id') . ' #' . $job['template_id']; } $this->success($msg); } foreach ($idList as $id) { AiRecognizer::dispatch($id); } $this->success(__('Job_queued', [count($idList)])); } /** * @param array $row * @return array */ protected function fillAiJobDefaults(array $row) { $fileUrl = isset($row['file_url']) ? $row['file_url'] : ''; $fileType = isset($row['file_type']) ? $row['file_type'] : ''; if ($fileType === '' && $fileUrl !== '') { $fileType = preg_match('/\.pdf$/i', $fileUrl) ? 'pdf' : 'image'; } if ($fileType === '') { $fileType = 'image'; } $row['file_type'] = $fileType; if (empty($row['model'])) { $row['model'] = LlmClient::forRecognize()->defaultModel($fileType); } if (empty($row['status'])) { $row['status'] = 'pending'; } if (!isset($row['user_id']) || $row['user_id'] === '') { $row['user_id'] = 0; } if (!isset($row['template_id']) || $row['template_id'] === '') { $row['template_id'] = 0; } if (!isset($row['error_msg'])) { $row['error_msg'] = ''; } return $row; } }