openai_response.go 8.7 KB

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