openai_response.go 5.6 KB

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