Merge pull request #17 from GitPusher99/main
feat: add /v1/chat/completions
This commit is contained in:
		
						commit
						7ca5d13c5d
					
				
							
								
								
									
										4
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@ -1,12 +1,12 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "suno-api",
 | 
					  "name": "suno-api",
 | 
				
			||||||
  "version": "1.0.0",
 | 
					  "version": "1.1.0",
 | 
				
			||||||
  "lockfileVersion": 3,
 | 
					  "lockfileVersion": 3,
 | 
				
			||||||
  "requires": true,
 | 
					  "requires": true,
 | 
				
			||||||
  "packages": {
 | 
					  "packages": {
 | 
				
			||||||
    "": {
 | 
					    "": {
 | 
				
			||||||
      "name": "suno-api",
 | 
					      "name": "suno-api",
 | 
				
			||||||
      "version": "1.0.0",
 | 
					      "version": "1.1.0",
 | 
				
			||||||
      "license": "LGPL-3.0-or-later",
 | 
					      "license": "LGPL-3.0-or-later",
 | 
				
			||||||
      "dependencies": {
 | 
					      "dependencies": {
 | 
				
			||||||
        "@vercel/analytics": "^1.2.2",
 | 
					        "@vercel/analytics": "^1.2.2",
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										72
									
								
								src/app/v1/chat/completions/route.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/app/v1/chat/completions/route.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
				
			|||||||
 | 
					import { NextResponse, NextRequest } from "next/server";
 | 
				
			||||||
 | 
					import { sunoApi } from "@/lib/SunoApi";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const dynamic = "force-dynamic";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * desc
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export async function POST(req: NextRequest) {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const body = await req.json();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let userMessage = null;
 | 
				
			||||||
 | 
					    const { messages } = body;
 | 
				
			||||||
 | 
					    for (let message of messages) {
 | 
				
			||||||
 | 
					      if (message.role == 'user') {
 | 
				
			||||||
 | 
					        userMessage = message;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!userMessage) {
 | 
				
			||||||
 | 
					      return new NextResponse(JSON.stringify({ error: 'Prompt message is required' }), {
 | 
				
			||||||
 | 
					        status: 400,
 | 
				
			||||||
 | 
					        headers: {
 | 
				
			||||||
 | 
					          'Content-Type': 'application/json',
 | 
				
			||||||
 | 
					          'Access-Control-Allow-Origin': '*',
 | 
				
			||||||
 | 
					          'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
 | 
				
			||||||
 | 
					          'Access-Control-Allow-Headers': 'Content-Type, Authorization',
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const audioInfo = await (await sunoApi).generate(userMessage.content, true, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const audio = audioInfo[0]
 | 
				
			||||||
 | 
					    const data = `## 歌曲名称:${audio.title}\n\n### 歌词:\n${audio.lyric}\n### 听歌链接: ${audio.audio_url}`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return new NextResponse(data, {
 | 
				
			||||||
 | 
					      status: 200,
 | 
				
			||||||
 | 
					      headers: {
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Origin': '*',
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Headers': 'Content-Type, Authorization',
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  } catch (error: any) {
 | 
				
			||||||
 | 
					    console.error('Error generating custom audio:', JSON.stringify(error.response.data));
 | 
				
			||||||
 | 
					    return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
 | 
				
			||||||
 | 
					      status: 500,
 | 
				
			||||||
 | 
					      headers: {
 | 
				
			||||||
 | 
					        'Content-Type': 'application/json',
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Origin': '*',
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
 | 
				
			||||||
 | 
					        'Access-Control-Allow-Headers': 'Content-Type, Authorization',
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function OPTIONS(request: Request) {
 | 
				
			||||||
 | 
					  return new Response(null, {
 | 
				
			||||||
 | 
					    status: 200,
 | 
				
			||||||
 | 
					    headers: {
 | 
				
			||||||
 | 
					      'Access-Control-Allow-Origin': '*',
 | 
				
			||||||
 | 
					      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
 | 
				
			||||||
 | 
					      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user