| package builtin |
|
|
| import ( |
| "strings" |
|
|
| "github.com/bytedance/sonic" |
| ) |
|
|
| |
| |
| |
| func buildOpenAISampling(req openAIChatRequest) *samplingParams { |
| sp := &samplingParams{ |
| Temperature: req.Temperature, |
| TopP: req.TopP, |
| TopK: req.TopK, |
| Seed: req.Seed, |
| FrequencyPenalty: req.FrequencyPenalty, |
| PresencePenalty: req.PresencePenalty, |
| User: strings.TrimSpace(req.User), |
| ReasoningEffort: strings.TrimSpace(strings.ToLower(req.ReasoningEffort)), |
| } |
| switch { |
| case req.MaxCompletionTokens != nil: |
| sp.MaxTokens = req.MaxCompletionTokens |
| case req.MaxTokens != nil: |
| sp.MaxTokens = req.MaxTokens |
| } |
| sp.Stop = parseStopSequences(req.Stop) |
| if samplingParamsIsZero(sp) { |
| return nil |
| } |
| return sp |
| } |
|
|
| |
| |
| func buildCodexSampling(req codexRequest) *samplingParams { |
| sp := &samplingParams{ |
| Temperature: req.Temperature, |
| TopP: req.TopP, |
| TopK: req.TopK, |
| MaxTokens: req.MaxOutputTokens, |
| Seed: req.Seed, |
| FrequencyPenalty: req.FrequencyPenalty, |
| PresencePenalty: req.PresencePenalty, |
| User: strings.TrimSpace(req.User), |
| } |
| if req.Reasoning != nil { |
| sp.ReasoningEffort = strings.ToLower(strings.TrimSpace(req.Reasoning.Effort)) |
| } |
| sp.Stop = parseStopSequences(req.Stop) |
| if samplingParamsIsZero(sp) { |
| return nil |
| } |
| return sp |
| } |
|
|
| |
| |
| func parseStopSequences(raw []byte) []string { |
| if len(raw) == 0 { |
| return nil |
| } |
| var asSlice []string |
| if err := sonic.Unmarshal(raw, &asSlice); err == nil { |
| out := asSlice[:0] |
| for _, s := range asSlice { |
| if s = strings.TrimSpace(s); s != "" { |
| out = append(out, s) |
| } |
| } |
| if len(out) == 0 { |
| return nil |
| } |
| return out |
| } |
| var asString string |
| if err := sonic.Unmarshal(raw, &asString); err == nil { |
| if s := strings.TrimSpace(asString); s != "" { |
| return []string{s} |
| } |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func openAIReasoningEffortToThinking(effort string) *anthropicThinkingConfig { |
| switch strings.ToLower(strings.TrimSpace(effort)) { |
| case "": |
| return nil |
| case "none": |
| return &anthropicThinkingConfig{Type: "disabled"} |
| case "minimal", "low": |
| return &anthropicThinkingConfig{Type: "enabled", BudgetTokens: 1024} |
| case "medium", "auto": |
| return &anthropicThinkingConfig{Type: "enabled", BudgetTokens: 4096} |
| case "high": |
| return &anthropicThinkingConfig{Type: "enabled", BudgetTokens: 16384} |
| default: |
| return &anthropicThinkingConfig{Type: "enabled", BudgetTokens: 4096} |
| } |
| } |
|
|
| func samplingParamsIsZero(sp *samplingParams) bool { |
| if sp == nil { |
| return true |
| } |
| return sp.Temperature == nil && sp.TopP == nil && sp.TopK == nil && |
| sp.MaxTokens == nil && len(sp.Stop) == 0 && sp.ReasoningEffort == "" && |
| sp.Seed == nil && sp.FrequencyPenalty == nil && sp.PresencePenalty == nil && |
| sp.User == "" |
| } |
|
|