openai_response.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package dto
  2. type TextResponseWithError struct {
  3. Id string `json:"id"`
  4. Object string `json:"object"`
  5. Created int64 `json:"created"`
  6. Choices []OpenAITextResponseChoice `json:"choices"`
  7. Data []OpenAIEmbeddingResponseItem `json:"data"`
  8. Model string `json:"model"`
  9. Usage `json:"usage"`
  10. Error OpenAIError `json:"error"`
  11. }
  12. type SimpleResponse struct {
  13. Usage `json:"usage"`
  14. Error OpenAIError `json:"error"`
  15. Choices []OpenAITextResponseChoice `json:"choices"`
  16. }
  17. type TextResponse struct {
  18. Id string `json:"id"`
  19. Object string `json:"object"`
  20. Created int64 `json:"created"`
  21. Model string `json:"model"`
  22. Choices []OpenAITextResponseChoice `json:"choices"`
  23. Usage `json:"usage"`
  24. }
  25. type OpenAITextResponseChoice struct {
  26. Index int `json:"index"`
  27. Message `json:"message"`
  28. FinishReason string `json:"finish_reason"`
  29. }
  30. type OpenAITextResponse struct {
  31. Id string `json:"id"`
  32. Model string `json:"model"`
  33. Object string `json:"object"`
  34. Created int64 `json:"created"`
  35. Choices []OpenAITextResponseChoice `json:"choices"`
  36. Usage `json:"usage"`
  37. }
  38. type OpenAIEmbeddingResponseItem struct {
  39. Object string `json:"object"`
  40. Index int `json:"index"`
  41. Embedding []float64 `json:"embedding"`
  42. }
  43. type OpenAIEmbeddingResponse struct {
  44. Object string `json:"object"`
  45. Data []OpenAIEmbeddingResponseItem `json:"data"`
  46. Model string `json:"model"`
  47. Usage `json:"usage"`
  48. }
  49. type ChatCompletionsStreamResponseChoice struct {
  50. Delta ChatCompletionsStreamResponseChoiceDelta `json:"delta,omitempty"`
  51. Logprobs *any `json:"logprobs"`
  52. FinishReason *string `json:"finish_reason"`
  53. Index int `json:"index"`
  54. }
  55. type ChatCompletionsStreamResponseChoiceDelta struct {
  56. Content *string `json:"content,omitempty"`
  57. ReasoningContent *string `json:"reasoning_content,omitempty"`
  58. Reasoning *string `json:"reasoning,omitempty"`
  59. Role string `json:"role,omitempty"`
  60. ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"`
  61. }
  62. func (c *ChatCompletionsStreamResponseChoiceDelta) SetContentString(s string) {
  63. c.Content = &s
  64. }
  65. func (c *ChatCompletionsStreamResponseChoiceDelta) GetContentString() string {
  66. if c.Content == nil {
  67. return ""
  68. }
  69. return *c.Content
  70. }
  71. func (c *ChatCompletionsStreamResponseChoiceDelta) GetReasoningContent() string {
  72. if c.ReasoningContent == nil && c.Reasoning == nil {
  73. return ""
  74. }
  75. if c.ReasoningContent != nil {
  76. return *c.ReasoningContent
  77. }
  78. return *c.Reasoning
  79. }
  80. func (c *ChatCompletionsStreamResponseChoiceDelta) SetReasoningContent(s string) {
  81. c.ReasoningContent = &s
  82. c.Reasoning = &s
  83. }
  84. type ToolCallResponse struct {
  85. // Index is not nil only in chat completion chunk object
  86. Index *int `json:"index,omitempty"`
  87. ID string `json:"id,omitempty"`
  88. Type any `json:"type"`
  89. Function FunctionResponse `json:"function"`
  90. }
  91. func (c *ToolCallResponse) SetIndex(i int) {
  92. c.Index = &i
  93. }
  94. type FunctionResponse struct {
  95. Description string `json:"description,omitempty"`
  96. Name string `json:"name,omitempty"`
  97. // call function with arguments in JSON format
  98. Parameters any `json:"parameters,omitempty"` // request
  99. Arguments string `json:"arguments"` // response
  100. }
  101. type ChatCompletionsStreamResponse struct {
  102. Id string `json:"id"`
  103. Object string `json:"object"`
  104. Created int64 `json:"created"`
  105. Model string `json:"model"`
  106. SystemFingerprint *string `json:"system_fingerprint"`
  107. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  108. Usage *Usage `json:"usage"`
  109. }
  110. func (c *ChatCompletionsStreamResponse) IsToolCall() bool {
  111. if len(c.Choices) == 0 {
  112. return false
  113. }
  114. return len(c.Choices[0].Delta.ToolCalls) > 0
  115. }
  116. func (c *ChatCompletionsStreamResponse) GetFirstToolCall() *ToolCallResponse {
  117. if c.IsToolCall() {
  118. return &c.Choices[0].Delta.ToolCalls[0]
  119. }
  120. return nil
  121. }
  122. func (c *ChatCompletionsStreamResponse) Copy() *ChatCompletionsStreamResponse {
  123. choices := make([]ChatCompletionsStreamResponseChoice, len(c.Choices))
  124. copy(choices, c.Choices)
  125. return &ChatCompletionsStreamResponse{
  126. Id: c.Id,
  127. Object: c.Object,
  128. Created: c.Created,
  129. Model: c.Model,
  130. SystemFingerprint: c.SystemFingerprint,
  131. Choices: choices,
  132. Usage: c.Usage,
  133. }
  134. }
  135. func (c *ChatCompletionsStreamResponse) GetSystemFingerprint() string {
  136. if c.SystemFingerprint == nil {
  137. return ""
  138. }
  139. return *c.SystemFingerprint
  140. }
  141. func (c *ChatCompletionsStreamResponse) SetSystemFingerprint(s string) {
  142. c.SystemFingerprint = &s
  143. }
  144. type ChatCompletionsStreamResponseSimple struct {
  145. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  146. Usage *Usage `json:"usage"`
  147. }
  148. type CompletionsStreamResponse struct {
  149. Choices []struct {
  150. Text string `json:"text"`
  151. FinishReason string `json:"finish_reason"`
  152. } `json:"choices"`
  153. }
  154. type Usage struct {
  155. PromptTokens int `json:"prompt_tokens"`
  156. CompletionTokens int `json:"completion_tokens"`
  157. TotalTokens int `json:"total_tokens"`
  158. PromptCacheHitTokens int `json:"prompt_cache_hit_tokens,omitempty"`
  159. PromptTokensDetails InputTokenDetails `json:"prompt_tokens_details"`
  160. CompletionTokenDetails OutputTokenDetails `json:"completion_tokens_details"`
  161. }