openai_request.go 9.7 KB

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