text_request.go 4.1 KB

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