text_request.go 4.0 KB

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