GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Suno-API/chat.go at main · Suno-API/Suno-API

Expand file tree

/

chat.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
342 lines (315 loc) · 10.6 KB
/

chat.go

Copy path

File metadata and controls

342 lines (315 loc) · 10.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
"io"
"net/http"
"suno-api/common"
"suno-api/entity/po"
"suno-api/middleware"
"time"
)
const (
SunoModelChirpV3 = "chirp-v3-0"
SunoModelChirpV35 = "chirp-v3-5"
)
func ChatCompletions(c *gin.Context) {
err := checkChatConfig()
if err != nil {
common.ReturnErr(c, err, "suno_config_invalid", http.StatusInternalServerError)
return
}
chatSubmitTemp := common.Templates["chat_stream_submit"]
chatTickTemp := common.Templates["chat_stream_tick"]
chatRespTemp := common.Templates["chat_resp"]
var requestData GeneralOpenAIRequest
err = c.ShouldBindJSON(&requestData)
if err != nil {
common.ReturnErr(c, err, "parse_body_failed", http.StatusBadRequest)
return
}
isStream := requestData.Stream
model := requestData.Model
switch requestData.Model {
case "suno-v3":
model = SunoModelChirpV3
case "suno-v3.5":
model = SunoModelChirpV35
}
chatID := "chatcmpl-" + c.GetString(middleware.RequestIdKey)
// do openai tools get params
_, params, relayErr := doTools(requestData)
if relayErr != nil {
common.ReturnRelayErr(c, relayErr)
return
}
params["mv"] = model
reqData, _ := json.Marshal(params)
taskID, relayErr := handles[po.TaskActionMusic](reqData)
if relayErr != nil {
common.ReturnRelayErr(c, relayErr)
return
}
timeout := time.After(5 * time.Minute)
tick := time.Tick(5 * time.Second)
if isStream {
setSSEHeaders(c)
c.Writer.WriteHeader(http.StatusOK)
}
first := false
for {
select {
case <-c.Request.Context().Done():
return
case <-timeout:
if isStream {
common.SendChatData(c.Writer, model, chatID, "timeout")
} else {
common.ReturnErr(c, fmt.Errorf("get feed task timeout"), "request_timeout", 504)
}
return
case <-tick:
task, relayErr := getTask[po.SunoSongs](taskID)
if relayErr != nil {
if isStream {
common.SendChatData(c.Writer, model, chatID, common.GetJsonString(relayErr))
} else {
common.ReturnRelayErr(c, relayErr)
}
return
}
if isStream {
if !first {
if chatSubmitTemp != nil {
var byteBuf bytes.Buffer
err = chatSubmitTemp.Execute(&byteBuf, task)
message := byteBuf.String()
common.SendChatData(c.Writer, model, chatID, message)
}
} else {
if chatTickTemp != nil {
var byteBuf bytes.Buffer
err = chatTickTemp.Execute(&byteBuf, task)
message := byteBuf.String()
common.SendChatData(c.Writer, model, chatID, message)
}
}
first = true
}
if !task.Status.IsDone() {
continue
}
var byteBuf bytes.Buffer
err = chatRespTemp.Execute(&byteBuf, task)
if err != nil {
relayErr = common.WrapperErr(err, common.ErrCodeInternalError, 500)
if requestData.Stream {
common.SendChatData(c.Writer, model, chatID, common.GetJsonString(relayErr))
} else {
common.ReturnRelayErr(c, relayErr)
}
return
}
message := byteBuf.String()
if isStream {
common.SendChatData(c.Writer, model, chatID, message)
common.SendChatDone(c.Writer)
return
} else {
responses := ChatCompletionsStreamResponse{
Id: chatID,
Object: "chat.completion",
Created: time.Now().Unix(),
Model: model,
Choices: []ChatCompletionsStreamResponseChoice{
{
Index: 0,
FinishReason: common.ToPtr("stop"),
Delta: struct {
Content string `json:"content"`
Role string `json:"role,omitempty"`
}{
Content: message,
Role: "assistant",
},
},
},
}
c.JSON(http.StatusOK, responses)
return
}
}
}
}
func checkChatConfig() error {
if common.ChatOpenaiApiBASE == "" {
return fmt.Errorf("CHAT_OPENAI_BASE is empty")
}
if common.ChatOpenaiApiKey == "" {
return fmt.Errorf("CHAT_OPENAI_KEY is empty")
}
_, ok := common.Templates["chat_resp"]
if !ok {
return fmt.Errorf("chat_resp template not found")
}
return nil
}
func doTools(requestData GeneralOpenAIRequest) (funcName string, res map[string]interface{}, opErr *common.RelayError) {
// do req
requestData.Model = common.ChatOpenaiModel
requestData.Tools = defaultToolsCalls
requestData.ToolChoice = "required"
requestData.Stream = false
b, err := json.Marshal(requestData)
if err != nil {
opErr = common.WrapperErr(err, "tools_json_body_failed", http.StatusInternalServerError)
return
}
resp, opErr := doOpenAIRequest(bytes.NewBuffer(b), false)
if opErr != nil {
return
}
defer func() {
if resp != nil {
err = resp.Body.Close()
if err != nil {
common.Logger.Errorw("body close", "err", err)
}
}
}()
// 使用 function call
body, _ := io.ReadAll(resp.Body)
var openaiResp openai.ChatCompletionResponse
err = json.Unmarshal(body, &openaiResp)
if err != nil {
opErr = common.WrapperErr(err, "tools_body_parse_failed", http.StatusInternalServerError)
return
}
if len(openaiResp.Choices) == 0 || len(openaiResp.Choices[0].Message.ToolCalls) == 0 {
opErr = common.WrapperErr(err, "no_tools", http.StatusInternalServerError)
return
}
callFunc := openaiResp.Choices[0].Message.ToolCalls[0]
res = make(map[string]interface{})
err = json.Unmarshal([]byte(callFunc.Function.Arguments), &res)
if err != nil {
opErr = common.WrapperErr(err, "parse_tools_failed", http.StatusBadRequest)
return
}
funcName = callFunc.Function.Name
return
}
func getTask[T po.TaskData](taskID string) (task *po.TaskWithData[T], opErr *common.RelayError) {
var err error
var exist bool
task, exist, err = po.GetTaskByTaskID[T](taskID)
if err != nil {
opErr = common.WrapperErr(err, common.ErrCodeInternalError, http.StatusInternalServerError)
return
}
if !exist {
opErr = common.WrapperErr(fmt.Errorf("task not exist"), common.ErrCodeInvalidRequest, http.StatusBadRequest)
return
}
return
}
var tagsDescription = `
## tags: The type of song. (Must be in english)
The following are the example options for each category:
Style = ['acoustic','aggressive','anthemic','atmospheric','bouncy','chill','dark','dreamy','electronic','emotional','epic','experimental','futuristic','groovy','heartfelt','infectious','melodic','mellow','powerful','psychedelic','romantic','smooth','syncopated','uplifting'];
Genres = ['afrobeat','anime','ballad','bedroom pop','bluegrass','blues','classical','country','cumbia','dance','dancepop','delta blues','electropop','disco','dream pop','drum and bass','edm','emo','folk','funk','future bass','gospel','grunge','grime','hip hop','house','indie','j-pop','jazz','k-pop','kids music','metal','new jack swing','new wave','opera','pop','punk','raga','rap','reggae','reggaeton','rock','rumba','salsa','samba','sertanejo','soul','synthpop','swing','synthwave','techno','trap','uk garage'];
Themes = ['a bad breakup','finding love on a rainy day','a cozy rainy day','dancing all night long','dancing with you for the last time','not being able to wait to see you again',"how you're always there for me","when you're not around",'a faded photo on the mantel','a literal banana','wanting to be with you','writing a face-melting guitar solo','the place where we used to go','being trapped in an AI song factory, help!'];
For example: epic new jack swing`
var promptDesc = `
Lyrics provided in Suno AI V3 optimized format. This format includes a combination structure such as [Intro] [Verse] [Bridge] [Chorus] [Inter] [Inter/solo] [Outro] [Ending], according to the 'Suno AI official instructions', note that about four lines of lyrics per part is the best choice.
The lyrics need to fit the user's description and can be appropriately extended enough to generate a 1 to 3 minute song.
【 Note 】
Sample lyrics (note the line wrapping format):
[Verse]
City streets, they come alive at night
Neon lights shining oh so bright (so bright)
Lost in the rhythm, caught in the beat
The energy's contagious, can't be discreet (ooh-yeah)
[Verse 2]
Dancin' like there's no tomorrow, we're in the zone
Fading into the music, we're not alone (alone)
Feel the passion in every move we make
We're shaking off the worries, we're wide awake (ooh-yeah)
[Chorus]
Under the neon lights, we come alive (come alive)
Feel the energy, we're soaring high (soaring high)
We'll dance until the break of dawn, all through the night (all night)
Under the neon lights (ooh-ooh-ooh)
`
var defaultToolsCalls = []Tool{
{
Type: string(openai.ToolTypeFunction),
Function: Function{
Name: "generate_song_custom",
Description: "You are sono ai, a songwriting AI.",
Parameters: Parameter{
Type: "object",
Required: []string{"tags", "prompt"},
Properties: map[string]Property{
"make_instrumental": {
Type: "boolean",
Description: "Specifies whether to generate instrumental music tracks? default is false, this property should be set to 'true' only if the user explicitly requests instrumental music.",
},
"prompt": {
Type: "string",
Description: promptDesc,
},
"title": {
Type: "string",
Description: "The name of the song,",
},
"tags": {
Type: "string",
Description: tagsDescription,
},
"continue_at": {
Type: "string",
Description: "The time to continue writing in seconds",
},
"continue_clip_id": {
Type: "string",
Description: "The id of the song to continue writing",
},
},
},
},
},
}
func doOpenAIRequest(requestBody io.Reader, isStream bool) (*http.Response, *common.RelayError) {
fullRequestURL := fmt.Sprintf("%s/v1/chat/completions", common.ChatOpenaiApiBASE)
req, err := http.NewRequest(http.MethodPost, fullRequestURL, requestBody)
if err != nil {
return nil, common.WrapperErr(err, "resp_body_null", http.StatusInternalServerError)
}
req.Header.Set("Authorization", "Bearer "+common.ChatOpenaiApiKey)
req.Header.Set("Content-Type", "application/json")
if isStream {
req.Header.Set("Accept", "text/event-stream")
} else {
req.Header.Set("Accept", "application/json")
}
resp, err := HTTPClient.Do(req)
if err != nil {
return nil, common.WrapperErr(err, "do_req_failed", http.StatusInternalServerError)
}
if resp == nil {
return nil, common.WrapperErr(err, "resp_body_null", http.StatusInternalServerError)
}
_ = req.Body.Close()
return resp, nil
}
func setSSEHeaders(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Transfer-Encoding", "chunked")
c.Writer.Header().Set("X-Accel-Buffering", "no")
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.