98 lines
2.4 KiB
PHP
98 lines
2.4 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/7/15} {16:14}
|
||
*/
|
||
|
||
namespace app\api\logic;
|
||
|
||
|
||
use app\api\controller\BaseWssApi;
|
||
use GatewayWorker\Lib\Gateway;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\gpt\chat\ChatGPT;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
|
||
/**
|
||
* 听译聊天
|
||
* Class TychatLogic
|
||
* @package app\api\logic
|
||
*/
|
||
class TtchatLogic extends BaseLogic
|
||
{
|
||
|
||
/**
|
||
* desc:保存听译聊天记录
|
||
*
|
||
* 注:
|
||
* 在websocket 的onMessage中send消息,消息格式如下:
|
||
*
|
||
* json参数:
|
||
* [
|
||
'action'=>'TychatLogic/saveChatHistory',
|
||
'items'=>[
|
||
'content'=>'聊天内容',
|
||
'username'=>'用户名',//医生说话就传医生的username,病人说话就传病人的username
|
||
]
|
||
];
|
||
*/
|
||
function saveChatHistory($chat_content){
|
||
$data = [
|
||
'chat_msg'=>$chat_content,
|
||
];
|
||
Db::table('fa_tt_chathistory')->insert($data);
|
||
|
||
|
||
return Tools::set_ok();
|
||
}
|
||
|
||
/**
|
||
* desc:健康洞察
|
||
*
|
||
* api/HealthInsights/getHealthInsight
|
||
*
|
||
* author:wh
|
||
*/
|
||
function getHealthInsight(){
|
||
$config = config('ai_health_insight_config');
|
||
|
||
$question = '';//input('question','');
|
||
|
||
$chatobj = new ChatGPT();
|
||
$chatobj->url = $config['base_url'];
|
||
$chatobj->model = '';
|
||
$chatobj->apiKey = $config['APIKey'];
|
||
|
||
$answer_json_arr = [];
|
||
|
||
$ticket = input('ticket');
|
||
if(empty($ticket)){
|
||
return json(Tools::set_fail('ticket必须'));
|
||
}
|
||
$user = Db::table('fa_hdrdoctorusers')->where('ticket',$ticket)->find();
|
||
if(empty($user)){
|
||
return json(Tools::set_fail('用户不存在'));
|
||
}
|
||
$day3 = date('Y-m-d 00:00:00',strtotime('-3 day'));
|
||
$his_record = Db::table('fa_ty_chathistory')
|
||
//->where('username',$user['username'])
|
||
->order('id asc')
|
||
->where('createtime','>',$day3)//3天之内
|
||
->select();
|
||
|
||
$config = [
|
||
'stream'=>false,
|
||
];
|
||
$content = [
|
||
//["role" => "user", "content" => '']
|
||
];
|
||
foreach ($his_record as $item){
|
||
$content[] = ["role" => "user", "content" => $item['chat_msg']];
|
||
}
|
||
$chatobj->setBefore($content);
|
||
|
||
$chatobj->chat($question,$config,$answer_json_arr);
|
||
}
|
||
} |