Index.php
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
declare (strict_types = 1);
namespace app\chat\controller;
use app\chat\controller\Base;
use think\facade\Db;
use app\base\model\BaseModel;
use think\facade\View;
use think\facade\Cookie;
class Index extends Base
{
public function index(){
$verify = Cookie::get('verify',0);
$agent = site_info();
View::assign('verify',$verify);
View::assign('agent',$agent);
return View::fetch('chat@index/index');
}
public function getGroupChatMsg(){
$group_id = $this->req->param('group_id');
$uip = $this->req->ip();
$where = [];
$where[] = ['uip','=',$uip];
$where[] = ['group_id','=',$group_id];
$msgList = Db::table('kt_chat_msg')->field('id,message,response')->where($where)->order('id asc')->select();
$msgs = [];
foreach ($msgList as $key => $msg) {
$msgs[] = [
'role' => '我',
'content' => $msg['message']
];
$msgs[] = [
'role' => '助手',
'content' => $msg['response']
];
}
return success('获取成功',$msgs);
}
public function getGroupList(){
$page = $this->req->param('page',1);
$size = $this->req->param('size',10);
$name = $this->req->param('name');
$uip = $this->req->ip();
$where = [];
$where[] = ['uip','=',$uip];
if($name) $where[] = ['name','like',"%{$name}%"];
$res = Db::table('kt_chat_msg_group')->where($where);
$data['count'] = $res->count();
$data['item'] = $res->field('id,name')->order('id desc')->page($page, $size)->select()->toArray();
if(!$name && empty($data['item'])){
$name = '新的会话';
$group_id = Db::table('kt_chat_msg_group')->insertGetId([
'uip' => $uip,
'name' => $name,
'c_time' => time()
]);
$data['item'] = [[
'id' => $group_id,
'name' => $name
]];
$data['count'] = 1;
}
return success('分组数据',$data);
}
public function addGroup(){
$uip = $this->req->ip();
$name = $this->req->param('name');
if(!$name) return error('会话标题不能为空');
$id = Db::table('kt_chat_msg_group')->insertGetId([
'uip' => $uip,
'name' => $name,
'c_time' => time()
]);
return success('添加成功',$id);
}
public function editGroup(){
$id = $this->req->param('id');
if(!$id) return error('缺少必要参数');
$name = $this->req->param('name');
if(!$name) return error('会话标题不能为空');
Db::table('kt_chat_msg_group')->where('id',$id)->update([
'name' => $name
]);
return success('更新成功');
}
public function verifyPwd(){
$pwd = $this->req->param('pwd');
if($pwd != 'bronet') return error('密码错误');
Cookie::forever('verify',1);
return success('登录成功');
}
public function sendtext(){
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no');
$message = $this->req->param('message');
$group_id = $this->req->param('group_id');
$uip = $this->req->ip();
if(!$message){
echo 'data:[error]请输入您的问题\n\n';ob_flush();flush();exit;
}
$response = ''; // 返回的文字
// 连续对话需要带着上一个问题请求接口
$now = time();
$lastMsg = Db::table('kt_chat_msg')->where([
['uip', '=', $uip],
['group_id', '=', $group_id],
['c_time', '>', ($now - 300)]
])->order('id desc')->find();
// 如果超长,就不关联上下文了
if ($lastMsg && (mb_strlen($lastMsg['message']) + mb_strlen($lastMsg['response']) + mb_strlen($message) < 3800)) {
$messages[] = [
'role' => 'user',
'content' => $lastMsg['message']
];
$messages[] = [
'role' => 'assistant',
'content' => $lastMsg['response']
];
}
$messages[] = [
'role'=>'user',
'content'=>$message
];
$uid = BaseModel::getUid();
$config = Db::table('kt_base_gpt_config')->json(['openai','api2d'])->where('uid',$uid)->find();
if($config){
if($config['channel'] == 2){
$aiconfig = $config['api2d'];
$ktadmin = new \Ktadmin\Chatgpt\Ktadmin(['channel'=>2,'api_key'=>$aiconfig['forward_key'],'diy_host'=>'']);
}else{
$aiconfig = $config['openai'];
$ktadmin = new \Ktadmin\Chatgpt\Ktadmin(['channel'=>1,'api_key'=>$aiconfig['api_key'],'diy_host'=>$aiconfig['diy_host']]);
}
}else{
echo "data:[error]未检查到配置信息\n\n";ob_flush();flush();exit;
}
$ktadmin->chat()->sendText($messages, function($ch, $data) use($ktadmin,$message,$uip,$group_id) {
global $response;
$complete = @json_decode($data);
if(isset($complete->error)){
echo 'data:[error]'.$complete->error->message."\n\n";ob_flush();flush();exit;
}elseif(@$complete->object == 'error'){
echo 'data:[error]'.$complete->message."\n\n";ob_flush();flush();exit;
}
$word = $ktadmin->chat()->parseData($data);
$word = str_replace("\n", '<br/>', $word);
if($complete){//一次性完整输出
if (!empty($word)) {
Db::table('kt_chat_msg')->insert([
'uip' => $uip,
'group_id' => $group_id,
'message' => $message,
'response' => $word,
'c_time' => time()
]);
echo "data:".$word."\n\n";
}
ob_flush();flush();
}else{//流式
if($word == 'data: [DONE]' || $word == 'data: [CONTINUE]'){
if (!empty($response)) {
Db::table('kt_chat_msg')->insert([
'uip' => $uip,
'group_id' => $group_id,
'message' => $message,
'response' => $response,
'c_time' => time()
]);
$response = '';
}
ob_flush();flush();
}else{
$response .= $word;
echo "data:".$word."\n\n";ob_flush();flush();
}
}
return strlen($data);
},['temperature'=>$aiconfig['temperature'],'max_tokens'=>$aiconfig['max_tokens'],'model'=>$aiconfig['model'],'stream'=>$aiconfig['stream']]);
exit();
}
}