openai_response.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package dto
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/types"
  7. )
  8. const (
  9. ResponsesOutputTypeImageGenerationCall = "image_generation_call"
  10. )
  11. type SimpleResponse struct {
  12. Usage `json:"usage"`
  13. Error any `json:"error"`
  14. }
  15. // GetOpenAIError 从动态错误类型中提取OpenAIError结构
  16. func (s *SimpleResponse) GetOpenAIError() *types.OpenAIError {
  17. return GetOpenAIError(s.Error)
  18. }
  19. type TextResponse struct {
  20. Id string `json:"id"`
  21. Object string `json:"object"`
  22. Created int64 `json:"created"`
  23. Model string `json:"model"`
  24. Choices []OpenAITextResponseChoice `json:"choices"`
  25. Usage `json:"usage"`
  26. }
  27. type OpenAITextResponseChoice struct {
  28. Index int `json:"index"`
  29. Message `json:"message"`
  30. FinishReason string `json:"finish_reason"`
  31. }
  32. type OpenAITextResponse struct {
  33. Id string `json:"id"`
  34. Model string `json:"model"`
  35. Object string `json:"object"`
  36. Created any `json:"created"`
  37. Choices []OpenAITextResponseChoice `json:"choices"`
  38. Error any `json:"error,omitempty"`
  39. Usage `json:"usage"`
  40. }
  41. // GetOpenAIError 从动态错误类型中提取OpenAIError结构
  42. func (o *OpenAITextResponse) GetOpenAIError() *types.OpenAIError {
  43. return GetOpenAIError(o.Error)
  44. }
  45. type OpenAIEmbeddingResponseItem struct {
  46. Object string `json:"object"`
  47. Index int `json:"index"`
  48. Embedding []float64 `json:"embedding"`
  49. }
  50. type OpenAIEmbeddingResponse struct {
  51. Object string `json:"object"`
  52. Data []OpenAIEmbeddingResponseItem `json:"data"`
  53. Model string `json:"model"`
  54. Usage `json:"usage"`
  55. }
  56. type FlexibleEmbeddingResponseItem struct {
  57. Object string `json:"object"`
  58. Index int `json:"index"`
  59. Embedding any `json:"embedding"`
  60. }
  61. type FlexibleEmbeddingResponse struct {
  62. Object string `json:"object"`
  63. Data []FlexibleEmbeddingResponseItem `json:"data"`
  64. Model string `json:"model"`
  65. Usage `json:"usage"`
  66. }
  67. type ChatCompletionsStreamResponseChoice struct {
  68. Delta ChatCompletionsStreamResponseChoiceDelta `json:"delta,omitempty"`
  69. Logprobs *any `json:"logprobs"`
  70. FinishReason *string `json:"finish_reason"`
  71. Index int `json:"index"`
  72. }
  73. type ChatCompletionsStreamResponseChoiceDelta struct {
  74. Content *string `json:"content,omitempty"`
  75. ReasoningContent *string `json:"reasoning_content,omitempty"`
  76. Reasoning *string `json:"reasoning,omitempty"`
  77. Role string `json:"role,omitempty"`
  78. ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"`
  79. }
  80. func (c *ChatCompletionsStreamResponseChoiceDelta) SetContentString(s string) {
  81. c.Content = &s
  82. }
  83. func (c *ChatCompletionsStreamResponseChoiceDelta) GetContentString() string {
  84. if c.Content == nil {
  85. return ""
  86. }
  87. return *c.Content
  88. }
  89. func (c *ChatCompletionsStreamResponseChoiceDelta) GetReasoningContent() string {
  90. if c.ReasoningContent == nil && c.Reasoning == nil {
  91. return ""
  92. }
  93. if c.ReasoningContent != nil {
  94. return *c.ReasoningContent
  95. }
  96. return *c.Reasoning
  97. }
  98. func (c *ChatCompletionsStreamResponseChoiceDelta) SetReasoningContent(s string) {
  99. c.ReasoningContent = &s
  100. //c.Reasoning = &s
  101. }
  102. type ToolCallResponse struct {
  103. // Index is not nil only in chat completion chunk object
  104. Index *int `json:"index,omitempty"`
  105. ID string `json:"id,omitempty"`
  106. Type any `json:"type"`
  107. Function FunctionResponse `json:"function"`
  108. }
  109. func (c *ToolCallResponse) SetIndex(i int) {
  110. c.Index = &i
  111. }
  112. type FunctionResponse struct {
  113. Description string `json:"description,omitempty"`
  114. Name string `json:"name,omitempty"`
  115. // call function with arguments in JSON format
  116. Parameters any `json:"parameters,omitempty"` // request
  117. Arguments string `json:"arguments"` // response
  118. }
  119. type ChatCompletionsStreamResponse struct {
  120. Id string `json:"id"`
  121. Object string `json:"object"`
  122. Created int64 `json:"created"`
  123. Model string `json:"model"`
  124. SystemFingerprint *string `json:"system_fingerprint"`
  125. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  126. Usage *Usage `json:"usage"`
  127. }
  128. func (c *ChatCompletionsStreamResponse) IsFinished() bool {
  129. if len(c.Choices) == 0 {
  130. return false
  131. }
  132. return c.Choices[0].FinishReason != nil && *c.Choices[0].FinishReason != ""
  133. }
  134. func (c *ChatCompletionsStreamResponse) IsToolCall() bool {
  135. if len(c.Choices) == 0 {
  136. return false
  137. }
  138. return len(c.Choices[0].Delta.ToolCalls) > 0
  139. }
  140. func (c *ChatCompletionsStreamResponse) GetFirstToolCall() *ToolCallResponse {
  141. if c.IsToolCall() {
  142. return &c.Choices[0].Delta.ToolCalls[0]
  143. }
  144. return nil
  145. }
  146. func (c *ChatCompletionsStreamResponse) ClearToolCalls() {
  147. if !c.IsToolCall() {
  148. return
  149. }
  150. for choiceIdx := range c.Choices {
  151. for callIdx := range c.Choices[choiceIdx].Delta.ToolCalls {
  152. c.Choices[choiceIdx].Delta.ToolCalls[callIdx].ID = ""
  153. c.Choices[choiceIdx].Delta.ToolCalls[callIdx].Type = nil
  154. c.Choices[choiceIdx].Delta.ToolCalls[callIdx].Function.Name = ""
  155. }
  156. }
  157. }
  158. func (c *ChatCompletionsStreamResponse) Copy() *ChatCompletionsStreamResponse {
  159. choices := make([]ChatCompletionsStreamResponseChoice, len(c.Choices))
  160. copy(choices, c.Choices)
  161. return &ChatCompletionsStreamResponse{
  162. Id: c.Id,
  163. Object: c.Object,
  164. Created: c.Created,
  165. Model: c.Model,
  166. SystemFingerprint: c.SystemFingerprint,
  167. Choices: choices,
  168. Usage: c.Usage,
  169. }
  170. }
  171. func (c *ChatCompletionsStreamResponse) GetSystemFingerprint() string {
  172. if c.SystemFingerprint == nil {
  173. return ""
  174. }
  175. return *c.SystemFingerprint
  176. }
  177. func (c *ChatCompletionsStreamResponse) SetSystemFingerprint(s string) {
  178. c.SystemFingerprint = &s
  179. }
  180. type ChatCompletionsStreamResponseSimple struct {
  181. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  182. Usage *Usage `json:"usage"`
  183. }
  184. type CompletionsStreamResponse struct {
  185. Choices []struct {
  186. Text string `json:"text"`
  187. FinishReason string `json:"finish_reason"`
  188. } `json:"choices"`
  189. }
  190. type Usage struct {
  191. PromptTokens int `json:"prompt_tokens"`
  192. CompletionTokens int `json:"completion_tokens"`
  193. TotalTokens int `json:"total_tokens"`
  194. PromptCacheHitTokens int `json:"prompt_cache_hit_tokens,omitempty"`
  195. UsageSemantic string `json:"usage_semantic,omitempty"`
  196. UsageSource string `json:"usage_source,omitempty"`
  197. PromptTokensDetails InputTokenDetails `json:"prompt_tokens_details"`
  198. CompletionTokenDetails OutputTokenDetails `json:"completion_tokens_details"`
  199. InputTokens int `json:"input_tokens"`
  200. OutputTokens int `json:"output_tokens"`
  201. InputTokensDetails *InputTokenDetails `json:"input_tokens_details"`
  202. // claude cache 1h
  203. ClaudeCacheCreation5mTokens int `json:"claude_cache_creation_5_m_tokens"`
  204. ClaudeCacheCreation1hTokens int `json:"claude_cache_creation_1_h_tokens"`
  205. // OpenRouter Params
  206. Cost any `json:"cost,omitempty"`
  207. }
  208. type OpenAIVideoResponse struct {
  209. Id string `json:"id" example:"file-abc123"`
  210. Object string `json:"object" example:"file"`
  211. Bytes int64 `json:"bytes" example:"120000"`
  212. CreatedAt int64 `json:"created_at" example:"1677610602"`
  213. ExpiresAt int64 `json:"expires_at" example:"1677614202"`
  214. Filename string `json:"filename" example:"mydata.jsonl"`
  215. Purpose string `json:"purpose" example:"fine-tune"`
  216. }
  217. type InputTokenDetails struct {
  218. CachedTokens int `json:"cached_tokens"`
  219. CachedCreationTokens int `json:"cached_creation_tokens,omitempty"`
  220. TextTokens int `json:"text_tokens"`
  221. AudioTokens int `json:"audio_tokens"`
  222. ImageTokens int `json:"image_tokens"`
  223. }
  224. type OutputTokenDetails struct {
  225. TextTokens int `json:"text_tokens"`
  226. AudioTokens int `json:"audio_tokens"`
  227. ImageTokens int `json:"image_tokens"`
  228. ReasoningTokens int `json:"reasoning_tokens"`
  229. }
  230. type OpenAIResponsesResponse struct {
  231. ID string `json:"id"`
  232. Object string `json:"object"`
  233. CreatedAt int `json:"created_at"`
  234. Status json.RawMessage `json:"status"`
  235. Error any `json:"error,omitempty"`
  236. IncompleteDetails *IncompleteDetails `json:"incomplete_details,omitempty"`
  237. Instructions json.RawMessage `json:"instructions"`
  238. MaxOutputTokens int `json:"max_output_tokens"`
  239. Model string `json:"model"`
  240. Output []ResponsesOutput `json:"output"`
  241. ParallelToolCalls bool `json:"parallel_tool_calls"`
  242. PreviousResponseID json.RawMessage `json:"previous_response_id"`
  243. Reasoning *Reasoning `json:"reasoning"`
  244. Store bool `json:"store"`
  245. Temperature float64 `json:"temperature"`
  246. ToolChoice json.RawMessage `json:"tool_choice"`
  247. Tools []map[string]any `json:"tools"`
  248. TopP float64 `json:"top_p"`
  249. Truncation json.RawMessage `json:"truncation"`
  250. Usage *Usage `json:"usage"`
  251. User json.RawMessage `json:"user"`
  252. Metadata json.RawMessage `json:"metadata"`
  253. }
  254. // GetOpenAIError 从动态错误类型中提取OpenAIError结构
  255. func (o *OpenAIResponsesResponse) GetOpenAIError() *types.OpenAIError {
  256. return GetOpenAIError(o.Error)
  257. }
  258. func (o *OpenAIResponsesResponse) HasImageGenerationCall() bool {
  259. if len(o.Output) == 0 {
  260. return false
  261. }
  262. for _, output := range o.Output {
  263. if output.Type == ResponsesOutputTypeImageGenerationCall {
  264. return true
  265. }
  266. }
  267. return false
  268. }
  269. func (o *OpenAIResponsesResponse) GetQuality() string {
  270. if len(o.Output) == 0 {
  271. return ""
  272. }
  273. for _, output := range o.Output {
  274. if output.Type == ResponsesOutputTypeImageGenerationCall {
  275. return output.Quality
  276. }
  277. }
  278. return ""
  279. }
  280. func (o *OpenAIResponsesResponse) GetSize() string {
  281. if len(o.Output) == 0 {
  282. return ""
  283. }
  284. for _, output := range o.Output {
  285. if output.Type == ResponsesOutputTypeImageGenerationCall {
  286. return output.Size
  287. }
  288. }
  289. return ""
  290. }
  291. type IncompleteDetails struct {
  292. Reasoning string `json:"reasoning"`
  293. }
  294. type ResponsesOutput struct {
  295. Type string `json:"type"`
  296. ID string `json:"id"`
  297. Status string `json:"status"`
  298. Role string `json:"role"`
  299. Content []ResponsesOutputContent `json:"content"`
  300. Quality string `json:"quality"`
  301. Size string `json:"size"`
  302. CallId string `json:"call_id,omitempty"`
  303. Name string `json:"name,omitempty"`
  304. Arguments json.RawMessage `json:"arguments,omitempty"`
  305. }
  306. // ArgumentsString returns function call arguments in the string form expected by Chat Completions.
  307. func (r *ResponsesOutput) ArgumentsString() string {
  308. if r == nil {
  309. return ""
  310. }
  311. return ResponsesArgumentsString(r.Arguments)
  312. }
  313. // ResponsesArgumentsString returns function call arguments in the string form expected by Chat Completions.
  314. func ResponsesArgumentsString(arguments json.RawMessage) string {
  315. return common.JsonRawMessageToString(arguments)
  316. }
  317. type ResponsesOutputContent struct {
  318. Type string `json:"type"`
  319. Text string `json:"text"`
  320. Annotations []interface{} `json:"annotations"`
  321. }
  322. type ResponsesReasoningSummaryPart struct {
  323. Type string `json:"type"`
  324. Text string `json:"text"`
  325. }
  326. const (
  327. BuildInToolWebSearchPreview = "web_search_preview"
  328. BuildInToolFileSearch = "file_search"
  329. )
  330. const (
  331. BuildInCallWebSearchCall = "web_search_call"
  332. )
  333. const (
  334. ResponsesOutputTypeItemAdded = "response.output_item.added"
  335. ResponsesOutputTypeItemDone = "response.output_item.done"
  336. )
  337. // ResponsesStreamResponse 用于处理 /v1/responses 流式响应
  338. type ResponsesStreamResponse struct {
  339. Type string `json:"type"`
  340. Response *OpenAIResponsesResponse `json:"response,omitempty"`
  341. Delta string `json:"delta,omitempty"`
  342. Item *ResponsesOutput `json:"item,omitempty"`
  343. // - response.function_call_arguments.delta
  344. // - response.function_call_arguments.done
  345. OutputIndex *int `json:"output_index,omitempty"`
  346. ContentIndex *int `json:"content_index,omitempty"`
  347. SummaryIndex *int `json:"summary_index,omitempty"`
  348. ItemID string `json:"item_id,omitempty"`
  349. Part *ResponsesReasoningSummaryPart `json:"part,omitempty"`
  350. }
  351. // GetOpenAIError 从动态错误类型中提取OpenAIError结构
  352. func GetOpenAIError(errorField any) *types.OpenAIError {
  353. if errorField == nil {
  354. return nil
  355. }
  356. switch err := errorField.(type) {
  357. case types.OpenAIError:
  358. return &err
  359. case *types.OpenAIError:
  360. return err
  361. case map[string]interface{}:
  362. // 处理从JSON解析来的map结构
  363. openaiErr := &types.OpenAIError{}
  364. if errType, ok := err["type"].(string); ok {
  365. openaiErr.Type = errType
  366. }
  367. if errMsg, ok := err["message"].(string); ok {
  368. openaiErr.Message = errMsg
  369. }
  370. if errParam, ok := err["param"].(string); ok {
  371. openaiErr.Param = errParam
  372. }
  373. if errCode, ok := err["code"]; ok {
  374. openaiErr.Code = errCode
  375. }
  376. return openaiErr
  377. case string:
  378. // 处理简单字符串错误
  379. return &types.OpenAIError{
  380. Type: "error",
  381. Message: err,
  382. }
  383. default:
  384. // 未知类型,尝试转换为字符串
  385. return &types.OpenAIError{
  386. Type: "unknown_error",
  387. Message: fmt.Sprintf("%v", err),
  388. }
  389. }
  390. }