text_request.go 4.6 KB

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