openai_request.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package dto
  2. import "encoding/json"
  3. type ResponseFormat struct {
  4. Type string `json:"type,omitempty"`
  5. }
  6. type GeneralOpenAIRequest struct {
  7. Model string `json:"model,omitempty"`
  8. Messages []Message `json:"messages,omitempty"`
  9. Prompt any `json:"prompt,omitempty"`
  10. Stream bool `json:"stream,omitempty"`
  11. StreamOptions *StreamOptions `json:"stream_options,omitempty"`
  12. MaxTokens uint `json:"max_tokens,omitempty"`
  13. MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
  14. ReasoningEffort string `json:"reasoning_effort,omitempty"`
  15. Temperature float64 `json:"temperature,omitempty"`
  16. TopP float64 `json:"top_p,omitempty"`
  17. TopK int `json:"top_k,omitempty"`
  18. Stop any `json:"stop,omitempty"`
  19. N int `json:"n,omitempty"`
  20. Input any `json:"input,omitempty"`
  21. Instruction string `json:"instruction,omitempty"`
  22. Size string `json:"size,omitempty"`
  23. Functions any `json:"functions,omitempty"`
  24. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  25. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  26. ResponseFormat any `json:"response_format,omitempty"`
  27. EncodingFormat any `json:"encoding_format,omitempty"`
  28. Seed float64 `json:"seed,omitempty"`
  29. Tools []ToolCall `json:"tools,omitempty"`
  30. ToolChoice any `json:"tool_choice,omitempty"`
  31. User string `json:"user,omitempty"`
  32. LogProbs bool `json:"logprobs,omitempty"`
  33. TopLogProbs int `json:"top_logprobs,omitempty"`
  34. Dimensions int `json:"dimensions,omitempty"`
  35. Modalities any `json:"modalities,omitempty"`
  36. Audio any `json:"audio,omitempty"`
  37. }
  38. type OpenAITools struct {
  39. Type string `json:"type"`
  40. Function OpenAIFunction `json:"function"`
  41. }
  42. type OpenAIFunction struct {
  43. Description string `json:"description,omitempty"`
  44. Name string `json:"name"`
  45. Parameters any `json:"parameters,omitempty"`
  46. }
  47. type StreamOptions struct {
  48. IncludeUsage bool `json:"include_usage,omitempty"`
  49. }
  50. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  51. return int(r.MaxTokens)
  52. }
  53. func (r GeneralOpenAIRequest) ParseInput() []string {
  54. if r.Input == nil {
  55. return nil
  56. }
  57. var input []string
  58. switch r.Input.(type) {
  59. case string:
  60. input = []string{r.Input.(string)}
  61. case []any:
  62. input = make([]string, 0, len(r.Input.([]any)))
  63. for _, item := range r.Input.([]any) {
  64. if str, ok := item.(string); ok {
  65. input = append(input, str)
  66. }
  67. }
  68. }
  69. return input
  70. }
  71. type Message struct {
  72. Role string `json:"role"`
  73. Content json.RawMessage `json:"content"`
  74. Name *string `json:"name,omitempty"`
  75. ToolCalls any `json:"tool_calls,omitempty"`
  76. ToolCallId string `json:"tool_call_id,omitempty"`
  77. }
  78. type MediaMessage struct {
  79. Type string `json:"type"`
  80. Text string `json:"text"`
  81. ImageUrl any `json:"image_url,omitempty"`
  82. InputAudio any `json:"input_audio,omitempty"`
  83. }
  84. type MessageImageUrl struct {
  85. Url string `json:"url"`
  86. Detail string `json:"detail"`
  87. }
  88. type MessageInputAudio struct {
  89. Data string `json:"data"` //base64
  90. Format string `json:"format"`
  91. }
  92. const (
  93. ContentTypeText = "text"
  94. ContentTypeImageURL = "image_url"
  95. ContentTypeInputAudio = "input_audio"
  96. )
  97. func (m Message) StringContent() string {
  98. var stringContent string
  99. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  100. return stringContent
  101. }
  102. return string(m.Content)
  103. }
  104. func (m *Message) SetStringContent(content string) {
  105. jsonContent, _ := json.Marshal(content)
  106. m.Content = jsonContent
  107. }
  108. func (m Message) IsStringContent() bool {
  109. var stringContent string
  110. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  111. return true
  112. }
  113. return false
  114. }
  115. func (m Message) ParseContent() []MediaMessage {
  116. var contentList []MediaMessage
  117. var stringContent string
  118. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  119. contentList = append(contentList, MediaMessage{
  120. Type: ContentTypeText,
  121. Text: stringContent,
  122. })
  123. return contentList
  124. }
  125. var arrayContent []json.RawMessage
  126. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  127. for _, contentItem := range arrayContent {
  128. var contentMap map[string]any
  129. if err := json.Unmarshal(contentItem, &contentMap); err != nil {
  130. continue
  131. }
  132. switch contentMap["type"] {
  133. case ContentTypeText:
  134. if subStr, ok := contentMap["text"].(string); ok {
  135. contentList = append(contentList, MediaMessage{
  136. Type: ContentTypeText,
  137. Text: subStr,
  138. })
  139. }
  140. case ContentTypeImageURL:
  141. if subObj, ok := contentMap["image_url"].(map[string]any); ok {
  142. detail, ok := subObj["detail"]
  143. if ok {
  144. subObj["detail"] = detail.(string)
  145. } else {
  146. subObj["detail"] = "high"
  147. }
  148. contentList = append(contentList, MediaMessage{
  149. Type: ContentTypeImageURL,
  150. ImageUrl: MessageImageUrl{
  151. Url: subObj["url"].(string),
  152. Detail: subObj["detail"].(string),
  153. },
  154. })
  155. } else if url, ok := contentMap["image_url"].(string); ok {
  156. contentList = append(contentList, MediaMessage{
  157. Type: ContentTypeImageURL,
  158. ImageUrl: MessageImageUrl{
  159. Url: url,
  160. Detail: "high",
  161. },
  162. })
  163. }
  164. case ContentTypeInputAudio:
  165. if subObj, ok := contentMap["input_audio"].(map[string]any); ok {
  166. contentList = append(contentList, MediaMessage{
  167. Type: ContentTypeInputAudio,
  168. InputAudio: MessageInputAudio{
  169. Data: subObj["data"].(string),
  170. Format: subObj["format"].(string),
  171. },
  172. })
  173. }
  174. }
  175. }
  176. return contentList
  177. }
  178. return nil
  179. }