java对接豆包AI视觉理解模型

火山方舟里的配置教程就不说了,一定要选择支持图片理解的ai模型,具体代码如下,如果有图片的外网链接的话,可直接拿外网链接传入识别,如果图片没有链接访问的话,可以尝试转base64传入,或者上传到oss

public static void main(String[] args) {
        // 4. 调用大模型Chat
        ArkService service = new ArkService("你的apikey");
        String url1="http:/外网ip:端口/image_1.jpg";

        System.out.println("----- image input -----");
        final List<ChatMessage> messages = new ArrayList<>();
        final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
        multiParts.add(ChatCompletionContentPart.builder().type("text").text(
                "。识别图片中的简历,并输出json字符串格式"
        ).build());
        multiParts.add(ChatCompletionContentPart.builder().type("image_url").imageUrl(
                new ChatCompletionContentPart.ChatCompletionContentPartImageURL(url1)
        ).build());
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER)
                .multiContent(multiParts).build();
        messages.add(userMessage);

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model("你的模型ep-开头的id")
                .messages(messages)
                .build();

        service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
        // shutdown service
        service.shutdownExecutor();
    }
### 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格式的数据作为模型输入;最后解析响应体中的内容填充至自定义的对象结构内以便于后续使用。 请注意,这只是一个简单的例子,实际情况可能涉及到更复杂的配置项设置、错误处理机制设计等方面的工作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值