Skip to content

Commit aa9590d

Browse files
committed
将API KEY设置为必填项
1 parent c751254 commit aa9590d

File tree

6 files changed

+51
-53
lines changed

6 files changed

+51
-53
lines changed

app/Client/TmsService.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ public function checkText(string $content): bool | Exception
5050

5151
// 返回的resp是一个TextModerationResponse的实例,与请求对象对应
5252
$resp = $client->TextModeration($req);
53-
if ($resp->Suggestion === 'Pass') {
54-
return true;
55-
} else {
53+
// 广告予以放行(适用于广告文案)
54+
if ($resp->Suggestion === 'Block' && $resp->Label !== 'Ad') {
5655
return false;
5756
}
57+
// 政治话题红线最高,即使疑似也予以拦截
58+
if ($resp->Suggestion === 'Review' && $resp->Label === 'Polity') {
59+
return false;
60+
}
61+
return true;
5862
} catch (TencentCloudSDKException $e) {
5963
throw $e;
6064
}

app/Http/Controllers/ChatController.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function chat(Request $request): JsonResponse
3838
$request->validate([
3939
'prompt' => 'required|string',
4040
'regen' => ['required', 'in:true,false'],
41-
'api_key' => 'sometimes|string',
41+
'api_key' => 'required|string',
4242
]);
4343
$passed = TmsFacade::checkText($request->input('prompt'));
4444
if (!$passed) {
@@ -73,7 +73,7 @@ public function translate(Request $request): JsonResponse
7373
$request->validate([
7474
'prompt' => 'required|string',
7575
'regen' => ['required', 'in:true,false'],
76-
'api_key' => 'sometimes|string',
76+
'api_key' => 'required|string',
7777
]);
7878
$passed = TmsFacade::checkText($request->input('prompt'));
7979
if (!$passed) {
@@ -109,6 +109,11 @@ public function stream(Request $request)
109109
if ($request->session()->get('chat_id') != $request->input('chat_id')) {
110110
abort(400);
111111
}
112+
$apiKey = $request->input('api_key');
113+
if (empty($apiKey)) {
114+
abort(403);
115+
}
116+
$apiKey = base64_decode($apiKey);
112117

113118
$messages = $request->session()->get('messages');
114119

@@ -120,22 +125,18 @@ public function stream(Request $request)
120125

121126
// 实时将流式响应数据发送到客户端
122127
$respData = '';
123-
$apiKey = $request->input('api_key');
124-
if ($apiKey) {
125-
$apiKey = base64_decode($apiKey);
126-
}
127128
header('Access-Control-Allow-Origin: *');
128129
header('Content-type: text/event-stream');
129130
header('Cache-Control: no-cache');
130131
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) {
132133
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
133134
if ($httpCode >= 400) {
134135
echo "data: [ERROR] $httpCode";
135-
if (($httpCode == 400 || $httpCode == 401) && empty($apiKey)) {
136+
/* if (($httpCode == 400 || $httpCode == 401) && empty($apiKey)) {
136137
// app key 耗尽自动切换到下一个免费的 key
137138
Artisan::call('app:update-open-ai-key');
138-
}
139+
}*/
139140
} else {
140141
$respData .= $data;
141142
echo $data;;
@@ -177,7 +178,7 @@ public function audio(Request $request): JsonResponse
177178
->min(1) // 最小不低于 1 KB
178179
->max(10 * 1024), // 最大不超过 10 MB
179180
],
180-
'api_key' => 'sometimes|string',
181+
'api_key' => 'required|string',
181182
]);
182183
// 保存到本地
183184
$fileName = Str::uuid() . '.wav';
@@ -225,7 +226,7 @@ public function image(Request $request): JsonResponse
225226
$request->validate([
226227
'prompt' => 'required|string',
227228
'regen' => ['required', 'in:true,false'],
228-
'api_key' => 'sometimes|string',
229+
'api_key' => 'required|string',
229230
]);
230231
$passed = TmsFacade::checkText($request->input('prompt'));
231232
if (!$passed) {
@@ -244,18 +245,14 @@ public function image(Request $request): JsonResponse
244245
array_pop($messages);
245246
}
246247
$apiKey = $request->input('api_key');
247-
$size = '256x256';
248-
if (!empty($apiKey)) {
249-
$size = '1024x1024';
250-
}
251248
$response = OpenAI::withToken($apiKey)->image([
252249
"prompt" => $prompt,
253250
"n" => 1,
254-
"size" => $size,
251+
"size" => '256x256',
255252
"response_format" => "url",
256253
]);
257254
$result = json_decode($response);
258-
$image = '画图失败,如果你设置了自己的key,请确保它是有效的';
255+
$image = '画图失败,请确保 API KEY 是有效的';
259256
if (isset($result->data[0]->url)) {
260257
$image = '![](' . $result->data[0]->url . ')';
261258
}
@@ -281,12 +278,7 @@ public function valid(Request $request): JsonResponse
281278
'api_key' => 'required|string'
282279
]);
283280
$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-')) {
290282
return response()->json(['valid' => false, 'error' => '无效的 API KEY']);
291283
}
292284
return response()->json(['valid' => true]);

resources/js/Pages/Chat.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const form = useForm({
2121
})
2222
2323
const chat = () => {
24+
if (apiKey.value == '') {
25+
alert('请先输入 API KEY')
26+
return
27+
}
2428
if (isTyping.value) {
2529
return;
2630
}
@@ -34,6 +38,10 @@ const reset = () => {
3438
}
3539
3640
const audio = (blob) => {
41+
if (apiKey.value == '') {
42+
alert('请先输入 API KEY')
43+
return
44+
}
3745
if (isTyping.value) {
3846
return;
3947
}
@@ -52,6 +60,10 @@ const image = () => {
5260
alert('请在输入框输入图片描述信息')
5361
return
5462
}
63+
if (apiKey.value == '') {
64+
alert('请先输入 API KEY')
65+
return
66+
}
5567
if (isTyping.value) {
5668
return;
5769
}
@@ -64,6 +76,10 @@ const translate = () => {
6476
alert('请在输入框输入待翻译内容')
6577
return
6678
}
79+
if (apiKey.value == '') {
80+
alert('请先输入 API KEY')
81+
return
82+
}
6783
if (isTyping.value) {
6884
return;
6985
}
@@ -72,7 +88,7 @@ const translate = () => {
7288
}
7389
7490
const enterApiKey = () => {
75-
const api_key = prompt("输入你的 OpenAI API KEY(使用自己的 KEY 可以不受共享通道频率限制,还可以绘制大尺寸图片)");
91+
const api_key = prompt("输入你的 OpenAI API KEY:");
7692
store.dispatch('validAndSetApiKey', api_key)
7793
}
7894
@@ -133,7 +149,7 @@ const regenerate = () => {
133149
<path
134150
d="M218.1 167.17c0 13 0 25.6 4.1 37.4-43.1 50.6-156.9 184.3-167.5 194.5a20.17 20.17 0 00-6.7 15c0 8.5 5.2 16.7 9.6 21.3 6.6 6.9 34.8 33 40 28 15.4-15 18.5-19 24.8-25.2 9.5-9.3-1-28.3 2.3-36s6.8-9.2 12.5-10.4 15.8 2.9 23.7 3c8.3.1 12.8-3.4 19-9.2 5-4.6 8.6-8.9 8.7-15.6.2-9-12.8-20.9-3.1-30.4s23.7 6.2 34 5 22.8-15.5 24.1-21.6-11.7-21.8-9.7-30.7c.7-3 6.8-10 11.4-11s25 6.9 29.6 5.9c5.6-1.2 12.1-7.1 17.4-10.4 15.5 6.7 29.6 9.4 47.7 9.4 68.5 0 124-53.4 124-119.2S408.5 48 340 48s-121.9 53.37-121.9 119.17zM400 144a32 32 0 11-32-32 32 32 0 0132 32z">
135151
</path>
136-
</svg><span>API KEY(可选)</span>
152+
</svg><span>API KEY(必填)</span>
137153
</button>
138154
<a href="https://t.zsxq.com/0cC0ngiSn" target="_blank"
139155
class="inline-flex items-center justify-center px-4 py-2 shadow-md bg-blue-300 hover:bg-blue-500 active:bg-blue-600 text-white rounded-full font-semibold">

resources/js/api/chat.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default {
1212
const formData = new FormData();
1313
formData.append('prompt', message);
1414
formData.append('regen', regen);
15+
const api_key = window.localStorage.getItem('GEEKCHAT_API_KEY', '')
16+
formData.append('api_key', api_key);
1517
return fetch(CHAT_CONFIG.BASE_URL + '/chat', { method: 'POST', body: formData })
1618
},
1719

@@ -20,6 +22,8 @@ export default {
2022
const formData = new FormData();
2123
formData.append('prompt', message);
2224
formData.append('regen', regen);
25+
const api_key = window.localStorage.getItem('GEEKCHAT_API_KEY', '')
26+
formData.append('api_key', api_key);
2327
return fetch(CHAT_CONFIG.BASE_URL + '/translate', { method: 'POST', body: formData })
2428
},
2529

@@ -28,9 +32,7 @@ export default {
2832
const formData = new FormData();
2933
formData.append('audio', blob);
3034
const api_key = window.localStorage.getItem('GEEKCHAT_API_KEY', '')
31-
if (api_key) {
32-
formData.append('api_key', api_key);
33-
}
35+
formData.append('api_key', api_key);
3436
return fetch(CHAT_CONFIG.BASE_URL + '/audio', { method: 'POST', body: formData })
3537
},
3638

@@ -40,9 +42,7 @@ export default {
4042
formData.append('prompt', message);
4143
formData.append('regen', regen);
4244
const api_key = window.localStorage.getItem('GEEKCHAT_API_KEY', '')
43-
if (api_key) {
44-
formData.append('api_key', api_key);
45-
}
45+
formData.append('api_key', api_key);
4646
return axios.post(CHAT_CONFIG.BASE_URL + '/image', formData, { responseType: 'json' })
4747
},
4848

resources/js/store.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const store = createStore({
6868
commit('addMessage', { 'role': 'assistant', 'content': '服务端异常,请稍后再试' })
6969
throw new Error('服务端异常,请稍后再试');
7070
} else if (response.status >= 400) {
71-
commit('addMessage', { 'role': 'assistant', 'content': "请求处理失败,请输入合规内容,如果使用的是自定义 KEY,请确保它是有效的" })
71+
commit('addMessage', { 'role': 'assistant', 'content': "请输入有效的 API KEY" })
7272
throw new Error('请求处理失败,请重试');
7373
}
7474
return response.json();
@@ -121,7 +121,7 @@ const store = createStore({
121121
commit('addMessage', { 'role': 'assistant', 'content': '服务端异常,请稍后再试' })
122122
throw new Error('服务端异常,请稍后再试');
123123
} else if (response.status >= 400) {
124-
commit('addMessage', { 'role': 'assistant', 'content': "请求处理失败,请输入合规内容,如果使用的是自定义 KEY,请确保它是有效的" })
124+
commit('addMessage', { 'role': 'assistant', 'content': "请输入有效的 API KEY" })
125125
throw new Error('请求处理失败,请重试');
126126
}
127127
return response.json();
@@ -168,7 +168,7 @@ const store = createStore({
168168
commit('addMessage', { 'role': 'assistant', 'content': '服务端异常,请稍后再试' })
169169
throw new Error('服务端异常,请稍后再试');
170170
} else if (response.status >= 400) {
171-
commit('addMessage', { 'role': 'assistant', 'content': "请求处理失败,请输入合规内容,如果使用的是自定义 KEY,请确保它是有效的" })
171+
commit('addMessage', { 'role': 'assistant', 'content': "请输入有效的 API KEY" })
172172
throw new Error('请求处理失败,请重试');
173173
}
174174
return response.json();
@@ -234,7 +234,7 @@ const store = createStore({
234234
if (error.response.status === 429) {
235235
commit('addMessage', { 'role': 'assistant', 'content': '请求过于频繁,请稍后再试' });
236236
} else {
237-
commit('addMessage', { 'role': 'assistant', 'content': '请求处理失败,请输入合规内容,如果使用的是自定义KEY,请确保它是有效的' });
237+
commit('addMessage', { 'role': 'assistant', 'content': '请输入有效的 API KEY' });
238238
}
239239
commit('toggleTyping')
240240
});

resources/views/app.blade.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,11 @@
2020

2121
<body class="font-sans antialiased">
2222
@inertia
23-
<div class="flex items-center justify-center">
24-
<div class="my-2 grid sm:grid-cols-1 gap-y-2 gap-x-6">
25-
<div class="flex items-center justify-start space-x-1">
26-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
27-
<path fill="#3B82F6" d="M22.282 9.821a5.985 5.985 0 0 0-.516-4.91a6.046 6.046 0 0 0-6.51-2.9A6.065 6.065 0 0 0 4.981 4.18a5.985 5.985 0 0 0-3.998 2.9a6.046 6.046 0 0 0 .743 7.097a5.98 5.98 0 0 0 .51 4.911a6.051 6.051 0 0 0 6.515 2.9A5.985 5.985 0 0 0 13.26 24a6.056 6.056 0 0 0 5.772-4.206a5.99 5.99 0 0 0 3.997-2.9a6.056 6.056 0 0 0-.747-7.073zM13.26 22.43a4.476 4.476 0 0 1-2.876-1.04l.141-.081l4.779-2.758a.795.795 0 0 0 .392-.681v-6.737l2.02 1.168a.071.071 0 0 1 .038.052v5.583a4.504 4.504 0 0 1-4.494 4.494zM3.6 18.304a4.47 4.47 0 0 1-.535-3.014l.142.085l4.783 2.759a.771.771 0 0 0 .78 0l5.843-3.369v2.332a.08.08 0 0 1-.033.062L9.74 19.95a4.5 4.5 0 0 1-6.14-1.646zM2.34 7.896a4.485 4.485 0 0 1 2.366-1.973V11.6a.766.766 0 0 0 .388.676l5.815 3.355l-2.02 1.168a.076.076 0 0 1-.071 0l-4.83-2.786A4.504 4.504 0 0 1 2.34 7.872zm16.597 3.855l-5.833-3.387L15.119 7.2a.076.076 0 0 1 .071 0l4.83 2.791a4.494 4.494 0 0 1-.676 8.105v-5.678a.79.79 0 0 0-.407-.667zm2.01-3.023l-.141-.085l-4.774-2.782a.776.776 0 0 0-.785 0L9.409 9.23V6.897a.066.066 0 0 1 .028-.061l4.83-2.787a4.5 4.5 0 0 1 6.68 4.66zm-12.64 4.135l-2.02-1.164a.08.08 0 0 1-.038-.057V6.075a4.5 4.5 0 0 1 7.375-3.453l-.142.08L8.704 5.46a.795.795 0 0 0-.393.681zm1.097-2.365l2.602-1.5l2.607 1.5v2.999l-2.597 1.5l-2.607-1.5Z"></path>
28-
</svg>
29-
<div class="text-sm">
30-
<a href="https://t.zsxq.com/0cC0ngiSn" class="underline text-blue-400 hover:text-blue-500" target="_blank">
31-
GeekChat即将接入GPT-4,加入社群了解动态
32-
</a>
33-
</div>
34-
</div>
35-
</div>
36-
</div>
3723
<footer class="text-center sm:text-left">
3824
<div class="p-4 text-center text-neutral-700">
3925
GeekChat 由
4026
<a href="https://geekr.dev" target="_blank" class="text-neutral-800 dark:text-neutral-400">极客书房</a>
41-
开发维护,系统已接入腾讯云内容过滤,请文明发言、合规使用。定制开发、合作咨询请<a href="https://image.gstatics.cn/wechat.webp" target="_blank" class="text-blue-300 dark:text-blue-500">点这里</a>。
27+
开发维护,请文明发言、合规使用。定制开发、合作咨询请<a href="https://image.gstatics.cn/wechat.webp" target="_blank" class="text-blue-300 dark:text-blue-500">点这里</a>。
4228
</div>
4329
</footer>
4430
</body>

0 commit comments

Comments
 (0)