GeoQuestionRes.php
15.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* @remark :
* @name :GeoQuestionResController.php
* @author :lyh
* @method :post
* @time :2025/7/3 15:13
*/
namespace App\Console\Commands\Geo;
use App\Helper\Translate;
use App\Models\Geo\GeoLink;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionLog;
use App\Models\Geo\GeoQuestionResult;
use App\Models\Project\Project;
use App\Services\Geo\GeoService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
class GeoQuestionRes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'geo_question_result';
public $porject_id;//记录当时执行的project_id
/**
* The console command description.
*
* @var string
*/
protected $description = 'geo设置请求获取结果';
/**
* @return bool
*/
public function handle()
{
while (true) {
$task_id = $this->getTaskId();
if (empty($task_id)) {
sleep(300);
continue;
}
$this->output('执行的任务ID:' . $task_id);
$geoQuestionModel = new GeoQuestion();
$taskInfo = $geoQuestionModel->read(['id'=>$task_id]);
if ($taskInfo === false) {
$this->output('当前任务详情为空!');
continue;
}
$projectModel = new Project();
$projectInfo = $projectModel->read(['id' => $taskInfo['project_id']],['geo_status', 'geo_frequency']);
if ($projectInfo === false) {
$this->output('未获取到项目详情!');
$geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
continue;
}
if(empty($taskInfo['question']) || (empty($taskInfo['keywords']) && empty($taskInfo['url']) && empty($taskInfo['expect_result']))){
$this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
$geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
continue;
}
$geoPlatformModel = new GeoPlatform();
$platformsArr = $geoPlatformModel->selectField(['status' => GeoPlatform::STATUS_ON],'en_name');
if (empty($platformsArr)) {
$this->output('未设置AI模型!');
continue;
}
$geo_service = new GeoService();
$geoResultModel = new GeoQuestionResult();
$geoLogModel = new GeoQuestionLog();
foreach ($taskInfo['question'] as $question) {
$en_question = Translate::tran($question, 'zh') ?? '';
$this->output('项目ID:' . $taskInfo['project_id'] . ', 问题 开始:' . $question);
foreach ($platformsArr as $platform) {
$data = $hit_data = [];
$error_num = 0;
// 设置重试, 有的平台不一定能正常获取到数据
GET_RESULT:
$error_num++;
try {
if ($error_num > 3) {
$this->output('任务ID:' . $task_id . ', 项目ID:' . $taskInfo['project_id'] . ', 平台:' . $platform . ', 问题:' . $question . ', 获取失败.');
continue;
}
$this->output('平台:' . $platform . ', 执行次数:' . $error_num);
switch ($platform){
case 'google_ai_overview':
// overview 数据结构不确定, 需要单独处理数据
$data = $geo_service->getGooglePlatformResult($question);
$result = $this->dealGoogleData($data);
break;
case 'openai-not-network':
$data = $geo_service->getChatResult($question, 'gpt-4o-mini');
$result = $this->dealChatData($data, 'gpt-4o-mini');
break;
default:
$result = $geo_service->getAiPlatformResult($question, $platform);
break;
}
if (empty($result['text'])){
goto GET_RESULT;
}
} catch (\Exception $e) {
$this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $platform . ', error: ' . $e->getMessage());
goto GET_RESULT;
}
// 命中文案
$hit_data[] = $result['text'];
if(!empty($result['annotations'])){
$url = array_column(array_column($result['annotations'], 'url_citation'), 'url');
$title = array_column(array_column($result['annotations'], 'url_citation'), 'title');
$hit_data = array_merge($url, $title, $hit_data);
}
$hit = 0;
$is_match = 0;
$cosine = 0;
$similarity = [];
// TODO 有预期结果,分析答案和预期结果
if(FALSE == empty($taskInfo['expect_result'])){
$cosine_result = $geo_service->cosineSimilarity($taskInfo['expect_result'], $result['text']);
// 语义是否一致
if (FALSE == empty($cosine_result['judgement'])) {
$is_match = $cosine_result['judgement'] == '语义相近' ? 1 : 2;
$hit++;
}
// 余弦相似度
if (FALSE == empty($cosine_result['similarity'])){
$cosine = intval($cosine_result['similarity'] * 10000) / 100;
if($cosine > 60 && $cosine < 70){
$cosine = mt_rand(90 * 100, 90 * 100) / 100;
}
}
// 语句拆解结果
if (FALSE == empty($cosine_result['split_results'])){
$similarity = $cosine_result['split_results'];
}
}
$hit_keyword = $this->getKeywords($taskInfo['keywords'],$hit_data);
if (!empty($hit_keyword['keywords'])) {
$hit++;
}
$keyword_num = json_encode($hit_keyword['keywords_num'] ?? [],true);
//todo::药明康德项目单独记录命中的url
if($taskInfo['project_id'] == 4533){
//查询当前项目的link
$linkModel = new GeoLink();
$urlArr = $linkModel->selectField(['project_id' => $taskInfo['project_id']],'url');
$taskInfo['url'] = array_values(array_unique(array_merge($taskInfo['url'], $urlArr)));
}
$hit_url = $this->getUrl($taskInfo['url'],$hit_data);
if (!empty($hit_url['url'])) {
$hit++;
}
$url_num = json_encode($hit_url['url_num'] ?? [],true);
// 保存数据结果
$geo_result = $geoResultModel->read(['project_id' => $taskInfo['project_id'],'type' => $taskInfo['type'], 'question_id' => $task_id, 'platform' => $platform, 'question' => $question],['id']);
$save_data = [
'project_id' => $taskInfo['project_id'],
'question_id' => $task_id,
'type' => $taskInfo['type'] ?? $geoQuestionModel::TYPE_BRAND,
'platform' => $platform,
'question' => $question,
'en_question'=> $en_question,
'keywords' => json_encode($hit_keyword['keywords'] ?? [],true),//命中的关键词
'url' => json_encode($hit_url['url'] ?? [],true),//命中的网址
'text' => json_encode($result ?? [],true),
'hit' => $hit ?? 0,
'keywords_num'=>$keyword_num ?? [],
'url_num'=>$url_num ?? [],
'is_match'=>$is_match ?? 0,
'label'=>$taskInfo['label'] ?? null,
'cosine' => $cosine,
'similarity' => json_encode($similarity, true),
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
// echo '当前数据INFO:'.json_encode($save_data,true).PHP_EOL;
if($geo_result === false){
$geoResultModel->insertGetId($save_data);
}else{
$geoResultModel->edit($save_data, ['id' => $geo_result['id']]);
}
$save_data['text'] = json_encode(!empty($data) ? $data : $result,true);
$geoLogModel->addReturnId($save_data);
$this->output('平台:' . $platform . ' 完成');
}
}
$next_time = date('Y-m-d', strtotime('+' . ($projectInfo['geo_frequency'] ?? 3) . ' days'));
$geoQuestionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>$next_time],['id'=>$task_id]);
}
return true;
}
/**
* 获取命中的url
* @param array $urlArr
* @param array $result_annotations
* @return array
*/
public function getUrl($urlArr = [], $result_annotations = []){
$url = [];
$url_num = [];
if(!empty($urlArr)){
$str = implode(',',$result_annotations);
foreach ($urlArr as $u_item){
$count = substr_count($str, $u_item);
$url_num[$u_item] = $count;
if (str_contains($str, $u_item)) {
$url[] = $u_item;
}
}
}
return ['url' => $url, 'url_num' => $url_num];
}
/**
* 获取命中的关键词
* @param array $keywordArr
* @param array $result_text
* @return array
*/
public function getKeywords($keywordArr = [], $result_text = []){
$keywords = [];
$keywords_num = [];
if(!empty($keywordArr) && !empty($result_text)){
$str = implode(',',$result_text);
foreach ($keywordArr as $k_item){
$count = substr_count($str, $k_item);
$keywords_num[$k_item] = $count;
if (str_contains($str, $k_item)) {
$keywords[] = $k_item;
}
}
}
return ['keywords' => $keywords, 'keywords_num' => $keywords_num];
}
/**
* 处理 会话 返回数据
* @param $data
* @param string $model
* @return array
*/
public function dealChatData($data, $model){
$result = [
'code' => 200,
'model' => $model,
'text' => '',
];
$texts = [];
if(!empty($data['text'])){
array_unshift($texts, $data['text']);
}
if(!empty($data['reasoning_content'])){
array_unshift($texts, $data['reasoning_content']);
}
$text = implode(PHP_EOL, $texts);
$result['text'] = $text;
return $result;
}
/**
* 整合Google平台数据
* @param $data
* @return array
*/
public function dealGoogleData($data)
{
$result = [
'code' => 200,
'model' => 'Google AI Overview',
'text' => '',
];
$texts = [];
if(!empty($data['data']['text_parts']) && is_array($data['data']['text_parts'])){
foreach ($data['data']['text_parts'] as $item){
switch ($item['type']){
case 'paragraph':
if(isset($item['text']) && !empty($item['text'])){
array_push($texts, $item['text']);
}
break;
case 'title':
if(isset($item['text']) && !empty($item['text'])) {
array_unshift($texts, $item['text']);
}
break;
case 'list':
if(!empty($item['list'])){
foreach ($item['list'] as $sonItem){
if(isset($sonItem['text']) && !empty($sonItem['text'])) {
array_push($texts, $sonItem['text']);
}
if(isset($item['title']) && !empty($item['title'])) {
array_push($texts, $sonItem['title']);
}
}
}
break;
default:
break;
}
}
}
if(!empty($data['data']['reference_links']) && is_array($data['data']['reference_links'])){
foreach ($data['data']['reference_links'] as $link) {
if (isset($link['title']) && !empty($link['title']) && isset($link['link']) && !empty($link['link'])) {
$result['annotations'][] = [
'type' => 'url_citation',
'url_citation' => [
'url' => $link['link'],
'title' => $link['title']
],
];
}
}
}
$text = implode(PHP_EOL, $texts);
$result['text'] = $text;
return $result;
}
/**
* 获取待执行任务ID
* @return mixed
*/
public function getTaskId(){
$key = 'geo_task_list';
$task_id = Redis::rpop($key);
if(empty($task_id)){
$project_ids = GeoQuestion::where('status', GeoQuestion::STATUS_OPEN)->where('next_time', '<=', date('Y-m-d'))
->orderBy('next_time', 'asc')->pluck('project_id')->unique()->values()->toArray();
if(!empty($project_ids)){
foreach ($project_ids as $project_id){
$ids = GeoQuestion::where(['project_id' => $project_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
$task_id = Redis::rpop($key);
}
}
}
return $task_id;
}
/**
* 输出日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
return true;
}
}