openai_response.go 9.0 KB

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