11package ali
22
3- import "github.com/QuantumNous/new-api/dto"
3+ import (
4+ "strings"
5+
6+ "github.com/QuantumNous/new-api/dto"
7+ "github.com/QuantumNous/new-api/logger"
8+ "github.com/QuantumNous/new-api/service"
9+ "github.com/gin-gonic/gin"
10+ )
411
512type AliMessage struct {
613 Content any `json:"content"`
@@ -65,6 +72,7 @@ type AliUsage struct {
6572 InputTokens int `json:"input_tokens"`
6673 OutputTokens int `json:"output_tokens"`
6774 TotalTokens int `json:"total_tokens"`
75+ ImageCount int `json:"image_count,omitempty"`
6876}
6977
7078type TaskResult struct {
@@ -75,14 +83,78 @@ type TaskResult struct {
7583}
7684
7785type AliOutput struct {
78- TaskId string `json:"task_id,omitempty"`
79- TaskStatus string `json:"task_status,omitempty"`
80- Text string `json:"text"`
81- FinishReason string `json:"finish_reason"`
82- Message string `json:"message,omitempty"`
83- Code string `json:"code,omitempty"`
84- Results []TaskResult `json:"results,omitempty"`
85- Choices []map [string ]any `json:"choices,omitempty"`
86+ TaskId string `json:"task_id,omitempty"`
87+ TaskStatus string `json:"task_status,omitempty"`
88+ Text string `json:"text"`
89+ FinishReason string `json:"finish_reason"`
90+ Message string `json:"message,omitempty"`
91+ Code string `json:"code,omitempty"`
92+ Results []TaskResult `json:"results,omitempty"`
93+ Choices []struct {
94+ FinishReason string `json:"finish_reason,omitempty"`
95+ Message struct {
96+ Role string `json:"role,omitempty"`
97+ Content []AliMediaContent `json:"content,omitempty"`
98+ ReasoningContent string `json:"reasoning_content,omitempty"`
99+ } `json:"message,omitempty"`
100+ } `json:"choices,omitempty"`
101+ }
102+
103+ func (o * AliOutput ) ChoicesToOpenAIImageDate (c * gin.Context , responseFormat string ) []dto.ImageData {
104+ var imageData []dto.ImageData
105+ if len (o .Choices ) > 0 {
106+ for _ , choice := range o .Choices {
107+ var data dto.ImageData
108+ for _ , content := range choice .Message .Content {
109+ if content .Image != "" {
110+ if strings .HasPrefix (content .Image , "http" ) {
111+ var b64Json string
112+ if responseFormat == "b64_json" {
113+ _ , b64 , err := service .GetImageFromUrl (content .Image )
114+ if err != nil {
115+ logger .LogError (c , "get_image_data_failed: " + err .Error ())
116+ continue
117+ }
118+ b64Json = b64
119+ }
120+ data .Url = content .Image
121+ data .B64Json = b64Json
122+ } else {
123+ data .B64Json = content .Image
124+ }
125+ } else if content .Text != "" {
126+ data .RevisedPrompt = content .Text
127+ }
128+ }
129+ imageData = append (imageData , data )
130+ }
131+ }
132+
133+ return imageData
134+ }
135+
136+ func (o * AliOutput ) ResultToOpenAIImageDate (c * gin.Context , responseFormat string ) []dto.ImageData {
137+ var imageData []dto.ImageData
138+ for _ , data := range o .Results {
139+ var b64Json string
140+ if responseFormat == "b64_json" {
141+ _ , b64 , err := service .GetImageFromUrl (data .Url )
142+ if err != nil {
143+ logger .LogError (c , "get_image_data_failed: " + err .Error ())
144+ continue
145+ }
146+ b64Json = b64
147+ } else {
148+ b64Json = data .B64Image
149+ }
150+
151+ imageData = append (imageData , dto.ImageData {
152+ Url : data .Url ,
153+ B64Json : b64Json ,
154+ RevisedPrompt : "" ,
155+ })
156+ }
157+ return imageData
86158}
87159
88160type AliResponse struct {
@@ -92,18 +164,26 @@ type AliResponse struct {
92164}
93165
94166type AliImageRequest struct {
95- Model string `json:"model"`
96- Input any `json:"input"`
97- Parameters any `json:"parameters,omitempty"`
98- ResponseFormat string `json:"response_format,omitempty"`
167+ Model string `json:"model"`
168+ Input any `json:"input"`
169+ Parameters AliImageParameters `json:"parameters,omitempty"`
170+ ResponseFormat string `json:"response_format,omitempty"`
99171}
100172
101173type AliImageParameters struct {
102- Size string `json:"size,omitempty"`
103- N int `json:"n,omitempty"`
104- Steps string `json:"steps,omitempty"`
105- Scale string `json:"scale,omitempty"`
106- Watermark * bool `json:"watermark,omitempty"`
174+ Size string `json:"size,omitempty"`
175+ N int `json:"n,omitempty"`
176+ Steps string `json:"steps,omitempty"`
177+ Scale string `json:"scale,omitempty"`
178+ Watermark * bool `json:"watermark,omitempty"`
179+ PromptExtend * bool `json:"prompt_extend,omitempty"`
180+ }
181+
182+ func (p * AliImageParameters ) PromptExtendValue () bool {
183+ if p != nil && p .PromptExtend != nil {
184+ return * p .PromptExtend
185+ }
186+ return false
107187}
108188
109189type AliImageInput struct {
0 commit comments