Skip to content
LlamaIndex 文档版本 跟随上游 源快照 33654c78 更新于 2026-10-18

注意: Vertex 已在很大程度上被 Google GenAI 取代,Google GenAI 通过 google-genai 包支持 Vertex 的相同功能。请访问 Google GenAI 页面获取最新示例和文档。

安装 Vertex AI

要安装 Vertex AI,你需要遵循以下步骤

服务账号的基本身份验证示例

%pip install llama-index-llms-vertex
from llama_index.llms.vertex import Vertex
from google.oauth2 import service_account

filename = "vertex-407108-37495ce6c303.json"
credentials: service_account.Credentials = (
    service_account.Credentials.from_service_account_file(filename)
)
Vertex(
    model="text-bison", project=credentials.project_id, credentials=credentials
)

基本用法

对 text-bison 模型的基本调用

from llama_index.llms.vertex import Vertex
from llama_index.core.llms import ChatMessage, MessageRole

llm = Vertex(model="text-bison", temperature=0, additional_kwargs={})
llm.complete("Hello this is a sample text").text
' ```\nHello this is a sample text\n```'

异步用法

异步

(await llm.acomplete("hello")).text
' Hello! How can I help you?'

流式输出用法

流式

list(llm.stream_complete("hello"))[-1].text
' Hello! How can I help you?'

聊天用法

聊天生成

chat = Vertex(model="chat-bison")
messages = [
    ChatMessage(role=MessageRole.SYSTEM, content="Reply everything in french"),
    ChatMessage(role=MessageRole.USER, content="Hello"),
]
chat.chat(messages=messages).message.content
' Bonjour! Comment vas-tu?'

异步聊天

异步聊天响应

(await chat.achat(messages=messages)).message.content
' Bonjour! Comment vas-tu?'

流式聊天

流式聊天响应

list(chat.stream_chat(messages=messages))[-1].message.content
' Bonjour! Comment vas-tu?'

Gemini 模型

完全支持通过 Vertex AI 调用 Google Gemini 模型。

Gemini Pro

llm = Vertex(
    model="gemini-pro",
    project=credentials.project_id,
    credentials=credentials,
    context_window=100000,
)
llm.complete("Hello Gemini").text

Gemini Vision 模型

支持视觉的 Gemini 模型现在支持 TextBlock 和 ImageBlock,用于结构化多模态输入,取代了旧的基于字典的 content 格式。使用 blocks 通过文件路径或 URL 包含文本和图像。

使用图像路径的示例:

from llama_index.llms.vertex import Vertex
from llama_index.core.llms import ChatMessage, TextBlock, ImageBlock

history = [
    ChatMessage(
        role="user",
        blocks=[
            ImageBlock(path="sample.jpg"),
            TextBlock(text="What is in this image?"),
        ],
    ),
]
llm = Vertex(
    model="gemini-1.5-flash",
    project=credentials.project_id,
    credentials=credentials,
    context_window=100000,
)
print(llm.chat(history).message.content)
The image shows a man in handcuffs being escorted by police officers. The man is wearing a black jacket and a black hooded sweatshirt. He is being escorted by two police officers who are wearing black balaclavas and bulletproof vests. The officers are from the Romanian Gendarmerie. The image is likely from a news report or a documentary about a crime.

使用图像 URL 的示例:

from llama_index.llms.vertex import Vertex
from llama_index.core.llms import ChatMessage, TextBlock, ImageBlock

history = [
    ChatMessage(
        role="user",
        blocks=[
            ImageBlock(
                url="https://upload.wikimedia.org/wikipedia/commons/7/71/Sibirischer_tiger_de_edit02.jpg"
            ),
            TextBlock(text="What is in this image?"),
        ],
    ),
]
llm = Vertex(
    model="gemini-1.5-flash",
    project=credentials.project_id,
    credentials=credentials,
    context_window=100000,
)
print(llm.chat(history).message.content)
A close-up of a tiger's face. The tiger is looking directly at the camera with a serious expression. The fur is a mix of orange, black, and white. The background is blurred, making the tiger the main focus of the image.

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。