dto.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package ali
  2. import (
  3. "strings"
  4. "github.com/QuantumNous/new-api/dto"
  5. "github.com/QuantumNous/new-api/logger"
  6. "github.com/QuantumNous/new-api/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type AliMessage struct {
  10. Content any `json:"content"`
  11. Role string `json:"role"`
  12. }
  13. type AliMediaContent struct {
  14. Image string `json:"image,omitempty"`
  15. Text string `json:"text,omitempty"`
  16. }
  17. type AliInput struct {
  18. Prompt string `json:"prompt,omitempty"`
  19. //History []AliMessage `json:"history,omitempty"`
  20. Messages []AliMessage `json:"messages"`
  21. }
  22. type AliParameters struct {
  23. TopP float64 `json:"top_p,omitempty"`
  24. TopK int `json:"top_k,omitempty"`
  25. Seed uint64 `json:"seed,omitempty"`
  26. EnableSearch bool `json:"enable_search,omitempty"`
  27. IncrementalOutput bool `json:"incremental_output,omitempty"`
  28. }
  29. type AliChatRequest struct {
  30. Model string `json:"model"`
  31. Input AliInput `json:"input,omitempty"`
  32. Parameters AliParameters `json:"parameters,omitempty"`
  33. }
  34. type AliEmbeddingRequest struct {
  35. Model string `json:"model"`
  36. Input struct {
  37. Texts []string `json:"texts"`
  38. } `json:"input"`
  39. Parameters *struct {
  40. TextType string `json:"text_type,omitempty"`
  41. } `json:"parameters,omitempty"`
  42. }
  43. type AliEmbedding struct {
  44. Embedding []float64 `json:"embedding"`
  45. TextIndex int `json:"text_index"`
  46. }
  47. type AliEmbeddingResponse struct {
  48. Output struct {
  49. Embeddings []AliEmbedding `json:"embeddings"`
  50. } `json:"output"`
  51. Usage AliUsage `json:"usage"`
  52. AliError
  53. }
  54. type AliError struct {
  55. Code string `json:"code"`
  56. Message string `json:"message"`
  57. RequestId string `json:"request_id"`
  58. }
  59. type AliUsage struct {
  60. InputTokens int `json:"input_tokens"`
  61. OutputTokens int `json:"output_tokens"`
  62. TotalTokens int `json:"total_tokens"`
  63. ImageCount int `json:"image_count,omitempty"`
  64. }
  65. type TaskResult struct {
  66. B64Image string `json:"b64_image,omitempty"`
  67. Url string `json:"url,omitempty"`
  68. Code string `json:"code,omitempty"`
  69. Message string `json:"message,omitempty"`
  70. }
  71. type AliOutput struct {
  72. TaskId string `json:"task_id,omitempty"`
  73. TaskStatus string `json:"task_status,omitempty"`
  74. Text string `json:"text"`
  75. FinishReason string `json:"finish_reason"`
  76. Message string `json:"message,omitempty"`
  77. Code string `json:"code,omitempty"`
  78. Results []TaskResult `json:"results,omitempty"`
  79. Choices []struct {
  80. FinishReason string `json:"finish_reason,omitempty"`
  81. Message struct {
  82. Role string `json:"role,omitempty"`
  83. Content []AliMediaContent `json:"content,omitempty"`
  84. ReasoningContent string `json:"reasoning_content,omitempty"`
  85. } `json:"message,omitempty"`
  86. } `json:"choices,omitempty"`
  87. }
  88. func (o *AliOutput) ChoicesToOpenAIImageDate(c *gin.Context, responseFormat string) []dto.ImageData {
  89. var imageData []dto.ImageData
  90. if len(o.Choices) > 0 {
  91. for _, choice := range o.Choices {
  92. var data dto.ImageData
  93. for _, content := range choice.Message.Content {
  94. if content.Image != "" {
  95. if strings.HasPrefix(content.Image, "http") {
  96. var b64Json string
  97. if responseFormat == "b64_json" {
  98. _, b64, err := service.GetImageFromUrl(content.Image)
  99. if err != nil {
  100. logger.LogError(c, "get_image_data_failed: "+err.Error())
  101. continue
  102. }
  103. b64Json = b64
  104. }
  105. data.Url = content.Image
  106. data.B64Json = b64Json
  107. } else {
  108. data.B64Json = content.Image
  109. }
  110. } else if content.Text != "" {
  111. data.RevisedPrompt = content.Text
  112. }
  113. }
  114. imageData = append(imageData, data)
  115. }
  116. }
  117. return imageData
  118. }
  119. func (o *AliOutput) ResultToOpenAIImageDate(c *gin.Context, responseFormat string) []dto.ImageData {
  120. var imageData []dto.ImageData
  121. for _, data := range o.Results {
  122. var b64Json string
  123. if responseFormat == "b64_json" {
  124. _, b64, err := service.GetImageFromUrl(data.Url)
  125. if err != nil {
  126. logger.LogError(c, "get_image_data_failed: "+err.Error())
  127. continue
  128. }
  129. b64Json = b64
  130. } else {
  131. b64Json = data.B64Image
  132. }
  133. imageData = append(imageData, dto.ImageData{
  134. Url: data.Url,
  135. B64Json: b64Json,
  136. RevisedPrompt: "",
  137. })
  138. }
  139. return imageData
  140. }
  141. type AliResponse struct {
  142. Output AliOutput `json:"output"`
  143. Usage AliUsage `json:"usage"`
  144. AliError
  145. }
  146. type AliImageRequest struct {
  147. Model string `json:"model"`
  148. Input any `json:"input"`
  149. Parameters AliImageParameters `json:"parameters,omitempty"`
  150. ResponseFormat string `json:"response_format,omitempty"`
  151. }
  152. type AliImageParameters struct {
  153. Size string `json:"size,omitempty"`
  154. N int `json:"n,omitempty"`
  155. Steps string `json:"steps,omitempty"`
  156. Scale string `json:"scale,omitempty"`
  157. Watermark *bool `json:"watermark,omitempty"`
  158. PromptExtend *bool `json:"prompt_extend,omitempty"`
  159. }
  160. func (p *AliImageParameters) PromptExtendValue() bool {
  161. if p != nil && p.PromptExtend != nil {
  162. return *p.PromptExtend
  163. }
  164. return false
  165. }
  166. type AliImageInput struct {
  167. Prompt string `json:"prompt,omitempty"`
  168. NegativePrompt string `json:"negative_prompt,omitempty"`
  169. Messages []AliMessage `json:"messages,omitempty"`
  170. }
  171. type WanImageInput struct {
  172. Prompt string `json:"prompt"` // 必需:文本提示词,描述生成图像中期望包含的元素和视觉特点
  173. Images []string `json:"images"` // 必需:图像URL数组,长度不超过2,支持HTTP/HTTPS URL或Base64编码
  174. NegativePrompt string `json:"negative_prompt,omitempty"` // 可选:反向提示词,描述不希望在画面中看到的内容
  175. }
  176. type WanImageParameters struct {
  177. N int `json:"n,omitempty"` // 生成图片数量,取值范围1-4,默认4
  178. Watermark *bool `json:"watermark,omitempty"` // 是否添加水印标识,默认false
  179. Seed int `json:"seed,omitempty"` // 随机数种子,取值范围[0, 2147483647]
  180. Strength float64 `json:"strength,omitempty"` // 修改幅度 0.0-1.0,默认0.5(部分模型支持)
  181. }
  182. type AliRerankParameters struct {
  183. TopN *int `json:"top_n,omitempty"`
  184. ReturnDocuments *bool `json:"return_documents,omitempty"`
  185. }
  186. type AliRerankInput struct {
  187. Query string `json:"query"`
  188. Documents []any `json:"documents"`
  189. }
  190. type AliRerankRequest struct {
  191. Model string `json:"model"`
  192. Input AliRerankInput `json:"input"`
  193. Parameters AliRerankParameters `json:"parameters,omitempty"`
  194. }
  195. type AliRerankResponse struct {
  196. Output struct {
  197. Results []dto.RerankResponseResult `json:"results"`
  198. } `json:"output"`
  199. Usage AliUsage `json:"usage"`
  200. RequestId string `json:"request_id"`
  201. AliError
  202. }