text_request.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. Seed float64 `json:"seed,omitempty"`
  27. Tools []ToolCall `json:"tools,omitempty"`
  28. ToolChoice any `json:"tool_choice,omitempty"`
  29. User string `json:"user,omitempty"`
  30. LogProbs bool `json:"logprobs,omitempty"`
  31. TopLogProbs int `json:"top_logprobs,omitempty"`
  32. Dimensions int `json:"dimensions,omitempty"`
  33. }
  34. type OpenAITools struct {
  35. Type string `json:"type"`
  36. Function OpenAIFunction `json:"function"`
  37. }
  38. type OpenAIFunction struct {
  39. Description string `json:"description,omitempty"`
  40. Name string `json:"name"`
  41. Parameters any `json:"parameters,omitempty"`
  42. }
  43. type StreamOptions struct {
  44. IncludeUsage bool `json:"include_usage,omitempty"`
  45. }
  46. func (r GeneralOpenAIRequest) GetMaxTokens() int {
  47. return int(r.MaxTokens)
  48. }
  49. func (r GeneralOpenAIRequest) ParseInput() []string {
  50. if r.Input == nil {
  51. return nil
  52. }
  53. var input []string
  54. switch r.Input.(type) {
  55. case string:
  56. input = []string{r.Input.(string)}
  57. case []any:
  58. input = make([]string, 0, len(r.Input.([]any)))
  59. for _, item := range r.Input.([]any) {
  60. if str, ok := item.(string); ok {
  61. input = append(input, str)
  62. }
  63. }
  64. }
  65. return input
  66. }
  67. type Message struct {
  68. Role string `json:"role"`
  69. Content json.RawMessage `json:"content"`
  70. Name *string `json:"name,omitempty"`
  71. ToolCalls any `json:"tool_calls,omitempty"`
  72. ToolCallId string `json:"tool_call_id,omitempty"`
  73. }
  74. type MediaMessage struct {
  75. Type string `json:"type"`
  76. Text string `json:"text"`
  77. ImageUrl any `json:"image_url,omitempty"`
  78. }
  79. type MessageImageUrl struct {
  80. Url string `json:"url"`
  81. Detail string `json:"detail"`
  82. }
  83. const (
  84. ContentTypeText = "text"
  85. ContentTypeImageURL = "image_url"
  86. )
  87. func (m Message) StringContent() string {
  88. var stringContent string
  89. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  90. return stringContent
  91. }
  92. return string(m.Content)
  93. }
  94. func (m *Message) SetStringContent(content string) {
  95. jsonContent, _ := json.Marshal(content)
  96. m.Content = jsonContent
  97. }
  98. func (m Message) IsStringContent() bool {
  99. var stringContent string
  100. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  101. return true
  102. }
  103. return false
  104. }
  105. func (m Message) ParseContent() []MediaMessage {
  106. var contentList []MediaMessage
  107. var stringContent string
  108. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  109. contentList = append(contentList, MediaMessage{
  110. Type: ContentTypeText,
  111. Text: stringContent,
  112. })
  113. return contentList
  114. }
  115. var arrayContent []json.RawMessage
  116. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  117. for _, contentItem := range arrayContent {
  118. var contentMap map[string]any
  119. if err := json.Unmarshal(contentItem, &contentMap); err != nil {
  120. continue
  121. }
  122. switch contentMap["type"] {
  123. case ContentTypeText:
  124. if subStr, ok := contentMap["text"].(string); ok {
  125. contentList = append(contentList, MediaMessage{
  126. Type: ContentTypeText,
  127. Text: subStr,
  128. })
  129. }
  130. case ContentTypeImageURL:
  131. if subObj, ok := contentMap["image_url"].(map[string]any); ok {
  132. detail, ok := subObj["detail"]
  133. if ok {
  134. subObj["detail"] = detail.(string)
  135. } else {
  136. subObj["detail"] = "high"
  137. }
  138. contentList = append(contentList, MediaMessage{
  139. Type: ContentTypeImageURL,
  140. ImageUrl: MessageImageUrl{
  141. Url: subObj["url"].(string),
  142. Detail: subObj["detail"].(string),
  143. },
  144. })
  145. } else if url, ok := contentMap["image_url"].(string); ok {
  146. contentList = append(contentList, MediaMessage{
  147. Type: ContentTypeImageURL,
  148. ImageUrl: MessageImageUrl{
  149. Url: url,
  150. Detail: "high",
  151. },
  152. })
  153. }
  154. }
  155. }
  156. return contentList
  157. }
  158. return nil
  159. }