openai_request.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Prefix any `json:"prefix,omitempty"`
  18. Suffix any `json:"suffix,omitempty"`
  19. Stream bool `json:"stream,omitempty"`
  20. StreamOptions *StreamOptions `json:"stream_options,omitempty"`
  21. MaxTokens uint `json:"max_tokens,omitempty"`
  22. MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
  23. ReasoningEffort string `json:"reasoning_effort,omitempty"`
  24. Temperature *float64 `json:"temperature,omitempty"`
  25. TopP float64 `json:"top_p,omitempty"`
  26. TopK int `json:"top_k,omitempty"`
  27. Stop any `json:"stop,omitempty"`
  28. N int `json:"n,omitempty"`
  29. Input any `json:"input,omitempty"`
  30. Instruction string `json:"instruction,omitempty"`
  31. Size string `json:"size,omitempty"`
  32. Functions any `json:"functions,omitempty"`
  33. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  34. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  35. ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
  36. EncodingFormat any `json:"encoding_format,omitempty"`
  37. Seed float64 `json:"seed,omitempty"`
  38. Tools []ToolCall `json:"tools,omitempty"`
  39. ToolChoice any `json:"tool_choice,omitempty"`
  40. User string `json:"user,omitempty"`
  41. LogProbs bool `json:"logprobs,omitempty"`
  42. TopLogProbs int `json:"top_logprobs,omitempty"`
  43. Dimensions int `json:"dimensions,omitempty"`
  44. Modalities any `json:"modalities,omitempty"`
  45. Audio any `json:"audio,omitempty"`
  46. }
  47. type OpenAITools struct {
  48. Type string `json:"type"`
  49. Function OpenAIFunction `json:"function"`
  50. }
  51. type OpenAIFunction struct {
  52. Description string `json:"description,omitempty"`
  53. Name string `json:"name"`
  54. Parameters any `json:"parameters,omitempty"`
  55. }
  56. type StreamOptions struct {
  57. IncludeUsage bool `json:"include_usage,omitempty"`
  58. }
  59. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  60. return int(r.MaxTokens)
  61. }
  62. func (r GeneralOpenAIRequest) ParseInput() []string {
  63. if r.Input == nil {
  64. return nil
  65. }
  66. var input []string
  67. switch r.Input.(type) {
  68. case string:
  69. input = []string{r.Input.(string)}
  70. case []any:
  71. input = make([]string, 0, len(r.Input.([]any)))
  72. for _, item := range r.Input.([]any) {
  73. if str, ok := item.(string); ok {
  74. input = append(input, str)
  75. }
  76. }
  77. }
  78. return input
  79. }
  80. type Message struct {
  81. Role string `json:"role"`
  82. Content json.RawMessage `json:"content"`
  83. Name *string `json:"name,omitempty"`
  84. Prefix *bool `json:"prefix,omitempty"`
  85. ReasoningContent string `json:"reasoning_content,omitempty"`
  86. ToolCalls json.RawMessage `json:"tool_calls,omitempty"`
  87. ToolCallId string `json:"tool_call_id,omitempty"`
  88. parsedContent []MediaContent
  89. parsedStringContent *string
  90. }
  91. type MediaContent struct {
  92. Type string `json:"type"`
  93. Text string `json:"text"`
  94. ImageUrl any `json:"image_url,omitempty"`
  95. InputAudio any `json:"input_audio,omitempty"`
  96. }
  97. type MessageImageUrl struct {
  98. Url string `json:"url"`
  99. Detail string `json:"detail"`
  100. }
  101. type MessageInputAudio struct {
  102. Data string `json:"data"` //base64
  103. Format string `json:"format"`
  104. }
  105. const (
  106. ContentTypeText = "text"
  107. ContentTypeImageURL = "image_url"
  108. ContentTypeInputAudio = "input_audio"
  109. )
  110. func (m *Message) GetPrefix() bool {
  111. if m.Prefix == nil {
  112. return false
  113. }
  114. return *m.Prefix
  115. }
  116. func (m *Message) SetPrefix(prefix bool) {
  117. m.Prefix = &prefix
  118. }
  119. func (m *Message) ParseToolCalls() []ToolCall {
  120. if m.ToolCalls == nil {
  121. return nil
  122. }
  123. var toolCalls []ToolCall
  124. if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
  125. return toolCalls
  126. }
  127. return toolCalls
  128. }
  129. func (m *Message) SetToolCalls(toolCalls any) {
  130. toolCallsJson, _ := json.Marshal(toolCalls)
  131. m.ToolCalls = toolCallsJson
  132. }
  133. func (m *Message) StringContent() string {
  134. if m.parsedStringContent != nil {
  135. return *m.parsedStringContent
  136. }
  137. var stringContent string
  138. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  139. return stringContent
  140. }
  141. return string(m.Content)
  142. }
  143. func (m *Message) SetStringContent(content string) {
  144. jsonContent, _ := json.Marshal(content)
  145. m.Content = jsonContent
  146. m.parsedStringContent = &content
  147. m.parsedContent = nil
  148. }
  149. func (m *Message) SetMediaContent(content []MediaContent) {
  150. jsonContent, _ := json.Marshal(content)
  151. m.Content = jsonContent
  152. m.parsedContent = nil
  153. m.parsedStringContent = nil
  154. }
  155. func (m *Message) IsStringContent() bool {
  156. if m.parsedStringContent != nil {
  157. return true
  158. }
  159. var stringContent string
  160. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  161. m.parsedStringContent = &stringContent
  162. return true
  163. }
  164. return false
  165. }
  166. func (m *Message) ParseContent() []MediaContent {
  167. if m.parsedContent != nil {
  168. return m.parsedContent
  169. }
  170. var contentList []MediaContent
  171. // 先尝试解析为字符串
  172. var stringContent string
  173. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  174. contentList = []MediaContent{{
  175. Type: ContentTypeText,
  176. Text: stringContent,
  177. }}
  178. m.parsedContent = contentList
  179. return contentList
  180. }
  181. // 尝试解析为数组
  182. var arrayContent []map[string]interface{}
  183. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  184. for _, contentItem := range arrayContent {
  185. contentType, ok := contentItem["type"].(string)
  186. if !ok {
  187. continue
  188. }
  189. switch contentType {
  190. case ContentTypeText:
  191. if text, ok := contentItem["text"].(string); ok {
  192. contentList = append(contentList, MediaContent{
  193. Type: ContentTypeText,
  194. Text: text,
  195. })
  196. }
  197. case ContentTypeImageURL:
  198. imageUrl := contentItem["image_url"]
  199. switch v := imageUrl.(type) {
  200. case string:
  201. contentList = append(contentList, MediaContent{
  202. Type: ContentTypeImageURL,
  203. ImageUrl: MessageImageUrl{
  204. Url: v,
  205. Detail: "high",
  206. },
  207. })
  208. case map[string]interface{}:
  209. url, ok1 := v["url"].(string)
  210. detail, ok2 := v["detail"].(string)
  211. if !ok2 {
  212. detail = "high"
  213. }
  214. if ok1 {
  215. contentList = append(contentList, MediaContent{
  216. Type: ContentTypeImageURL,
  217. ImageUrl: MessageImageUrl{
  218. Url: url,
  219. Detail: detail,
  220. },
  221. })
  222. }
  223. }
  224. case ContentTypeInputAudio:
  225. if audioData, ok := contentItem["input_audio"].(map[string]interface{}); ok {
  226. data, ok1 := audioData["data"].(string)
  227. format, ok2 := audioData["format"].(string)
  228. if ok1 && ok2 {
  229. contentList = append(contentList, MediaContent{
  230. Type: ContentTypeInputAudio,
  231. InputAudio: MessageInputAudio{
  232. Data: data,
  233. Format: format,
  234. },
  235. })
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if len(contentList) > 0 {
  242. m.parsedContent = contentList
  243. }
  244. return contentList
  245. }