1. Installation
Install the traceAI and Vertex AI packages.
pip install traceAI-vertexai
pip install vertexai
2. Set Environment Variables
Set up your environment variables to authenticate with FutureAGI .
import os
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"
3. Initialize Trace Provider
Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines .
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
trace_provider = register(
project_type=ProjectType.OBSERVE,
project_name="vertexai_project",
)
Instrument your Project with VertexAI Instrumentor. This step ensures that all interactions with the VertexAI are tracked and monitored.
from traceai_vertexai import VertexAIInstrumentor
VertexAIInstrumentor().instrument(tracer_provider=trace_provider)
5. Create Vertex AI Components
Interact with Vertex AI as you normally would. Our Instrumentor will automatically trace and send the telemetry data to our platform.
import vertexai
from vertexai.generative_models import FunctionDeclaration, GenerativeModel, Part, Tool
vertexai.init(
project="project_name",
)
# Describe a function by specifying its schema (JsonSchema format)
get_current_weather_func = FunctionDeclaration(
name="get_current_weather",
description="Get the current weather in a given location",
parameters={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
)
# Tool is a collection of related functions
weather_tool = Tool(function_declarations=[get_current_weather_func])
# Use tools in chat
chat = GenerativeModel("gemini-1.5-flash", tools=[weather_tool]).start_chat()
6. Execute
Run your Vertex AI application.
if __name__ == "__main__":
# Send a message to the model. The model will respond with a function call.
for response in chat.send_message(
"What is the weather like in Boston?", stream=True
):
print(response)
# Then send a function response to the model. The model will use it to answer.
for response in chat.send_message(
Part.from_function_response(
name="get_current_weather",
response={"content": {"weather": "super nice"}},
),
stream=True,
):
print(response)