Getting Started#
Installing Vertex AI#
To Install Vertex AI you need to follow the following steps
Install Vertex Cloud SDK (https://googleapis.dev/python/aiplatform/latest/index.html)
Setup your Default Project, credentials, region
Basic auth example for service account#
%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
)
Basic Usage#
Basic call to the text-bison model
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```'
Streaming Usage#
Streaming#
list(llm.stream_complete("hello"))[-1].text
' Hello! How can I help you?'
Chat Usage#
chat generation#
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?'
Async Chat#
Asynchronous chat response#
(await chat.achat(messages=messages)).message.content
' Bonjour! Comment vas-tu?'
Streaming Chat#
streaming chat response#
list(chat.stream_chat(messages=messages))[-1].message.content
' Bonjour! Comment vas-tu?'
Gemini Models#
Calling Google Gemini Models using Vertex AI is fully supported.
Gemini Pro#
llm = Vertex(
model="gemini-pro", project=credentials.project_id, credentials=credentials
)
llm.complete("Hello Gemini").text
Gemini Pro Vision#
history = [
ChatMessage(
role="user",
content=[
{"type": "text", "text": "Explain what is in the image below:"},
{
"type": "image_url",
"image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBUWFRgWFhYVGRgaGRoZHBwZGBwaGBkcGhgZGRgYGBocIS4lHB4rIRgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QGhIRHDEhISE0NDQ0NDQxNDQ0NDE0NDQ0NDQ0NDQ0NDQ0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SMm2HEgHZDjhjAGdrg9EDnn4nCBjAhsHCYEnK0WSSQOHgy4QGRFxMzj2TMxfH1UD6wBDO6fhJAdO4BvdA6JkkBVoZZ0jdcnzAkxylQ1qga0BxvYgZGd8N+aSSFTmg7fHAF0f9kkkkH//Z",
},
],
),
]
llm = Vertex(
model="gemini-pro-vision",
project=credentials.project_id,
credentials=credentials,
)
llm.chat(history).message.content