openai_request.go 6.2 KB

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