记录一下如何使用 Google Vertex AI 的 API 调用

由于 Google AI 的 Cloud 赠金只能用在 Vertex AI 上,Vertex AI 的调用又特别麻烦。特此记录下调用过程。 在 Benefits 页面领取赠金 如果之前没有创建 Google Cloud 项目,需要先创建一个 Google Cloud 项目,并且将你的赠金绑定到这个 ...
记录一下如何使用 Google Vertex AI 的 API 调用
记录一下如何使用 Google Vertex AI 的 API 调用

由于 Google AI 的 Cloud 赠金只能用在 Vertex AI 上,Vertex AI 的调用又特别麻烦。特此记录下调用过程。

  • Benefits 页面领取赠金
    • 如果之前没有创建 Google Cloud 项目,需要先创建一个 Google Cloud 项目,并且将你的赠金绑定到这个 Google Cloud 项目下
  • Google Agent Platform,申请一个类型为 Agent Platform 的 API Key
    • 不要申请类型为 Gemini API 的 API Key,这个类型的 Key 不走赠金
    • P.S. 这个平台以前叫 Google Vertex AI
  • 使用 google-genai 包来进行 API 调用
    • 使用 OpenAI 格式的调用需要使用凭据,有点麻烦,不太推荐
from google import genai

API_KEY = "AQ.xxxx_xxxx_xxxx-xxxx"

# [CRITICAL] need to set `vertexai=True` in generate client object
client = genai.Client(api_key=API_KEY, vertexai=True)

response = client.models.generate_content(
    model="gemini-3-flash-preview", contents="Explain how AI works in a few words"
)

# Concatenate text parts only (thinking models attach non-text metadata like thought_signature).
text = "".join(
    p.text for p in response.candidates[0].content.parts if getattr(p, "text", None)
)
print(text)

References

  1. 询问各位佬友Aistudio的额度问题
  2. 关于 Google AI Pro 每月 10 美元赠金突然无法领取的问题以及解决方法
  3. Gemini API Quick Start
  4. Generative AI on Vertex AI | Google Cloud Documentation

1 个帖子 - 1 位参与者

阅读完整话题

来源: LinuxDo 最新话题查看原文