-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yuluo-yx <[email protected]>
- Loading branch information
Showing
6 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
Copyright 2023-2024 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.3.3</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<groupId>com.alibaba.cloud.ai</groupId> | ||
<artifactId>multi-model-example</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>multi-model-example</name> | ||
<description>Multi Model Example project for Spring AI Alibaba</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version> | ||
|
||
<!-- Spring AI --> | ||
<spring-ai-alibaba.version>1.0.0-M2.1</spring-ai-alibaba.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.cloud.ai</groupId> | ||
<artifactId>spring-ai-alibaba-starter</artifactId> | ||
<version>${spring-ai-alibaba.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>${maven-deploy-plugin.version}</version> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>https://repo.spring.io/milestone</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
</repositories> | ||
|
||
</project> |
19 changes: 19 additions & 0 deletions
19
...model-example/src/main/java/com/alibaba/cloud/ai/example/multi/MultiModelApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.alibaba.cloud.ai.example.multi; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author yuluo | ||
* @author <a href="mailto:[email protected]">yuluo</a> | ||
*/ | ||
|
||
@SpringBootApplication | ||
public class MultiModelApplication { | ||
|
||
public static void main(String[] args) { | ||
|
||
SpringApplication.run(MultiModelApplication.class, args); | ||
} | ||
|
||
} |
172 changes: 172 additions & 0 deletions
172
...ple/src/main/java/com/alibaba/cloud/ai/example/multi/controller/MultiModelController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package com.alibaba.cloud.ai.example.multi.controller; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel; | ||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; | ||
import com.alibaba.cloud.ai.dashscope.chat.MessageFormat; | ||
import jakarta.annotation.Resource; | ||
|
||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.ai.chat.model.ChatModel; | ||
import org.springframework.ai.chat.model.ChatResponse; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.model.Media; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.util.MimeType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author yuluo | ||
* @author <a href="mailto:[email protected]">yuluo</a> | ||
*/ | ||
|
||
@RestController | ||
@RequestMapping("/ai/multi") | ||
public class MultiModelController { | ||
|
||
@Resource | ||
private ChatModel chatModel; | ||
|
||
@Resource | ||
private ResourceLoader resourceLoader; | ||
|
||
private static final String DEFAULT_PROMPT = "这些是什么?"; | ||
|
||
private static final String DEFAULT_MODEL = "qwen-vl-max-latest"; | ||
|
||
@GetMapping("/image") | ||
public String image( | ||
@RequestParam(value = "prompt", required = false, defaultValue = DEFAULT_PROMPT) | ||
String prompt | ||
) throws Exception { | ||
|
||
List<Media> mediaList = List.of( | ||
new Media( | ||
MimeType.valueOf("image/png"), | ||
new URI("https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg").toURL() | ||
) | ||
); | ||
|
||
UserMessage message = new UserMessage(prompt, mediaList); | ||
ChatResponse response = chatModel.call( | ||
new Prompt( | ||
message, | ||
DashScopeChatOptions.builder() | ||
.withModel(DEFAULT_MODEL) | ||
.withMultiModel(true) | ||
.build() | ||
) | ||
); | ||
|
||
return response.getResult().getOutput().getContent(); | ||
} | ||
|
||
@GetMapping("/video") | ||
public String video( | ||
@RequestParam(value = "prompt", required = false, defaultValue = DEFAULT_PROMPT) | ||
String prompt | ||
) throws Exception { | ||
|
||
List<Media> mediaList = List.of( | ||
|
||
new Media( | ||
MimeType.valueOf("image/png"), | ||
new URI("https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg").toURL() | ||
), | ||
new Media( | ||
MimeType.valueOf("image/png"), | ||
new URI("https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg").toURL() | ||
), | ||
new Media( | ||
MimeType.valueOf("image/png"), | ||
new URI("https://img.alicdn.com/imgextra/i4/O1CN01Ib0clU27vTgBdbVLQ_!!6000000007859-0-tps-3840-2160.jpg").toURL() | ||
), | ||
new Media( | ||
MimeType.valueOf("image/png"), | ||
new URI("https://img.alicdn.com/imgextra/i1/O1CN01aygPLW1s3EXCdSN4X_!!6000000005710-0-tps-3840-2160.jpg").toURL() | ||
)); | ||
|
||
UserMessage message = new UserMessage(prompt, mediaList); | ||
message.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.VIDEO); | ||
|
||
ChatResponse response = chatModel.call( | ||
new Prompt( | ||
message, | ||
DashScopeChatOptions.builder() | ||
.withModel(DEFAULT_MODEL) | ||
.withMultiModel(true) | ||
.build() | ||
) | ||
); | ||
|
||
return response.getResult().getOutput().getContent(); | ||
} | ||
|
||
@GetMapping("/image/bin") | ||
public String imagesBinary( | ||
@RequestParam(value = "prompt", required = false, defaultValue = DEFAULT_PROMPT) | ||
String prompt | ||
) { | ||
|
||
UserMessage message = new UserMessage( | ||
prompt, | ||
new Media( | ||
MimeType.valueOf("image/jpeg"), | ||
resourceLoader.getResource("classpath:/multimodel/dog_and_girl.jpeg") | ||
)); | ||
message.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.VIDEO); | ||
|
||
ChatResponse response = chatModel.call( | ||
new Prompt( | ||
message, | ||
DashScopeChatOptions.builder() | ||
.withModel(DEFAULT_MODEL) | ||
.withMultiModel(true) | ||
.build() | ||
) | ||
); | ||
|
||
return response.getResult().getOutput().getContent(); | ||
} | ||
|
||
@GetMapping("/stream/image") | ||
public String streamImage( | ||
@RequestParam(value = "prompt", required = false, defaultValue = DEFAULT_PROMPT) | ||
String prompt | ||
) { | ||
|
||
UserMessage message = new UserMessage( | ||
prompt, | ||
new Media( | ||
MimeType.valueOf("image/jpeg"), | ||
resourceLoader.getResource("classpath:/multimodel/dog_and_girl.jpeg") | ||
)); | ||
|
||
message.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.VIDEO); | ||
List<ChatResponse> response = chatModel.stream( | ||
new Prompt( | ||
message, | ||
DashScopeChatOptions.builder() | ||
.withModel(DEFAULT_MODEL) | ||
.withMultiModel(true) | ||
.build() | ||
) | ||
).collectList().block(); | ||
|
||
StringBuilder result = new StringBuilder(); | ||
if (response != null) { | ||
for (ChatResponse chatResponse : response) { | ||
String outputContent = chatResponse.getResult().getOutput().getContent(); | ||
result.append(outputContent); | ||
} | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
spring-ai-alibaba-examples/multi-model-example/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
server: | ||
port: 8080 | ||
|
||
spring: | ||
application: | ||
name: multi-model-example-application | ||
|
||
ai: | ||
dashscope: | ||
chat: | ||
api-key: ${AI_DASHSCOPE_API_KEY} |
Binary file added
BIN
+485 KB
...ba-examples/multi-model-example/src/main/resources/multimodel/dog_and_girl.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters