openai_request.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. Stream bool `json:"stream,omitempty"`
  18. StreamOptions *StreamOptions `json:"stream_options,omitempty"`
  19. MaxTokens uint `json:"max_tokens,omitempty"`
  20. MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
  21. Temperature float64 `json:"temperature,omitempty"`
  22. TopP float64 `json:"top_p,omitempty"`
  23. TopK int `json:"top_k,omitempty"`
  24. Stop any `json:"stop,omitempty"`
  25. N int `json:"n,omitempty"`
  26. Input any `json:"input,omitempty"`
  27. Instruction string `json:"instruction,omitempty"`
  28. Size string `json:"size,omitempty"`
  29. Functions any `json:"functions,omitempty"`
  30. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  31. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  32. ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
  33. EncodingFormat any `json:"encoding_format,omitempty"`
  34. Seed float64 `json:"seed,omitempty"`
  35. Tools []ToolCall `json:"tools,omitempty"`
  36. ToolChoice any `json:"tool_choice,omitempty"`
  37. User string `json:"user,omitempty"`
  38. LogProbs bool `json:"logprobs,omitempty"`
  39. TopLogProbs int `json:"top_logprobs,omitempty"`
  40. Dimensions int `json:"dimensions,omitempty"`
  41. Modalities any `json:"modalities,omitempty"`
  42. Audio any `json:"audio,omitempty"`
  43. }
  44. type OpenAITools struct {
  45. Type string `json:"type"`
  46. Function OpenAIFunction `json:"function"`
  47. }
  48. type OpenAIFunction struct {
  49. Description string `json:"description,omitempty"`
  50. Name string `json:"name"`
  51. Parameters any `json:"parameters,omitempty"`
  52. }
  53. type StreamOptions struct {
  54. IncludeUsage bool `json:"include_usage,omitempty"`
  55. }
  56. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  57. return int(r.MaxTokens)
  58. }
  59. func (r GeneralOpenAIRequest) ParseInput() []string {
  60. if r.Input == nil {
  61. return nil
  62. }
  63. var input []string
  64. switch r.Input.(type) {
  65. case string:
  66. input = []string{r.Input.(string)}
  67. case []any:
  68. input = make([]string, 0, len(r.Input.([]any)))
  69. for _, item := range r.Input.([]any) {
  70. if str, ok := item.(string); ok {
  71. input = append(input, str)
  72. }
  73. }
  74. }
  75. return input
  76. }
  77. type Message struct {
  78. Role string `json:"role"`
  79. Content json.RawMessage `json:"content"`
  80. Name *string `json:"name,omitempty"`
  81. ToolCalls any `json:"tool_calls,omitempty"`
  82. ToolCallId string `json:"tool_call_id,omitempty"`
  83. }
  84. type MediaMessage struct {
  85. Type string `json:"type"`
  86. Text string `json:"text"`
  87. ImageUrl any `json:"image_url,omitempty"`
  88. InputAudio any `json:"input_audio,omitempty"`
  89. }
  90. type MessageImageUrl struct {
  91. Url string `json:"url"`
  92. Detail string `json:"detail"`
  93. }
  94. type MessageInputAudio struct {
  95. Data string `json:"data"` //base64
  96. Format string `json:"format"`
  97. }
  98. const (
  99. ContentTypeText = "text"
  100. ContentTypeImageURL = "image_url"
  101. ContentTypeInputAudio = "input_audio"
  102. )
  103. func (m Message) StringContent() string {
  104. var stringContent string
  105. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  106. return stringContent
  107. }
  108. return string(m.Content)
  109. }
  110. func (m *Message) SetStringContent(content string) {
  111. jsonContent, _ := json.Marshal(content)
  112. m.Content = jsonContent
  113. }
  114. func (m Message) IsStringContent() bool {
  115. var stringContent string
  116. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  117. return true
  118. }
  119. return false
  120. }
  121. func (m Message) ParseContent() []MediaMessage {
  122. var contentList []MediaMessage
  123. var stringContent string
  124. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  125. contentList = append(contentList, MediaMessage{
  126. Type: ContentTypeText,
  127. Text: stringContent,
  128. })
  129. return contentList
  130. }
  131. var arrayContent []json.RawMessage
  132. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  133. for _, contentItem := range arrayContent {
  134. var contentMap map[string]any
  135. if err := json.Unmarshal(contentItem, &contentMap); err != nil {
  136. continue
  137. }
  138. switch contentMap["type"] {
  139. case ContentTypeText:
  140. if subStr, ok := contentMap["text"].(string); ok {
  141. contentList = append(contentList, MediaMessage{
  142. Type: ContentTypeText,
  143. Text: subStr,
  144. })
  145. }
  146. case ContentTypeImageURL:
  147. if subObj, ok := contentMap["image_url"].(map[string]any); ok {
  148. detail, ok := subObj["detail"]
  149. if ok {
  150. subObj["detail"] = detail.(string)
  151. } else {
  152. subObj["detail"] = "high"
  153. }
  154. contentList = append(contentList, MediaMessage{
  155. Type: ContentTypeImageURL,
  156. ImageUrl: MessageImageUrl{
  157. Url: subObj["url"].(string),
  158. Detail: subObj["detail"].(string),
  159. },
  160. })
  161. } else if url, ok := contentMap["image_url"].(string); ok {
  162. contentList = append(contentList, MediaMessage{
  163. Type: ContentTypeImageURL,
  164. ImageUrl: MessageImageUrl{
  165. Url: url,
  166. Detail: "high",
  167. },
  168. })
  169. }
  170. case ContentTypeInputAudio:
  171. if subObj, ok := contentMap["input_audio"].(map[string]any); ok {
  172. contentList = append(contentList, MediaMessage{
  173. Type: ContentTypeInputAudio,
  174. InputAudio: MessageInputAudio{
  175. Data: subObj["data"].(string),
  176. Format: subObj["format"].(string),
  177. },
  178. })
  179. }
  180. }
  181. }
  182. return contentList
  183. }
  184. return nil
  185. }