text_request.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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) ParseInput() []string {
  41. if r.Input == nil {
  42. return nil
  43. }
  44. var input []string
  45. switch r.Input.(type) {
  46. case string:
  47. input = []string{r.Input.(string)}
  48. case []any:
  49. input = make([]string, 0, len(r.Input.([]any)))
  50. for _, item := range r.Input.([]any) {
  51. if str, ok := item.(string); ok {
  52. input = append(input, str)
  53. }
  54. }
  55. }
  56. return input
  57. }
  58. type Message struct {
  59. Role string `json:"role"`
  60. Content json.RawMessage `json:"content"`
  61. Name *string `json:"name,omitempty"`
  62. ToolCalls any `json:"tool_calls,omitempty"`
  63. ToolCallId string `json:"tool_call_id,omitempty"`
  64. }
  65. type MediaMessage struct {
  66. Type string `json:"type"`
  67. Text string `json:"text"`
  68. ImageUrl any `json:"image_url,omitempty"`
  69. }
  70. type MessageImageUrl struct {
  71. Url string `json:"url"`
  72. Detail string `json:"detail"`
  73. }
  74. const (
  75. ContentTypeText = "text"
  76. ContentTypeImageURL = "image_url"
  77. )
  78. func (m Message) StringContent() string {
  79. var stringContent string
  80. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  81. return stringContent
  82. }
  83. return string(m.Content)
  84. }
  85. func (m Message) IsStringContent() bool {
  86. var stringContent string
  87. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  88. return true
  89. }
  90. return false
  91. }
  92. func (m Message) ParseContent() []MediaMessage {
  93. var contentList []MediaMessage
  94. var stringContent string
  95. if err := json.Unmarshal(m.Content, &stringContent); err == nil {
  96. contentList = append(contentList, MediaMessage{
  97. Type: ContentTypeText,
  98. Text: stringContent,
  99. })
  100. return contentList
  101. }
  102. var arrayContent []json.RawMessage
  103. if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
  104. for _, contentItem := range arrayContent {
  105. var contentMap map[string]any
  106. if err := json.Unmarshal(contentItem, &contentMap); err != nil {
  107. continue
  108. }
  109. switch contentMap["type"] {
  110. case ContentTypeText:
  111. if subStr, ok := contentMap["text"].(string); ok {
  112. contentList = append(contentList, MediaMessage{
  113. Type: ContentTypeText,
  114. Text: subStr,
  115. })
  116. }
  117. case ContentTypeImageURL:
  118. if subObj, ok := contentMap["image_url"].(map[string]any); ok {
  119. detail, ok := subObj["detail"]
  120. if ok {
  121. subObj["detail"] = detail.(string)
  122. } else {
  123. subObj["detail"] = "auto"
  124. }
  125. contentList = append(contentList, MediaMessage{
  126. Type: ContentTypeImageURL,
  127. ImageUrl: MessageImageUrl{
  128. Url: subObj["url"].(string),
  129. Detail: subObj["detail"].(string),
  130. },
  131. })
  132. }
  133. }
  134. }
  135. return contentList
  136. }
  137. return nil
  138. }