openai_response.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package dto
  2. import (
  3. "encoding/json"
  4. "one-api/types"
  5. )
  6. type SimpleResponse struct {
  7. Usage `json:"usage"`
  8. Error *OpenAIError `json:"error"`
  9. }
  10. type TextResponse struct {
  11. Id string `json:"id"`
  12. Object string `json:"object"`
  13. Created int64 `json:"created"`
  14. Model string `json:"model"`
  15. Choices []OpenAITextResponseChoice `json:"choices"`
  16. Usage `json:"usage"`
  17. }
  18. type OpenAITextResponseChoice struct {
  19. Index int `json:"index"`
  20. Message `json:"message"`
  21. FinishReason string `json:"finish_reason"`
  22. }
  23. type OpenAITextResponse struct {
  24. Id string `json:"id"`
  25. Model string `json:"model"`
  26. Object string `json:"object"`
  27. Created any `json:"created"`
  28. Choices []OpenAITextResponseChoice `json:"choices"`
  29. Error *types.OpenAIError `json:"error,omitempty"`
  30. Usage `json:"usage"`
  31. }
  32. type OpenAIEmbeddingResponseItem struct {
  33. Object string `json:"object"`
  34. Index int `json:"index"`
  35. Embedding []float64 `json:"embedding"`
  36. }
  37. type OpenAIEmbeddingResponse struct {
  38. Object string `json:"object"`
  39. Data []OpenAIEmbeddingResponseItem `json:"data"`
  40. Model string `json:"model"`
  41. Usage `json:"usage"`
  42. }
  43. type ChatCompletionsStreamResponseChoice struct {
  44. Delta ChatCompletionsStreamResponseChoiceDelta `json:"delta,omitempty"`
  45. Logprobs *any `json:"logprobs"`
  46. FinishReason *string `json:"finish_reason"`
  47. Index int `json:"index"`
  48. }
  49. type ChatCompletionsStreamResponseChoiceDelta struct {
  50. Content *string `json:"content,omitempty"`
  51. ReasoningContent *string `json:"reasoning_content,omitempty"`
  52. Reasoning *string `json:"reasoning,omitempty"`
  53. Role string `json:"role,omitempty"`
  54. ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"`
  55. }
  56. func (c *ChatCompletionsStreamResponseChoiceDelta) SetContentString(s string) {
  57. c.Content = &s
  58. }
  59. func (c *ChatCompletionsStreamResponseChoiceDelta) GetContentString() string {
  60. if c.Content == nil {
  61. return ""
  62. }
  63. return *c.Content
  64. }
  65. func (c *ChatCompletionsStreamResponseChoiceDelta) GetReasoningContent() string {
  66. if c.ReasoningContent == nil && c.Reasoning == nil {
  67. return ""
  68. }
  69. if c.ReasoningContent != nil {
  70. return *c.ReasoningContent
  71. }
  72. return *c.Reasoning
  73. }
  74. func (c *ChatCompletionsStreamResponseChoiceDelta) SetReasoningContent(s string) {
  75. c.ReasoningContent = &s
  76. c.Reasoning = &s
  77. }
  78. type ToolCallResponse struct {
  79. // Index is not nil only in chat completion chunk object
  80. Index *int `json:"index,omitempty"`
  81. ID string `json:"id,omitempty"`
  82. Type any `json:"type"`
  83. Function FunctionResponse `json:"function"`
  84. }
  85. func (c *ToolCallResponse) SetIndex(i int) {
  86. c.Index = &i
  87. }
  88. type FunctionResponse struct {
  89. Description string `json:"description,omitempty"`
  90. Name string `json:"name,omitempty"`
  91. // call function with arguments in JSON format
  92. Parameters any `json:"parameters,omitempty"` // request
  93. Arguments string `json:"arguments"` // response
  94. }
  95. type ChatCompletionsStreamResponse struct {
  96. Id string `json:"id"`
  97. Object string `json:"object"`
  98. Created int64 `json:"created"`
  99. Model string `json:"model"`
  100. SystemFingerprint *string `json:"system_fingerprint"`
  101. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  102. Usage *Usage `json:"usage"`
  103. }
  104. func (c *ChatCompletionsStreamResponse) IsToolCall() bool {
  105. if len(c.Choices) == 0 {
  106. return false
  107. }
  108. return len(c.Choices[0].Delta.ToolCalls) > 0
  109. }
  110. func (c *ChatCompletionsStreamResponse) GetFirstToolCall() *ToolCallResponse {
  111. if c.IsToolCall() {
  112. return &c.Choices[0].Delta.ToolCalls[0]
  113. }
  114. return nil
  115. }
  116. func (c *ChatCompletionsStreamResponse) Copy() *ChatCompletionsStreamResponse {
  117. choices := make([]ChatCompletionsStreamResponseChoice, len(c.Choices))
  118. copy(choices, c.Choices)
  119. return &ChatCompletionsStreamResponse{
  120. Id: c.Id,
  121. Object: c.Object,
  122. Created: c.Created,
  123. Model: c.Model,
  124. SystemFingerprint: c.SystemFingerprint,
  125. Choices: choices,
  126. Usage: c.Usage,
  127. }
  128. }
  129. func (c *ChatCompletionsStreamResponse) GetSystemFingerprint() string {
  130. if c.SystemFingerprint == nil {
  131. return ""
  132. }
  133. return *c.SystemFingerprint
  134. }
  135. func (c *ChatCompletionsStreamResponse) SetSystemFingerprint(s string) {
  136. c.SystemFingerprint = &s
  137. }
  138. type ChatCompletionsStreamResponseSimple struct {
  139. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  140. Usage *Usage `json:"usage"`
  141. }
  142. type CompletionsStreamResponse struct {
  143. Choices []struct {
  144. Text string `json:"text"`
  145. FinishReason string `json:"finish_reason"`
  146. } `json:"choices"`
  147. }
  148. type Usage struct {
  149. PromptTokens int `json:"prompt_tokens"`
  150. CompletionTokens int `json:"completion_tokens"`
  151. TotalTokens int `json:"total_tokens"`
  152. PromptCacheHitTokens int `json:"prompt_cache_hit_tokens,omitempty"`
  153. PromptTokensDetails InputTokenDetails `json:"prompt_tokens_details"`
  154. CompletionTokenDetails OutputTokenDetails `json:"completion_tokens_details"`
  155. InputTokens int `json:"input_tokens"`
  156. OutputTokens int `json:"output_tokens"`
  157. InputTokensDetails *InputTokenDetails `json:"input_tokens_details"`
  158. // OpenRouter Params
  159. Cost float64 `json:"cost,omitempty"`
  160. }
  161. type InputTokenDetails struct {
  162. CachedTokens int `json:"cached_tokens"`
  163. CachedCreationTokens int `json:"-"`
  164. TextTokens int `json:"text_tokens"`
  165. AudioTokens int `json:"audio_tokens"`
  166. ImageTokens int `json:"image_tokens"`
  167. }
  168. type OutputTokenDetails struct {
  169. TextTokens int `json:"text_tokens"`
  170. AudioTokens int `json:"audio_tokens"`
  171. ReasoningTokens int `json:"reasoning_tokens"`
  172. }
  173. type OpenAIResponsesResponse struct {
  174. ID string `json:"id"`
  175. Object string `json:"object"`
  176. CreatedAt int `json:"created_at"`
  177. Status string `json:"status"`
  178. Error *types.OpenAIError `json:"error,omitempty"`
  179. IncompleteDetails *IncompleteDetails `json:"incomplete_details,omitempty"`
  180. Instructions string `json:"instructions"`
  181. MaxOutputTokens int `json:"max_output_tokens"`
  182. Model string `json:"model"`
  183. Output []ResponsesOutput `json:"output"`
  184. ParallelToolCalls bool `json:"parallel_tool_calls"`
  185. PreviousResponseID string `json:"previous_response_id"`
  186. Reasoning *Reasoning `json:"reasoning"`
  187. Store bool `json:"store"`
  188. Temperature float64 `json:"temperature"`
  189. ToolChoice string `json:"tool_choice"`
  190. Tools []ResponsesToolsCall `json:"tools"`
  191. TopP float64 `json:"top_p"`
  192. Truncation string `json:"truncation"`
  193. Usage *Usage `json:"usage"`
  194. User json.RawMessage `json:"user"`
  195. Metadata json.RawMessage `json:"metadata"`
  196. }
  197. type IncompleteDetails struct {
  198. Reasoning string `json:"reasoning"`
  199. }
  200. type ResponsesOutput struct {
  201. Type string `json:"type"`
  202. ID string `json:"id"`
  203. Status string `json:"status"`
  204. Role string `json:"role"`
  205. Content []ResponsesOutputContent `json:"content"`
  206. }
  207. type ResponsesOutputContent struct {
  208. Type string `json:"type"`
  209. Text string `json:"text"`
  210. Annotations []interface{} `json:"annotations"`
  211. }
  212. const (
  213. BuildInToolWebSearchPreview = "web_search_preview"
  214. BuildInToolFileSearch = "file_search"
  215. )
  216. const (
  217. BuildInCallWebSearchCall = "web_search_call"
  218. )
  219. const (
  220. ResponsesOutputTypeItemAdded = "response.output_item.added"
  221. ResponsesOutputTypeItemDone = "response.output_item.done"
  222. )
  223. // ResponsesStreamResponse 用于处理 /v1/responses 流式响应
  224. type ResponsesStreamResponse struct {
  225. Type string `json:"type"`
  226. Response *OpenAIResponsesResponse `json:"response,omitempty"`
  227. Delta string `json:"delta,omitempty"`
  228. Item *ResponsesOutput `json:"item,omitempty"`
  229. }