openai_request.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package dto
  2. import "encoding/json"
  3. type ResponseFormat struct {
  4. Type string `json:"type,omitempty"`
  5. JsonSchema *FormatJsonSchema `json:"json_schema,omitempty"`
  6. }
  7. type FormatJsonSchema struct {
  8. Description string `json:"description,omitempty"`
  9. Name string `json:"name"`
  10. Schema any `json:"schema,omitempty"`
  11. Strict any `json:"strict,omitempty"`
  12. }
  13. type GeneralOpenAIRequest struct {
  14. Model string `json:"model,omitempty"`
  15. Messages []Message `json:"messages,omitempty"`
  16. Prompt any `json:"prompt,omitempty"`
  17. Prefix any `json:"prefix,omitempty"`
  18. Suffix any `json:"suffix,omitempty"`
  19. Stream bool `json:"stream,omitempty"`
  20. StreamOptions *StreamOptions `json:"stream_options,omitempty"`
  21. MaxTokens uint `json:"max_tokens,omitempty"`
  22. MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
  23. ReasoningEffort string `json:"reasoning_effort,omitempty"`
  24. Temperature *float64 `json:"temperature,omitempty"`
  25. TopP float64 `json:"top_p,omitempty"`
  26. TopK int `json:"top_k,omitempty"`
  27. Stop any `json:"stop,omitempty"`
  28. N int `json:"n,omitempty"`
  29. Input any `json:"input,omitempty"`
  30. Instruction string `json:"instruction,omitempty"`
  31. Size string `json:"size,omitempty"`
  32. Functions any `json:"functions,omitempty"`
  33. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  34. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  35. ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
  36. EncodingFormat any `json:"encoding_format,omitempty"`
  37. Seed float64 `json:"seed,omitempty"`
  38. Tools []ToolCall `json:"tools,omitempty"`
  39. ToolChoice any `json:"tool_choice,omitempty"`
  40. User string `json:"user,omitempty"`
  41. LogProbs bool `json:"logprobs,omitempty"`
  42. TopLogProbs int `json:"top_logprobs,omitempty"`
  43. Dimensions int `json:"dimensions,omitempty"`
  44. Modalities any `json:"modalities,omitempty"`
  45. Audio any `json:"audio,omitempty"`
  46. ExtraBody any `json:"extra_body,omitempty"`
  47. }
  48. type OpenAITools struct {
  49. Type string `json:"type"`
  50. Function OpenAIFunction `json:"function"`
  51. }
  52. type OpenAIFunction struct {
  53. Description string `json:"description,omitempty"`
  54. Name string `json:"name"`
  55. Parameters any `json:"parameters,omitempty"`
  56. }
  57. type StreamOptions struct {
  58. IncludeUsage bool `json:"include_usage,omitempty"`
  59. }
  60. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  61. return int(r.MaxTokens)
  62. }
  63. func (r GeneralOpenAIRequest) ParseInput() []string {
  64. if r.Input == nil {
  65. return nil
  66. }
  67. var input []string
  68. switch r.Input.(type) {
  69. case string:
  70. input = []string{r.Input.(string)}
  71. case []any:
  72. input = make([]string, 0, len(r.Input.([]any)))
  73. for _, item := range r.Input.([]any) {
  74. if str, ok := item.(string); ok {
  75. input = append(input, str)
  76. }
  77. }
  78. }
  79. return input
  80. }
  81. type Message struct {
  82. Role string `json:"role"`
  83. Content json.RawMessage `json:"content"`
  84. Name *string `json:"name,omitempty"`
  85. Prefix *bool `json:"prefix,omitempty"`
  86. ReasoningContent string `json:"reasoning_content,omitempty"`
  87. ToolCalls json.RawMessage `json:"tool_calls,omitempty"`
  88. ToolCallId string `json:"tool_call_id,omitempty"`
  89. parsedContent []MediaContent
  90. parsedStringContent *string
  91. }
  92. type MediaContent struct {
  93. Type string `json:"type"`
  94. Text string `json:"text,omitempty"`
  95. ImageUrl any `json:"image_url,omitempty"`
  96. InputAudio any `json:"input_audio,omitempty"`
  97. }
  98. type MessageImageUrl struct {
  99. Url string `json:"url"`
  100. Detail string `json:"detail"`
  101. }
  102. type MessageInputAudio struct {
  103. Data string `json:"data"` //base64
  104. Format string `json:"format"`
  105. }
  106. const (
  107. ContentTypeText = "text"
  108. ContentTypeImageURL = "image_url"
  109. ContentTypeInputAudio = "input_audio"
  110. )
  111. func (m *Message) GetPrefix() bool {
  112. if m.Prefix == nil {
  113. return false
  114. }
  115. return *m.Prefix
  116. }
  117. func (m *Message) SetPrefix(prefix bool) {
  118. m.Prefix = &prefix
  119. }
  120. func (m *Message) ParseToolCalls() []ToolCall {
  121. if m.ToolCalls == nil {
  122. return nil
  123. }
  124. var toolCalls []ToolCall
  125. if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
  126. return toolCalls
  127. }
  128. return toolCalls
  129. }
  130. func (m *Message) SetToolCalls(toolCalls any) {
  131. toolCallsJson, _ := json.Marshal(toolCalls)
  132. m.ToolCalls = toolCallsJson
  133. }
  134. func (m *Message) StringContent() string {
  135. if m.parsedStringContent != nil {
  136. return *m.parsedStringContent
  137. }
  138. var stringContent string
  139. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  140. return stringContent
  141. }
  142. return string(m.Content)
  143. }
  144. func (m *Message) SetStringContent(content string) {
  145. jsonContent, _ := json.Marshal(content)
  146. m.Content = jsonContent
  147. m.parsedStringContent = &content
  148. m.parsedContent = nil
  149. }
  150. func (m *Message) SetMediaContent(content []MediaContent) {
  151. jsonContent, _ := json.Marshal(content)
  152. m.Content = jsonContent
  153. m.parsedContent = nil
  154. m.parsedStringContent = nil
  155. }
  156. func (m *Message) IsStringContent() bool {
  157. if m.parsedStringContent != nil {
  158. return true
  159. }
  160. var stringContent string
  161. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  162. m.parsedStringContent = &stringContent
  163. return true
  164. }
  165. return false
  166. }
  167. func (m *Message) ParseContent() []MediaContent {
  168. if m.parsedContent != nil {
  169. return m.parsedContent
  170. }
  171. var contentList []MediaContent
  172. // 先尝试解析为字符串
  173. var stringContent string
  174. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  175. contentList = []MediaContent{{
  176. Type: ContentTypeText,
  177. Text: stringContent,
  178. }}
  179. m.parsedContent = contentList
  180. return contentList
  181. }
  182. // 尝试解析为数组
  183. var arrayContent []map[string]interface{}
  184. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  185. for _, contentItem := range arrayContent {
  186. contentType, ok := contentItem["type"].(string)
  187. if !ok {
  188. continue
  189. }
  190. switch contentType {
  191. case ContentTypeText:
  192. if text, ok := contentItem["text"].(string); ok {
  193. contentList = append(contentList, MediaContent{
  194. Type: ContentTypeText,
  195. Text: text,
  196. })
  197. }
  198. case ContentTypeImageURL:
  199. imageUrl := contentItem["image_url"]
  200. switch v := imageUrl.(type) {
  201. case string:
  202. contentList = append(contentList, MediaContent{
  203. Type: ContentTypeImageURL,
  204. ImageUrl: MessageImageUrl{
  205. Url: v,
  206. Detail: "high",
  207. },
  208. })
  209. case map[string]interface{}:
  210. url, ok1 := v["url"].(string)
  211. detail, ok2 := v["detail"].(string)
  212. if !ok2 {
  213. detail = "high"
  214. }
  215. if ok1 {
  216. contentList = append(contentList, MediaContent{
  217. Type: ContentTypeImageURL,
  218. ImageUrl: MessageImageUrl{
  219. Url: url,
  220. Detail: detail,
  221. },
  222. })
  223. }
  224. }
  225. case ContentTypeInputAudio:
  226. if audioData, ok := contentItem["input_audio"].(map[string]interface{}); ok {
  227. data, ok1 := audioData["data"].(string)
  228. format, ok2 := audioData["format"].(string)
  229. if ok1 && ok2 {
  230. contentList = append(contentList, MediaContent{
  231. Type: ContentTypeInputAudio,
  232. InputAudio: MessageInputAudio{
  233. Data: data,
  234. Format: format,
  235. },
  236. })
  237. }
  238. }
  239. }
  240. }
  241. }
  242. if len(contentList) > 0 {
  243. m.parsedContent = contentList
  244. }
  245. return contentList
  246. }