text_request.go 4.5 KB

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