1const n=`--- 2slug: spring-cloud-alibaba-ai-use 3title: Spring Cloud Alibaba AI å¿«éä½éª 4date: 2024-04-30 21:01:08 5authors: yuluo 6tags: [LLM, Spring AI Alibaba, Java, Spring Cloud Alibaba] 7keywords: [LLM, Spring AI Alibaba, Java, Spring Cloud Alibaba] 8--- 9 10> **Spring Cloud Alibaba AI å·²ç»è¿ç§»å° Spring AI Alibaba.** 11 12<!-- truncate --> 13 14## Spring Cloud Alibaba AI å¿«éä½éª 15 16[Spring AI](https://docs.spring.io/spring-ai/reference/0.8-SNAPSHOT/index.html) æ¨å¨ç®åå å«äººå·¥æºè½ç¸å ³åè½çåºç¨ç¨åºçå¼åï¼é¿å ä¸å¿ è¦ç夿æ§ã 17 18Spring AI çæ ¸å¿æ¯æä¾æ½è±¡ï¼ä½ä¸ºå¼å AI åºç¨ç¨åºçåºç¡ãè¿äºæ½è±¡æå¤ç§å®ç°æ¹å¼ï¼åªéæå°çä»£ç æ¹å¨å³å¯è½»æ¾å®ç°å个大模åä¹é´ç忢ã 19 20Spring AI æä¾ä»¥ä¸åè½ï¼ 21 22- æ¯æææä¸»è¦æ¨¡åæä¾åï¼ä¾å¦ OpenAIãMicrosoftãAmazonãGoogle å Huggingfaceã 23- æ¯æçæ¨¡åç±»åå æ¬âè天âåâææ¬å°å¾åâï¼è¿ææ´å¤æ¨¡åç±»åæ£å¨å¼åä¸ã 24- è·¨ AI æä¾åçå¯ç§»æ¤ APIï¼ç¨äºè天ååµå ¥æ¨¡åãæ¯æåæ¥åæµ API é项ãè¿æ¯æä¸æè®¿é®æ¨¡åç¹å®åè½ã 25- AI 模åè¾åºå° POJO çæ å°ã 26- æ¯æææä¸»è¦ç¢éæ°æ®åºæä¾åï¼ä¾å¦ Azure ç¢éæç´¢ãChromaãMilvusãNeo4jãPostgreSQL/PGVectorãPineConeãQdrantãRedis å Weaviate 27- è·¨ Vector Store æä¾åçå¯ç§»æ¤ APIï¼å æ¬åæ ·å¯ç§»æ¤çæ°é¢ç类似 SQL çå æ°æ®è¿æ»¤å¨ APIã 28- 彿°è°ç¨ 29- AI 模åååéåå¨ç Spring Boot èªå¨é ç½®åå¯å¨å¨ã 30- æ°æ®å·¥ç¨ç ETL æ¡æ¶ 31 32## Spring Cloud Alibaba AI 33 34Spring Cloud Alibaba AI ï¼ä»¥ä¸ç®ç§° SCA AIï¼åºäº Spring AI 0.8.1 çæ¬å®æéä¹ç³»å大模åçæ¥å ¥ã 35 36DashScopeçµç§¯æ¨¡åæå¡å»ºç«å¨ *模å峿å¡*ï¼Model-as-a-Serviceï¼MaaSï¼çç念åºç¡ä¹ä¸ï¼å´ç»AIåé¢å模åï¼éè¿æ ååçAPIæä¾å æ¬æ¨¡åæ¨çãæ¨¡åå¾®è°è®ç»å¨å çå¤ç§æ¨¡åæå¡ãã 37 38SCA AI ç®åæ¯æçæ¨¡åä¸»è¦æï¼å¯¹è¯ãæçå¾ãæçè¯é³ï¼æ´å¤åè½ç¹æ§æ£å¨éé ä¸ã 39 40## å¿«éä½éª 41 42### å建 SCA AI åºç¨ 43 44å¨ pom.xml ä¸å¼å ¥å¦ä¸ä¾èµé ç½®ï¼ 45 46\`\`\`xml 47<dependency> 48 <groupId>com.alibaba.cloud</groupId> 49 <artifactId>spring-cloud-starter-alibaba-ai</artifactId> 50</dependency> 51 52<dependencyManagement> 53 <dependencies> 54 <dependency> 55 <groupId>com.alibaba.cloud</groupId> 56 <artifactId>spring-cloud-alibaba-dependencies</artifactId> 57 <version>\${spring.cloud.alibaba.version}</version> 58 <type>pom</type> 59 <scope>import</scope> 60 </dependency> 61 </dependencies> 62</dependencyManagement> 63 64<!-- å 为 Spring AI è¿æ²¡ææ£å¼åå¸å° maven ä»åºï¼æä»¥éè¦æ·»å æ¤é 置项 ç®å maven ä»åºä¸ºåçã 65issueï¼https://github.com/spring-projects/spring-ai/issues/537 66--> 67<repositories> 68 <repository> 69 <id>spring-milestones</id> 70 <name>Spring Milestones</name> 71 <url>https://repo.spring.io/milestone</url> 72 <snapshots> 73 <enabled>false</enabled> 74 </snapshots> 75 </repository> 76 <repository> 77 <id>spring-snapshots</id> 78 <name>Spring Snapshots</name> 79 <url>https://repo.spring.io/snapshot</url> 80 <releases> 81 <enabled>false</enabled> 82 </releases> 83 </repository> 84</repositories> 85\`\`\` 86 87#### api-key é ç½® 88 89卿£å¼å¼å§ä½éªä¹åï¼éè¦ç³è¯·å°æ¨¡åç api-keyãç³è¯·å°åï¼https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key 90 91æ¨å¯ä»¥éè¿ DashScope æä¾çæ¹å¼é ç½® api-keyï¼SCA AI å®å ¨å ¼å®¹ DashScope ç¯å¢åéé ç½® key çæ¹å¼ï¼https://help.aliyun.com/zh/dashscope/developer-reference/api-key-settings 92 93å½ç¶ä¹å¯ä»¥éè¿ SCA AI 䏿ä¾çå¦ä¸é 置项é ç½®ï¼ 94 95\`\`\`yml 96spring: 97 cloud: 98 ai: 99 tongyi: 100 # api-key setting: 101 api-key: sk-xxxxxxxxxxxxxxxxxxx 102\`\`\` 103 104#### è天对è¯ä½éª 105 106宿¹ Exampleï¼https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/spring-cloud-ai-example 107 108\`\`\`java 109public class ChatService { 110 111 // è天客æ·ç«¯ 112 private final ChatClient chatClient; 113 // stream æµå¼å®¢æ·ç«¯ 114 private final StreamingChatClient streamingChatClient; 115 116 @Autowired 117 public ChatService(ChatClient chatClient, StreamingChatClient streamingChatClient) { 118 119 this.chatClient = chatClient; 120 this.streamingChatClient = streamingChatClient; 121 } 122 123 @Override 124 public String normalCompletion(String message) { 125 126 Prompt prompt = new Prompt(new UserMessage(message)); 127 return chatClient.call(prompt).getResult().getOutput().getContent(); 128 } 129 130 @Override 131 public Map<String, String> streamCompletion(String message) { 132 133 StringBuilder fullContent = new StringBuilder(); 134 135 streamingChatClient.stream(new Prompt(message)) 136 .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults())) 137 .map(content -> content.getOutput().getContent()) 138 .doOnNext(fullContent::append) 139 .last() 140 .map(lastContent -> Map.of(message, fullContent.toString())) 141 .block(); 142 return Map.of(message, fullContent.toString()); 143 } 144 145} 146\`\`\` 147 148ä¹åï¼å建 controller æ¥å£è°ç¨ service æå¡ï¼ 149 150\`\`\`java 151@Autowired 152private ChatService chatService; 153 154@GetMapping("/example") 155public String completion( 156 @RequestParam(value = "message", defaultValue = "Tell me a joke") 157 String message 158) { 159 160 return chatService.completion(message); 161} 162 163@GetMapping("/stream") 164public Map<String, String> streamCompletion( 165 @RequestParam(value = "message", defaultValue = "请åè¯æè¥¿çº¢æ¿ççè ©æä¹åï¼") 166 String message 167) { 168 169 return chatService.streamCompletion(message); 170} 171\`\`\` 172 173ä¸é¢è¿è¡æ¥å£æµè¯ï¼ 174 175\`\`\`bash 176$ curl --get --data-urlencode 'message=Tell me a joke about a cow.' http://localhost:8080/ai/example 177 178# è·å¾çååºå¦ä¸ï¼ 179Here's a classic cow joke for you: 180 181Why did the farmer separate the chicken and the sheep from the cows? 182 183Because he wanted to have eggs-clusive relationships with his hens! 184\`\`\` 185 186#### æçå¾ä½éª 187 188\`\`\`java 189public class ImagesService { 190 191 // å¾åçæå®¢æ·ç«¯ 192 private final ImageClient imageClient; 193 194 @Autowired 195 public ImagesService(ImageClient client) { 196 197 this.imageClient = client; 198 } 199 200 @Override 201 public ImageResponse genImg(String imgPrompt) { 202 203 var prompt = new ImagePrompt(imgPrompt); 204 return imageClient.call(prompt); 205 } 206 207} 208\`\`\` 209 210æçå¾å¯¹åºç controller æ¥å£å¦ä¸ï¼ 211 212\`\`\`java 213@Autowired 214private ImagesService imagesService; 215 216@GetMapping("/img") 217public ImageResponse genImg( 218 @RequestParam(value = "prompt", defaultValue = "Painting a picture of blue water and blue sky.") String imgPrompt 219) { 220 221 return imagesService.genImg(imgPrompt); 222} 223\`\`\` 224 225æ¥å£è°ç¨ä½éªï¼ 226 227\`\`\`bash 228$ curl http://localhost:8080/img?prompt="ç¾å¥³" 229 230# ååºæ°æ®ä¸º base64 ç¼ç å¾çåå¾çç½åï¼æ³¨æï¼å¾ç url å°åæ¯ææææçã 231\`\`\` 232
233ç¹å»å°åæä»¬å¯ä»¥çå°å¦ä¸çæçç¾å¥³å¾çï¼ 234 235 236 237#### æçè¯é³ä½éª 238 239\`\`\`java 240public class AudioService { 241 242 private final SpeechClient speechClient; 243 244 @Autowired 245 public AudioService(SpeechClient client) { 246 247 this.speechClient = client; 248 } 249 250 @Override 251 public String genAudio(String text) { 252 253 var resWAV = speechClient.call(text); 254 return save(resWAV, SpeechSynthesisAudioFormat.WAV.getValue()); 255 } 256 257 // è¾ å©æ¹æ³ï¼ç¨äºå°æ¨¡åçååºä¿åå°æ¬å°. 258 private String save(ByteBuffer audio, String type) { 259 260 String currentPath = System.getProperty("user.dir"); 261 LocalDateTime now = LocalDateTime.now(); 262 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-HH-mm-ss"); 263 String fileName = currentPath + File.separator + now.format(formatter) + "." + type; 264 File file = new File(fileName); 265 266 try (FileOutputStream fos = new FileOutputStream(file)) { 267 fos.write(audio.array()); 268 } 269 catch (Exception e) { 270 throw new RuntimeException(e); 271 } 272 273 return fileName; 274 } 275 276} 277\`\`\` 278 279controller æ¥å£ä»£ç å¦ä¸ï¼ 280 281\`\`\`java 282 @Autowired 283 private AudioService audioService; 284 285 @GetMapping("/audio") 286 public String genAudio(@RequestParam(value = "prompt", 287 defaultValue = "ä½ å¥½ï¼Spring Cloud Alibaba AI æ¡æ¶ï¼") String prompt) { 288 289 return audioService.genAudio(prompt); 290 } 291\`\`\` 292 293è°ç¨æ¥å£ä½éªï¼ 294 295\`\`\`bash 296$ curl http://localhost:8080/ai/audio?prompt="ä½ å¥½ï¼äººå·¥æºè½ï¼" 297 298# ååºå¼ä¸ºä¿åçé³é¢è·¯å¾ã 299D:\\open_sources\\sca-ai\\spring-ai\\04-29-22-44-22.wav 300\`\`\` 301 302 303 304è³æ¤ï¼Spring Cloud Alibaba AI æä¾çä¸ç§åºæ¬åè½å·²ç»ä½éªå®æï¼åç»æ¨è¿å¯ä»¥éè¿è®¾ç½®ä¸äºä¸ªæ§åçåæ°æ¥ä½¿å¤§æ¨¡åçæåç§åæ ·çå¾çåé³é¢çã 305 306æ´å¤é 置项å¯ä»¥åèï¼https://help.aliyun.com/zh/dashscope/developer-reference/api-detailsã社åºç¸å ³çææ¡£æ£å¨å®åï¼æ´å¤åè½æ£å¨å¼åä¸ï¼ 307`;export{n as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.