response.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package model
  2. import (
  3. "github.com/labring/aiproxy/core/model"
  4. )
  5. // ResponseStatus represents the status of a response
  6. type ResponseStatus string
  7. const (
  8. ResponseStatusInProgress ResponseStatus = "in_progress"
  9. ResponseStatusCompleted ResponseStatus = "completed"
  10. ResponseStatusFailed ResponseStatus = "failed"
  11. ResponseStatusIncomplete ResponseStatus = "incomplete"
  12. ResponseStatusCancelled ResponseStatus = "cancelled"
  13. )
  14. // ResponseError represents an error in a response
  15. type ResponseError struct {
  16. Code string `json:"code"`
  17. Message string `json:"message"`
  18. }
  19. // IncompleteDetails represents details about why a response is incomplete
  20. type IncompleteDetails struct {
  21. Reason string `json:"reason"`
  22. }
  23. // ResponseReasoning represents reasoning information
  24. type ResponseReasoning struct {
  25. Effort *string `json:"effort"`
  26. Summary *string `json:"summary"`
  27. }
  28. // ResponseTextFormat represents text format configuration
  29. type ResponseTextFormat struct {
  30. Type string `json:"type"`
  31. }
  32. // ResponseText represents text configuration
  33. type ResponseText struct {
  34. Format ResponseTextFormat `json:"format"`
  35. }
  36. // OutputContent represents content in an output item
  37. type OutputContent struct {
  38. Type string `json:"type"`
  39. Text string `json:"text,omitempty"`
  40. Annotations []any `json:"annotations,omitempty"`
  41. }
  42. // OutputItem represents an output item in a response
  43. type OutputItem struct {
  44. ID string `json:"id"`
  45. Type string `json:"type"`
  46. Status ResponseStatus `json:"status,omitempty"`
  47. Role string `json:"role"`
  48. Content []OutputContent `json:"content"`
  49. }
  50. // InputContent represents content in an input item
  51. type InputContent struct {
  52. Type string `json:"type"`
  53. Text string `json:"text,omitempty"`
  54. }
  55. // InputItem represents an input item
  56. type InputItem struct {
  57. ID string `json:"id"`
  58. Type string `json:"type"`
  59. Role string `json:"role"`
  60. Content []InputContent `json:"content"`
  61. }
  62. // ResponseUsageDetails represents detailed token usage information
  63. type ResponseUsageDetails struct {
  64. CachedTokens int64 `json:"cached_tokens,omitempty"`
  65. ReasoningTokens int64 `json:"reasoning_tokens,omitempty"`
  66. }
  67. // ResponseUsage represents usage information for a response
  68. type ResponseUsage struct {
  69. InputTokens int64 `json:"input_tokens"`
  70. OutputTokens int64 `json:"output_tokens"`
  71. TotalTokens int64 `json:"total_tokens"`
  72. InputTokensDetails *ResponseUsageDetails `json:"input_tokens_details,omitempty"`
  73. OutputTokensDetails *ResponseUsageDetails `json:"output_tokens_details,omitempty"`
  74. }
  75. // Response represents an OpenAI response object
  76. type Response struct {
  77. ID string `json:"id"`
  78. Object string `json:"object"`
  79. CreatedAt int64 `json:"created_at"`
  80. Status ResponseStatus `json:"status"`
  81. Error *ResponseError `json:"error"`
  82. IncompleteDetails *IncompleteDetails `json:"incomplete_details"`
  83. Instructions *string `json:"instructions"`
  84. MaxOutputTokens *int `json:"max_output_tokens"`
  85. Model string `json:"model"`
  86. Output []OutputItem `json:"output"`
  87. ParallelToolCalls bool `json:"parallel_tool_calls"`
  88. PreviousResponseID *string `json:"previous_response_id"`
  89. Reasoning ResponseReasoning `json:"reasoning"`
  90. Store bool `json:"store"`
  91. Temperature float64 `json:"temperature"`
  92. Text ResponseText `json:"text"`
  93. ToolChoice any `json:"tool_choice"`
  94. Tools []Tool `json:"tools"`
  95. TopP float64 `json:"top_p"`
  96. Truncation string `json:"truncation"`
  97. Usage *ResponseUsage `json:"usage"`
  98. User *string `json:"user"`
  99. Metadata map[string]any `json:"metadata"`
  100. }
  101. // CreateResponseRequest represents a request to create a response
  102. type CreateResponseRequest struct {
  103. Model string `json:"model"`
  104. Messages []Message `json:"messages"`
  105. Instructions *string `json:"instructions,omitempty"`
  106. MaxOutputTokens *int `json:"max_output_tokens,omitempty"`
  107. ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
  108. PreviousResponseID *string `json:"previous_response_id,omitempty"`
  109. Store *bool `json:"store,omitempty"`
  110. Temperature *float64 `json:"temperature,omitempty"`
  111. ToolChoice any `json:"tool_choice,omitempty"`
  112. Tools []Tool `json:"tools,omitempty"`
  113. TopP *float64 `json:"top_p,omitempty"`
  114. Truncation *string `json:"truncation,omitempty"`
  115. User *string `json:"user,omitempty"`
  116. Metadata map[string]any `json:"metadata,omitempty"`
  117. Stream bool `json:"stream,omitempty"`
  118. }
  119. // InputItemList represents a list of input items
  120. type InputItemList struct {
  121. Object string `json:"object"`
  122. Data []InputItem `json:"data"`
  123. FirstID string `json:"first_id"`
  124. LastID string `json:"last_id"`
  125. HasMore bool `json:"has_more"`
  126. }
  127. // ResponseStreamEvent represents a server-sent event for response streaming
  128. type ResponseStreamEvent struct {
  129. Type string `json:"type"`
  130. Response *Response `json:"response,omitempty"`
  131. OutputIndex *int `json:"output_index,omitempty"`
  132. Item *OutputItem `json:"item,omitempty"`
  133. SequenceNumber int `json:"sequence_number,omitempty"`
  134. }
  135. func (u *ResponseUsage) ToModelUsage() model.Usage {
  136. usage := model.Usage{
  137. InputTokens: model.ZeroNullInt64(u.InputTokens),
  138. OutputTokens: model.ZeroNullInt64(u.OutputTokens),
  139. TotalTokens: model.ZeroNullInt64(u.TotalTokens),
  140. }
  141. if u.InputTokensDetails != nil {
  142. usage.CachedTokens = model.ZeroNullInt64(u.InputTokensDetails.CachedTokens)
  143. }
  144. if u.OutputTokensDetails != nil {
  145. usage.ReasoningTokens = model.ZeroNullInt64(u.OutputTokensDetails.ReasoningTokens)
  146. }
  147. return usage
  148. }