Skip to content

Commit 1fe2914

Browse files
authored
Merge pull request #63 from tbphp/feat-gemini-openai-compatibility
feat: 支持Gemini官方的OpenAi兼容
2 parents 29277ac + fd0a3af commit 1fe2914

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

internal/channel/gemini_channel.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ func newGeminiChannel(f *Factory, group *models.Group) (ChannelProxy, error) {
3636

3737
// ModifyRequest adds the API key as a query parameter for Gemini requests.
3838
func (ch *GeminiChannel) ModifyRequest(req *http.Request, apiKey *models.APIKey, group *models.Group) {
39-
q := req.URL.Query()
40-
q.Set("key", apiKey.KeyValue)
41-
req.URL.RawQuery = q.Encode()
39+
if strings.Contains(req.URL.Path, "v1beta/openai") {
40+
req.Header.Set("Authorization", "Bearer "+apiKey.KeyValue)
41+
} else {
42+
q := req.URL.Query()
43+
q.Set("key", apiKey.KeyValue)
44+
req.URL.RawQuery = q.Encode()
45+
}
4246
}
4347

4448
// IsStreamRequest checks if the request is for a streaming response.
@@ -56,6 +60,14 @@ func (ch *GeminiChannel) IsStreamRequest(c *gin.Context, bodyBytes []byte) bool
5660
return true
5761
}
5862

63+
type streamPayload struct {
64+
Stream bool `json:"stream"`
65+
}
66+
var p streamPayload
67+
if err := json.Unmarshal(bodyBytes, &p); err == nil {
68+
return p.Stream
69+
}
70+
5971
return false
6072
}
6173

0 commit comments

Comments
 (0)