今天尝试使用java调用通义千问API,结果在pom.xml中引入dashscope-sdk-java后,直接所有dependency爆红,然后看了网上的方法,把所有dependency删掉,刷新,再重新粘上,再刷新,然后发现只有一个dashscope-sdk-java爆红,于是运行idea,采用了idea里提供的建议,重新下载dashscope-sdk-java,成功解决。
<!--阿里巴巴大模型-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>
            <version>2.8.3</version>
        </dependency>

使用的是2.8.3版本,里面为什么要加一个exclusion,是因为在解决之后运行时又报错,查了一下是和slf4j这个包冲突,所以把它排除掉,具体参考:java调用通义千问API-CSDN博客

然后从官网的文档里面找了一个代码测试:

// Copyright (c) Alibaba, Inc. and its affiliates.

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;


public class MyTest {
    public static void callWithMessage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("如何做西红柿鸡蛋?").build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        System.out.println(result);
    }


    public static void main(String[] args){
        try {
            Constants.apiKey="你自己的API-KEY";
            callWithMessage();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

结果本以为没什么问题了,运行之后又报错:

Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody;
	at com.alibaba.dashscope.protocol.okhttp.OkHttpHttpClient.buildRequest(OkHttpHttpClient.java:152)
	at com.alibaba.dashscope.protocol.okhttp.OkHttpHttpClient.send(OkHttpHttpClient.java:180)
	at com.alibaba.dashscope.api.SynchronizeHalfDuplexApi.call(SynchronizeHalfDuplexApi.java:55)
	at com.alibaba.dashscope.aigc.generation.Generation.call(Generation.java:86)
	at com.example.hjwoyu.MyTest.callWithMessage(MyTest.java:35)
	at com.example.hjwoyu.MyTest.main(MyTest.java:43)

于是引入okhttp包,结果依然报错。

看了pom.xml,里面虽然没有爆红,但是有omitted for conflict with,应该还是版本不兼容问题,于是我换上最新的okhttp

<!--ok http client-->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
        </dependency>

刷新,然后运行代码,这次没有报错了,成功解决!

更多推荐