今天尝试使用java调用通义千问AL模型,实现图片问答功能,结果在pom.xml中引入最新的dashscope-sdk-java后,按照阿里云官方文档使用流式调用报错:okhttp3/internal/connection/RealCall,在网上搜索无果,经过分析相关功能博主的相关问题博客,终于找到问题根源,特做此记录,希望可以帮助到相关问题的朋友。
阿里云官方调用文档:https://help.aliyun.com/document_detail/2712587.html?spm=a2c4g.2712569.0.i12
1.引入的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
<version>2.14.0</version>
</dependency>
2.官方流式调用代码:
public static void streamCall()
throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
// must create mutable map.
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(new HashMap<String, Object>(){{put("image", "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg");}},
new HashMap<String, Object>(){{put("text", "基于这幅图,讲一个故事");}})).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.model(MultiModalConversation.Models.QWEN_VL_PLUS)
.message(userMessage)
.apiKey("you api key")
.build();
Flowable<MultiModalConversationResult> result = conv.streamCall(param);
result.blockingForEach(item -> {
System.out.println(item);
});
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
运行错误信息
Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/internal/connection/RealCall
at okhttp3.internal.sse.RealEventSource.connect(RealEventSource.kt:41)
at okhttp3.sse.EventSources.createFactory$lambda$1(EventSources.kt:34)
at com.alibaba.dashscope.protocol.okhttp.OkHttpHttpClient.lambda$streamOut$0(OkHttpHttpClient.java:279)
at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.BlockingFlowableIterable.iterator(BlockingFlowableIterable.java:42)
at io.reactivex.Flowable.blockingForEach(Flowable.java:5772)
at com.gkza.ruizhi.service.impl.TuringServiceImpl.streamCall(TuringServiceImpl.java:926)
at com.gkza.ruizhi.service.impl.TuringServiceImpl.main(TuringServiceImpl.java:933)
Caused by: java.lang.ClassNotFoundException: okhttp3.internal.connection.RealCall
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 11 more
错误原因:
经过分析错误是与项目中的huaweicloud的esdk-obs-java依赖里的okhttp版本冲突了,需注意pom文件里esdk-obs-java依赖在dashscope-sdk-java上方才会冲突,由此推断只要dashscope-sdk-java依赖上方有依赖里包括okhttp版本与dashscope-sdk-java冲突必会出现此问题。
具体如图:
以下是Maven Helper插件pom文件冲突的分析图
解决方法:
1.将esdk-obs-java依赖更换为与dashscope-sdk-java中的okhttp版本不冲突的版本,
2.pom文件里将esdk-obs-java依赖放到dashscope-sdk-java下方
3.删除esdk-obs-java依赖!