feat: 更新AI模块的transcriptions方法,增加扩展请求头参数,优化语音识别功能

This commit is contained in:
kuaifan 2025-11-09 04:43:17 +00:00
parent 425d6f9a06
commit 7a6bbfac75
2 changed files with 11 additions and 6 deletions

View File

@ -1395,7 +1395,9 @@ class DialogController extends AbstractController
'prompt' => "将此语音识别为“" . Doo::getLanguages($language) . "”。", 'prompt' => "将此语音识别为“" . Doo::getLanguages($language) . "”。",
]; ];
} }
$result = AI::transcriptions($recordData['file'], $extParams); $result = AI::transcriptions($recordData['file'], $extParams, [
'accept-language' => Request::header('Accept-Language', 'zh')
]);
if (Base::isError($result)) { if (Base::isError($result)) {
return $result; return $result;
} }
@ -1988,7 +1990,9 @@ class DialogController extends AbstractController
} }
WebSocketDialog::checkDialog($msg->dialog_id); WebSocketDialog::checkDialog($msg->dialog_id);
// //
$result = AI::transcriptions(public_path($msgData['path'])); $result = AI::transcriptions(public_path($msgData['path']), [], [
'accept-language' => Request::header('Accept-Language', 'zh')
]);
if (Base::isError($result)) { if (Base::isError($result)) {
return $result; return $result;
} }

View File

@ -140,10 +140,11 @@ class AI
* 通过 openAI 语音转文字 * 通过 openAI 语音转文字
* @param string $filePath 语音文件路径 * @param string $filePath 语音文件路径
* @param array $extParams 扩展参数 * @param array $extParams 扩展参数
* @param array $extHeaders 扩展请求头
* @param bool $noCache 是否禁用缓存 * @param bool $noCache 是否禁用缓存
* @return array * @return array
*/ */
public static function transcriptions($filePath, $extParams = [], $noCache = false) public static function transcriptions($filePath, $extParams = [], $extHeaders = [], $noCache = false)
{ {
if (!file_exists($filePath)) { if (!file_exists($filePath)) {
return Base::retError("语音文件不存在"); return Base::retError("语音文件不存在");
@ -158,14 +159,14 @@ class AI
return Base::retError("请先在 AI 设置中配置 OpenAI 语音模型"); return Base::retError("请先在 AI 设置中配置 OpenAI 语音模型");
} }
$result = Cache::remember($cacheKey, Carbon::now()->addDays(), function () use ($extParams, $filePath, $audioProvider) { $result = Cache::remember($cacheKey, Carbon::now()->addDays(), function () use ($extParams, $extHeaders, $filePath, $audioProvider) {
$post = array_merge($extParams, [ $post = array_merge($extParams, [
'file' => new \CURLFile($filePath), 'file' => new \CURLFile($filePath),
'model' => 'whisper-1', 'model' => 'whisper-1',
]); ]);
$header = [ $header = array_merge($extHeaders, [
'Content-Type' => 'multipart/form-data', 'Content-Type' => 'multipart/form-data',
]; ]);
$ai = new self($post, $header); $ai = new self($post, $header);
$ai->setProvider($audioProvider); $ai->setProvider($audioProvider);