text_request.go 4.8 KB

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