SpringBoot整合Doubao豆包大模型 doubao-1-5-vision-pro-32k 版本

1.准备工作-注册账号-开通大模型

进入火山云引擎,注册账号,完成个人认证(无需拍身份证,直接抖音号登录 扫脸即可)
https://www.volcengine.com/

选择产品-大模型-模型,设置限额(防止乱扣费)

1.1 开通大模型 ↓

1.2 注册API_KEY↓

2.引入依赖

这里为什么要exclusion了io.swagger 因为他这个丝袜哥有点怪怪的,不兼容我愿项目,大家自行exclusion

     <dependency>
                <groupId>com.volcengine</groupId>
                <artifactId>volcengine-java-sdk-ark-runtime</artifactId>
                <version>0.2.3</version>
            </dependency>
            <dependency>
                <groupId>com.volcengine</groupId>
                <artifactId>volcengine-java-sdk-core</artifactId>
                <version>0.2.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger.core.v3</groupId>
                        <artifactId>swagger-annotations</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

3.编写代码

 	 // https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey?apikey=%7B%7D
        static String apiKey = "[你的APIKEY]"; // 注意,是第二列的哦,就是*号的,点击显示,然后复制

        static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
        static Dispatcher dispatcher = new Dispatcher();
        static ArkService service =
                ArkService.builder()
                        .dispatcher(dispatcher)
                        .connectionPool(connectionPool)
                        .apiKey(apiKey)
                        .build();

        public static void main(String[] args) throws JsonProcessingException {

            List<ChatMessage> messagesForReqList = new ArrayList<>();

            ChatMessage elementForMessagesForReqList0 =
                    ChatMessage.builder().role(ChatMessageRole.USER).content("天空为什么是蓝色的?").build();

            messagesForReqList.add(elementForMessagesForReqList0);

            ChatCompletionRequest req =
                    ChatCompletionRequest.builder()
                            .model("doubao-1-5-vision-pro-32k-250115") // 这里替换成你的模型,注意都是小写的
                            .messages(messagesForReqList)
                            .build();

            service.createChatCompletion(req)
                    .getChoices()
                    .forEach(choice -> System.out.println(choice.getMessage().getContent()));
            // shutdown service after all requests is finished
            service.shutdownExecutor();
        }
    }

4.查看输出结果

在这里插入图片描述

5.扩展

### Spring Boot 集成豆包大模型 在现代应用程序开发中,将机器学习模型集成到Web应用是一个常见的需求。对于Spring Boot项目来说,可以采用多种方式来实现这一目标。下面介绍一种通过REST API调用来完成Spring Boot豆包大模型集成的方法。 #### 创建控制器类处理请求 为了使前端能够方便地向后端发送数据并接收预测结果,在Spring Boot工程里定义一个新的Controller用于接受HTTP POST请求: ```java @RestController @RequestMapping("/api/v1/doubao") public class DoubaoModelController { @PostMapping("/predict") public ResponseEntity<PredictionResponse> predict(@RequestBody PredictionRequest request){ // 调用服务层方法获取预测结果 PredictionService predictionService = new PredictionServiceImpl(); PredictionResponse response = predictionService.predict(request); return ResponseEntity.ok(response); } } ``` 这里假设`PredictionRequest`和`PredictionResponse`分别是输入参数以及返回的结果对象[^1]。 #### 实现业务逻辑的服务接口 接着创建一个名为`PredictionService`的接口及其具体实现类`PredictionServiceImpl`,负责实际调用外部API或者加载本地部署好的豆包大模型来进行推理计算: ```java @Service public interface PredictionService { PredictionResponse predict(PredictionRequest request); } @Component class PredictionServiceImpl implements PredictionService{ private static final String DOUBAO_API_URL = "https://example.com/api/predict"; @Override public PredictionResponse predict(PredictionRequest request) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers); ResponseEntity<PredictionResponse> response = restTemplate.exchange(DOUBAO_API_URL, HttpMethod.POST, entity, PredictionResponse.class); return response.getBody(); } } ``` 上述代码片段展示了如何利用`RestTemplate`工具发起对外部API服务器(`DOUBAO_API_URL`)的POST请求,并传入JSON格式的数据作为模型输入;最后解析响应体中的内容填充至自定义的对象结构内以便于后续使用。 请注意,这只是一个简单的例子,实际情况可能涉及到更复杂的配置项设置、错误处理机制设计等方面的工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder阿龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值