chat_to_responses.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package openaicompat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/dto"
  9. )
  10. func normalizeChatImageURLToString(v any) any {
  11. switch vv := v.(type) {
  12. case string:
  13. return vv
  14. case map[string]any:
  15. if url := common.Interface2String(vv["url"]); url != "" {
  16. return url
  17. }
  18. return v
  19. case dto.MessageImageUrl:
  20. if vv.Url != "" {
  21. return vv.Url
  22. }
  23. return v
  24. case *dto.MessageImageUrl:
  25. if vv != nil && vv.Url != "" {
  26. return vv.Url
  27. }
  28. return v
  29. default:
  30. return v
  31. }
  32. }
  33. func ChatCompletionsRequestToResponsesRequest(req *dto.GeneralOpenAIRequest) (*dto.OpenAIResponsesRequest, error) {
  34. if req == nil {
  35. return nil, errors.New("request is nil")
  36. }
  37. if req.Model == "" {
  38. return nil, errors.New("model is required")
  39. }
  40. if req.N > 1 {
  41. return nil, fmt.Errorf("n>1 is not supported in responses compatibility mode")
  42. }
  43. var instructionsParts []string
  44. inputItems := make([]map[string]any, 0, len(req.Messages))
  45. for _, msg := range req.Messages {
  46. role := strings.TrimSpace(msg.Role)
  47. if role == "" {
  48. continue
  49. }
  50. if role == "tool" || role == "function" {
  51. callID := strings.TrimSpace(msg.ToolCallId)
  52. var output any
  53. if msg.Content == nil {
  54. output = ""
  55. } else if msg.IsStringContent() {
  56. output = msg.StringContent()
  57. } else {
  58. if b, err := common.Marshal(msg.Content); err == nil {
  59. output = string(b)
  60. } else {
  61. output = fmt.Sprintf("%v", msg.Content)
  62. }
  63. }
  64. if callID == "" {
  65. inputItems = append(inputItems, map[string]any{
  66. "role": "user",
  67. "content": fmt.Sprintf("[tool_output_missing_call_id] %v", output),
  68. })
  69. continue
  70. }
  71. inputItems = append(inputItems, map[string]any{
  72. "type": "function_call_output",
  73. "call_id": callID,
  74. "output": output,
  75. })
  76. continue
  77. }
  78. // Prefer mapping system/developer messages into `instructions`.
  79. if role == "system" || role == "developer" {
  80. if msg.Content == nil {
  81. continue
  82. }
  83. if msg.IsStringContent() {
  84. if s := strings.TrimSpace(msg.StringContent()); s != "" {
  85. instructionsParts = append(instructionsParts, s)
  86. }
  87. continue
  88. }
  89. parts := msg.ParseContent()
  90. var sb strings.Builder
  91. for _, part := range parts {
  92. if part.Type == dto.ContentTypeText && strings.TrimSpace(part.Text) != "" {
  93. if sb.Len() > 0 {
  94. sb.WriteString("\n")
  95. }
  96. sb.WriteString(part.Text)
  97. }
  98. }
  99. if s := strings.TrimSpace(sb.String()); s != "" {
  100. instructionsParts = append(instructionsParts, s)
  101. }
  102. continue
  103. }
  104. item := map[string]any{
  105. "role": role,
  106. }
  107. if msg.Content == nil {
  108. item["content"] = ""
  109. inputItems = append(inputItems, item)
  110. if role == "assistant" {
  111. for _, tc := range msg.ParseToolCalls() {
  112. if strings.TrimSpace(tc.ID) == "" {
  113. continue
  114. }
  115. if tc.Type != "" && tc.Type != "function" {
  116. continue
  117. }
  118. name := strings.TrimSpace(tc.Function.Name)
  119. if name == "" {
  120. continue
  121. }
  122. inputItems = append(inputItems, map[string]any{
  123. "type": "function_call",
  124. "call_id": tc.ID,
  125. "name": name,
  126. "arguments": tc.Function.Arguments,
  127. })
  128. }
  129. }
  130. continue
  131. }
  132. if msg.IsStringContent() {
  133. item["content"] = msg.StringContent()
  134. inputItems = append(inputItems, item)
  135. if role == "assistant" {
  136. for _, tc := range msg.ParseToolCalls() {
  137. if strings.TrimSpace(tc.ID) == "" {
  138. continue
  139. }
  140. if tc.Type != "" && tc.Type != "function" {
  141. continue
  142. }
  143. name := strings.TrimSpace(tc.Function.Name)
  144. if name == "" {
  145. continue
  146. }
  147. inputItems = append(inputItems, map[string]any{
  148. "type": "function_call",
  149. "call_id": tc.ID,
  150. "name": name,
  151. "arguments": tc.Function.Arguments,
  152. })
  153. }
  154. }
  155. continue
  156. }
  157. parts := msg.ParseContent()
  158. contentParts := make([]map[string]any, 0, len(parts))
  159. for _, part := range parts {
  160. switch part.Type {
  161. case dto.ContentTypeText:
  162. contentParts = append(contentParts, map[string]any{
  163. "type": "input_text",
  164. "text": part.Text,
  165. })
  166. case dto.ContentTypeImageURL:
  167. contentParts = append(contentParts, map[string]any{
  168. "type": "input_image",
  169. "image_url": normalizeChatImageURLToString(part.ImageUrl),
  170. })
  171. case dto.ContentTypeInputAudio:
  172. contentParts = append(contentParts, map[string]any{
  173. "type": "input_audio",
  174. "input_audio": part.InputAudio,
  175. })
  176. case dto.ContentTypeFile:
  177. contentParts = append(contentParts, map[string]any{
  178. "type": "input_file",
  179. "file": part.File,
  180. })
  181. case dto.ContentTypeVideoUrl:
  182. contentParts = append(contentParts, map[string]any{
  183. "type": "input_video",
  184. "video_url": part.VideoUrl,
  185. })
  186. default:
  187. contentParts = append(contentParts, map[string]any{
  188. "type": part.Type,
  189. })
  190. }
  191. }
  192. item["content"] = contentParts
  193. inputItems = append(inputItems, item)
  194. if role == "assistant" {
  195. for _, tc := range msg.ParseToolCalls() {
  196. if strings.TrimSpace(tc.ID) == "" {
  197. continue
  198. }
  199. if tc.Type != "" && tc.Type != "function" {
  200. continue
  201. }
  202. name := strings.TrimSpace(tc.Function.Name)
  203. if name == "" {
  204. continue
  205. }
  206. inputItems = append(inputItems, map[string]any{
  207. "type": "function_call",
  208. "call_id": tc.ID,
  209. "name": name,
  210. "arguments": tc.Function.Arguments,
  211. })
  212. }
  213. }
  214. }
  215. inputRaw, err := common.Marshal(inputItems)
  216. if err != nil {
  217. return nil, err
  218. }
  219. var instructionsRaw json.RawMessage
  220. if len(instructionsParts) > 0 {
  221. instructions := strings.Join(instructionsParts, "\n\n")
  222. instructionsRaw, _ = common.Marshal(instructions)
  223. }
  224. var toolsRaw json.RawMessage
  225. if req.Tools != nil {
  226. tools := make([]map[string]any, 0, len(req.Tools))
  227. for _, tool := range req.Tools {
  228. switch tool.Type {
  229. case "function":
  230. tools = append(tools, map[string]any{
  231. "type": "function",
  232. "name": tool.Function.Name,
  233. "description": tool.Function.Description,
  234. "parameters": tool.Function.Parameters,
  235. })
  236. default:
  237. // Best-effort: keep original tool shape for unknown types.
  238. var m map[string]any
  239. if b, err := common.Marshal(tool); err == nil {
  240. _ = common.Unmarshal(b, &m)
  241. }
  242. if len(m) == 0 {
  243. m = map[string]any{"type": tool.Type}
  244. }
  245. tools = append(tools, m)
  246. }
  247. }
  248. toolsRaw, _ = common.Marshal(tools)
  249. }
  250. var toolChoiceRaw json.RawMessage
  251. if req.ToolChoice != nil {
  252. switch v := req.ToolChoice.(type) {
  253. case string:
  254. toolChoiceRaw, _ = common.Marshal(v)
  255. default:
  256. var m map[string]any
  257. if b, err := common.Marshal(v); err == nil {
  258. _ = common.Unmarshal(b, &m)
  259. }
  260. if m == nil {
  261. toolChoiceRaw, _ = common.Marshal(v)
  262. } else if t, _ := m["type"].(string); t == "function" {
  263. // Chat: {"type":"function","function":{"name":"..."}}
  264. // Responses: {"type":"function","name":"..."}
  265. if name, ok := m["name"].(string); ok && name != "" {
  266. toolChoiceRaw, _ = common.Marshal(map[string]any{
  267. "type": "function",
  268. "name": name,
  269. })
  270. } else if fn, ok := m["function"].(map[string]any); ok {
  271. if name, ok := fn["name"].(string); ok && name != "" {
  272. toolChoiceRaw, _ = common.Marshal(map[string]any{
  273. "type": "function",
  274. "name": name,
  275. })
  276. } else {
  277. toolChoiceRaw, _ = common.Marshal(v)
  278. }
  279. } else {
  280. toolChoiceRaw, _ = common.Marshal(v)
  281. }
  282. } else {
  283. toolChoiceRaw, _ = common.Marshal(v)
  284. }
  285. }
  286. }
  287. var parallelToolCallsRaw json.RawMessage
  288. if req.ParallelTooCalls != nil {
  289. parallelToolCallsRaw, _ = common.Marshal(*req.ParallelTooCalls)
  290. }
  291. var textRaw json.RawMessage
  292. if req.ResponseFormat != nil && req.ResponseFormat.Type != "" {
  293. textRaw, _ = common.Marshal(map[string]any{
  294. "format": req.ResponseFormat,
  295. })
  296. }
  297. maxOutputTokens := req.MaxTokens
  298. if req.MaxCompletionTokens > maxOutputTokens {
  299. maxOutputTokens = req.MaxCompletionTokens
  300. }
  301. var topP *float64
  302. if req.TopP != 0 {
  303. topP = common.GetPointer(req.TopP)
  304. }
  305. out := &dto.OpenAIResponsesRequest{
  306. Model: req.Model,
  307. Input: inputRaw,
  308. Instructions: instructionsRaw,
  309. MaxOutputTokens: maxOutputTokens,
  310. Stream: req.Stream,
  311. Temperature: req.Temperature,
  312. Text: textRaw,
  313. ToolChoice: toolChoiceRaw,
  314. Tools: toolsRaw,
  315. TopP: topP,
  316. User: req.User,
  317. ParallelToolCalls: parallelToolCallsRaw,
  318. Store: req.Store,
  319. Metadata: req.Metadata,
  320. }
  321. if req.ReasoningEffort != "" {
  322. out.Reasoning = &dto.Reasoning{
  323. Effort: req.ReasoningEffort,
  324. Summary: "detailed",
  325. }
  326. }
  327. return out, nil
  328. }