Skip to content

Commit

Permalink
Merge pull request #64 from yuluo-yx/1018-yuluo/add-multi-model-examples
Browse files Browse the repository at this point in the history
example: add multi model examples
  • Loading branch information
chickenlj authored Oct 28, 2024
2 parents da4c364 + 005c079 commit 5b55475
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<modules>
<module>spring-ai-alibaba-core</module>
<module>spring-ai-alibaba-starter</module>
<module>spring-ai-alibaba-examples</module>
</modules>

<properties>
Expand Down
95 changes: 95 additions & 0 deletions spring-ai-alibaba-examples/multi-model-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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>

<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 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.
*/

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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright 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.
*/

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 com.alibaba.cloud.ai.example.multi.helper.FrameExtraHelper;
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
) {

List<Media> mediaList = FrameExtraHelper.createMediaList(10);

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();
}

}
Loading

0 comments on commit 5b55475

Please sign in to comment.