mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
fix(codex): convert system role to developer for codex input
This commit is contained in:
@@ -2,7 +2,9 @@ package responses
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
@@ -20,5 +22,31 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte,
|
||||
rawJSON, _ = sjson.DeleteBytes(rawJSON, "top_p")
|
||||
rawJSON, _ = sjson.DeleteBytes(rawJSON, "service_tier")
|
||||
|
||||
// Convert role "system" to "developer" in input array to comply with Codex API requirements.
|
||||
rawJSON = convertSystemRoleToDeveloper(rawJSON)
|
||||
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
// convertSystemRoleToDeveloper traverses the input array and converts any message items
|
||||
// with role "system" to role "developer". This is necessary because Codex API does not
|
||||
// accept "system" role in the input array.
|
||||
func convertSystemRoleToDeveloper(rawJSON []byte) []byte {
|
||||
inputResult := gjson.GetBytes(rawJSON, "input")
|
||||
if !inputResult.IsArray() {
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
inputArray := inputResult.Array()
|
||||
result := rawJSON
|
||||
|
||||
// Directly modify role values for items with "system" role
|
||||
for i := 0; i < len(inputArray); i++ {
|
||||
rolePath := fmt.Sprintf("input.%d.role", i)
|
||||
if gjson.GetBytes(result, rolePath).String() == "system" {
|
||||
result, _ = sjson.SetBytes(result, rolePath, "developer")
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user