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/)首发,欢迎大家访问。

相关推荐
Lee川9 小时前
Milvus 实战:当 RAG 遇上向量数据库,从"玩具 Demo"到"生产可用的"那一步
前端·数据库·人工智能
小a彤10 小时前
elec-ops-inspection:电力巡检缺陷检测,NPU推理速度提升3倍
人工智能·cann
ZhengEnCi11 小时前
09aaa-LayerNorm是什么?
人工智能
这是谁的博客?11 小时前
AI Agent 安全架构设计:漏洞分析与防护策略深度解析
人工智能·安全·网络安全·ai·agent·安全架构·架构设计
人月神话-Lee11 小时前
【图像处理】Sobel 边缘检测——让机器“看见“轮廓
图像处理·人工智能·计算机视觉·ios·ai编程·swift
冬奇Lab11 小时前
Agent系列(四):工具调用深度解析——Agent 的手和眼
人工智能·llm
Black蜡笔小新11 小时前
自动化AI算法训练服务器DLTM助力医学影像分析进入AI智能分析新时代
人工智能·算法·自动化
冬奇Lab12 小时前
一天一个开源项目(第111篇):Understand Anything - 把代码库变成可探索知识图谱的 AI 引擎
人工智能·开源·llm
猿饵块12 小时前
git--github
人工智能
黎阳之光12 小时前
黎阳之光:以视频孪生重构智慧防火,打造“天空地人智”一体化森林防火新范式
大数据·运维·人工智能·物联网·安全