openai_request.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package dto
  2. import (
  3. "encoding/json"
  4. "strings"
  5. )
  6. type ResponseFormat struct {
  7. Type string `json:"type,omitempty"`
  8. JsonSchema *FormatJsonSchema `json:"json_schema,omitempty"`
  9. }
  10. type FormatJsonSchema struct {
  11. Description string `json:"description,omitempty"`
  12. Name string `json:"name"`
  13. Schema any `json:"schema,omitempty"`
  14. Strict any `json:"strict,omitempty"`
  15. }
  16. type GeneralOpenAIRequest struct {
  17. Model string `json:"model,omitempty"`
  18. Messages []Message `json:"messages,omitempty"`
  19. Prompt any `json:"prompt,omitempty"`
  20. Prefix any `json:"prefix,omitempty"`
  21. Suffix any `json:"suffix,omitempty"`
  22. Stream bool `json:"stream,omitempty"`
  23. StreamOptions *StreamOptions `json:"stream_options,omitempty"`
  24. MaxTokens uint `json:"max_tokens,omitempty"`
  25. MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
  26. ReasoningEffort string `json:"reasoning_effort,omitempty"`
  27. Temperature *float64 `json:"temperature,omitempty"`
  28. TopP float64 `json:"top_p,omitempty"`
  29. TopK int `json:"top_k,omitempty"`
  30. Stop any `json:"stop,omitempty"`
  31. N int `json:"n,omitempty"`
  32. Input any `json:"input,omitempty"`
  33. Instruction string `json:"instruction,omitempty"`
  34. Size string `json:"size,omitempty"`
  35. Functions any `json:"functions,omitempty"`
  36. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  37. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  38. ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
  39. EncodingFormat any `json:"encoding_format,omitempty"`
  40. Seed float64 `json:"seed,omitempty"`
  41. Tools []ToolCallRequest `json:"tools,omitempty"`
  42. ToolChoice any `json:"tool_choice,omitempty"`
  43. User string `json:"user,omitempty"`
  44. LogProbs bool `json:"logprobs,omitempty"`
  45. TopLogProbs int `json:"top_logprobs,omitempty"`
  46. Dimensions int `json:"dimensions,omitempty"`
  47. Modalities any `json:"modalities,omitempty"`
  48. Audio any `json:"audio,omitempty"`
  49. ExtraBody any `json:"extra_body,omitempty"`
  50. }
  51. type ToolCallRequest struct {
  52. ID string `json:"id,omitempty"`
  53. Type string `json:"type"`
  54. Function FunctionRequest `json:"function"`
  55. }
  56. type FunctionRequest struct {
  57. Description string `json:"description,omitempty"`
  58. Name string `json:"name"`
  59. Parameters any `json:"parameters,omitempty"`
  60. Arguments string `json:"arguments,omitempty"`
  61. }
  62. type StreamOptions struct {
  63. IncludeUsage bool `json:"include_usage,omitempty"`
  64. }
  65. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  66. return int(r.MaxTokens)
  67. }
  68. func (r GeneralOpenAIRequest) ParseInput() []string {
  69. if r.Input == nil {
  70. return nil
  71. }
  72. var input []string
  73. switch r.Input.(type) {
  74. case string:
  75. input = []string{r.Input.(string)}
  76. case []any:
  77. input = make([]string, 0, len(r.Input.([]any)))
  78. for _, item := range r.Input.([]any) {
  79. if str, ok := item.(string); ok {
  80. input = append(input, str)
  81. }
  82. }
  83. }
  84. return input
  85. }
  86. type Message struct {
  87. Role string `json:"role"`
  88. Content json.RawMessage `json:"content"`
  89. Name *string `json:"name,omitempty"`
  90. Prefix *bool `json:"prefix,omitempty"`
  91. ReasoningContent string `json:"reasoning_content,omitempty"`
  92. Reasoning string `json:"reasoning,omitempty"`
  93. ToolCalls json.RawMessage `json:"tool_calls,omitempty"`
  94. ToolCallId string `json:"tool_call_id,omitempty"`
  95. parsedContent []MediaContent
  96. parsedStringContent *string
  97. }
  98. type MediaContent struct {
  99. Type string `json:"type"`
  100. Text string `json:"text,omitempty"`
  101. ImageUrl any `json:"image_url,omitempty"`
  102. InputAudio any `json:"input_audio,omitempty"`
  103. File any `json:"file,omitempty"`
  104. }
  105. func (m *MediaContent) GetImageMedia() *MessageImageUrl {
  106. if m.ImageUrl != nil {
  107. return m.ImageUrl.(*MessageImageUrl)
  108. }
  109. return nil
  110. }
  111. func (m *MediaContent) GetInputAudio() *MessageInputAudio {
  112. if m.InputAudio != nil {
  113. return m.InputAudio.(*MessageInputAudio)
  114. }
  115. return nil
  116. }
  117. func (m *MediaContent) GetFile() *MessageFile {
  118. if m.File != nil {
  119. return m.File.(*MessageFile)
  120. }
  121. return nil
  122. }
  123. type MessageImageUrl struct {
  124. Url string `json:"url"`
  125. Detail string `json:"detail"`
  126. MimeType string
  127. }
  128. func (m *MessageImageUrl) IsRemoteImage() bool {
  129. return strings.HasPrefix(m.Url, "http")
  130. }
  131. type MessageInputAudio struct {
  132. Data string `json:"data"` //base64
  133. Format string `json:"format"`
  134. }
  135. type MessageFile struct {
  136. FileName string `json:"filename,omitempty"`
  137. FileData string `json:"file_data,omitempty"`
  138. FileId string `json:"file_id,omitempty"`
  139. }
  140. const (
  141. ContentTypeText = "text"
  142. ContentTypeImageURL = "image_url"
  143. ContentTypeInputAudio = "input_audio"
  144. ContentTypeFile = "file"
  145. )
  146. func (m *Message) GetPrefix() bool {
  147. if m.Prefix == nil {
  148. return false
  149. }
  150. return *m.Prefix
  151. }
  152. func (m *Message) SetPrefix(prefix bool) {
  153. m.Prefix = &prefix
  154. }
  155. func (m *Message) ParseToolCalls() []ToolCallRequest {
  156. if m.ToolCalls == nil {
  157. return nil
  158. }
  159. var toolCalls []ToolCallRequest
  160. if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
  161. return toolCalls
  162. }
  163. return toolCalls
  164. }
  165. func (m *Message) SetToolCalls(toolCalls any) {
  166. toolCallsJson, _ := json.Marshal(toolCalls)
  167. m.ToolCalls = toolCallsJson
  168. }
  169. func (m *Message) StringContent() string {
  170. if m.parsedStringContent != nil {
  171. return *m.parsedStringContent
  172. }
  173. var stringContent string
  174. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  175. m.parsedStringContent = &stringContent
  176. return stringContent
  177. }
  178. contentStr := new(strings.Builder)
  179. arrayContent := m.ParseContent()
  180. for _, content := range arrayContent {
  181. if content.Type == ContentTypeText {
  182. contentStr.WriteString(content.Text)
  183. }
  184. }
  185. stringContent = contentStr.String()
  186. m.parsedStringContent = &stringContent
  187. return stringContent
  188. }
  189. func (m *Message) SetNullContent() {
  190. m.Content = nil
  191. m.parsedStringContent = nil
  192. m.parsedContent = nil
  193. }
  194. func (m *Message) SetStringContent(content string) {
  195. jsonContent, _ := json.Marshal(content)
  196. m.Content = jsonContent
  197. m.parsedStringContent = &content
  198. m.parsedContent = nil
  199. }
  200. func (m *Message) SetMediaContent(content []MediaContent) {
  201. jsonContent, _ := json.Marshal(content)
  202. m.Content = jsonContent
  203. m.parsedContent = nil
  204. m.parsedStringContent = nil
  205. }
  206. func (m *Message) IsStringContent() bool {
  207. if m.parsedStringContent != nil {
  208. return true
  209. }
  210. var stringContent string
  211. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  212. m.parsedStringContent = &stringContent
  213. return true
  214. }
  215. return false
  216. }
  217. func (m *Message) ParseContent() []MediaContent {
  218. if m.parsedContent != nil {
  219. return m.parsedContent
  220. }
  221. var contentList []MediaContent
  222. // 先尝试解析为字符串
  223. var stringContent string
  224. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  225. contentList = []MediaContent{{
  226. Type: ContentTypeText,
  227. Text: stringContent,
  228. }}
  229. m.parsedContent = contentList
  230. return contentList
  231. }
  232. // 尝试解析为数组
  233. var arrayContent []map[string]interface{}
  234. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  235. for _, contentItem := range arrayContent {
  236. contentType, ok := contentItem["type"].(string)
  237. if !ok {
  238. continue
  239. }
  240. switch contentType {
  241. case ContentTypeText:
  242. if text, ok := contentItem["text"].(string); ok {
  243. contentList = append(contentList, MediaContent{
  244. Type: ContentTypeText,
  245. Text: text,
  246. })
  247. }
  248. case ContentTypeImageURL:
  249. imageUrl := contentItem["image_url"]
  250. temp := &MessageImageUrl{
  251. Detail: "high",
  252. }
  253. switch v := imageUrl.(type) {
  254. case string:
  255. temp.Url = v
  256. case map[string]interface{}:
  257. url, ok1 := v["url"].(string)
  258. detail, ok2 := v["detail"].(string)
  259. if ok2 {
  260. temp.Detail = detail
  261. }
  262. if ok1 {
  263. temp.Url = url
  264. }
  265. }
  266. contentList = append(contentList, MediaContent{
  267. Type: ContentTypeImageURL,
  268. ImageUrl: temp,
  269. })
  270. case ContentTypeInputAudio:
  271. if audioData, ok := contentItem["input_audio"].(map[string]interface{}); ok {
  272. data, ok1 := audioData["data"].(string)
  273. format, ok2 := audioData["format"].(string)
  274. if ok1 && ok2 {
  275. temp := &MessageInputAudio{
  276. Data: data,
  277. Format: format,
  278. }
  279. contentList = append(contentList, MediaContent{
  280. Type: ContentTypeInputAudio,
  281. InputAudio: temp,
  282. })
  283. }
  284. }
  285. case ContentTypeFile:
  286. if fileData, ok := contentItem["file"].(map[string]interface{}); ok {
  287. fileId, ok3 := fileData["file_id"].(string)
  288. if ok3 {
  289. contentList = append(contentList, MediaContent{
  290. Type: ContentTypeFile,
  291. File: &MessageFile{
  292. FileId: fileId,
  293. },
  294. })
  295. } else {
  296. fileName, ok1 := fileData["filename"].(string)
  297. fileDataStr, ok2 := fileData["file_data"].(string)
  298. if ok1 && ok2 {
  299. contentList = append(contentList, MediaContent{
  300. Type: ContentTypeFile,
  301. File: &MessageFile{
  302. FileName: fileName,
  303. FileData: fileDataStr,
  304. },
  305. })
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. if len(contentList) > 0 {
  313. m.parsedContent = contentList
  314. }
  315. return contentList
  316. }