简单易用的 Open Ai Java SDK 2024.01.3 发布

来源: 投稿
2024-05-17 08:38:00

推荐一套基于 SpringBoot 开发的简单、易用的开源权限管理平台,建议下载使用: https://github.com/devlive-community/authx

推荐一套功能强大的开源数据中台系统:https://github.com/devlive-community/datacap

推荐一套全平台数据库管理工具,建议下载使用: https://github.com/devlive-community/dbm

OpenAI Java SDK 主要为 Java 开发人员提供方便易用的 SDK 来与开放 AI 模型的 API 进行交互依赖库。

本次发布增加了 OpenAI 开发的新模型 gpt-4o 和 Google Gemini

Google Gemini

  • 支持简单对话

    try (GoogleClient client = GoogleClient.builder()
        .apiKey(token)
        .build()) {
        PartEntity part = PartEntity.builder()
            .text("Hello, Open AI Java SDK!")
            .build();
        ObjectEntity object = ObjectEntity.builder()
            .parts(Lists.newArrayList(part))
            .build();
        ChatEntity chat = ChatEntity.builder()
            .contents(Lists.newArrayList(object))
            .build();
    
        ChatResponse response = client.createChatCompletions(chat);
        response.getCandidates()
            .forEach(item -> item.getContent()
                .getParts()
                .forEach(value -> log.info(value.getText())));
    }
    
     
  • 支持连续对话

    List<ObjectEntity> contents = Lists.newArrayList();
    PartEntity part = PartEntity.builder()
            .text("你好,我叫小明")
            .build();
    ObjectEntity object = ObjectEntity.builder()
            .parts(Lists.newArrayList(part))
            .build();
    contents.add(object);
    ChatEntity chat = ChatEntity.builder()
            .contents(contents)
            .build();
    ChatResponse response = client.createChatCompletions(chat);
    response.getCandidates()
            .forEach(item -> item.getContent()
                    .getParts()
                    .forEach(value -> {
                        log.info(value.getText());
    
                        contents.add(ObjectEntity.builder()
                                .role(RoleModel.MODEL)
                                .parts(Lists.newArrayList(PartEntity.builder()
                                        .text(value.getText())
                                        .build()))
                                .build());
                    }));
    
    ObjectEntity newObject = ObjectEntity.builder()
            .parts(Lists.newArrayList(PartEntity.builder()
                    .text("我刚刚说了什么")
                    .build()))
            .build();
    contents.add(newObject);
    ChatEntity newChat = ChatEntity.builder()
            .contents(contents)
            .build();
    client.createChatCompletions(newChat);
    
     
  • 支持流式响应

    // 构建客户端
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ConsoleEventSourceListener listener = ConsoleEventSourceListener.builder()
            .countDownLatch(countDownLatch)
            .build();
    GoogleClient client = GoogleClient.builder()
            .apiKey(ResourceUtils.getValue("google.token"))
            .listener(listener)
            .build();
    
    List<ObjectEntity> contents = Lists.newArrayList();
    PartEntity part = PartEntity.builder()
            .text("帮我写一万字的作文")
            .build();
    ObjectEntity object = ObjectEntity.builder()
            .parts(Lists.newArrayList(part))
            .build();
            contents.add(object);
    ChatEntity chat = ChatEntity.builder()
            .contents(contents)
            .build();
    client.createChatCompletions(chat);
    try {
        countDownLatch.await();
    }
    catch (InterruptedException e) {
        log.error("Interrupted while waiting", e);
    }
    
     

如果您对我们的项目感兴趣,欢迎贡献源码或 fork 源码。

GitHub

Gitee

感谢大家支持。

展开阅读全文
点击引领话题📣 发布并加入讨论🔥
0 评论
1 收藏
分享
返回顶部
顶部