openai_response.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Role string `json:"role,omitempty"`
  59. ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"`
  60. }
  61. func (c *ChatCompletionsStreamResponseChoiceDelta) SetContentString(s string) {
  62. c.Content = &s
  63. }
  64. func (c *ChatCompletionsStreamResponseChoiceDelta) GetContentString() string {
  65. if c.Content == nil {
  66. return ""
  67. }
  68. return *c.Content
  69. }
  70. func (c *ChatCompletionsStreamResponseChoiceDelta) GetReasoningContent() string {
  71. if c.ReasoningContent == nil {
  72. return ""
  73. }
  74. return *c.ReasoningContent
  75. }
  76. func (c *ChatCompletionsStreamResponseChoiceDelta) SetReasoningContent(s string) {
  77. c.ReasoningContent = &s
  78. }
  79. type ToolCallResponse struct {
  80. // Index is not nil only in chat completion chunk object
  81. Index *int `json:"index,omitempty"`
  82. ID string `json:"id,omitempty"`
  83. Type any `json:"type"`
  84. Function FunctionResponse `json:"function"`
  85. }
  86. func (c *ToolCallResponse) SetIndex(i int) {
  87. c.Index = &i
  88. }
  89. type FunctionResponse struct {
  90. Description string `json:"description,omitempty"`
  91. Name string `json:"name,omitempty"`
  92. // call function with arguments in JSON format
  93. Parameters any `json:"parameters,omitempty"` // request
  94. Arguments string `json:"arguments"` // response
  95. }
  96. type ChatCompletionsStreamResponse struct {
  97. Id string `json:"id"`
  98. Object string `json:"object"`
  99. Created int64 `json:"created"`
  100. Model string `json:"model"`
  101. SystemFingerprint *string `json:"system_fingerprint"`
  102. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  103. Usage *Usage `json:"usage"`
  104. }
  105. func (c *ChatCompletionsStreamResponse) Copy() *ChatCompletionsStreamResponse {
  106. choices := make([]ChatCompletionsStreamResponseChoice, len(c.Choices))
  107. copy(choices, c.Choices)
  108. return &ChatCompletionsStreamResponse{
  109. Id: c.Id,
  110. Object: c.Object,
  111. Created: c.Created,
  112. Model: c.Model,
  113. SystemFingerprint: c.SystemFingerprint,
  114. Choices: choices,
  115. Usage: c.Usage,
  116. }
  117. }
  118. func (c *ChatCompletionsStreamResponse) GetSystemFingerprint() string {
  119. if c.SystemFingerprint == nil {
  120. return ""
  121. }
  122. return *c.SystemFingerprint
  123. }
  124. func (c *ChatCompletionsStreamResponse) SetSystemFingerprint(s string) {
  125. c.SystemFingerprint = &s
  126. }
  127. type ChatCompletionsStreamResponseSimple struct {
  128. Choices []ChatCompletionsStreamResponseChoice `json:"choices"`
  129. Usage *Usage `json:"usage"`
  130. }
  131. type CompletionsStreamResponse struct {
  132. Choices []struct {
  133. Text string `json:"text"`
  134. FinishReason string `json:"finish_reason"`
  135. } `json:"choices"`
  136. }
  137. type Usage struct {
  138. PromptTokens int `json:"prompt_tokens"`
  139. CompletionTokens int `json:"completion_tokens"`
  140. TotalTokens int `json:"total_tokens"`
  141. PromptTokensDetails InputTokenDetails `json:"prompt_tokens_details"`
  142. CompletionTokenDetails OutputTokenDetails `json:"completion_tokens_details"`
  143. }