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

完事!!!

相关推荐
呱牛do it1 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 3)
java·vue
神の愛2 小时前
左连接查询数据 left join
java·服务器·前端
南境十里·墨染春水2 小时前
linux学习进展 线程同步——互斥锁
java·linux·学习
雨奔3 小时前
Kubernetes 联邦 Deployment 指南:跨集群统一管理 Pod
java·容器·kubernetes
杨凯凡3 小时前
【021】反射与注解:Spring 里背后的影子
java·后端·spring
lulu12165440783 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
riNt PTIP3 小时前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
Ares-Wang3 小时前
Flask》》 Flask-Bcrypt 哈希加密
后端·python·flask
小码哥_常3 小时前
Spring Boot项目大变身:为何要拆成这六大模块?
后端
老星*3 小时前
AI选股核心设计思路
java·ai·开源·软件开发