openai_request.go 8.0 KB

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