GO中使用谷歌GEMINI模型

Google 最近通过 API 免费提供了其最新的多模态 LLMs 家族,同时还发布了慷慨的免费套餐。Google 还在多种流行的编程语言中发布了 SDK,包括 Go 语言。 这篇文章是如何使用 Go SDK 快速入门,以向模型提出混合文本和图像的问题的概述。

译自Using Gemini models from Go。作者 Eli Bendersky 。

任务

我们将要求模型解释两张龟的图像之间的区别,这张:

和这张:

使用 Google AI SDK

使用 Google AI SDK,您只需生成一个 API 密钥(与 OpenAI 的 API 类似)即可访问模型。Go SDK 位于github.com/google/gene...pkg.go.dev/github.com/...

以下是我们任务的代码:

go 复制代码
package main

import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "os"

  "github.com/google/generative-ai-go/genai"
  "google.golang.org/api/option"
)

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
  if err != nil {
    log.Fatal(err)
  }
  defer client.Close()

  model := client.GenerativeModel("gemini-pro-vision")

  imgData1, err := os.ReadFile("../images/turtle1.png")
  if err != nil {
    log.Fatal(err)
  }

  imgData2, err := os.ReadFile("../images/turtle2.png")
  if err != nil {
    log.Fatal(err)
  }

  prompt := []genai.Part{
    genai.ImageData("png", imgData1),
    genai.ImageData("png", imgData2),
    genai.Text("Describe the difference between these two pictures, with scientific detail"),
  }
  resp, err := model.GenerateContent(ctx, prompt...)

  if err != nil {
    log.Fatal(err)
  }

  bs, _ := json.MarshalIndent(resp, "", "    ")
  fmt.Println(string(bs))
}

由于 LLM API 是多模态的,SDK 提供了像 genai.ImageData 和 genai.Text 这样的辅助类型,以一种类型安全的方式包装输入。当我们运行此示例时,模型的响应会以 JSON 对象的形式输出。其中重要的部分是:

css 复制代码
"Content": {
  "Parts": [
    "The first picture is of a tortoise, which is a reptile characterized by
    its hard shell. The second picture is of a sea turtle, which is a reptile
    characterized by its flippers and streamlined shell. Tortoises are
    terrestrial animals, while sea turtles are marine animals. Tortoises have
    a domed shell, while sea turtles have a flattened shell. Tortoises have
    thick, scaly skin, while sea turtles have smooth, leathery skin. Tortoises
    have short legs with claws, while sea turtles have long flippers.
    Tortoises have a slow metabolism and can live for over 100 years, while
    sea turtles have a faster metabolism and typically live for around 50
    years."
  ],
  "Role": "model"
},

好的,现在我们知道了!

使用 GCP Vertex SDK

如果您是 GCP 的客户,并且已经设置了 GCP 项目的计费等其他事项,您可能想使用 Vertex Go SDK。 Go SDK 的一个很棒之处在于您几乎不需要更改代码!唯一的更改是导入行,从:

arduino 复制代码
"github.com/google/generative-ai-go/genai"

修改为:

arduino 复制代码
"cloud.google.com/go/vertexai/genai"

然后更改创建客户端的方式,因为身份验证是不同的。对于 Vertex,应该像这样创建客户端:

css 复制代码
client, err := genai.NewClient(ctx, os.Getenv("GCP_PROJECT_ID"), "us-central1")

其中GCP_PROJECT_ID是具有您的 GCP 项目的 env 变量,位置/区域可以根据您的偏好进行设置。其余代码保持完全相同!

有两个 SDK 是因为两个产品提供的功能在某些情况下可能有所不同。例如,GCP 的 SDK 可能允许您直接从存储桶或数据库表中读取数据。

代码

本文所有示例的完整代码 - 包括示例图像 - 可在 GitHub 上找到

本文在云云众生yylives.cc/)首发,欢迎大家访问。

相关推荐
AKAMAI9 分钟前
Akamai 宣布收购功能即服务公司 Fermyon
人工智能·云计算
河南博为智能科技有限公司16 分钟前
高集成度国产八串口联网服务器:工业级多设备联网解决方案
大数据·运维·服务器·数据库·人工智能·物联网
光路科技44 分钟前
人工智能时代,工业以太网正在“进化”成什么样?
人工智能
翔云 OCR API1 小时前
承兑汇票识别接口技术解析-开发者接口
开发语言·前端·数据库·人工智能·ocr
roman_日积跬步-终至千里1 小时前
【模式识别与机器学习(16)】聚类分析【1】:基础概念与常见方法
人工智能·机器学习
nvd111 小时前
一个简单的GitHub AI Agent 实现指南
人工智能·langchain
阿里云大数据AI技术1 小时前
【新模型速递】PAI-Model Gallery云上一键部署DeepSeek-V3.2模型
人工智能
阿恩.7702 小时前
2026年1月最新计算机、人工智能、经济管理国际会议:选对会议 = 论文成功率翻倍
人工智能·经验分享·笔记·计算机网络·金融·区块链
高-老师2 小时前
WRF模式与Python融合技术在多领域中的应用及精美绘图
人工智能·python·wrf模式
xinyu_Jina2 小时前
ikTok Watermark Remover:客户端指纹、行为建模与自动化逆向工程
前端·人工智能·程序人生·信息可视化