Skip to content

Commit

Permalink
优化错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Apr 6, 2024
1 parent 5c76f38 commit 8bdfc4d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions server/Module/Chat/Service/ChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace app\Module\Chat\Service;

use app\Exception\ErrorException;
use app\Exception\NotFoundException;
use app\Module\Admin\Enum\OperationLogObject;
use app\Module\Admin\Enum\OperationLogStatus;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function chatStream(string $id, int $memberId, string $ip): \Iterator
$record = goWait(fn () => $this->getById($id, $memberId), 30, true);
if (QAStatus::ANSWER !== $record->qaStatus)
{
throw new \RuntimeException('AI 已回答完毕');
throw new ErrorException('AI 已回答完毕,请刷新界面');
}
$params = [];
foreach (self::ALLOW_PARAMS as $name)
Expand All @@ -111,7 +112,7 @@ public function chatStream(string $id, int $memberId, string $ip): \Iterator
$modelConfig = $config->getModelConfig($params['model']);
if (!$modelConfig || !$modelConfig->enable)
{
throw new \RuntimeException('不允许使用模型:' . $params['model']);
throw new ErrorException('不允许使用模型:' . $params['model']);
}
$model = $params['model'];
$client = OpenAIUtil::makeClient($model);
Expand Down Expand Up @@ -144,6 +145,10 @@ public function chatStream(string $id, int $memberId, string $ip): \Iterator
}
$messages[] = ['role' => $message->role, 'content' => $message->message];
}
if ($historyMessages && !$messages)
{
throw new ErrorException(sprintf('输入内容不能超过 %d Tokens', $modelMaxTokens));
}
unset($historyMessages); // 释放空间
// 保存消息记录
Coroutine::create(function () use (&$saveMessages) {
Expand All @@ -170,7 +175,7 @@ public function chatStream(string $id, int $memberId, string $ip): \Iterator
$messages = array_reverse($messages);
if (!$messages)
{
throw new \RuntimeException('没有消息');
throw new ErrorException('没有消息');
}
// 每条消息额外的Tokens + 每次消息之后额外的Tokens
$beginTime = time();
Expand Down

0 comments on commit 8bdfc4d

Please sign in to comment.