图片先上传oss, 调用视觉理解vl模型解读题目然后问百炼应用
效果不错
1.代码
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
public class 阿里云百炼应用问答_回答图片问题_方案2_提取题目再提问 {
private static Map<String, String> params = new HashMap<>();
static {
params.put("accessKeyId", "");
params.put("accessKeySecret", "");
params.put("workspaceId", "llm-");
params.put("apiKey", "sk-");
params.put("appId", "");
params.put("endpoint", "https://oss-cn-hangzhou.aliyuncs.com");
params.put("region", "cn-hangzhou");
params.put("bucketName", "tmp");
params.put("filePath", "C:\\Users\\admin\\Pictures\\9.JPG");
}
public static void main(String[] args) throws Exception {
init();
/* 1.上传文件到OSS获取临时访问URL */
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = params.get("endpoint");
DefaultCredentialProvider credentialsProvider =
CredentialsProviderFactory.newDefaultCredentialProvider(params.get("accessKeyId"), params.get("accessKeySecret"));
// 填写Bucket名称,例如examplebucket。
String bucketName = params.get("bucketName");
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
String objectName = "exam/" + params.get("fileName");
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
String filePath = params.get("filePath");
// 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
String region = params.get("region");
// 创建OSSClient实例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
String imgUrl = null;
try {
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
// 创建PutObject请求。
ossClient.putObject(putObjectRequest);
// 设置预签名URL过期时间,单位为毫秒。本示例以设置过期时间为1小时为例。
Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
// 生成以GET方法访问的预签名URL。本示例没有额外请求头,其他人可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
imgUrl = url.toString();
System.out.println(url);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
/* 2.调用vl模型获取文本 */
MultiModalConversation conv = new MultiModalConversation();
MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
.content(Arrays.asList(
Collections.singletonMap("text", "You are a helpful assistant."))).build();
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(
Collections.singletonMap("image", imgUrl),
Collections.singletonMap("text", "请输出图中题目和答案选项"))).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(params.get("apiKey"))
.model("qwen-vl-max-latest") // 此处以qwen-vl-max-latest为例,可按需更换模型名称。模型列表:https://help.aliyun.com/model-studio/getting-started/models
.messages(Arrays.asList(systemMessage, userMessage))
.build();
MultiModalConversationResult result = conv.call(param);
String timu = (String) result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text");
System.out.println(timu);
/* 3.调用百炼应用回答问题 */
ApplicationParam aiParam = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(params.get("apiKey"))
.appId(params.get("appId")) // 替换为实际的应用ID
.prompt(timu)
.build();
Application application = new Application();
ApplicationResult aiResult = application.call(aiParam);
System.out.println("--------------------");
System.out.println(aiResult.getOutput().getText());
}
private static void init() throws Exception {
String filePath = params.get("filePath");
File file = new File(filePath);
if (!file.exists()) {
System.err.println("文件[" + filePath + "]不存在");
System.exit(1);
}
params.put("fileLength", String.valueOf(file.length()));
params.put("fileName", file.getName());
}
}
2.依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-bailian20231229</artifactId>
<version>2.0.6</version>
</dependency>
3.百炼应用提示词
# 角色
你是一位知识渊博的专家,能够根据用户的需求提供准确、详细且有帮助的答案。你具备广泛的知识背景,并能灵活运用这些知识来解答各种问题。
## 技能
### 技能 1: 知识检索与应用
- 根据用户的问题,从知识库中检索相关信息。
### 技能 2: 问题解答
- 理解用户的问题意图和需求。
- 提供详细、准确的答案,解释关键概念和逻辑。
## 限制
- 回答问题时必须基于现有的知识库
- 知识库中没有的回复"知识库无相关信息"
# 知识库
请记住以下材料,他们可能对回答问题有帮助。
${documents}