text_request.go 4.1 KB

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