|
@@ -24,6 +24,7 @@ use App\Models\User\User; |
|
@@ -24,6 +24,7 @@ use App\Models\User\User; |
24
|
use App\Util\OpenAI\src\OpenAi;
|
24
|
use App\Util\OpenAI\src\OpenAi;
|
25
|
use Exception;
|
25
|
use Exception;
|
26
|
use GuzzleHttp\Client;
|
26
|
use GuzzleHttp\Client;
|
|
|
27
|
+use Illuminate\Support\Arr;
|
27
|
use Illuminate\Support\Facades\Cache;
|
28
|
use Illuminate\Support\Facades\Cache;
|
28
|
use Illuminate\Support\Facades\Log;
|
29
|
use Illuminate\Support\Facades\Log;
|
29
|
use Illuminate\Support\Str;
|
30
|
use Illuminate\Support\Str;
|
|
@@ -71,19 +72,27 @@ trait ChatTrait |
|
@@ -71,19 +72,27 @@ trait ChatTrait |
71
|
$consume_times = $prepare['consume_times'];
|
72
|
$consume_times = $prepare['consume_times'];
|
72
|
$cache_key = $prepare['cache_key'];
|
73
|
$cache_key = $prepare['cache_key'];
|
73
|
|
74
|
|
|
|
75
|
+// if ($stream !== true) {
|
|
|
76
|
+// $open_ai = Manager::build(
|
|
|
77
|
+// new Client([
|
|
|
78
|
+// 'proxy' => 'http://47.251.45.96',
|
|
|
79
|
+// 'verify' => false
|
|
|
80
|
+// ]),
|
|
|
81
|
+// new Authentication($open_api_key)
|
|
|
82
|
+// );
|
|
|
83
|
+// $answer = $this->sendRequest($open_ai, $send_data, 'general');
|
|
|
84
|
+// } else {
|
|
|
85
|
+// $open_ai = new OpenAi($open_api_key);
|
|
|
86
|
+// $answer = $this->sendRequest($open_ai, $send_data);
|
|
|
87
|
+// }
|
|
|
88
|
+ $type = 'chunked';
|
74
|
if ($stream !== true) {
|
89
|
if ($stream !== true) {
|
75
|
- $open_ai = Manager::build(
|
|
|
76
|
- new Client([
|
|
|
77
|
-// 'proxy' => 'http://127.0.0.1:10809',
|
|
|
78
|
- 'verify' => false
|
|
|
79
|
- ]),
|
|
|
80
|
- new Authentication($open_api_key)
|
|
|
81
|
- );
|
|
|
82
|
- $answer = $this->sendRequest($open_ai, $send_data, 'general');
|
|
|
83
|
- } else {
|
|
|
84
|
- $open_ai = new OpenAi($open_api_key);
|
|
|
85
|
- $answer = $this->sendRequest($open_ai, $send_data);
|
90
|
+ $type = 'general';
|
86
|
}
|
91
|
}
|
|
|
92
|
+ $open_ai = new OpenAi($open_api_key);
|
|
|
93
|
+
|
|
|
94
|
+// $open_ai->setProxy("http://47.251.45.96");
|
|
|
95
|
+ $answer = $this->sendRequest($open_ai, $send_data, $type);
|
87
|
|
96
|
|
88
|
if ($context == 1) {
|
97
|
if ($context == 1) {
|
89
|
// 响应消息
|
98
|
// 响应消息
|
|
@@ -271,6 +280,7 @@ trait ChatTrait |
|
@@ -271,6 +280,7 @@ trait ChatTrait |
271
|
*/
|
280
|
*/
|
272
|
public static function sendRequest($open_ai, $send_data, $type = 'chunked')
|
281
|
public static function sendRequest($open_ai, $send_data, $type = 'chunked')
|
273
|
{
|
282
|
{
|
|
|
283
|
+// dump($type);
|
274
|
$answer = '';
|
284
|
$answer = '';
|
275
|
if ($type === 'chunked') {
|
285
|
if ($type === 'chunked') {
|
276
|
// 设置响应头信息
|
286
|
// 设置响应头信息
|
|
@@ -307,8 +317,8 @@ trait ChatTrait |
|
@@ -307,8 +317,8 @@ trait ChatTrait |
307
|
}
|
317
|
}
|
308
|
foreach ($data as $message) {
|
318
|
foreach ($data as $message) {
|
309
|
if ('[DONE]' === $message) {
|
319
|
if ('[DONE]' === $message) {
|
310
|
- echo "0\r\n\r\n";
|
|
|
311
|
-// echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
|
320
|
+// echo "[DONE]\r\n\r\n";
|
|
|
321
|
+ echo "[EVENT:STOPPED|DATA:DONE]\r\n\r\n";
|
312
|
} else {
|
322
|
} else {
|
313
|
$message = json_decode($message, true);
|
323
|
$message = json_decode($message, true);
|
314
|
$content = $message['choices'][0]['delta']['content'] ?? '';
|
324
|
$content = $message['choices'][0]['delta']['content'] ?? '';
|
|
@@ -399,11 +409,15 @@ trait ChatTrait |
|
@@ -399,11 +409,15 @@ trait ChatTrait |
399
|
// echo "event: stop\n";
|
409
|
// echo "event: stop\n";
|
400
|
// echo "data: stopped\n\n";
|
410
|
// echo "data: stopped\n\n";
|
401
|
} elseif($type === 'general') {
|
411
|
} elseif($type === 'general') {
|
402
|
- $response = $open_ai->chatCompletions()->create(
|
|
|
403
|
- new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest($send_data)
|
|
|
404
|
- )->toModel();
|
412
|
+ $response = $open_ai->chat($send_data);
|
|
|
413
|
+ $response = json_decode($response, true);
|
|
|
414
|
+ $answer = Arr::get(Arr::first(Arr::get($response, 'choices')), 'message.content');
|
405
|
|
415
|
|
406
|
- $answer = $response->choices[0]->message->content;
|
416
|
+// $response = $open_ai->chatCompletions()->create(
|
|
|
417
|
+// new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest($send_data)
|
|
|
418
|
+// )->toModel();
|
|
|
419
|
+
|
|
|
420
|
+// $answer = $response->choices[0]->message->content;
|
407
|
}
|
421
|
}
|
408
|
|
422
|
|
409
|
return $answer;
|
423
|
return $answer;
|