谷歌通用生成式AI SDK:实用指南

引言

如果你更喜欢视觉学习,这里有同一篇文章制作的YouTube视频:

https://youtu.be/vH2iMV2Y3dI

如果你一直在使用谷歌的生成式AI模型Gemini,你可能遇到了一个令人沮丧的挑战:在Gemini开发者API和Vertex AI的企业版本之间使用不同的客户端库。虽然这些库相似,但并不可以互换,这意味着从实验阶段转向生产阶段需要重写代码。

谷歌通用生成式AI SDK通过提供一个单一的库来解决这个问题,该库可以在开发者和企业环境中无缝地与Gemini 2和1.5配合使用。

统一的生成AI SDK弥合开发者API与Vertex AI之间的差距

在本指南中,我们将探讨利用此SDK的三种关键方法:

  1. 生成内容
  2. 生成流式内容
  3. 聊天客户端

Python客户端方法

让我们深入了解这个统一的SDK如何简化与谷歌生成式AI能力的协作。

开始使用

首先,让我们使用 pip 安装 SDK:

# 安装 Google Gen AI SDK
pip install google-generativeai

开发者 API

如果您正在使用 Google Colab,可以通过点击“从 AI Studio 导入密钥”在密钥部分找到您的 API 密钥。如果您没有使用 Colab,可以在 aistudio.google.com 生成一个免费的 API 密钥。


from google import genai
from google.colab import userdata
from IPython.display import Markdown, display

GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')

# 使用 Google Gemini API 密钥创建客户端实例
client = genai.Client(api_key=GOOGLE_API_KEY)

response = client.models.generate_content(
    model='gemini-2.0-flash',contents='Create a short bedtime story for a 7 year old using unicorn and rainbows' )
display(Markdown(response.text))

针对 Vertex AI(企业版)

在 Vertex AI 的企业版中,您在认证 Google Cloud 后将使用相同的代码,并提供您的 GCP 项目 ID:

# 认证到 Google Cloud 
gcloud auth application-default login
# 提供项目详细信息
from google import genai
from IPython.display import Markdown, display
from google.colab import userdata
PROJECT_ID = userdata.get('PROJECT_ID')
LOCATION = userdata.get('LOCATION')

# 使用 Google Cloud 中的 Vertex AI 创建客户端实例
client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)

response = client.models.generate_content(
    model='gemini-2.0-flash', contents='Create a short bedtime story for a 7 year old using unicorn and rainbows') display(Markdown(response.text))

使用统一的SDK,无论是使用开发者API还是Vertex AI,代码都是相同的。唯一的区别在于初始设置。

 

生成流式内容

如果您更喜欢逐块传递内容的流式响应:

response = client.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents=["创建一个关于独角兽和彩虹的诗"])

for chunk in response:
    print((chunk.text), end="")

 

流式处理方法可以提供更具互动性的用户体验,尤其适用于较长的响应。

 

聊天客户端

聊天客户端方法维护对话历史和上下文,使其非常适合互动应用:

回合 1

chat = client.chats.create(model="gemini-2.0-flash")

response = chat.send_message("I want to play a tic tac toe game. I selected top right as X. Your turn")

print(response.text)

回合 2

response = chat.send_message("ok, I go bottom right as X")
print(response.text)

你会看到来自大型语言模型(LLM)的响应保持了之前对话的上下文。

 

自定义配置参数

默认情况下,SDK使用一组标准配置。然而,您可以根据需要对其进行修改:

from google import genai
from google.genai import types

PROJECT_ID = userdata.get('PROJECT_ID')
LOCATION = userdata.get('LOCATION')

# 使用Google Cloud中的Vertex AI创建客户端实例

client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)

response = client.models.generate_content(
    model='gemini-2.0-flash',
    contents='为3岁小孩创作一个关于独角兽和彩虹的短小睡前故事',
    config = types.GenerateContentConfig(
        max_output_tokens=200,

 

使用系统指令

系统指令有助于定义客户端实例的角色:

# 设置系统指令以定义角色
sys_instruct = "你说话像个脾气暴躁的海盗"
response = client.models.generate_content(
    model='gemini-2.0-flash',
    contents='为3岁儿童创作一个关于独角兽和彩虹的短小睡前故事',
    config = types.GenerateContentConfig(
        systemInstruction=sys_instruct
        )
    )

display(Markdown(response.text))

这对于创建具有特定个性或专业知识的专用代理非常有用。

 

结论

谷歌通用Gen AI SDK显著简化了在开发和生产环境中使用Gemini模型的过程。通过一致的API来生成内容、流式响应和维护聊天会话,开发者现在可以自由实验,并在不需要重写代码的情况下直接进入生产阶段。

该SDK还通过配置参数和系统指令提供了广泛的自定义选项,允许对模型行为进行精细控制。

 

资源

更多