mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 04:10:51 +08:00
fix(translator): improve content handling for system and user messages
- Added support for single and array-based `content` cases. - Enhanced `system_instruction` structure population logic. - Improved handling of user role assignment for string-based `content`.
This commit is contained in:
@@ -117,19 +117,29 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte
|
|||||||
switch itemType {
|
switch itemType {
|
||||||
case "message":
|
case "message":
|
||||||
if strings.EqualFold(itemRole, "system") {
|
if strings.EqualFold(itemRole, "system") {
|
||||||
if contentArray := item.Get("content"); contentArray.Exists() && contentArray.IsArray() {
|
if contentArray := item.Get("content"); contentArray.Exists() {
|
||||||
var builder strings.Builder
|
systemInstr := ""
|
||||||
contentArray.ForEach(func(_, contentItem gjson.Result) bool {
|
if systemInstructionResult := gjson.Get(out, "system_instruction"); systemInstructionResult.Exists() {
|
||||||
text := contentItem.Get("text").String()
|
systemInstr = systemInstructionResult.Raw
|
||||||
if builder.Len() > 0 && text != "" {
|
} else {
|
||||||
builder.WriteByte('\n')
|
systemInstr = `{"parts":[]}`
|
||||||
}
|
}
|
||||||
builder.WriteString(text)
|
|
||||||
return true
|
if contentArray.IsArray() {
|
||||||
})
|
contentArray.ForEach(func(_, contentItem gjson.Result) bool {
|
||||||
if !gjson.Get(out, "system_instruction").Exists() {
|
part := `{"text":""}`
|
||||||
systemInstr := `{"parts":[{"text":""}]}`
|
text := contentItem.Get("text").String()
|
||||||
systemInstr, _ = sjson.Set(systemInstr, "parts.0.text", builder.String())
|
part, _ = sjson.Set(part, "text", text)
|
||||||
|
systemInstr, _ = sjson.SetRaw(systemInstr, "parts.-1", part)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
} else if contentArray.Type == gjson.String {
|
||||||
|
part := `{"text":""}`
|
||||||
|
part, _ = sjson.Set(part, "text", contentArray.String())
|
||||||
|
systemInstr, _ = sjson.SetRaw(systemInstr, "parts.-1", part)
|
||||||
|
}
|
||||||
|
|
||||||
|
if systemInstr != `{"parts":[]}` {
|
||||||
out, _ = sjson.SetRaw(out, "system_instruction", systemInstr)
|
out, _ = sjson.SetRaw(out, "system_instruction", systemInstr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,8 +246,22 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte
|
|||||||
})
|
})
|
||||||
|
|
||||||
flush()
|
flush()
|
||||||
}
|
} else if contentArray.Type == gjson.String {
|
||||||
|
effRole := "user"
|
||||||
|
if itemRole != "" {
|
||||||
|
switch strings.ToLower(itemRole) {
|
||||||
|
case "assistant", "model":
|
||||||
|
effRole = "model"
|
||||||
|
default:
|
||||||
|
effRole = strings.ToLower(itemRole)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
one := `{"role":"","parts":[{"text":""}]}`
|
||||||
|
one, _ = sjson.Set(one, "role", effRole)
|
||||||
|
one, _ = sjson.Set(one, "parts.0.text", contentArray.String())
|
||||||
|
out, _ = sjson.SetRaw(out, "contents.-1", one)
|
||||||
|
}
|
||||||
case "function_call":
|
case "function_call":
|
||||||
// Handle function calls - convert to model message with functionCall
|
// Handle function calls - convert to model message with functionCall
|
||||||
name := item.Get("name").String()
|
name := item.Get("name").String()
|
||||||
|
|||||||
Reference in New Issue
Block a user