作者 Richer

功能优化:chatgpt接口改为反向代理模式

@@ -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;
@@ -4,7 +4,9 @@ namespace App\Util\OpenAI\src; @@ -4,7 +4,9 @@ namespace App\Util\OpenAI\src;
4 4
5 class Url 5 class Url
6 { 6 {
7 - public const ORIGIN = 'https://api.openai.com'; 7 +// public const ORIGIN = 'https://api.openai.com';
  8 + // add by Richer 于 2023年5月17日14:37:12 反向代理域名
  9 + public const ORIGIN = 'http://47.251.45.96';
8 public const API_VERSION = 'v1'; 10 public const API_VERSION = 'v1';
9 public const OPEN_AI_URL = self::ORIGIN . "/" . self::API_VERSION; 11 public const OPEN_AI_URL = self::ORIGIN . "/" . self::API_VERSION;
10 12
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.9f0b94b0.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.3ca82745.js></script></body></html>