1032 lines
40 KiB
PHP
1032 lines
40 KiB
PHP
<?php
|
|
|
|
/**
|
|
* The control file of ai module of ZenTaoPMS.
|
|
*
|
|
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
|
|
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
|
|
* @author Wenrui LI <liwenrui@easycorp.ltd>
|
|
* @package ai
|
|
* @link https://www.zentao.net
|
|
*/
|
|
class ai extends control
|
|
{
|
|
/**
|
|
* AI model.
|
|
*
|
|
* @var aiModel
|
|
* @access public
|
|
*/
|
|
public $ai;
|
|
|
|
/**
|
|
* User model.
|
|
*
|
|
* @var userModel
|
|
* @access public
|
|
*/
|
|
public $user;
|
|
|
|
public function __construct($module = '', $method = '')
|
|
{
|
|
parent::__construct($module, $method);
|
|
$this->loadModel('user');
|
|
}
|
|
|
|
/**
|
|
* Check for user's privs and model configs, redirect to most relevant page.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function adminIndex()
|
|
{
|
|
/* Redirect to model edit ifuser has priv and model is unconfigured. */
|
|
if(commonModel::hasPriv('ai', 'models') && !$this->ai->hasModelsAvailable()) return $this->locate($this->createLink('ai', 'models'));
|
|
|
|
/* Redirect to prompts ifuser has priv. */
|
|
if(commonModel::hasPriv('ai', 'prompts')) return $this->locate($this->createLink('ai', 'prompts'));
|
|
|
|
/* Redirect to miniPrograms ifuser has priv. */
|
|
if(commonModel::hasPriv('ai', 'miniPrograms')) return $this->locate($this->createLink('ai', 'miniPrograms'));
|
|
|
|
/* Redirect to models ifuser has priv. */
|
|
if(commonModel::hasPriv('ai', 'models')) return $this->locate($this->createLink('ai', 'models'));
|
|
|
|
/* Redirect to assistants ifuser has priv. */
|
|
if(commonModel::hasPriv('ai', 'assistants')) return $this->locate($this->createLink('ai', 'assistants'));
|
|
|
|
/* User has no priv, deny access. */
|
|
return $this->send(array('result' => 'fail', 'message' => $this->lang->error->accessDenied, 'locate' => 'back'));
|
|
}
|
|
|
|
/**
|
|
* List models.
|
|
*
|
|
* @param string $orderBy
|
|
* @param int $recTotal
|
|
* @param int $recPerPage
|
|
* @param int $pageID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function models($orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
|
|
{
|
|
$this->app->loadClass('pager', $static = true);
|
|
$pager = new pager($recTotal, $recPerPage, $pageID);
|
|
|
|
/* Adapt order. */
|
|
$actualOrdering = $orderBy;
|
|
if(strpos(strtolower($actualOrdering), 'usesproxy') !== false) $actualOrdering = str_ireplace('usesproxy', 'proxy', $actualOrdering);
|
|
|
|
$models = $this->ai->getLanguageModels('', false, $pager, $actualOrdering);
|
|
|
|
/* Process model props to displayable format. */
|
|
foreach($models as $model)
|
|
{
|
|
$model->usesProxy = !empty($model->proxy) ? 'on' : 'off';
|
|
$model->vendor = $this->lang->ai->models->vendorList->{$model->type}[$model->vendor];
|
|
|
|
if(empty($model->name)) $model->name = $this->lang->ai->models->typeList[$model->type];
|
|
}
|
|
|
|
$this->view->models = $models;
|
|
$this->view->orderBy = $orderBy;
|
|
$this->view->pager = $pager;
|
|
$this->view->title = $this->lang->ai->models->title;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* View language model configuration.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelView($modelID)
|
|
{
|
|
$model = $this->ai->getLanguageModel($modelID);
|
|
if(empty($model)) return $this->locate($this->createLink('ai', 'models'));
|
|
|
|
$this->view->model = $this->ai->unserializeModel($model);
|
|
$this->view->title = $this->lang->ai->models->view;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Create language model configuration.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelCreate()
|
|
{
|
|
if(strtolower($this->server->request_method) == 'post')
|
|
{
|
|
$modelConfig = fixer::input('post')->get();
|
|
|
|
/* Check for required credentials. */
|
|
$credentials = array();
|
|
if(!empty($this->config->ai->vendorList[$modelConfig->vendor]['credentials'])) $credentials = $this->config->ai->vendorList[$modelConfig->vendor]['credentials'];
|
|
foreach($credentials as $credKey)
|
|
{
|
|
if(empty($modelConfig->$credKey)) dao::$errors[$credKey][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$credKey);
|
|
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr)) dao::$errors['proxyAddr'][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
|
|
}
|
|
if(!empty(dao::$errors)) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
$result = $this->ai->createModel($modelConfig);
|
|
|
|
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('modelView', "modelID=$result") . '#app=admin'));
|
|
}
|
|
|
|
$this->view->title = $this->lang->ai->models->create;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Edit language model configuration.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelEdit($modelID)
|
|
{
|
|
if(strtolower($this->server->request_method) == 'post')
|
|
{
|
|
$modelConfig = fixer::input('post')->get();
|
|
|
|
/* Check for required credentials. */
|
|
$credentials = array();
|
|
if(!empty($this->config->ai->vendorList[$modelConfig->vendor]['credentials'])) $credentials = $this->config->ai->vendorList[$modelConfig->vendor]['credentials'];
|
|
foreach($credentials as $credKey)
|
|
{
|
|
if(empty($modelConfig->$credKey)) dao::$errors[$credKey][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$credKey);
|
|
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr)) dao::$errors['proxyAddr'][] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
|
|
}
|
|
if(!empty(dao::$errors)) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
$result = $this->ai->updateModel($modelID, $modelConfig);
|
|
|
|
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('modelView', "modelID=$modelID") . '#app=admin'));
|
|
}
|
|
|
|
$model = $this->ai->getLanguageModel($modelID);
|
|
if(empty($model)) return $this->locate($this->createLink('ai', 'models'));
|
|
|
|
$modelData = $this->ai->unserializeModel($model);
|
|
if(empty($model->name)) $modelData->name = ''; // Set name to empty if it was not set.
|
|
|
|
$this->view->model = $modelData;
|
|
$this->view->title = $this->lang->ai->models->edit;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Enable language model.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelEnable($modelID)
|
|
{
|
|
$result = $this->ai->toggleModel($modelID, true);
|
|
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
return $this->sendSuccess(array('load' => true, 'message' => ''));
|
|
}
|
|
|
|
/**
|
|
* Disable language model.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelDisable($modelID)
|
|
{
|
|
$result = $this->ai->toggleModel($modelID, false);
|
|
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
return $this->sendSuccess(array('load' => true, 'message' => ''));
|
|
}
|
|
|
|
/**
|
|
* Delete language model.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelDelete($modelID)
|
|
{
|
|
$result = $this->ai->deleteModel($modelID);
|
|
if($result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
return $this->send(array('result' => 'success', 'load' => array('locate' => inlink('models'))));
|
|
}
|
|
|
|
/**
|
|
* Test connection with model configuration.
|
|
*
|
|
* @param int $modelID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function modelTestConnection($modelID = 0)
|
|
{
|
|
$result = false;
|
|
|
|
if(strtolower($this->server->request_method) == 'post')
|
|
{
|
|
/* Test model connection from form if method is POST. */
|
|
$modelConfig = fixer::input('post')->get();
|
|
|
|
$currentVendor = empty($modelConfig->vendor) ? key($this->lang->ai->models->vendorList->{empty($modelConfig->type) ? key($this->lang->ai->models->typeList) : $modelConfig->type}) : $modelConfig->vendor;
|
|
$vendorRequiredFields = $this->config->ai->vendorList[$currentVendor]['credentials'];
|
|
|
|
$errors = array();
|
|
if(empty($modelConfig->type)) $errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->type);
|
|
foreach($vendorRequiredFields as $field)
|
|
{
|
|
if(empty($modelConfig->$field)) $errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->$field);
|
|
}
|
|
if(!empty($modelConfig->proxyType) && empty($modelConfig->proxyAddr))
|
|
{
|
|
$errors[] = sprintf($this->lang->ai->validate->noEmpty, $this->lang->ai->models->proxyAddr);
|
|
}
|
|
if(!empty($errors)) return $this->send(array('result' => 'fail', 'message' => implode(' ', $errors)));
|
|
|
|
$this->ai->setModelConfig($modelConfig);
|
|
|
|
if($this->config->ai->models[$modelConfig->type] == 'ernie' || $currentVendor == 'azure' || $modelConfig->type == 'openai-gpt4' || $modelConfig->vendor == 'openaiCompatible')
|
|
{
|
|
$messages = array((object)array('role' => 'user', 'content' => 'test'));
|
|
$result = $this->ai->converse(null, $messages, array('maxTokens' => 1));
|
|
}
|
|
else
|
|
{
|
|
$result = $this->ai->complete(null, 'test', 1); // Test completing 'test' with length of 1.
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Test model with id if not POST. */
|
|
$result = $this->ai->testModelConnection($modelID);
|
|
}
|
|
|
|
if($result === false)
|
|
{
|
|
return $this->send(array('result' => 'fail', 'message' => empty($this->ai->errors) ? $this->lang->ai->models->testConnectionResult->fail : sprintf($this->lang->ai->models->testConnectionResult->failFormat, implode(', ', $this->ai->errors))));
|
|
}
|
|
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->ai->models->testConnectionResult->success));
|
|
}
|
|
|
|
/**
|
|
* List mini programs.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function miniPrograms($category = '', $status = '', $orderBy = 'createdDate_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
|
|
{
|
|
$this->lang->aiapp->menu->generalAgent['subModule'] = 'ai';
|
|
|
|
$this->app->loadClass('pager', true);
|
|
$pager = new pager($recTotal, $recPerPage, $pageID);
|
|
$order = common::appendOrder($orderBy);
|
|
$categoryList = array_merge($this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategories());
|
|
|
|
$programs = $this->ai->getMiniPrograms($category, $status, $order, $pager);
|
|
foreach($programs as $program)
|
|
{
|
|
$program->canPublish = empty($program->published) && $this->ai->canPublishMiniProgram($program);
|
|
$program->createdByLabel = $program->createdBy === 'system' ? $this->lang->admin->system : $this->loadModel('user')->getById($program->createdBy, 'account')->realname;
|
|
$program->categoryLabel = $categoryList[$program->category];
|
|
$program->publishedLabel = $program->published === '1'
|
|
? $this->lang->ai->miniPrograms->statuses['active']
|
|
: $this->lang->ai->miniPrograms->statuses['draft'];
|
|
}
|
|
|
|
$this->app->loadLang('aiapp');
|
|
$this->view->title = $this->lang->aiapp->manageGeneralAgent;
|
|
$this->view->miniPrograms = $programs;
|
|
$this->view->category = $category;
|
|
$this->view->categoryList = $categoryList;
|
|
$this->view->status = $status;
|
|
$this->view->orderBy = $orderBy;
|
|
$this->view->pager = $pager;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Edit mini program category.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function editMiniProgramCategory()
|
|
{
|
|
$this->lang->aiapp->menu->generalAgent['subModule'] = 'ai';
|
|
|
|
if(!empty($_POST))
|
|
{
|
|
$_POST = array_filter($_POST, function($key)
|
|
{
|
|
return strpos($key, 'custom') === 0;
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
if($this->ai->checkDuplicatedCategory()) return $this->sendError($this->lang->ai->maintenanceGroupDuplicated);
|
|
$this->ai->updateCustomCategories();
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => true));
|
|
}
|
|
|
|
$this->view->usedCustomCategories = $this->ai->getUsedCustomCategories();
|
|
$this->view->categoryList = $this->ai->getCustomCategories();
|
|
$this->view->title = $this->lang->ai->miniPrograms->common;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Publish a mini program.
|
|
*
|
|
* @param string $appID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function publishMiniProgram($appID)
|
|
{
|
|
$result = $this->ai->publishMiniProgram($appID, '1');
|
|
if($result) return $this->send(array('result' => 'success', 'load' => true));
|
|
$this->sendError(dao::getError());
|
|
}
|
|
|
|
/**
|
|
* Unpublish a mini program.
|
|
*
|
|
* @param string $appID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function unpublishMiniProgram($appID)
|
|
{
|
|
$result = $this->ai->publishMiniProgram($appID, '0');
|
|
if($result) return $this->send(array('result' => 'success', 'load' => true));
|
|
$this->sendError(dao::getError());
|
|
}
|
|
|
|
/**
|
|
* Import mini program from zip file.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function importMiniProgram()
|
|
{
|
|
if(empty($_FILES)) return $this->sendError(array('file' => sprintf($this->lang->error->notempty, $this->lang->ai->installPackage)));
|
|
|
|
$failResponse = array('result' => 'fail', 'message' => $this->lang->ai->saveFail, 'locate' => $this->createLink('ai', 'miniprograms'));
|
|
if(empty($_POST)) return $this->send($failResponse);
|
|
|
|
$errors = $this->ai->verifyRequiredFields(array('category' => $this->lang->ai->miniPrograms->category, 'published' => $this->lang->ai->toPublish));
|
|
if($errors !== false) return $this->sendError($errors);
|
|
|
|
$file = $_FILES['file'];
|
|
$filePath = $file['tmp_name'];
|
|
$result = $this->ai->extractZtAppZip($filePath);
|
|
if(!is_array($result)) return $this->send($failResponse);
|
|
|
|
$info = $result[0];
|
|
$fileName = $info['filename'];
|
|
if(!is_file($fileName)) return $this->send($failResponse);
|
|
|
|
$content = file_get_contents($fileName);
|
|
if(empty($content)) return $this->send($failResponse);
|
|
|
|
$ztApp = json_decode($content);
|
|
if(!is_object($ztApp)) return $this->send($failResponse);
|
|
|
|
$ztApp->name = $this->ai->getUniqueAppName($ztApp->name);
|
|
$ztApp->published = $_POST['published'];
|
|
$ztApp->category = $_POST['category'];
|
|
$this->ai->createMiniProgram($ztApp);
|
|
return $this->sendSuccess(array('message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
|
|
}
|
|
|
|
/**
|
|
* List prompts.
|
|
*
|
|
* @param string $module
|
|
* @param string $status
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function prompts($module = '', $status = '', $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
|
|
{
|
|
$this->loadModel('user');
|
|
$users = $this->user->getPairs('noletter');
|
|
|
|
/* Set pager and order. */
|
|
$this->app->loadClass('pager', $static = true);
|
|
$pager = new pager($recTotal, $recPerPage, $pageID);
|
|
$order = common::appendOrder($orderBy);
|
|
|
|
$this->view->prompts = $this->ai->getPrompts($module, $status, $order, $pager);
|
|
$this->view->module = $module;
|
|
$this->view->status = $status;
|
|
$this->view->orderBy = $orderBy;
|
|
$this->view->pager = $pager;
|
|
$this->view->title = $this->lang->aiapp->zentaoAgent;
|
|
$this->view->users = $users;
|
|
|
|
if($this->config->edition == 'open')
|
|
{
|
|
$this->view->promptModules = array_map(function ($prompt)
|
|
{
|
|
return $prompt->module;
|
|
}, $this->ai->getPrompts());
|
|
}
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* View prompt details.
|
|
*
|
|
* @param int $id
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptView($id)
|
|
{
|
|
$prompt = $this->ai->getPromptById($id);
|
|
|
|
$this->view->prompt = $prompt;
|
|
$this->view->preAndNext = $this->loadModel('common')->getPreAndNextObject('prompt', $id);
|
|
$this->view->actions = $this->loadModel('action')->getList('prompt', $id);
|
|
$this->view->dataPreview = $this->ai->generateDemoDataPrompt($prompt->module, $prompt->source);
|
|
$this->view->users = $this->loadModel('user')->getPairs('noletter');
|
|
$this->view->title = "{$this->lang->aiapp->zentaoAgent}#{$prompt->id} $prompt->name";
|
|
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Create a prompt.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function createPrompt()
|
|
{
|
|
if($_POST)
|
|
{
|
|
$prompt = form::data($this->config->ai->form->createPrompt)->get();
|
|
$promptID = $this->ai->createPrompt($prompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
$url = commonModel::hasPriv('ai', 'designPrompt') ? $this->createLink('ai', 'promptassignrole', "prompt=$promptID") : $this->createLink('ai', 'promptview', "id=$promptID");
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => $url));
|
|
}
|
|
|
|
$this->view->title = $this->lang->ai->prompts->create;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Edit a prompt.
|
|
*
|
|
* @param int $id
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptEdit($id)
|
|
{
|
|
$prompt = $this->ai->getPromptByID($id);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = form::data($this->config->ai->form->createPrompt)->get();
|
|
|
|
$prompt->name = $data->name;
|
|
$prompt->desc = $data->desc;
|
|
|
|
$this->ai->updatePrompt($prompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => $this->inlink('promptView', "id={$prompt->id}")));
|
|
}
|
|
|
|
$this->view->prompt = $prompt;
|
|
$this->view->title = $this->lang->ai->prompts->edit;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Delete a prompt.
|
|
*
|
|
* @param int $prompt
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptDelete($prompt)
|
|
{
|
|
$result = $this->ai->deletePrompt($prompt);
|
|
|
|
if(dao::isError() || $result === false) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->ai->prompts->action->deleteSuccess, 'load' => $this->inlink('prompts')));
|
|
}
|
|
|
|
/**
|
|
* Edit role of prompt, prompt editing step 2.
|
|
*
|
|
* @param int $promptID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptAssignRole($promptID)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
$prompt = $this->ai->getPromptByID($promptID);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->model = $data->model;
|
|
$prompt->role = $data->role;
|
|
$prompt->characterization = $data->characterization;
|
|
|
|
if(!empty($data->saveTemplate) && $data->saveTemplate == 'save')
|
|
{
|
|
$this->ai->createRoleTemplate($prompt->role, $prompt->characterization);
|
|
}
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->jumpToNext)) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSelectDataSource', "promptID=$promptID")));
|
|
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptAssignRole', "promptID=$promptID")));
|
|
}
|
|
|
|
$this->view->prompt = $prompt;
|
|
$this->view->promptID = $promptID;
|
|
$this->view->lastActiveStep = $this->ai->getLastActiveStep($prompt);
|
|
$this->view->title = "{$this->lang->ai->prompts->common}#{$prompt->id} $prompt->name {$this->lang->hyphen} " . $this->lang->ai->prompts->assignRole . " {$this->lang->hyphen} " . $this->lang->ai->prompts->common;
|
|
$this->view->roleTemplates = $this->ai->getRoleTemplates();
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Set data source of prompt, prompt editing step 3.
|
|
*
|
|
* @param int $promptID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptSelectDataSource($promptID)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
$prompt = $this->ai->getPromptByID($promptID);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->module = $data->datagroup;
|
|
$prompt->source = ",$data->datasource,";
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->jumpToNext)) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSetPurpose', "promptID=$promptID")));
|
|
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSelectDataSource', "promptID=$promptID")));
|
|
}
|
|
|
|
$this->view->activeDataSource = empty($prompt->module) ? current(array_keys($this->config->ai->dataSource)) : $prompt->module;
|
|
$this->view->dataSource = $this->config->ai->dataSource;
|
|
$this->view->prompt = $prompt;
|
|
$this->view->promptID = $promptID;
|
|
$this->view->lastActiveStep = $this->ai->getLastActiveStep($prompt);
|
|
$this->view->title = "{$this->lang->ai->prompts->common}#{$prompt->id} $prompt->name {$this->lang->hyphen} " . $this->lang->ai->prompts->selectDataSource . " {$this->lang->hyphen} " . $this->lang->ai->prompts->common;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Edit purpose and purpose elaboration of prompt, prompt editing step 4.
|
|
*
|
|
* @param int $promptID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptSetPurpose($promptID)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
$prompt = $this->ai->getPromptByID($promptID);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->purpose = $data->purpose;
|
|
$prompt->elaboration = $data->elaboration;
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->jumpToNext)) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSetTargetForm', "promptID=$promptID")));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSetPurpose', "promptID=$promptID")));
|
|
}
|
|
|
|
$this->view->dataPreview = $this->ai->generateDemoDataPrompt($prompt->module, $prompt->source);
|
|
$this->view->prompt = $prompt;
|
|
$this->view->promptID = $promptID;
|
|
$this->view->lastActiveStep = $this->ai->getLastActiveStep($prompt);
|
|
$this->view->title = "{$this->lang->ai->prompts->common}#{$prompt->id} $prompt->name {$this->lang->hyphen} " . $this->lang->ai->prompts->setPurpose . " {$this->lang->hyphen} " . $this->lang->ai->prompts->common;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Set target form of prompt, prompt editing step 5.
|
|
*
|
|
* @param int $promptID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptSetTargetForm($promptID)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
$prompt = $this->ai->getPromptByID($promptID);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->targetForm = $data->targetForm;
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->goTesting)) // Go to testing object view.
|
|
{
|
|
$location = $this->ai->getTestingLocation($prompt);
|
|
return $this->send(empty($location) ? array('result' => 'fail', 'target' => '#go-test-btn', 'message' => $this->lang->ai->prompts->goingTestingFail) : array('result' => 'success', 'target' => '#go-test-btn', 'msg' => $this->lang->ai->prompts->goingTesting, 'locate' => $location));
|
|
}
|
|
|
|
if(!empty($data->jumpToNext)) $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptFinalize', "promptID=$promptID")));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptSetTargetForm', "promptID=$promptID")));
|
|
}
|
|
|
|
$this->view->dataPreview = $this->ai->generateDemoDataPrompt($prompt->module, $prompt->source);
|
|
$this->view->prompt = $prompt;
|
|
$this->view->promptID = $promptID;
|
|
$this->view->lastActiveStep = $this->ai->getLastActiveStep($prompt);
|
|
$this->view->title = "{$this->lang->ai->prompts->common}#{$prompt->id} $prompt->name {$this->lang->hyphen} " . $this->lang->ai->prompts->setTargetForm . " {$this->lang->hyphen} " . $this->lang->ai->prompts->common;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Edit additional information of prompt, final prompt editing step.
|
|
*
|
|
* @param int $promptID
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptFinalize($promptID)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
$prompt = $this->ai->getPromptByID($promptID);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->name = $data->name;
|
|
$prompt->desc = $data->desc;
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->jumpToNext)) $this->ai->togglePromptStatus($prompt, 'active');
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(!empty($data->jumpToNext)) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('prompts')));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->inlink('promptFinalize', "promptID=$promptID")));
|
|
}
|
|
|
|
$this->view->prompt = $prompt;
|
|
$this->view->promptID = $promptID;
|
|
$this->view->lastActiveStep = $this->ai->getLastActiveStep($prompt);
|
|
$this->view->title = "{$this->lang->ai->prompts->common}#{$prompt->id} $prompt->name {$this->lang->hyphen} " . $this->lang->ai->prompts->finalize . " {$this->lang->hyphen} " . $this->lang->ai->prompts->common;
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Execute prompt on obejct, and redirect to target form page.
|
|
*
|
|
* @param int $promptId
|
|
* @param int $objectId
|
|
* @param bool $auto Auto open target form and apply changes.
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptExecute($promptId, $objectId, $auto = true)
|
|
{
|
|
$prompt = $this->ai->getPromptByID($promptId);
|
|
if(empty($prompt)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->failReasons['noPrompt'])));
|
|
|
|
$object = $this->ai->getObjectForPromptById($prompt, $objectId);
|
|
if(empty($object)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->failReasons['noObjectData'])));
|
|
|
|
list($objectData, $rawObject) = $object;
|
|
|
|
list($location, $stop) = $this->ai->getTargetFormLocation($prompt, $rawObject);
|
|
if(empty($location)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->failReasons['noTargetForm'])));
|
|
if(!empty($stop)) return header("location: $location", true, 302);
|
|
|
|
/* Execute prompt and catch exceptions. */
|
|
try
|
|
{
|
|
$response = $this->ai->executePrompt($prompt, $object);
|
|
}
|
|
catch (AIResponseException $e)
|
|
{
|
|
$output = array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $e->getMessage()));
|
|
|
|
/* Audition shall quit on such exception. */
|
|
if(isset($_SESSION['auditPrompt']) && time() - $_SESSION['auditPrompt']['time'] < 10 * 60)
|
|
{
|
|
$output['locate'] = $this->inlink('promptAudit', "promptID=$promptId&objectId=$objectId&exit=true");
|
|
}
|
|
return $this->send($output);
|
|
}
|
|
|
|
if(is_int($response)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->executeErrors["$response"]) . (empty($this->ai->errors) ? '' : implode(', ', $this->ai->errors))));
|
|
if(empty($response)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->failReasons['noResponse'])));
|
|
|
|
if(!empty($prompt->targetForm))
|
|
{
|
|
$targetFormPaths = explode('.', $prompt->targetForm);
|
|
$response['targetFormName'] = $this->lang->ai->targetForm[$targetFormPaths[0]][$targetFormPaths[1]];
|
|
$response['dataPropNames'] = $this->lang->ai->dataSource[$prompt->module];
|
|
}
|
|
|
|
$response['objectID'] = $objectId;
|
|
$response['objectType'] = $prompt->module;
|
|
$response['object'] = $objectData;
|
|
$response['formLocation'] = $location;
|
|
$response['promptConfig'] = $prompt;
|
|
$response['promptAudit'] = $this->ai->isClickable($prompt, 'promptaudit');
|
|
|
|
return $this->send(array('result' => 'success', 'callback' => array('name' => 'parent.executeZentaoPrompt', 'params' => array($response, $auto))));
|
|
}
|
|
|
|
/**
|
|
* Reset prompt execution.
|
|
*
|
|
* @param bool $failed Whether the execution failed, triggers error message.
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptExecutionReset($failed = false)
|
|
{
|
|
/* Reset session. */
|
|
unset($_SESSION['aiPrompt']);
|
|
unset($_SESSION['auditPrompt']);
|
|
|
|
if($failed) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $this->lang->ai->execute->failReasons['noTargetForm']), 'locate' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''));
|
|
return $this->send(array('result' => 'success'));
|
|
}
|
|
|
|
/**
|
|
* Audit prompt modal.
|
|
*
|
|
* @param int $promptId
|
|
* @param int $objectId
|
|
* @param bool $exit
|
|
* @access public
|
|
* @return void|int
|
|
*/
|
|
public function promptAudit($promptId, $objectId, $exit = false)
|
|
{
|
|
if(!common::hasPriv('ai', 'designPrompt')) $this->loadModel('common')->deny('ai', 'designPrompt', false);
|
|
|
|
if(!empty($exit))
|
|
{
|
|
unset($_SESSION['auditPrompt']);
|
|
return $this->send(array('result' => 'success', 'load' => $this->inlink('promptview', "promptID=$promptId")));
|
|
}
|
|
|
|
$prompt = $this->ai->getPromptByID($promptId);
|
|
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
|
|
$originalPrompt = clone $prompt;
|
|
|
|
$prompt->role = $data->role;
|
|
$prompt->characterization = $data->characterization;
|
|
$prompt->purpose = $data->purpose;
|
|
$prompt->elaboration = $data->elaboration;
|
|
|
|
$this->ai->updatePrompt($prompt, $originalPrompt);
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
|
|
if(empty($data->backLocation))
|
|
{
|
|
$response = array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true);
|
|
}
|
|
else
|
|
{
|
|
$response = array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('ai', 'promptexecute', "promptId=$promptId&objectId=$objectId"));
|
|
}
|
|
|
|
$this->sendSuccess($response);
|
|
}
|
|
|
|
$objectForPrompt = $this->ai->getObjectForPromptById($prompt, $objectId);
|
|
if(empty($objectForPrompt)) return $this->send(array('result' => 'fail', 'message' => $this->lang->ai->execute->fail));
|
|
|
|
list($objectData, $object) = $objectForPrompt;
|
|
|
|
$this->view->prompt = $prompt;
|
|
$this->view->object = $object;
|
|
$this->view->dataPrompt = $this->ai->serializeDataToPrompt($prompt->module, $prompt->source, $objectData);
|
|
|
|
$this->display();
|
|
}
|
|
|
|
/**
|
|
* Publish prompt, set status to active.
|
|
*
|
|
* @param int $id
|
|
* @param bool $backToTestingLocation
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptPublish($id, $backToTestingLocation = false)
|
|
{
|
|
unset($_SESSION['auditPrompt']);
|
|
$this->ai->togglePromptStatus($id, 'active');
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
if($backToTestingLocation)
|
|
{
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->ai->prompts->action->publishSuccess, 'load' => $this->inlink('promptview', "id=$id")));
|
|
}
|
|
|
|
return $this->send(array('result' => 'success'));
|
|
}
|
|
|
|
/**
|
|
* Unpublish prompt, set status to draft.
|
|
*
|
|
* @param int $id
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function promptUnpublish($id)
|
|
{
|
|
$this->ai->togglePromptStatus($id, 'draft');
|
|
|
|
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
|
|
return $this->send(array('result' => 'success', 'message' => $this->lang->ai->prompts->action->publishSuccess, 'load' => true));
|
|
}
|
|
|
|
/**
|
|
* Get testing location.
|
|
*
|
|
* @param int $promptID
|
|
* @param string $module
|
|
* @param string $targetForm
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function ajaxGetTestingLocation($promptID, $module, $targetForm)
|
|
{
|
|
$prompt = new stdclass();
|
|
$prompt->id = $promptID;
|
|
$prompt->module = $module;
|
|
$prompt->targetForm = $targetForm;
|
|
|
|
$location = $this->ai->getTestingLocation($prompt);
|
|
if($location === false) return $this->send(array('error' => $this->lang->ai->prompts->goingTestingFail));
|
|
|
|
return $this->send(array('data' => $location));
|
|
}
|
|
|
|
/**
|
|
* Return html element for the role template list.
|
|
* This is used in prompt designing step 2.
|
|
* Return html will deserialize in the front end js and replace the original role template list.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function roleTemplates()
|
|
{
|
|
if($_POST)
|
|
{
|
|
$data = fixer::input('post')->get();
|
|
$method = $data->method;
|
|
$result = false;
|
|
$message = '';
|
|
|
|
switch ($method)
|
|
{
|
|
case 'create':
|
|
$result = $this->ai->createRoleTemplate($data->role, $data->characterization);
|
|
$message = $result ? $this->lang->saveSuccess : $this->lang->ai->saveFail;
|
|
break;
|
|
case 'delete':
|
|
$result = $this->ai->deleteRoleTemplate($data->id);
|
|
$message = $result ? $this->lang->ai->prompts->roleDelSuccess : $this->lang->ai->saveFail;
|
|
break;
|
|
case 'edit':
|
|
$result = $this->ai->updateRoleTemplate($data->id, $data->role, $data->characterization);
|
|
$message = $result ? $this->lang->saveSuccess : $this->lang->ai->saveFail;
|
|
break;
|
|
}
|
|
|
|
$roleTemplates = $this->ai->getRoleTemplates();
|
|
$roleTemplatesArray = array_values((array)$roleTemplates);
|
|
|
|
return $this->send(array(
|
|
'result' => $result ? 'success' : 'fail',
|
|
'message' => $message,
|
|
'data' => array(
|
|
'roleTemplates' => $roleTemplatesArray,
|
|
'method' => $method
|
|
)
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Chat with LLMs.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function chat()
|
|
{
|
|
$messages = array();
|
|
|
|
if(!empty($_POST))
|
|
{
|
|
$history = $this->post->history;
|
|
$message = $this->post->message;
|
|
$isRetry = $this->post->retry == 'true';
|
|
|
|
$messages = json_decode($history);
|
|
if(empty($messages)) $messages[] = (object)array('role' => 'system', 'content' => $this->lang->ai->chatSystemMessage);
|
|
|
|
if(!$isRetry) $messages[] = (object)array('role' => 'user', 'content' => $message);
|
|
|
|
if($this->ai->hasModelsAvailable())
|
|
{
|
|
$response = $this->ai->converse(null, $messages);
|
|
if(empty($response))
|
|
{
|
|
$this->view->error = $this->lang->ai->chatNoResponse;
|
|
}
|
|
else
|
|
{
|
|
$messages[] = (object)array('role' => 'assistant', 'content' => is_array($response) ? current($response) : $response);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$this->view->error = $this->lang->ai->models->noModelError;
|
|
}
|
|
}
|
|
|
|
$this->view->title = $this->lang->ai->chat;
|
|
$this->view->messages = $messages;
|
|
$this->display();
|
|
}
|
|
}
|