Files
CLIProxyAPI/internal/translator/common/request.go

34 lines
839 B
Go

package common
import (
"strings"
"github.com/tidwall/gjson"
)
// RequestModelName returns the model name from the original request, falling
// back to the translated request when the original request is unavailable.
func RequestModelName(originalRequestRawJSON, requestRawJSON []byte) string {
for _, rawJSON := range [][]byte{originalRequestRawJSON, requestRawJSON} {
if modelName := requestModelName(rawJSON); modelName != "" {
return modelName
}
}
return ""
}
func requestModelName(rawJSON []byte) string {
if len(rawJSON) == 0 || !gjson.ValidBytes(rawJSON) {
return ""
}
root := gjson.ParseBytes(rawJSON)
for _, path := range []string{"model", "request.model"} {
model := root.Get(path)
if model.Type == gjson.String && strings.TrimSpace(model.String()) != "" {
return model.String()
}
}
return ""
}