Files
fast_response/digital_doctor/application/api/logic/TtchatLogic.php
2025-03-17 10:56:09 +08:00

98 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* description
* authorwh
* 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
*
* authorwh
*/
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);
}
}