Skip to content

Commit 3dd98f4

Browse files
authored
feat: 优化密钥测试 (#140)
* fix: rm max_tokens * feat: 测试耗时
1 parent d3dd4fe commit 3dd98f4

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

internal/channel/anthropic_channel.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func (ch *AnthropicChannel) ValidateKey(ctx context.Context, key string) (bool,
9090

9191
// Use a minimal, low-cost payload for validation
9292
payload := gin.H{
93-
"model": ch.TestModel,
94-
"max_tokens": 100,
93+
"model": ch.TestModel,
9594
"messages": []gin.H{
9695
{"role": "user", "content": "hi"},
9796
},

internal/channel/openai_channel.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (ch *OpenAIChannel) ValidateKey(ctx context.Context, key string) (bool, err
9393
"messages": []gin.H{
9494
{"role": "user", "content": "hi"},
9595
},
96-
"max_tokens": 100,
9796
}
9897
body, err := json.Marshal(payload)
9998
if err != nil {

internal/handler/key_handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/gin-gonic/gin"
1314
"gorm.io/gorm"
@@ -247,7 +248,9 @@ func (s *Server) TestMultipleKeys(c *gin.Context) {
247248
return
248249
}
249250

251+
start := time.Now()
250252
results, err := s.KeyService.TestMultipleKeys(group, req.KeysText)
253+
duration := time.Since(start).Milliseconds()
251254
if err != nil {
252255
if strings.Contains(err.Error(), "batch size exceeds the limit") {
253256
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, err.Error()))
@@ -259,7 +262,10 @@ func (s *Server) TestMultipleKeys(c *gin.Context) {
259262
return
260263
}
261264

262-
response.Success(c, results)
265+
response.Success(c, gin.H{
266+
"results": results,
267+
"total_duration": duration,
268+
})
263269
}
264270

265271
// ValidateGroupKeys initiates a manual validation task for all keys in a group.

web/src/api/keys.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ export const keysApi = {
9191
async testKeys(
9292
group_id: number,
9393
keys_text: string
94-
): Promise<
95-
{
94+
): Promise<{
95+
results: {
9696
key_value: string;
9797
is_valid: boolean;
9898
error: string;
99-
}[]
100-
> {
99+
}[];
100+
total_duration: number;
101+
}> {
101102
const res = await http.post(
102103
"/keys/test-multiple",
103104
{

web/src/components/keys/GroupFormModal.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,15 +843,15 @@ async function handleSubmit() {
843843
<template #trigger>
844844
<n-icon :component="HelpCircleOutline" class="help-icon config-help" />
845845
</template>
846-
使用JSON格式定义要覆盖的API请求参数。例如: {&quot;temperature&quot;: 0.7,
847-
&quot;max_tokens&quot;: 2000}。这些参数会在发送请求时合并到原始参数中
846+
使用JSON格式定义要覆盖的API请求参数。例如: {&quot;temperature&quot;:
847+
0.7}。这些参数会在发送请求时合并到原始参数中
848848
</n-tooltip>
849849
</div>
850850
</template>
851851
<n-input
852852
v-model:value="formData.param_overrides"
853853
type="textarea"
854-
placeholder='{"temperature": 0.7, "max_tokens": 2000}'
854+
placeholder='{"temperature": 0.7}'
855855
:rows="4"
856856
/>
857857
</n-form-item>

web/src/components/keys/KeyTable.vue

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ async function testKey(_key: KeyRow) {
223223
});
224224
225225
try {
226-
const res = await keysApi.testKeys(props.selectedGroup.id, _key.key_value);
227-
const curValid = res?.[0] || {};
226+
const response = await keysApi.testKeys(props.selectedGroup.id, _key.key_value);
227+
const curValid = response.results?.[0] || {};
228228
if (curValid.is_valid) {
229-
window.$message.success("密钥测试成功");
229+
window.$message.success(`密钥测试成功 (耗时: ${formatDuration(response.total_duration)})`);
230230
} else {
231231
window.$message.error(curValid.error || "密钥测试失败: 无效的API密钥", {
232232
keepAliveOnHover: true,
@@ -245,6 +245,29 @@ async function testKey(_key: KeyRow) {
245245
}
246246
}
247247
248+
function formatDuration(ms: number): string {
249+
if (ms < 0) {
250+
return "0ms";
251+
}
252+
253+
const minutes = Math.floor(ms / 60000);
254+
const seconds = Math.floor((ms % 60000) / 1000);
255+
const milliseconds = ms % 1000;
256+
257+
let result = "";
258+
if (minutes > 0) {
259+
result += `${minutes}m`;
260+
}
261+
if (seconds > 0) {
262+
result += `${seconds}s`;
263+
}
264+
if (milliseconds > 0 || result === "") {
265+
result += `${milliseconds}ms`;
266+
}
267+
268+
return result;
269+
}
270+
248271
function toggleKeyVisibility(key: KeyRow) {
249272
key.is_visible = !key.is_visible;
250273
}

0 commit comments

Comments
 (0)