代码提交优化
This commit is contained in:
@@ -40,6 +40,7 @@ class AiSettingsController extends BaseController
|
||||
|
||||
// 确保智能体已创建
|
||||
if (empty($settings->botId)) {
|
||||
$settings->releaseTime = 0;
|
||||
$botCreated = $this->createBot($settings);
|
||||
if (!$botCreated) {
|
||||
return ResponseHelper::error('智能体创建失败');
|
||||
@@ -48,12 +49,13 @@ class AiSettingsController extends BaseController
|
||||
|
||||
// 确保知识库已创建
|
||||
if (empty($settings->datasetId)) {
|
||||
$settings->releaseTime = 0;
|
||||
$knowledgeCreated = $this->createKnowledge($settings);
|
||||
if (!$knowledgeCreated) {
|
||||
return ResponseHelper::error('知识库创建失败');
|
||||
}
|
||||
}
|
||||
if (!empty($settings->botId) && !empty($settings->datasetId)) {
|
||||
if (!empty($settings->botId) && !empty($settings->datasetId) && $settings->releaseTime <= 0) {
|
||||
$cozeAI = new CozeAI();
|
||||
$config = json_decode($settings->config,true);
|
||||
$config['bot_id'] = $settings->botId;
|
||||
@@ -133,7 +135,8 @@ class AiSettingsController extends BaseController
|
||||
## 限制
|
||||
- 仅依据知识库内容回答问题,对于知识库中没有的信息,如实告知用户无法回答。
|
||||
- 回答必须严格遵循中国法律法规,不得出现任何违法违规内容。
|
||||
- 回答需简洁明了,避免冗长复杂的表述。';
|
||||
- 回答需简洁明了,避免冗长复杂的表述(尽量在100字内)。
|
||||
- 适当加些表情点缀。';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,4 +344,90 @@ class AiSettingsController extends BaseController
|
||||
$settings->save();
|
||||
return ResponseHelper::success('', '发布成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存统一提示词
|
||||
* 先更新数据库,再调用CozeAI接口更新智能体
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function savePrompt()
|
||||
{
|
||||
try {
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
if (empty($companyId)) {
|
||||
return ResponseHelper::error('公司信息获取失败');
|
||||
}
|
||||
|
||||
// 获取提示词参数
|
||||
$promptInfo = $this->request->param('promptInfo', '');
|
||||
if (empty($promptInfo)) {
|
||||
return ResponseHelper::error('提示词内容不能为空');
|
||||
}
|
||||
|
||||
// 查找AI设置
|
||||
$settings = AiSettingsModel::where(['companyId' => $companyId])->find();
|
||||
if (empty($settings)) {
|
||||
return ResponseHelper::error('AI设置不存在,请先初始化');
|
||||
}
|
||||
|
||||
// 检查智能体是否已创建
|
||||
if (empty($settings->botId)) {
|
||||
return ResponseHelper::error('智能体未创建,请先初始化AI设置');
|
||||
}
|
||||
|
||||
// 解析现有配置
|
||||
$config = json_decode($settings->config, true);
|
||||
if (!is_array($config)) {
|
||||
$config = [];
|
||||
}
|
||||
|
||||
// 更新提示词
|
||||
$config['prompt_info'] = $promptInfo;
|
||||
|
||||
// 第一步:更新数据库
|
||||
$settings->config = json_encode($config, JSON_UNESCAPED_UNICODE);
|
||||
$settings->isRelease = 0; // 标记为未发布状态
|
||||
$settings->updateTime = time();
|
||||
|
||||
if (!$settings->save()) {
|
||||
return ResponseHelper::error('数据库更新失败');
|
||||
}
|
||||
|
||||
// 第二步:调用CozeAI接口更新智能体
|
||||
try {
|
||||
$cozeAI = new CozeAI();
|
||||
|
||||
// 参考 init 方法的参数格式,传递完整的 config
|
||||
$updateData = $config;
|
||||
$updateData['bot_id'] = $settings->botId;
|
||||
|
||||
// 如果有知识库,也一并传入
|
||||
if (!empty($settings->datasetId)) {
|
||||
$updateData['dataset_ids'] = [$settings->datasetId];
|
||||
}
|
||||
|
||||
$result = $cozeAI->updateBot($updateData);
|
||||
$result = json_decode($result, true);
|
||||
|
||||
if ($result['code'] != 200) {
|
||||
\think\facade\Log::error('更新智能体提示词失败:' . json_encode($result));
|
||||
return ResponseHelper::error('更新智能体失败:' . ($result['msg'] ?? '未知错误'));
|
||||
}
|
||||
|
||||
return ResponseHelper::success([
|
||||
'prompt_info' => $promptInfo,
|
||||
'isRelease' => 0
|
||||
], '提示词保存成功,请重新发布智能体');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('调用CozeAI更新接口异常:' . $e->getMessage());
|
||||
return ResponseHelper::error('更新智能体接口调用失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\think\facade\Log::error('保存提示词异常:' . $e->getMessage());
|
||||
return ResponseHelper::error('系统异常:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user