openai_request.go 10 KB

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