mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-13 03:52:50 +00:00
perf: AI机器人支持多会话
This commit is contained in:
parent
ffe7ebf711
commit
50203fbcb3
@ -3,6 +3,8 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
|
use App\Module\Extranet;
|
||||||
|
use Swoole\Coroutine;
|
||||||
use Cache;
|
use Cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,17 +71,30 @@ class WebSocketDialogSession extends AbstractModel
|
|||||||
if (Cache::has($cacheKey)) {
|
if (Cache::has($cacheKey)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$originalTitle = $dialogMsg->key ?: $dialogMsg->msg['text'] ?: 'Untitled';
|
||||||
|
$title = Base::cutStr($originalTitle, 100);
|
||||||
|
if ($title == '...') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$session = self::whereId($sessionId)->first();
|
$session = self::whereId($sessionId)->first();
|
||||||
if (!$session) {
|
if (!$session) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$title = Base::cutStr($dialogMsg->key ?: $dialogMsg->msg['text'], 100);
|
|
||||||
if (empty($title)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$session->title = $title;
|
$session->title = $title;
|
||||||
$session->save();
|
$session->save();
|
||||||
// todo 通过AI生成标题
|
|
||||||
Cache::forever($cacheKey, true);
|
Cache::forever($cacheKey, true);
|
||||||
|
// 通过AI接口更新对话标题
|
||||||
|
go(function () use ($session, $title, $originalTitle) {
|
||||||
|
Coroutine::sleep(0.1);
|
||||||
|
$res = Extranet::openAIGenerateTitle($originalTitle);
|
||||||
|
if (Base::isError($res)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$newTitle = $res['data'];
|
||||||
|
if ($newTitle && $newTitle != $title) {
|
||||||
|
$session->title = Base::cutStr($newTitle, 100);
|
||||||
|
$session->save();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -108,6 +108,57 @@ class Extranet
|
|||||||
return Base::retSuccess("success", $result);
|
return Base::retSuccess("success", $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过 openAI 生成标题
|
||||||
|
* @param $text
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function openAIGenerateTitle($text)
|
||||||
|
{
|
||||||
|
$aibotSetting = Base::setting('aibotSetting');
|
||||||
|
if (empty($aibotSetting['openai_key'])) {
|
||||||
|
return Base::retError("AI接口未配置");
|
||||||
|
}
|
||||||
|
$extra = [
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
'Authorization' => 'Bearer ' . $aibotSetting['openai_key'],
|
||||||
|
];
|
||||||
|
if ($aibotSetting['openai_agency']) {
|
||||||
|
$extra['CURLOPT_PROXY'] = $aibotSetting['openai_agency'];
|
||||||
|
if (str_contains($aibotSetting['openai_agency'], 'socks')) {
|
||||||
|
$extra['CURLOPT_PROXYTYPE'] = CURLPROXY_SOCKS5;
|
||||||
|
} else {
|
||||||
|
$extra['CURLOPT_PROXYTYPE'] = CURLPROXY_HTTP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$res = Ihttp::ihttp_request('https://api.openai.com/v1/chat/completions', json_encode([
|
||||||
|
"model" => "gpt-3.5-turbo",
|
||||||
|
"messages" => [
|
||||||
|
[
|
||||||
|
"role" => "system",
|
||||||
|
"content" => "你是一个专业的标题生成器,擅长为对话生成简洁的标题,请将提供的文本生成一个标题。"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"role" => "user",
|
||||||
|
"content" => $text
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]), $extra, 15);
|
||||||
|
if (Base::isError($res)) {
|
||||||
|
return Base::retError("生成失败", $res);
|
||||||
|
}
|
||||||
|
$resData = Base::json2array($res['data']);
|
||||||
|
if (empty($resData['choices'])) {
|
||||||
|
return Base::retError("生成失败", $resData);
|
||||||
|
}
|
||||||
|
$result = $resData['choices'][0]['message']['content'];
|
||||||
|
$result = preg_replace('/^\"|\"$/', '', $result);
|
||||||
|
if (empty($result)) {
|
||||||
|
return Base::retError("生成失败", $result);
|
||||||
|
}
|
||||||
|
return Base::retSuccess("success", $result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取IP地址经纬度
|
* 获取IP地址经纬度
|
||||||
* @param string $ip
|
* @param string $ip
|
||||||
|
|||||||
@ -29,9 +29,7 @@
|
|||||||
> li {
|
> li {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
margin: 6px;
|
margin: 0 6px;
|
||||||
border-top-right-radius: 6px;
|
|
||||||
border-top-left-radius: 6px;
|
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|
||||||
&::after {
|
&::after {
|
||||||
@ -80,6 +78,7 @@
|
|||||||
.history-time {
|
.history-time {
|
||||||
float: right;
|
float: right;
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user