作者 Richer

功能优化

... ... @@ -41,7 +41,7 @@ class ChatRecordItemJob extends BaseJob
$this->desc = '同步用户聊天明细记录';
$this->channel = 'sync';
// 执行
parent::__construct($model);
parent::__construct($model, 1);
}
/**
... ... @@ -53,10 +53,11 @@ class ChatRecordItemJob extends BaseJob
{
record_log($this->channel, $this->desc, 'begin');
if ($this->attempts() > 3) {
if ($this->attempts() > 1) {
record_log($this->channel, '尝试次数过多');
} else {
// 同步增加记录
record_log($this->channel, '同步数据:' . json_encode($this->model));
$this->syncRecordItem($this->model);
}
record_log($this->channel, $this->desc, 'end');
... ...
... ... @@ -53,9 +53,10 @@ class ChatRecordJob extends BaseJob
{
record_log($this->channel, $this->desc, 'begin');
if ($this->attempts() > 3) {
if ($this->attempts() > 1) {
record_log($this->channel, '尝试次数过多');
} else {
record_log($this->channel, '同步数据:' . json_encode($this->model));
// 同步增加记录
$this->syncRecord($this->model);
}
... ...
... ... @@ -308,11 +308,13 @@ trait ChatTrait
foreach ($data as $message) {
if ('[DONE]' === $message) {
echo "0\r\n\r\n";
// echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
} else {
$message = json_decode($message, true);
$content = $message['choices'][0]['delta']['content'] ?? '';
$answer .= $content;
// record_log('openai', '内容: ' . $content );
record_log('openai', '内容: ' . $content );
record_log('openai', '内容urlencode: ' . urlencode($content));
echo urlencode($content) . "\r\n";
}
}
... ... @@ -338,7 +340,7 @@ trait ChatTrait
}
foreach ($data as $message) {
if ('[DONE]' === $message) {
echo "0\r\n\r\n";
echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
} else {
$message = json_decode($message, true);
$content = $message['choices'][0]['delta']['content'] ?? '';
... ...
... ... @@ -33,6 +33,7 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Class ChatRecordService
... ... @@ -71,7 +72,7 @@ class ChatRecordService extends BaseService
// 聊天方式
$type = $request->type;
// 根据标签获取标签内容
$category_id = $request->category_id;
$category_id = $request->category_id ? : 0;
// 默认带上下文
$context = 1;
$labels = [];
... ... @@ -256,19 +257,21 @@ class ChatRecordService extends BaseService
DB::beginTransaction();
try {
$result = $model->items()->create($item);
if (!$result) {
$result1 = $model->items()->create($item);
if (!$result1) {
$this->message = '聊天失败。';
return false;
}
ChatRecordItemJob::dispatch($result);
// 发送消息
$result = $this->send($user, $model, $body, $model->context, $stream);
$result2 = $this->send($user, $model, $body, $model->context, $stream);
DB::commit();
return $result;
ChatRecordItemJob::dispatch($result1);
ChatRecordItemJob::dispatch($result2);
return $result2;
} catch (\Exception $e) {
$this->message = $e->getMessage();
... ... @@ -352,11 +355,12 @@ class ChatRecordService extends BaseService
$type = request('type', 1);
$query = $user->chatRecords()->with(['items'])->where('type', $type);
$category_id = request('category_id');
if ($type === 2) {
if ($type == 2) {
$query = $query->where('category_id', $category_id);
}
$model = $query->latest()->first();
Log::info($model);
if (\request('test') == 1) {
dump($model);
}
... ... @@ -413,7 +417,7 @@ class ChatRecordService extends BaseService
$type = request('type', 1);
$query = $user->chatRecords()->with(['items'])->where('type', $type);
$category_id = request('category_id');
if ($type === 2) {
if ($type == 2) {
$query = $query->where('category_id', $category_id);
}
... ...
<!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)'))
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>
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>
... ...