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.扩展

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐