Skip to content

Commit b94ea00

Browse files
committed
feat: #15 支持不修改数据库和强制使用内置模型
1 parent 2bf5552 commit b94ea00

File tree

6 files changed

+74
-47
lines changed

6 files changed

+74
-47
lines changed

README-zh.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ docker-compose up -d
8787
"exclude_model": ["advanced-voice", "minimax_s2v-01", "minimax_video-01", "minimax_video-01-live2d"],
8888
"models": ["gpt-3.5-turbo", "gpt-4o"],
8989
"force_models": false,
90+
"force_inside_models": false,
9091
"time_period": "1h",
9192
"max_concurrent": 5,
9293
"rps": 5,
9394
"db_type": "YOUR_DB_TYPE",
9495
"db_dsn": "YOUR_DB_DSN",
96+
"do_not_modify_db": false,
9597
"base_url": "http://localhost:3000",
9698
"system_token": "YOUR_SYSTEM_TOKEN",
9799
"uptime-kuma": {
@@ -133,11 +135,13 @@ docker-compose up -d
133135
- exclude_model: 排除不予监控的模型ID
134136
- models: 模型列表,仅当获取不到渠道的模型(/v1/models)时使用
135137
- force_models: 如果为true,将强制只测试上述模型,不再获取渠道的模型,默认为false
138+
- force_inside_models: 如果为true,将强制只测试OneAPI设置的模型,不再获取模型列表,默认为false。如果force_models为true,此项无效
136139
- time_period: 模型可用性测试的时间间隔,建议不小于30分钟,接收的时间格式为s、m、h
137140
- max_concurrency: 在一个渠道内测试的最大并发数,默认为5
138141
- rps: 在一个渠道内测试的每秒请求数,默认为5
139142
- db_type: 数据库类型,包括mysql、sqlite、postgres、sqlserver
140143
- db_dsn: 数据库DSN字符串,不同数据库类型的DSN格式不同,示例如下
144+
- do_not_modify_db: 如果为true,将不会修改数据库中的可用模型,默认为false
141145
- base_url: OneAPI/NewAPI/OneHub的基础URL,如果使用host模式,可以直接使用http://localhost:3000,目前只有OneHub需要填写
142146
- system_token: 系统Token,目前只有OneHub需要填写
143147
- uptime-kuma: Uptime Kuma的配置,status为`enabled``disabled`,model_url和channel_url为模型和渠道的可用性Push URL

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ The configuration file is `config.json` located in the same directory, with the
8686
"exclude_model": ["advanced-voice", "minimax_s2v-01", "minimax_video-01", "minimax_video-01-live2d"],
8787
"models": ["gpt-3.5-turbo", "gpt-4o"],
8888
"force_models": false,
89+
"force_inside_models": false,
8990
"time_period": "1h",
9091
"max_concurrent": 5,
9192
"rps": 5,
9293
"db_type": "YOUR_DB_TYPE",
9394
"db_dsn": "YOUR_DB_DSN",
95+
"do_not_modify_db": false,
9496
"base_url": "http://localhost:3000",
9597
"system_token": "YOUR_SYSTEM_TOKEN",
9698
"uptime-kuma": {
@@ -132,11 +134,13 @@ Configuration explanation:
132134
- exclude_model: IDs of models to exclude from monitoring
133135
- models: List of models, used only when unable to retrieve models from the channel (/v1/models)
134136
- force_models: If true, only the above models will be tested, and channel models will not be fetched. Default is false
137+
- force_inside_models: If true, only the models set in OneAPI will be tested, and the model list will not be fetched. Default is false. If force_models is true, this option is invalid.
135138
- time_period: Interval for testing model availability, recommended not less than 30 minutes, accepts time formats s, m, h
136139
- max_concurrency: Maximum number of concurrent tests within a channel, default is 5
137140
- rps: Requests per second within a channel, default is 5
138141
- db_type: Database type, including mysql, sqlite, postgres, sqlserver
139142
- db_dsn: Database DSN string, the format varies by database type. Examples below
143+
- do_not_modify_db: If true, the available models in the database will not be modified. Default is false
140144
- base_url: The base URL for OneAPI/NewAPI/OneHub. If using host mode, you can directly use http://localhost:3000. Currently, only OneHub requires this field.
141145
- system_token: System token, currently only required for OneHub.
142146
- uptime-kuma: Configuration for Uptime Kuma. The status can be `enabled` or `disabled`. The model_url and channel_url are the availability Push URLs for models and channels.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.4.5
1+
v0.4.6

config.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import (
77
)
88

99
type Config struct {
10-
OneAPIType string `json:"oneapi_type"`
11-
ExcludeChannel []int `json:"exclude_channel"`
12-
ExcludeModel []string `json:"exclude_model"`
13-
Models []string `json:"models"`
14-
ForceModels bool `json:"force_models"`
15-
TimePeriod string `json:"time_period"`
16-
MaxConcurrent int `json:"max_concurrent"`
17-
RPS int `json:"rps"`
18-
DbType string `json:"db_type"`
19-
DbDsn string `json:"db_dsn"`
20-
BaseURL string `json:"base_url"`
21-
SystemToken string `json:"system_token"`
22-
UptimeKuma struct {
10+
OneAPIType string `json:"oneapi_type"`
11+
ExcludeChannel []int `json:"exclude_channel"`
12+
ExcludeModel []string `json:"exclude_model"`
13+
Models []string `json:"models"`
14+
ForceModels bool `json:"force_models"`
15+
ForceInsideModels bool `json:"force_inside_models"`
16+
TimePeriod string `json:"time_period"`
17+
MaxConcurrent int `json:"max_concurrent"`
18+
RPS int `json:"rps"`
19+
DbType string `json:"db_type"`
20+
DbDsn string `json:"db_dsn"`
21+
DoNotModifyDb bool `json:"do_not_modify_db"`
22+
BaseURL string `json:"base_url"`
23+
SystemToken string `json:"system_token"`
24+
UptimeKuma struct {
2325
Status string `json:"status"`
2426
ModelURL map[string]string `json:"model_url"`
2527
ChannelURL map[string]string `json:"channel_url"`

config_example.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"exclude_model": ["advanced-voice", "minimax_s2v-01", "minimax_video-01", "minimax_video-01-live2d"],
55
"models": ["gpt-3.5-turbo", "gpt-4o"],
66
"force_models": false,
7+
"force_inside_models": false,
78
"time_period": "1h",
89
"max_concurrent": 5,
910
"rps": 5,
1011
"db_type": "YOUR_DB_TYPE",
1112
"db_dsn": "YOUR_DB_DSN",
13+
"do_not_modify_db": false,
1214
"base_url": "http://localhost:3000",
1315
"system_token": "YOUR_SYSTEM_TOKEN",
1416
"uptime-kuma": {

main.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,45 +101,56 @@ func testModels(channel Channel, wg *sync.WaitGroup, mu *sync.Mutex) {
101101
log.Println("强制使用自定义模型列表")
102102
modelList = config.Models
103103
} else {
104-
// 从/v1/models接口获取模型列表
105-
req, err := http.NewRequest("GET", channel.BaseURL+"/v1/models", nil)
106-
if err != nil {
107-
log.Printf("创建请求失败:%v\n", err)
108-
return
109-
}
110-
req.Header.Set("Authorization", "Bearer "+channel.Key)
111-
112-
client := &http.Client{}
113-
resp, err := client.Do(req)
114-
if err != nil {
115-
log.Println("获取模型列表失败:", err, "尝试自定义模型列表")
116-
modelList = config.Models
104+
if config.ForceInsideModels {
105+
log.Println("强制使用内置模型列表")
106+
// 从数据库获取模型列表
107+
var models string
108+
if err := db.Raw("SELECT models FROM channels WHERE id = ?", channel.ID).Scan(&models).Error; err != nil {
109+
log.Printf("获取渠道 %s(ID:%d) 的模型列表失败:%v\n", channel.Name, channel.ID, err)
110+
return
111+
}
112+
modelList = strings.Split(models, ",")
117113
} else {
118-
defer resp.Body.Close()
119-
body, _ := ioutil.ReadAll(resp.Body)
120-
if resp.StatusCode != http.StatusOK {
121-
log.Printf("获取模型列表失败,状态码:%d,响应:%s\n", resp.StatusCode, string(body))
114+
// 从/v1/models接口获取模型列表
115+
req, err := http.NewRequest("GET", channel.BaseURL+"/v1/models", nil)
116+
if err != nil {
117+
log.Printf("创建请求失败:%v\n", err)
122118
return
123119
}
120+
req.Header.Set("Authorization", "Bearer "+channel.Key)
124121

125-
// 解析响应JSON
126-
var response struct {
127-
Data []struct {
128-
ID string `json:"id"`
129-
} `json:"data"`
130-
}
122+
client := &http.Client{}
123+
resp, err := client.Do(req)
124+
if err != nil {
125+
log.Println("获取模型列表失败:", err, "尝试自定义模型列表")
126+
modelList = config.Models
127+
} else {
128+
defer resp.Body.Close()
129+
body, _ := ioutil.ReadAll(resp.Body)
130+
if resp.StatusCode != http.StatusOK {
131+
log.Printf("获取模型列表失败,状态码:%d,响应:%s\n", resp.StatusCode, string(body))
132+
return
133+
}
131134

132-
if err := json.Unmarshal(body, &response); err != nil {
133-
log.Printf("解析模型列表失败:%v\n", err)
134-
return
135-
}
136-
// 提取模型ID列表
137-
for _, model := range response.Data {
138-
if containsString(config.ExcludeModel, model.ID) {
139-
log.Printf("模型 %s 在排除列表中,跳过\n", model.ID)
140-
continue
135+
// 解析响应JSON
136+
var response struct {
137+
Data []struct {
138+
ID string `json:"id"`
139+
} `json:"data"`
140+
}
141+
142+
if err := json.Unmarshal(body, &response); err != nil {
143+
log.Printf("解析模型列表失败:%v\n", err)
144+
return
145+
}
146+
// 提取模型ID列表
147+
for _, model := range response.Data {
148+
if containsString(config.ExcludeModel, model.ID) {
149+
log.Printf("模型 %s 在排除列表中,跳过\n", model.ID)
150+
continue
151+
}
152+
modelList = append(modelList, model.ID)
141153
}
142-
modelList = append(modelList, model.ID)
143154
}
144155
}
145156
}
@@ -220,6 +231,10 @@ func testModels(channel Channel, wg *sync.WaitGroup, mu *sync.Mutex) {
220231
modelWg.Wait()
221232

222233
// 更新模型
234+
if config.DoNotModifyDb {
235+
log.Println("跳过数据库更新")
236+
return
237+
}
223238
mu.Lock()
224239
err := updateModels(channel.ID, availableModels, channel.ModelMapping)
225240
mu.Unlock()

0 commit comments

Comments
 (0)