openai_request.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 []ToolCall `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 OpenAITools struct {
  52. Type string `json:"type"`
  53. Function OpenAIFunction `json:"function"`
  54. }
  55. type OpenAIFunction struct {
  56. Description string `json:"description,omitempty"`
  57. Name string `json:"name"`
  58. Parameters any `json:"parameters,omitempty"`
  59. }
  60. type StreamOptions struct {
  61. IncludeUsage bool `json:"include_usage,omitempty"`
  62. }
  63. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  64. return int(r.MaxTokens)
  65. }
  66. func (r GeneralOpenAIRequest) ParseInput() []string {
  67. if r.Input == nil {
  68. return nil
  69. }
  70. var input []string
  71. switch r.Input.(type) {
  72. case string:
  73. input = []string{r.Input.(string)}
  74. case []any:
  75. input = make([]string, 0, len(r.Input.([]any)))
  76. for _, item := range r.Input.([]any) {
  77. if str, ok := item.(string); ok {
  78. input = append(input, str)
  79. }
  80. }
  81. }
  82. return input
  83. }
  84. type Message struct {
  85. Role string `json:"role"`
  86. Content json.RawMessage `json:"content"`
  87. Name *string `json:"name,omitempty"`
  88. Prefix *bool `json:"prefix,omitempty"`
  89. ReasoningContent string `json:"reasoning_content,omitempty"`
  90. ToolCalls json.RawMessage `json:"tool_calls,omitempty"`
  91. ToolCallId string `json:"tool_call_id,omitempty"`
  92. parsedContent []MediaContent
  93. parsedStringContent *string
  94. }
  95. type MediaContent struct {
  96. Type string `json:"type"`
  97. Text string `json:"text,omitempty"`
  98. ImageUrl any `json:"image_url,omitempty"`
  99. InputAudio any `json:"input_audio,omitempty"`
  100. }
  101. type MessageImageUrl struct {
  102. Url string `json:"url"`
  103. Detail string `json:"detail"`
  104. }
  105. type MessageInputAudio struct {
  106. Data string `json:"data"` //base64
  107. Format string `json:"format"`
  108. }
  109. const (
  110. ContentTypeText = "text"
  111. ContentTypeImageURL = "image_url"
  112. ContentTypeInputAudio = "input_audio"
  113. )
  114. func (m *Message) GetPrefix() bool {
  115. if m.Prefix == nil {
  116. return false
  117. }
  118. return *m.Prefix
  119. }
  120. func (m *Message) SetPrefix(prefix bool) {
  121. m.Prefix = &prefix
  122. }
  123. func (m *Message) ParseToolCalls() []ToolCall {
  124. if m.ToolCalls == nil {
  125. return nil
  126. }
  127. var toolCalls []ToolCall
  128. if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
  129. return toolCalls
  130. }
  131. return toolCalls
  132. }
  133. func (m *Message) SetToolCalls(toolCalls any) {
  134. toolCallsJson, _ := json.Marshal(toolCalls)
  135. m.ToolCalls = toolCallsJson
  136. }
  137. func (m *Message) StringContent() string {
  138. if m.parsedStringContent != nil {
  139. return *m.parsedStringContent
  140. }
  141. var stringContent string
  142. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  143. m.parsedStringContent = &stringContent
  144. return stringContent
  145. }
  146. contentStr := new(strings.Builder)
  147. arrayContent := m.ParseContent()
  148. for _, content := range arrayContent {
  149. if content.Type == ContentTypeText {
  150. contentStr.WriteString(content.Text)
  151. }
  152. }
  153. stringContent = contentStr.String()
  154. m.parsedStringContent = &stringContent
  155. return stringContent
  156. }
  157. func (m *Message) SetStringContent(content string) {
  158. jsonContent, _ := json.Marshal(content)
  159. m.Content = jsonContent
  160. m.parsedStringContent = &content
  161. m.parsedContent = nil
  162. }
  163. func (m *Message) SetMediaContent(content []MediaContent) {
  164. jsonContent, _ := json.Marshal(content)
  165. m.Content = jsonContent
  166. m.parsedContent = nil
  167. m.parsedStringContent = nil
  168. }
  169. func (m *Message) IsStringContent() bool {
  170. if m.parsedStringContent != nil {
  171. return true
  172. }
  173. var stringContent string
  174. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  175. m.parsedStringContent = &stringContent
  176. return true
  177. }
  178. return false
  179. }
  180. func (m *Message) ParseContent() []MediaContent {
  181. if m.parsedContent != nil {
  182. return m.parsedContent
  183. }
  184. var contentList []MediaContent
  185. // 先尝试解析为字符串
  186. var stringContent string
  187. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  188. contentList = []MediaContent{{
  189. Type: ContentTypeText,
  190. Text: stringContent,
  191. }}
  192. m.parsedContent = contentList
  193. return contentList
  194. }
  195. // 尝试解析为数组
  196. var arrayContent []map[string]interface{}
  197. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  198. for _, contentItem := range arrayContent {
  199. contentType, ok := contentItem["type"].(string)
  200. if !ok {
  201. continue
  202. }
  203. switch contentType {
  204. case ContentTypeText:
  205. if text, ok := contentItem["text"].(string); ok {
  206. contentList = append(contentList, MediaContent{
  207. Type: ContentTypeText,
  208. Text: text,
  209. })
  210. }
  211. case ContentTypeImageURL:
  212. imageUrl := contentItem["image_url"]
  213. switch v := imageUrl.(type) {
  214. case string:
  215. contentList = append(contentList, MediaContent{
  216. Type: ContentTypeImageURL,
  217. ImageUrl: MessageImageUrl{
  218. Url: v,
  219. Detail: "high",
  220. },
  221. })
  222. case map[string]interface{}:
  223. url, ok1 := v["url"].(string)
  224. detail, ok2 := v["detail"].(string)
  225. if !ok2 {
  226. detail = "high"
  227. }
  228. if ok1 {
  229. contentList = append(contentList, MediaContent{
  230. Type: ContentTypeImageURL,
  231. ImageUrl: MessageImageUrl{
  232. Url: url,
  233. Detail: detail,
  234. },
  235. })
  236. }
  237. }
  238. case ContentTypeInputAudio:
  239. if audioData, ok := contentItem["input_audio"].(map[string]interface{}); ok {
  240. data, ok1 := audioData["data"].(string)
  241. format, ok2 := audioData["format"].(string)
  242. if ok1 && ok2 {
  243. contentList = append(contentList, MediaContent{
  244. Type: ContentTypeInputAudio,
  245. InputAudio: MessageInputAudio{
  246. Data: data,
  247. Format: format,
  248. },
  249. })
  250. }
  251. }
  252. }
  253. }
  254. }
  255. if len(contentList) > 0 {
  256. m.parsedContent = contentList
  257. }
  258. return contentList
  259. }