openai_response.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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) Copy() *ChatCompletionsStreamResponse {
  111. choices := make([]ChatCompletionsStreamResponseChoice, len(c.Choices))
  112. copy(choices, c.Choices)
  113. return &ChatCompletionsStreamResponse{
  114. Id: c.Id,
  115. Object: c.Object,
  116. Created: c.Created,
  117. Model: c.Model,
  118. SystemFingerprint: c.SystemFingerprint,
  119. Choices: choices,
  120. Usage: c.Usage,
  121. }
  122. }
  123. func (c *ChatCompletionsStreamResponse) GetSystemFingerprint() string {
  124. if c.SystemFingerprint == nil {
  125. return ""
  126. }
  127. return *c.SystemFingerprint
  128. }
  129. func (c *ChatCompletionsStreamResponse) SetSystemFingerprint(s string) {
  130. c.SystemFingerprint = &s
  131. }
  132. type ChatCompletionsStreamResponseSimple struct {
  133. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  134. Usage *Usage `json:"usage"`
  135. }
  136. type CompletionsStreamResponse struct {
  137. Choices []struct {
  138. Text string `json:"text"`
  139. FinishReason string `json:"finish_reason"`
  140. } `json:"choices"`
  141. }
  142. type Usage struct {
  143. PromptTokens int `json:"prompt_tokens"`
  144. CompletionTokens int `json:"completion_tokens"`
  145. TotalTokens int `json:"total_tokens"`
  146. PromptCacheHitTokens int `json:"prompt_cache_hit_tokens,omitempty"`
  147. PromptTokensDetails InputTokenDetails `json:"prompt_tokens_details"`
  148. CompletionTokenDetails OutputTokenDetails `json:"completion_tokens_details"`
  149. }