feat: refactor extra_body handling for improved configuration parsing
This commit is contained in:
@@ -229,13 +229,14 @@ func CovertOpenAI2Gemini(c *gin.Context, textRequest dto.GeneralOpenAIRequest, i
|
|||||||
|
|
||||||
// patch extra_body
|
// patch extra_body
|
||||||
if len(textRequest.ExtraBody) > 0 {
|
if len(textRequest.ExtraBody) > 0 {
|
||||||
if !strings.HasSuffix(info.UpstreamModelName, "-nothinking") {
|
|
||||||
var extraBody map[string]interface{}
|
var extraBody map[string]interface{}
|
||||||
if err := common.Unmarshal(textRequest.ExtraBody, &extraBody); err != nil {
|
if err := common.Unmarshal(textRequest.ExtraBody, &extraBody); err != nil {
|
||||||
return nil, fmt.Errorf("invalid extra body: %w", err)
|
return nil, fmt.Errorf("invalid extra body: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// eg. {"google":{"thinking_config":{"thinking_budget":5324,"include_thoughts":true}}}
|
// eg. {"google":{"thinking_config":{"thinking_budget":5324,"include_thoughts":true}}}
|
||||||
if googleBody, ok := extraBody["google"].(map[string]interface{}); ok {
|
if googleBody, ok := extraBody["google"].(map[string]interface{}); ok {
|
||||||
|
if !strings.HasSuffix(info.UpstreamModelName, "-nothinking") {
|
||||||
adaptorWithExtraBody = true
|
adaptorWithExtraBody = true
|
||||||
// check error param name like thinkingConfig, should be thinking_config
|
// check error param name like thinkingConfig, should be thinking_config
|
||||||
if _, hasErrorParam := googleBody["thinkingConfig"]; hasErrorParam {
|
if _, hasErrorParam := googleBody["thinkingConfig"]; hasErrorParam {
|
||||||
@@ -247,15 +248,58 @@ func CovertOpenAI2Gemini(c *gin.Context, textRequest dto.GeneralOpenAIRequest, i
|
|||||||
if _, hasErrorParam := thinkingConfig["thinkingBudget"]; hasErrorParam {
|
if _, hasErrorParam := thinkingConfig["thinkingBudget"]; hasErrorParam {
|
||||||
return nil, errors.New("extra_body.google.thinking_config.thinkingBudget is not supported, use extra_body.google.thinking_config.thinking_budget instead")
|
return nil, errors.New("extra_body.google.thinking_config.thinkingBudget is not supported, use extra_body.google.thinking_config.thinking_budget instead")
|
||||||
}
|
}
|
||||||
if budget, ok := thinkingConfig["thinking_budget"].(float64); ok {
|
var hasThinkingConfig bool
|
||||||
budgetInt := int(budget)
|
var tempThinkingConfig dto.GeminiThinkingConfig
|
||||||
geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{
|
|
||||||
ThinkingBudget: common.GetPointer(budgetInt),
|
if thinkingBudget, exists := thinkingConfig["thinking_budget"]; exists {
|
||||||
IncludeThoughts: true,
|
switch v := thinkingBudget.(type) {
|
||||||
}
|
case float64:
|
||||||
|
budgetInt := int(v)
|
||||||
|
tempThinkingConfig.ThinkingBudget = common.GetPointer(budgetInt)
|
||||||
|
if budgetInt > 0 {
|
||||||
|
// 有正数预算
|
||||||
|
tempThinkingConfig.IncludeThoughts = true
|
||||||
} else {
|
} else {
|
||||||
geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{
|
// 存在但为0或负数,禁用思考
|
||||||
IncludeThoughts: true,
|
tempThinkingConfig.IncludeThoughts = false
|
||||||
|
}
|
||||||
|
hasThinkingConfig = true
|
||||||
|
default:
|
||||||
|
return nil, errors.New("extra_body.google.thinking_config.thinking_budget must be an integer")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if includeThoughts, exists := thinkingConfig["include_thoughts"]; exists {
|
||||||
|
if v, ok := includeThoughts.(bool); ok {
|
||||||
|
tempThinkingConfig.IncludeThoughts = v
|
||||||
|
hasThinkingConfig = true
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("extra_body.google.thinking_config.include_thoughts must be a boolean")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if thinkingLevel, exists := thinkingConfig["thinking_level"]; exists {
|
||||||
|
if v, ok := thinkingLevel.(string); ok {
|
||||||
|
tempThinkingConfig.ThinkingLevel = v
|
||||||
|
hasThinkingConfig = true
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("extra_body.google.thinking_config.thinking_level must be a string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasThinkingConfig {
|
||||||
|
// 避免 panic: 仅在获得配置时分配,防止后续赋值时空指针
|
||||||
|
if geminiRequest.GenerationConfig.ThinkingConfig == nil {
|
||||||
|
geminiRequest.GenerationConfig.ThinkingConfig = &tempThinkingConfig
|
||||||
|
} else {
|
||||||
|
// 如果已分配,则合并内容
|
||||||
|
if tempThinkingConfig.ThinkingBudget != nil {
|
||||||
|
geminiRequest.GenerationConfig.ThinkingConfig.ThinkingBudget = tempThinkingConfig.ThinkingBudget
|
||||||
|
}
|
||||||
|
geminiRequest.GenerationConfig.ThinkingConfig.IncludeThoughts = tempThinkingConfig.IncludeThoughts
|
||||||
|
if tempThinkingConfig.ThinkingLevel != "" {
|
||||||
|
geminiRequest.GenerationConfig.ThinkingConfig.ThinkingLevel = tempThinkingConfig.ThinkingLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,7 +338,6 @@ func CovertOpenAI2Gemini(c *gin.Context, textRequest dto.GeneralOpenAIRequest, i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !adaptorWithExtraBody {
|
if !adaptorWithExtraBody {
|
||||||
ThinkingAdaptor(&geminiRequest, info, textRequest)
|
ThinkingAdaptor(&geminiRequest, info, textRequest)
|
||||||
|
|||||||
Reference in New Issue
Block a user