@@ -61,6 +61,17 @@ func (ch *AnthropicChannel) IsStreamRequest(c *gin.Context, bodyBytes []byte) bo
|
||||
return false
|
||||
}
|
||||
|
||||
func (ch *AnthropicChannel) ExtractModel(c *gin.Context, bodyBytes []byte) string {
|
||||
type modelPayload struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
var p modelPayload
|
||||
if err := json.Unmarshal(bodyBytes, &p); err == nil {
|
||||
return p.Model
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ValidateKey checks if the given API key is valid by making a messages request.
|
||||
func (ch *AnthropicChannel) ValidateKey(ctx context.Context, key string) (bool, error) {
|
||||
upstreamURL := ch.getUpstreamURL()
|
||||
|
@@ -29,6 +29,9 @@ type ChannelProxy interface {
|
||||
// IsStreamRequest checks if the request is for a streaming response,
|
||||
IsStreamRequest(c *gin.Context, bodyBytes []byte) bool
|
||||
|
||||
// ExtractModel extracts the model name from the request.
|
||||
ExtractModel(c *gin.Context, bodyBytes []byte) string
|
||||
|
||||
// ValidateKey checks if the given API key is valid.
|
||||
ValidateKey(ctx context.Context, key string) (bool, error)
|
||||
}
|
||||
|
@@ -71,6 +71,29 @@ func (ch *GeminiChannel) IsStreamRequest(c *gin.Context, bodyBytes []byte) bool
|
||||
return false
|
||||
}
|
||||
|
||||
func (ch *GeminiChannel) ExtractModel(c *gin.Context, bodyBytes []byte) string {
|
||||
// gemini format
|
||||
path := c.Request.URL.Path
|
||||
parts := strings.Split(path, "/")
|
||||
for i, part := range parts {
|
||||
if part == "models" && i+1 < len(parts) {
|
||||
modelPart := parts[i+1]
|
||||
return strings.Split(modelPart, ":")[0]
|
||||
}
|
||||
}
|
||||
|
||||
// openai format
|
||||
type modelPayload struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
var p modelPayload
|
||||
if err := json.Unmarshal(bodyBytes, &p); err == nil && p.Model != "" {
|
||||
return strings.TrimPrefix(p.Model, "models/")
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ValidateKey checks if the given API key is valid by making a generateContent request.
|
||||
func (ch *GeminiChannel) ValidateKey(ctx context.Context, key string) (bool, error) {
|
||||
upstreamURL := ch.getUpstreamURL()
|
||||
|
@@ -60,6 +60,17 @@ func (ch *OpenAIChannel) IsStreamRequest(c *gin.Context, bodyBytes []byte) bool
|
||||
return false
|
||||
}
|
||||
|
||||
func (ch *OpenAIChannel) ExtractModel(c *gin.Context, bodyBytes []byte) string {
|
||||
type modelPayload struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
var p modelPayload
|
||||
if err := json.Unmarshal(bodyBytes, &p); err == nil {
|
||||
return p.Model
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ValidateKey checks if the given API key is valid by making a chat completion request.
|
||||
func (ch *OpenAIChannel) ValidateKey(ctx context.Context, key string) (bool, error) {
|
||||
upstreamURL := ch.getUpstreamURL()
|
||||
|
Reference in New Issue
Block a user