作者 lyh

gx

@@ -59,7 +59,13 @@ class Common @@ -59,7 +59,13 @@ class Common
59 if($info === false){ 59 if($info === false){
60 response('指令不存在',400); 60 response('指令不存在',400);
61 } 61 }
62 - $lang = self::detectLanguage($param['keywords']); 62 + //带原语种翻译
  63 + $result = Translate::translateSl($param['keywords'])->json();
  64 + if (FALSE == empty($result['texts']['sl']) && FALSE == empty(Translate::$tls_list[$result['texts']['sl']])) {
  65 + $lang = Translate::$tls_list[$result['texts']['sl']]['lang_en'];
  66 + } else {
  67 + $lang = '中文';
  68 + }
63 $str = ',请使用'.$lang.'回答'; 69 $str = ',请使用'.$lang.'回答';
64 //替换关键字 70 //替换关键字
65 $content = str_replace('$keyword$', $param['keywords'], $info['ai']); 71 $content = str_replace('$keyword$', $param['keywords'], $info['ai']);
@@ -745,6 +745,24 @@ class Translate @@ -745,6 +745,24 @@ class Translate
745 return $retsult[0]['texts'] ?? ''; 745 return $retsult[0]['texts'] ?? '';
746 746
747 } 747 }
748 - 748 + /**
  749 + * 翻译带源语种
  750 + * @param $message
  751 + * @param string $tl
  752 + * @return \Illuminate\Http\Client\Response
  753 + */
  754 + public static function translateSl($message, $tl = 'en')
  755 + {
  756 + $action = 'auto_sl';
  757 + if (is_string($message)) {
  758 + $message = [$message];
  759 + }
  760 + $data = [
  761 + 'texts' => $message,
  762 + 'sl' => 'auto',
  763 + 'tl' => $tl,
  764 + ];
  765 + return Http::post(self::$url . $action, $data);
  766 + }
749 767
750 } 768 }
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside\Ai; @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside\Ai;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Helper\Common; 6 use App\Helper\Common;
  7 +use App\Helper\Translate;
7 use App\Http\Controllers\Bside\BaseController; 8 use App\Http\Controllers\Bside\BaseController;
8 use App\Http\Controllers\Bside\:写入日志; 9 use App\Http\Controllers\Bside\:写入日志;
9 use App\Models\Ai\AiLog; 10 use App\Models\Ai\AiLog;
@@ -53,5 +54,35 @@ class AiCommandController extends BaseController @@ -53,5 +54,35 @@ class AiCommandController extends BaseController
53 return $aiLog->add($param); 54 return $aiLog->add($param);
54 } 55 }
55 56
  57 + /**
  58 + * 处理指令中的参数
  59 + * 语种
  60 + * @param $prompt
  61 + * @param $model_config
  62 + * @param $message
  63 + * @return string|string[]
  64 + */
  65 + public static function promptParamDeal($prompt, $model_config, $message)
  66 + {
  67 + if (FALSE === strpos($prompt, '{lang}')) {
  68 + return $prompt;
  69 + }
  70 + // 没有设置或者没有获取到正确的语种信息 则通过接口获取回复语种
  71 + if (empty($model_config['lang']) || empty(Translate::$tls_list[$model_config['lang']])) {
  72 + $string = is_array($message) ? end($message) : $message;
  73 + $result = Translate::translateSl($string)->json();
  74 + if (FALSE == empty($result['texts']['sl']) && FALSE == empty(Translate::$tls_list[$result['texts']['sl']])) {
  75 + $lang = 'use ' . Translate::$tls_list[$result['texts']['sl']]['lang_en'];
  76 + } else {
  77 + $lang = '';
  78 + }
56 79
  80 + } else {
  81 + $lang = Translate::$tls_list[$model_config['lang']]['lang_en'];
  82 + $lang = 'use ' . $lang;
  83 + }
  84 + // 指令中有需要替换语种的地方
  85 + $prompt = str_replace('{lang}', $lang, $prompt);
  86 + return $prompt;
  87 + }
57 } 88 }