mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	【增加】AI Music支持描述模式、歌词模式生成音乐
This commit is contained in:
		| @@ -4,7 +4,7 @@ import cn.hutool.core.io.IoUtil; | ||||
| import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory; | ||||
| import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.suno.SunoConfig; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.suno.api.AceDataSunoApi; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal; | ||||
| import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions; | ||||
| @@ -150,8 +150,8 @@ public class YudaoAiAutoConfiguration { | ||||
|  | ||||
|     @Bean | ||||
|     @ConditionalOnProperty(value = "yudao.ai.suno.enable", havingValue = "true") | ||||
|     public AceDataSunoApi sunoApi(YudaoAiProperties yudaoAiProperties) { | ||||
|         return new AceDataSunoApi(new SunoConfig(yudaoAiProperties.getSuno().getToken())); | ||||
|     public SunoApi sunoApi(YudaoAiProperties yudaoAiProperties) { | ||||
|         return new SunoApi(new SunoConfig(yudaoAiProperties.getSuno().getBaseUrl())); | ||||
|     } | ||||
|  | ||||
|     private static @NotNull MidjourneyConfig getMidjourneyConfig(ApplicationContext applicationContext, | ||||
|   | ||||
| @@ -141,9 +141,9 @@ public class YudaoAiProperties { | ||||
|  | ||||
|         private boolean enable = false; | ||||
|         /** | ||||
|          * token | ||||
|          * suno-api 服务的基本地址 | ||||
|          */ | ||||
|         private String token; | ||||
|         private String baseUrl; | ||||
|  | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -13,7 +13,7 @@ import lombok.NoArgsConstructor; | ||||
| @AllArgsConstructor | ||||
| public class SunoConfig { | ||||
|     /** | ||||
|      * token信息 | ||||
|      * suno-api服务的基本路径 | ||||
|      */ | ||||
|     private String token; | ||||
|     private String baseUrl; | ||||
| } | ||||
|   | ||||
| @@ -1,115 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.ai.core.model.suno.api; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.core.model.suno.SunoConfig; | ||||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.ai.openai.api.ApiUtils; | ||||
| import org.springframework.web.reactive.function.client.WebClient; | ||||
| import reactor.core.publisher.Mono; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Suno API | ||||
|  * <br> | ||||
|  * 文档地址:https://platform.acedata.cloud/documents/d016ee3f-421b-4b6e-989a-8beba8701701 | ||||
|  * | ||||
|  * @Author xiaoxin | ||||
|  * @Date 2024/5/27 | ||||
|  */ | ||||
| @Slf4j | ||||
| public class AceDataSunoApi { | ||||
|  | ||||
|     public static final String DEFAULT_BASE_URL = "https://api.acedata.cloud/suno"; | ||||
|     private final WebClient webClient; | ||||
|  | ||||
|     public AceDataSunoApi(SunoConfig config) { | ||||
|         this.webClient = WebClient.builder() | ||||
|                 .baseUrl(DEFAULT_BASE_URL) | ||||
|                 .defaultHeaders(ApiUtils.getJsonContentHeaders(config.getToken())) | ||||
|                 .build(); | ||||
|     } | ||||
|  | ||||
|     // TODO @芋艿:方法名,要考虑下; | ||||
|     public SunoResp musicGen(SunoReq sunReq) { | ||||
|         return this.webClient.post() | ||||
|                 .uri("/audios") | ||||
|                 .body(Mono.just(sunReq), SunoReq.class) | ||||
|                 .retrieve() | ||||
|                 .onStatus(status -> !status.is2xxSuccessful(), | ||||
|                         response -> response.bodyToMono(String.class) | ||||
|                                 .handle((respBody, sink) -> { | ||||
|                                     log.error("【Suno】调用失败!resp: 【{}】", respBody); | ||||
|                                     sink.error(new IllegalStateException("【Suno】调用失败!")); | ||||
|                                 })) | ||||
|                 .bodyToMono(SunoResp.class) | ||||
|                 .block(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 请求数据对象,用于生成音乐音频。 | ||||
|      * | ||||
|      * @param prompt      用于生成音乐音频的提示 | ||||
|      * @param lyric       用于生成音乐音频的歌词 | ||||
|      * @param custom      指示音乐音频是否为定制,如果为 true,则从歌词生成,否则从提示生成 | ||||
|      * @param title       音乐音频的标题 | ||||
|      * @param style       音乐音频的风格 | ||||
|      * @param callbackUrl 音乐音频生成后回调的 URL | ||||
|      */ | ||||
|     @JsonInclude(value = JsonInclude.Include.NON_NULL) | ||||
|     public record SunoReq( | ||||
|             String prompt, | ||||
|             String lyric, | ||||
|             boolean custom, | ||||
|             String title, | ||||
|             String style, | ||||
|             String callbackUrl | ||||
|     ) { | ||||
|         public SunoReq(String prompt) { | ||||
|             this(prompt, null, false, null, null, null); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * SunoAPI 响应的数据。 | ||||
|      * | ||||
|      * @param success 表示请求是否成功 | ||||
|      * @param taskId  任务 ID | ||||
|      * @param data    音乐数据列表 | ||||
|      */ | ||||
|     public record SunoResp( | ||||
|             boolean success, | ||||
|             @JsonProperty("task_id") String taskId, | ||||
|             List<MusicData> data | ||||
|     ) { | ||||
|         /** | ||||
|          * 单个音乐数据。 | ||||
|          * | ||||
|          * @param id        音乐数据的 ID | ||||
|          * @param title     音乐音频的标题 | ||||
|          * @param imageUrl  音乐音频的图片 URL | ||||
|          * @param lyric     音乐音频的歌词 | ||||
|          * @param audioUrl  音乐音频的 URL | ||||
|          * @param videoUrl  音乐视频的 URL | ||||
|          * @param createdAt 音乐音频的创建时间 | ||||
|          * @param model     使用的模型名称 | ||||
|          * @param prompt    生成音乐音频的提示 | ||||
|          * @param style     音乐音频的风格 | ||||
|          */ | ||||
|         public record MusicData( | ||||
|                 String id, | ||||
|                 String title, | ||||
|                 @JsonProperty("image_url") String imageUrl, | ||||
|                 String lyric, | ||||
|                 @JsonProperty("audio_url") String audioUrl, | ||||
|                 @JsonProperty("video_url") String videoUrl, | ||||
|                 @JsonProperty("created_at") String createdAt, | ||||
|                 String model, | ||||
|                 String prompt, | ||||
|                 String style | ||||
|         ) { | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.yudao.framework.ai.core.model.suno.api; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.ai.core.model.suno.SunoConfig; | ||||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| @@ -25,10 +26,7 @@ import java.util.function.Predicate; | ||||
| @Slf4j | ||||
| public class SunoApi { | ||||
|  | ||||
|     public static final String DEFAULT_BASE_URL = "https://suno-9323szg26-status2xxs-projects.vercel.app"; | ||||
|     private final WebClient webClient; | ||||
|  | ||||
|  | ||||
|     private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful(); | ||||
|     private final Function<ClientResponse, Mono<? extends Throwable>> EXCEPTION_FUNCTION = response -> response.bodyToMono(String.class) | ||||
|             .handle((respBody, sink) -> { | ||||
| @@ -37,9 +35,9 @@ public class SunoApi { | ||||
|             }); | ||||
|  | ||||
|  | ||||
|     public SunoApi() { | ||||
|     public SunoApi(SunoConfig config) { | ||||
|         this.webClient = WebClient.builder() | ||||
|                 .baseUrl(DEFAULT_BASE_URL) | ||||
|                 .baseUrl(config.getBaseUrl()) | ||||
|                 .defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON)) | ||||
|                 .build(); | ||||
|     } | ||||
| @@ -141,8 +139,8 @@ public class SunoApi { | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public SunoReq(String prompt, String tags, String title) { | ||||
|             this(prompt, tags, title, null, false, false); | ||||
|         public SunoReq(String prompt, String mv, String tags, String title) { | ||||
|             this(prompt, tags, title, mv, false, false); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -13,24 +13,22 @@ import java.util.List; | ||||
|  */ | ||||
| public class SunoTests { | ||||
|  | ||||
|     private SunoConfig sunoConfig; | ||||
|     SunoApi sunoApi; | ||||
|  | ||||
|     @Before | ||||
|     public void setup() { | ||||
|         String token = "16b4356581984d538652354b60d69ff0"; | ||||
|         this.sunoConfig = new SunoConfig(token); | ||||
|         String url = "https://suno-ix9nve79x-status2xxs-projects.vercel.app"; | ||||
|         this.sunoApi = new SunoApi(new SunoConfig(url)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void selectById() { | ||||
|         SunoApi sunoApi = new SunoApi(); | ||||
|         System.out.println(sunoApi.selectById("d460ddda-7c87-4f34-b751-419b08a590ca,ff90ea66-49cd-4fd2-b44c-44267dfd5551")); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void generate() { | ||||
|         SunoApi sunoApi = new SunoApi(); | ||||
|         List<SunoApi.MusicData> generate = sunoApi.generate(new SunoApi.SunoReq("创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。")); | ||||
|         System.out.println(generate); | ||||
|     } | ||||
| @@ -38,7 +36,6 @@ public class SunoTests { | ||||
|  | ||||
|     @Test | ||||
|     public void doChatCompletion() { | ||||
|         SunoApi sunoApi = new SunoApi(); | ||||
|         List<SunoApi.MusicData> generate = sunoApi.doChatCompletion("创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。"); | ||||
|         System.out.println(generate); | ||||
|     } | ||||
| @@ -46,16 +43,13 @@ public class SunoTests { | ||||
|  | ||||
|     @Test | ||||
|     public void generateLyrics() { | ||||
|         SunoApi sunoApi = new SunoApi(); | ||||
|         SunoApi.LyricsData lyricsData = sunoApi.generateLyrics("A soothing lullaby"); | ||||
|         System.out.println(lyricsData); | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     @Test | ||||
|     public void selectLimit() { | ||||
|         SunoApi sunoApi = new SunoApi(); | ||||
|         SunoApi.LimitData limitData = sunoApi.selectLimit(); | ||||
|         System.out.println(limitData); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 xiaoxin
					xiaoxin