作者 Richer

功能优化

@@ -41,7 +41,7 @@ class ChatRecordItemJob extends BaseJob @@ -41,7 +41,7 @@ class ChatRecordItemJob extends BaseJob
41 $this->desc = '同步用户聊天明细记录'; 41 $this->desc = '同步用户聊天明细记录';
42 $this->channel = 'sync'; 42 $this->channel = 'sync';
43 // 执行 43 // 执行
44 - parent::__construct($model); 44 + parent::__construct($model, 1);
45 } 45 }
46 46
47 /** 47 /**
@@ -53,10 +53,11 @@ class ChatRecordItemJob extends BaseJob @@ -53,10 +53,11 @@ class ChatRecordItemJob extends BaseJob
53 { 53 {
54 record_log($this->channel, $this->desc, 'begin'); 54 record_log($this->channel, $this->desc, 'begin');
55 55
56 - if ($this->attempts() > 3) { 56 + if ($this->attempts() > 1) {
57 record_log($this->channel, '尝试次数过多'); 57 record_log($this->channel, '尝试次数过多');
58 } else { 58 } else {
59 // 同步增加记录 59 // 同步增加记录
  60 + record_log($this->channel, '同步数据:' . json_encode($this->model));
60 $this->syncRecordItem($this->model); 61 $this->syncRecordItem($this->model);
61 } 62 }
62 record_log($this->channel, $this->desc, 'end'); 63 record_log($this->channel, $this->desc, 'end');
@@ -53,9 +53,10 @@ class ChatRecordJob extends BaseJob @@ -53,9 +53,10 @@ class ChatRecordJob extends BaseJob
53 { 53 {
54 record_log($this->channel, $this->desc, 'begin'); 54 record_log($this->channel, $this->desc, 'begin');
55 55
56 - if ($this->attempts() > 3) { 56 + if ($this->attempts() > 1) {
57 record_log($this->channel, '尝试次数过多'); 57 record_log($this->channel, '尝试次数过多');
58 } else { 58 } else {
  59 + record_log($this->channel, '同步数据:' . json_encode($this->model));
59 // 同步增加记录 60 // 同步增加记录
60 $this->syncRecord($this->model); 61 $this->syncRecord($this->model);
61 } 62 }
@@ -308,11 +308,13 @@ trait ChatTrait @@ -308,11 +308,13 @@ trait ChatTrait
308 foreach ($data as $message) { 308 foreach ($data as $message) {
309 if ('[DONE]' === $message) { 309 if ('[DONE]' === $message) {
310 echo "0\r\n\r\n"; 310 echo "0\r\n\r\n";
  311 +// echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
311 } else { 312 } else {
312 $message = json_decode($message, true); 313 $message = json_decode($message, true);
313 $content = $message['choices'][0]['delta']['content'] ?? ''; 314 $content = $message['choices'][0]['delta']['content'] ?? '';
314 $answer .= $content; 315 $answer .= $content;
315 -// record_log('openai', '内容: ' . $content ); 316 + record_log('openai', '内容: ' . $content );
  317 + record_log('openai', '内容urlencode: ' . urlencode($content));
316 echo urlencode($content) . "\r\n"; 318 echo urlencode($content) . "\r\n";
317 } 319 }
318 } 320 }
@@ -338,7 +340,7 @@ trait ChatTrait @@ -338,7 +340,7 @@ trait ChatTrait
338 } 340 }
339 foreach ($data as $message) { 341 foreach ($data as $message) {
340 if ('[DONE]' === $message) { 342 if ('[DONE]' === $message) {
341 - echo "0\r\n\r\n"; 343 + echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
342 } else { 344 } else {
343 $message = json_decode($message, true); 345 $message = json_decode($message, true);
344 $content = $message['choices'][0]['delta']['content'] ?? ''; 346 $content = $message['choices'][0]['delta']['content'] ?? '';
@@ -33,6 +33,7 @@ use Illuminate\Support\Arr; @@ -33,6 +33,7 @@ use Illuminate\Support\Arr;
33 use Illuminate\Support\Collection; 33 use Illuminate\Support\Collection;
34 use Illuminate\Support\Facades\Cache; 34 use Illuminate\Support\Facades\Cache;
35 use Illuminate\Support\Facades\DB; 35 use Illuminate\Support\Facades\DB;
  36 +use Illuminate\Support\Facades\Log;
36 37
37 /** 38 /**
38 * Class ChatRecordService 39 * Class ChatRecordService
@@ -71,7 +72,7 @@ class ChatRecordService extends BaseService @@ -71,7 +72,7 @@ class ChatRecordService extends BaseService
71 // 聊天方式 72 // 聊天方式
72 $type = $request->type; 73 $type = $request->type;
73 // 根据标签获取标签内容 74 // 根据标签获取标签内容
74 - $category_id = $request->category_id; 75 + $category_id = $request->category_id ? : 0;
75 // 默认带上下文 76 // 默认带上下文
76 $context = 1; 77 $context = 1;
77 $labels = []; 78 $labels = [];
@@ -256,19 +257,21 @@ class ChatRecordService extends BaseService @@ -256,19 +257,21 @@ class ChatRecordService extends BaseService
256 257
257 DB::beginTransaction(); 258 DB::beginTransaction();
258 try { 259 try {
259 - $result = $model->items()->create($item);  
260 - if (!$result) { 260 + $result1 = $model->items()->create($item);
  261 + if (!$result1) {
261 $this->message = '聊天失败。'; 262 $this->message = '聊天失败。';
262 return false; 263 return false;
263 } 264 }
264 265
265 - ChatRecordItemJob::dispatch($result);  
266 -  
267 // 发送消息 266 // 发送消息
268 - $result = $this->send($user, $model, $body, $model->context, $stream); 267 + $result2 = $this->send($user, $model, $body, $model->context, $stream);
269 DB::commit(); 268 DB::commit();
270 269
271 - return $result; 270 + ChatRecordItemJob::dispatch($result1);
  271 +
  272 + ChatRecordItemJob::dispatch($result2);
  273 +
  274 + return $result2;
272 } catch (\Exception $e) { 275 } catch (\Exception $e) {
273 $this->message = $e->getMessage(); 276 $this->message = $e->getMessage();
274 277
@@ -352,11 +355,12 @@ class ChatRecordService extends BaseService @@ -352,11 +355,12 @@ class ChatRecordService extends BaseService
352 $type = request('type', 1); 355 $type = request('type', 1);
353 $query = $user->chatRecords()->with(['items'])->where('type', $type); 356 $query = $user->chatRecords()->with(['items'])->where('type', $type);
354 $category_id = request('category_id'); 357 $category_id = request('category_id');
355 - if ($type === 2) { 358 + if ($type == 2) {
356 $query = $query->where('category_id', $category_id); 359 $query = $query->where('category_id', $category_id);
357 } 360 }
358 361
359 $model = $query->latest()->first(); 362 $model = $query->latest()->first();
  363 + Log::info($model);
360 if (\request('test') == 1) { 364 if (\request('test') == 1) {
361 dump($model); 365 dump($model);
362 } 366 }
@@ -413,7 +417,7 @@ class ChatRecordService extends BaseService @@ -413,7 +417,7 @@ class ChatRecordService extends BaseService
413 $type = request('type', 1); 417 $type = request('type', 1);
414 $query = $user->chatRecords()->with(['items'])->where('type', $type); 418 $query = $user->chatRecords()->with(['items'])->where('type', $type);
415 $category_id = request('category_id'); 419 $category_id = request('category_id');
416 - if ($type === 2) { 420 + if ($type == 2) {
417 $query = $query->where('category_id', $category_id); 421 $query = $query->where('category_id', $category_id);
418 } 422 }
419 423
1 <!DOCTYPE html><html lang=zh-CN><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>chatApp</title><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)')) 1 <!DOCTYPE html><html lang=zh-CN><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>chatApp</title><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
2 - document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel=stylesheet href=./static/index.b0707a6a.css></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id=app></div><script src=./static/js/chunk-vendors.fc0b6f42.js></script><script src=./static/js/index.6ce75ea3.js></script></body></html> 2 + document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel=stylesheet href=./static/index.b0707a6a.css></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id=app></div><script src=./static/js/chunk-vendors.fc0b6f42.js></script><script src=./static/js/index.9f0b94b0.js></script></body></html>