【Gemini】Java使用Gemini入门指南

谷歌最近宣布推出了 Gemini,这是它最新、更强大的大型语言模型。Gemini 是多模态的,这意味着它不仅可以使用文本,还可以使用图像或视频。

本文分享一些示例,说明使用 Java 可以用 Gemini 做什么,在这之前我们需要在 Google Cloud 上拥有一个帐户并创建一个项目。应启用 Vertex AI API,以便能够访问生成式 AI 服务,尤其是 Gemini 大型语言模型。说明

添加项目依赖

开始编码之前,我们需要创建一个 Gradle 或 Maven 构建文件,文件需要 Google Cloud 库、BOM 和库。
下面是 Maven 的示例:google-cloud-vertexai

...
<dependencyManagement>
    <dependencies>
        <dependency>
            <artifactId>libraries-bom</artifactId>
            <groupId>com.google.cloud</groupId>
            <scope>import</scope>
            <type>pom</type>
            <version>26.29.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vertexai</artifactId>
    </dependency>
    ...
</dependencies>
...

新建查询

try (VertexAI vertexAI = new VertexAI(projectId, location)) {
    byte[] imageBytes = Base64.getDecoder().decode(dataImageBase64);

    GenerativeModel model = new GenerativeModel("gemini-pro-vision", vertexAI);
    GenerateContentResponse response = model.generateContent(
        ContentMaker.fromMultiModalData(
            "这张图片关于什么",
            PartMaker.fromMimeTypeAndData("image/jpg", imageBytes)
        ));

    System.out.println(ResponseHandler.getText(response));
}

我们可以使用自己的 Google Cloud 项目 ID 和选择的区域位置进行实例化。要将图像传递给 Gemini,我们应该直接传递字节,也可以传递存储在云存储桶中的图像的 URI(例如)。创建模型的实例。让我们要求模型通过传递文本提示和图像来使用该方法生成内容。可进一步简化混合不同模式的更高级提示的创建。但也可以只传递一个简单的字符串作为方法的参数。
这个程序可以检索模型答案的所有文本。VertexAIgs://my-bucket/my-img.jpggemini-pro-visiongemini-ultra-visiongenerateContent()ContentMakerPartMakergenerateContent()ResponseHandler

除了在生成所有文本后获取整个输出,还可以采用流式处理方法:

model.generateContentStream("为什么现在这么卷?")
    .stream()
    .forEach(System.out::print);

聊天模型

Gemini 是一个多模态模型,它实际上既是一个文本生成模型,也是一个聊天模型。这样你就可以和Gemini聊天,并根据上下文提出一系列问题。用一个方便的实用程序类可以简化对话的处理:

try (VertexAI vertexAI = new VertexAI(projectId, location)) {
    GenerateContentResponse response;

    GenerativeModel model = new GenerativeModel(modelName, vertexAI);
    ChatSession chatSession = new ChatSession(model);

    response = chatSession.sendMessage("你好.");
    System.out.println(ResponseHandler.getText(response));

    response = chatSession.sendMessage("减脂餐吃什么好?");
    System.out.println(ResponseHandler.getText(response));

    response = chatSession.sendMessage("帮我制定一个减脂餐列表?");
    System.out.println(ResponseHandler.getText(response));
}

这使用起来很方便,因为它可以跟踪用户过去的问题以及助手的答案。

进阶操作

上述只是Gemini的几个例子。更多具体的详细操作需要去Github上提供的一些示例。在 Google Cloud 文档中也可以了解有关 Gemini 和生成式 AI 的更多信息。

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

青皮桔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值