text_request.go 5.2 KB

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