Springboot引入通义千文大模型API

1、首先需要注册阿里云的账号(这里就不说了,很简单)

2、获得APIKEY 找到阿里云的这个网站

https://bailian.console.aliyun.com/#/app-center

或者直接在阿里云搜索阿里云百练

右上角用户的头像那里有个API-KEY,申请一个即可。

3、引入依赖

bash 复制代码
    <!--阿里巴巴大模型-->
            <!-- 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>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.11.1</version>
            </dependency>

4、编写代码

java 复制代码
 public static GenerationResult callWithMessage() throws NoApiKeyException, ApiException, InputRequiredException {
        Constants.apiKey = "你的API-KEY";
        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);
        log.info("千问api回答的话语为:{}", result);
        System.out.println(result);
        return result;
    }

完事!!!

相关推荐
raoxiaoya1 小时前
同时安装多个版本的golang
开发语言·后端·golang
此木|西贝2 小时前
【设计模式】享元模式
java·设计模式·享元模式
考虑考虑3 小时前
go使用gorilla/websocket实现websocket
后端·程序员·go
李少兄3 小时前
解决Spring Boot多模块自动配置失效问题
java·spring boot·后端
bxlj_jcj3 小时前
JVM性能优化之年轻代参数设置
java·性能优化
八股文领域大手子3 小时前
深入理解缓存淘汰策略:LRU 与 LFU 算法详解及 Java 实现
java·数据库·算法·缓存·mybatis·哈希算法
不当菜虚困4 小时前
JAVA设计模式——(八)单例模式
java·单例模式·设计模式
Piper蛋窝4 小时前
Go 1.19 相比 Go 1.18 有哪些值得注意的改动?
后端
m0_740154674 小时前
Maven概述
java·maven