@@ -38,7 +38,7 @@ public function chat(Request $request): JsonResponse
38
38
$ request ->validate ([
39
39
'prompt ' => 'required|string ' ,
40
40
'regen ' => ['required ' , 'in:true,false ' ],
41
- 'api_key ' => 'sometimes |string ' ,
41
+ 'api_key ' => 'required |string ' ,
42
42
]);
43
43
$ passed = TmsFacade::checkText ($ request ->input ('prompt ' ));
44
44
if (!$ passed ) {
@@ -73,7 +73,7 @@ public function translate(Request $request): JsonResponse
73
73
$ request ->validate ([
74
74
'prompt ' => 'required|string ' ,
75
75
'regen ' => ['required ' , 'in:true,false ' ],
76
- 'api_key ' => 'sometimes |string ' ,
76
+ 'api_key ' => 'required |string ' ,
77
77
]);
78
78
$ passed = TmsFacade::checkText ($ request ->input ('prompt ' ));
79
79
if (!$ passed ) {
@@ -109,6 +109,11 @@ public function stream(Request $request)
109
109
if ($ request ->session ()->get ('chat_id ' ) != $ request ->input ('chat_id ' )) {
110
110
abort (400 );
111
111
}
112
+ $ apiKey = $ request ->input ('api_key ' );
113
+ if (empty ($ apiKey )) {
114
+ abort (403 );
115
+ }
116
+ $ apiKey = base64_decode ($ apiKey );
112
117
113
118
$ messages = $ request ->session ()->get ('messages ' );
114
119
@@ -120,22 +125,18 @@ public function stream(Request $request)
120
125
121
126
// 实时将流式响应数据发送到客户端
122
127
$ respData = '' ;
123
- $ apiKey = $ request ->input ('api_key ' );
124
- if ($ apiKey ) {
125
- $ apiKey = base64_decode ($ apiKey );
126
- }
127
128
header ('Access-Control-Allow-Origin: * ' );
128
129
header ('Content-type: text/event-stream ' );
129
130
header ('Cache-Control: no-cache ' );
130
131
header ('X-Accel-Buffering: no ' );
131
- OpenAI::withToken ($ apiKey )->chat ($ params , function ($ ch , $ data ) use ($ apiKey , &$ respData ) {
132
+ OpenAI::withToken ($ apiKey )->chat ($ params , function ($ ch , $ data ) use (&$ respData ) {
132
133
$ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
133
134
if ($ httpCode >= 400 ) {
134
135
echo "data: [ERROR] $ httpCode " ;
135
- if (($ httpCode == 400 || $ httpCode == 401 ) && empty ($ apiKey )) {
136
+ /* if (($httpCode == 400 || $httpCode == 401) && empty($apiKey)) {
136
137
// app key 耗尽自动切换到下一个免费的 key
137
138
Artisan::call('app:update-open-ai-key');
138
- }
139
+ }*/
139
140
} else {
140
141
$ respData .= $ data ;
141
142
echo $ data ;;
@@ -177,7 +178,7 @@ public function audio(Request $request): JsonResponse
177
178
->min (1 ) // 最小不低于 1 KB
178
179
->max (10 * 1024 ), // 最大不超过 10 MB
179
180
],
180
- 'api_key ' => 'sometimes |string ' ,
181
+ 'api_key ' => 'required |string ' ,
181
182
]);
182
183
// 保存到本地
183
184
$ fileName = Str::uuid () . '.wav ' ;
@@ -225,7 +226,7 @@ public function image(Request $request): JsonResponse
225
226
$ request ->validate ([
226
227
'prompt ' => 'required|string ' ,
227
228
'regen ' => ['required ' , 'in:true,false ' ],
228
- 'api_key ' => 'sometimes |string ' ,
229
+ 'api_key ' => 'required |string ' ,
229
230
]);
230
231
$ passed = TmsFacade::checkText ($ request ->input ('prompt ' ));
231
232
if (!$ passed ) {
@@ -244,18 +245,14 @@ public function image(Request $request): JsonResponse
244
245
array_pop ($ messages );
245
246
}
246
247
$ apiKey = $ request ->input ('api_key ' );
247
- $ size = '256x256 ' ;
248
- if (!empty ($ apiKey )) {
249
- $ size = '1024x1024 ' ;
250
- }
251
248
$ response = OpenAI::withToken ($ apiKey )->image ([
252
249
"prompt " => $ prompt ,
253
250
"n " => 1 ,
254
- "size " => $ size ,
251
+ "size " => ' 256x256 ' ,
255
252
"response_format " => "url " ,
256
253
]);
257
254
$ result = json_decode ($ response );
258
- $ image = '画图失败,如果你设置了自己的key,请确保它是有效的 ' ;
255
+ $ image = '画图失败,请确保 API KEY 是有效的 ' ;
259
256
if (isset ($ result ->data [0 ]->url )) {
260
257
$ image = ' ' ;
261
258
}
@@ -281,12 +278,7 @@ public function valid(Request $request): JsonResponse
281
278
'api_key ' => 'required|string '
282
279
]);
283
280
$ apiKey = $ request ->input ('api_key ' );
284
- if (empty ($ apiKey )) {
285
- return response ()->json (['valid ' => false , 'error ' => '无效的 API KEY ' ]);
286
- }
287
- $ response = Http::withToken ($ apiKey )->timeout (15 )
288
- ->get (config ('openai.base_uri ' ) . '/dashboard/billing/credit_grants ' );
289
- if ($ response ->failed ()) {
281
+ if (empty ($ apiKey ) || strlen ($ apiKey ) != 51 || !Str::startsWith ($ apiKey , 'sk- ' )) {
290
282
return response ()->json (['valid ' => false , 'error ' => '无效的 API KEY ' ]);
291
283
}
292
284
return response ()->json (['valid ' => true ]);
0 commit comments