Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d7491096
authored
Aug 02, 2025
by
Calcium-Ion
Committed by
GitHub
Aug 02, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1486 from nekohy/fix-get-google-models
fix: correct Gemini channel model retrieval logic
parents
f4f73016
272c52b4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
3 deletions
+55
-3
controller/channel.go
+55
-3
No files found.
controller/channel.go
View file @
d7491096
...
@@ -36,11 +36,30 @@ type OpenAIModel struct {
...
@@ -36,11 +36,30 @@ type OpenAIModel struct {
Parent
string
`json:"parent"`
Parent
string
`json:"parent"`
}
}
type
GoogleOpenAICompatibleModels
[]
struct
{
Name
string
`json:"name"`
Version
string
`json:"version"`
DisplayName
string
`json:"displayName"`
Description
string
`json:"description,omitempty"`
InputTokenLimit
int
`json:"inputTokenLimit"`
OutputTokenLimit
int
`json:"outputTokenLimit"`
SupportedGenerationMethods
[]
string
`json:"supportedGenerationMethods"`
Temperature
float64
`json:"temperature,omitempty"`
TopP
float64
`json:"topP,omitempty"`
TopK
int
`json:"topK,omitempty"`
MaxTemperature
int
`json:"maxTemperature,omitempty"`
}
type
OpenAIModelsResponse
struct
{
type
OpenAIModelsResponse
struct
{
Data
[]
OpenAIModel
`json:"data"`
Data
[]
OpenAIModel
`json:"data"`
Success
bool
`json:"success"`
Success
bool
`json:"success"`
}
}
type
GoogleOpenAICompatibleResponse
struct
{
Models
[]
GoogleOpenAICompatibleModels
`json:"models"`
NextPageToken
string
`json:"nextPageToken"`
}
func
parseStatusFilter
(
statusParam
string
)
int
{
func
parseStatusFilter
(
statusParam
string
)
int
{
switch
strings
.
ToLower
(
statusParam
)
{
switch
strings
.
ToLower
(
statusParam
)
{
case
"enabled"
,
"1"
:
case
"enabled"
,
"1"
:
...
@@ -168,20 +187,52 @@ func FetchUpstreamModels(c *gin.Context) {
...
@@ -168,20 +187,52 @@ func FetchUpstreamModels(c *gin.Context) {
if
channel
.
GetBaseURL
()
!=
""
{
if
channel
.
GetBaseURL
()
!=
""
{
baseURL
=
channel
.
GetBaseURL
()
baseURL
=
channel
.
GetBaseURL
()
}
}
url
:=
fmt
.
Sprintf
(
"%s/v1/models"
,
baseURL
)
var
url
string
switch
channel
.
Type
{
switch
channel
.
Type
{
case
constant
.
ChannelTypeGemini
:
case
constant
.
ChannelTypeGemini
:
url
=
fmt
.
Sprintf
(
"%s/v1beta/openai/models"
,
baseURL
)
// curl https://example.com/v1beta/models?key=$GEMINI_API_KEY
url
=
fmt
.
Sprintf
(
"%s/v1beta/openai/models?key=%s"
,
baseURL
,
channel
.
Key
)
case
constant
.
ChannelTypeAli
:
case
constant
.
ChannelTypeAli
:
url
=
fmt
.
Sprintf
(
"%s/compatible-mode/v1/models"
,
baseURL
)
url
=
fmt
.
Sprintf
(
"%s/compatible-mode/v1/models"
,
baseURL
)
default
:
url
=
fmt
.
Sprintf
(
"%s/v1/models"
,
baseURL
)
}
// 获取响应体 - 根据渠道类型决定是否添加 AuthHeader
var
body
[]
byte
if
channel
.
Type
==
constant
.
ChannelTypeGemini
{
body
,
err
=
GetResponseBody
(
"GET"
,
url
,
channel
,
nil
)
// I don't know why, but Gemini requires no AuthHeader
}
else
{
body
,
err
=
GetResponseBody
(
"GET"
,
url
,
channel
,
GetAuthHeader
(
channel
.
Key
))
}
}
body
,
err
:=
GetResponseBody
(
"GET"
,
url
,
channel
,
GetAuthHeader
(
channel
.
Key
))
if
err
!=
nil
{
if
err
!=
nil
{
common
.
ApiError
(
c
,
err
)
common
.
ApiError
(
c
,
err
)
return
return
}
}
var
result
OpenAIModelsResponse
var
result
OpenAIModelsResponse
var
parseSuccess
bool
// 适配特殊格式
switch
channel
.
Type
{
case
constant
.
ChannelTypeGemini
:
var
googleResult
GoogleOpenAICompatibleResponse
if
err
=
json
.
Unmarshal
(
body
,
&
googleResult
);
err
==
nil
{
// 转换Google格式到OpenAI格式
for
_
,
model
:=
range
googleResult
.
Models
{
for
_
,
gModel
:=
range
model
{
result
.
Data
=
append
(
result
.
Data
,
OpenAIModel
{
ID
:
gModel
.
Name
,
})
}
}
parseSuccess
=
true
}
}
// 如果解析失败,尝试OpenAI格式
if
!
parseSuccess
{
if
err
=
json
.
Unmarshal
(
body
,
&
result
);
err
!=
nil
{
if
err
=
json
.
Unmarshal
(
body
,
&
result
);
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -189,6 +240,7 @@ func FetchUpstreamModels(c *gin.Context) {
...
@@ -189,6 +240,7 @@ func FetchUpstreamModels(c *gin.Context) {
})
})
return
return
}
}
}
var
ids
[]
string
var
ids
[]
string
for
_
,
model
:=
range
result
.
Data
{
for
_
,
model
:=
range
result
.
Data
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment