@@ -40,17 +40,17 @@ func fetchChannels() ([]Channel, error) {
4040 if err := rows .Scan (& c .ID , & c .Type , & c .Name , & c .BaseURL , & c .Key , & c .Status ); err != nil {
4141 return nil , err
4242 }
43-
43+
4444 switch c .Type {
4545 case 40 :
4646 c .BaseURL = "https://api.siliconflow.cn"
4747 case 999 :
4848 c .BaseURL = "https://api.siliconflow.cn"
49- case 1 :
50- if c .BaseURL == "" {
51- c .BaseURL = "https://api.openai.com"
52- }
53- }
49+ case 1 :
50+ if c .BaseURL == "" {
51+ c .BaseURL = "https://api.openai.com"
52+ }
53+ }
5454 // 检查是否在排除列表中
5555 if contains (config .ExcludeChannel , c .ID ) {
5656 log .Printf ("渠道 %s(ID:%d) 在排除列表中,跳过\n " , c .Name , c .ID )
@@ -83,43 +83,49 @@ func containsString(slice []string, item string) bool {
8383
8484func testModels (channel Channel ) ([]string , error ) {
8585 var availableModels []string
86- // 从/v1/models接口获取模型列表
87- req , err := http .NewRequest ("GET" , channel .BaseURL + "/v1/models" , nil )
88- if err != nil {
89- return nil , fmt .Errorf ("创建请求失败:%v" , err )
90- }
91- req .Header .Set ("Authorization" , "Bearer " + channel .Key )
92-
93- client := & http.Client {}
94- resp , err := client .Do (req )
9586 modelList := []string {}
96- if err != nil {
97- log .Println ("获取模型列表失败:" , err , "尝试自定义模型列表 " )
87+ if config . ForceModels {
88+ log .Println ("强制使用自定义模型列表 " )
9889 modelList = config .Models
99- } else {
100- defer resp .Body .Close ()
101- body , _ := ioutil .ReadAll (resp .Body )
102- if resp .StatusCode != http .StatusOK {
103- return nil , fmt .Errorf ("获取模型列表失败,状态码:%d,响应:%s" , resp .StatusCode , string (body ))
90+ } else
91+ {
92+ // 从/v1/models接口获取模型列表
93+ req , err := http .NewRequest ("GET" , channel .BaseURL + "/v1/models" , nil )
94+ if err != nil {
95+ return nil , fmt .Errorf ("创建请求失败:%v" , err )
10496 }
97+ req .Header .Set ("Authorization" , "Bearer " + channel .Key )
10598
106- // 解析响应JSON
107- var response struct {
108- Data []struct {
109- ID string `json:"id"`
110- } `json:"data"`
111- }
99+ client := & http.Client {}
100+ resp , err := client .Do (req )
101+ if err != nil {
102+ log .Println ("获取模型列表失败:" , err , "尝试自定义模型列表" )
103+ modelList = config .Models
104+ } else {
105+ defer resp .Body .Close ()
106+ body , _ := ioutil .ReadAll (resp .Body )
107+ if resp .StatusCode != http .StatusOK {
108+ return nil , fmt .Errorf ("获取模型列表失败,状态码:%d,响应:%s" , resp .StatusCode , string (body ))
109+ }
112110
113- if err := json .Unmarshal (body , & response ); err != nil {
114- return nil , fmt .Errorf ("解析模型列表失败:%v" , err )
115- }
116- // 提取模型ID列表
117- for _ , model := range response .Data {
118- if containsString (config .ExcludeModel , model .ID ) {
119- log .Printf ("模型 %s 在排除列表中,跳过\n " , model .ID )
120- continue
111+ // 解析响应JSON
112+ var response struct {
113+ Data []struct {
114+ ID string `json:"id"`
115+ } `json:"data"`
116+ }
117+
118+ if err := json .Unmarshal (body , & response ); err != nil {
119+ return nil , fmt .Errorf ("解析模型列表失败:%v" , err )
120+ }
121+ // 提取模型ID列表
122+ for _ , model := range response .Data {
123+ if containsString (config .ExcludeModel , model .ID ) {
124+ log .Printf ("模型 %s 在排除列表中,跳过\n " , model .ID )
125+ continue
126+ }
127+ modelList = append (modelList , model .ID )
121128 }
122- modelList = append (modelList , model .ID )
123129 }
124130 }
125131 // 测试模型
@@ -195,7 +201,7 @@ func main() {
195201 }
196202
197203 db , err = NewDB (* config )
198-
204+
199205 if err != nil {
200206 log .Fatal ("数据库连接失败:" , err )
201207 }
0 commit comments