openai_request.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. }
  104. type MessageImageUrl struct {
  105. Url string `json:"url"`
  106. Detail string `json:"detail"`
  107. }
  108. type MessageInputAudio struct {
  109. Data string `json:"data"` //base64
  110. Format string `json:"format"`
  111. }
  112. const (
  113. ContentTypeText = "text"
  114. ContentTypeImageURL = "image_url"
  115. ContentTypeInputAudio = "input_audio"
  116. )
  117. func (m *Message) GetPrefix() bool {
  118. if m.Prefix == nil {
  119. return false
  120. }
  121. return *m.Prefix
  122. }
  123. func (m *Message) SetPrefix(prefix bool) {
  124. m.Prefix = &prefix
  125. }
  126. func (m *Message) ParseToolCalls() []ToolCallRequest {
  127. if m.ToolCalls == nil {
  128. return nil
  129. }
  130. var toolCalls []ToolCallRequest
  131. if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
  132. return toolCalls
  133. }
  134. return toolCalls
  135. }
  136. func (m *Message) SetToolCalls(toolCalls any) {
  137. toolCallsJson, _ := json.Marshal(toolCalls)
  138. m.ToolCalls = toolCallsJson
  139. }
  140. func (m *Message) StringContent() string {
  141. if m.parsedStringContent != nil {
  142. return *m.parsedStringContent
  143. }
  144. var stringContent string
  145. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  146. m.parsedStringContent = &stringContent
  147. return stringContent
  148. }
  149. contentStr := new(strings.Builder)
  150. arrayContent := m.ParseContent()
  151. for _, content := range arrayContent {
  152. if content.Type == ContentTypeText {
  153. contentStr.WriteString(content.Text)
  154. }
  155. }
  156. stringContent = contentStr.String()
  157. m.parsedStringContent = &stringContent
  158. return stringContent
  159. }
  160. func (m *Message) SetStringContent(content string) {
  161. jsonContent, _ := json.Marshal(content)
  162. m.Content = jsonContent
  163. m.parsedStringContent = &content
  164. m.parsedContent = nil
  165. }
  166. func (m *Message) SetMediaContent(content []MediaContent) {
  167. jsonContent, _ := json.Marshal(content)
  168. m.Content = jsonContent
  169. m.parsedContent = nil
  170. m.parsedStringContent = nil
  171. }
  172. func (m *Message) IsStringContent() bool {
  173. if m.parsedStringContent != nil {
  174. return true
  175. }
  176. var stringContent string
  177. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  178. m.parsedStringContent = &stringContent
  179. return true
  180. }
  181. return false
  182. }
  183. func (m *Message) ParseContent() []MediaContent {
  184. if m.parsedContent != nil {
  185. return m.parsedContent
  186. }
  187. var contentList []MediaContent
  188. // 先尝试解析为字符串
  189. var stringContent string
  190. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  191. contentList = []MediaContent{{
  192. Type: ContentTypeText,
  193. Text: stringContent,
  194. }}
  195. m.parsedContent = contentList
  196. return contentList
  197. }
  198. // 尝试解析为数组
  199. var arrayContent []map[string]interface{}
  200. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  201. for _, contentItem := range arrayContent {
  202. contentType, ok := contentItem["type"].(string)
  203. if !ok {
  204. continue
  205. }
  206. switch contentType {
  207. case ContentTypeText:
  208. if text, ok := contentItem["text"].(string); ok {
  209. contentList = append(contentList, MediaContent{
  210. Type: ContentTypeText,
  211. Text: text,
  212. })
  213. }
  214. case ContentTypeImageURL:
  215. imageUrl := contentItem["image_url"]
  216. switch v := imageUrl.(type) {
  217. case string:
  218. contentList = append(contentList, MediaContent{
  219. Type: ContentTypeImageURL,
  220. ImageUrl: MessageImageUrl{
  221. Url: v,
  222. Detail: "high",
  223. },
  224. })
  225. case map[string]interface{}:
  226. url, ok1 := v["url"].(string)
  227. detail, ok2 := v["detail"].(string)
  228. if !ok2 {
  229. detail = "high"
  230. }
  231. if ok1 {
  232. contentList = append(contentList, MediaContent{
  233. Type: ContentTypeImageURL,
  234. ImageUrl: MessageImageUrl{
  235. Url: url,
  236. Detail: detail,
  237. },
  238. })
  239. }
  240. }
  241. case ContentTypeInputAudio:
  242. if audioData, ok := contentItem["input_audio"].(map[string]interface{}); ok {
  243. data, ok1 := audioData["data"].(string)
  244. format, ok2 := audioData["format"].(string)
  245. if ok1 && ok2 {
  246. contentList = append(contentList, MediaContent{
  247. Type: ContentTypeInputAudio,
  248. InputAudio: MessageInputAudio{
  249. Data: data,
  250. Format: format,
  251. },
  252. })
  253. }
  254. }
  255. }
  256. }
  257. }
  258. if len(contentList) > 0 {
  259. m.parsedContent = contentList
  260. }
  261. return contentList
  262. }