vertex ai agent builder deploy langchain app

 We will be using google colab to demostrate the deployment of a simple app to Vertex AI agent builder 

Create your notebook and then run the following codes 

!pip install --upgrade --quiet google-cloud-aiplatform[agent_engines,langchain]>=1.112

Next we will do some authentication and initialization

import vertexai

vertexai.init(
    project="project-xxxxxxx",               # Your project ID.
    location="us-central1",
    staging_bucket="gs://staging-bucket" # Replace with your GCS bucket
)

client = vertexai.Client(
    project="project-xxxxxxxxx",   # Your project ID.
    location="us-central1"
)

Creating a simple function

def get_exchange_rate(
    currency_from: str = "USD",
    currency_to: str = "EUR",
    currency_date: str = "latest",
):
    """Retrieves the exchange rate between two currencies on a specified date."""
    import requests

    response = requests.get(
        f"https://api.frankfurter.app/{currency_date}",
        params={"from": currency_from, "to": currency_to},
    )
    return response.json()


And invoking the tool using gemini-flash

from vertexai import agent_engines

agent = agent_engines.LanggraphAgent(
    model="gemini-2.0-flash",
    tools=[get_exchange_rate],
    model_kwargs={
        "temperature": 0.28,
        "max_output_tokens": 1000,
        "top_p": 0.95,
    },
)

Getting response from the agent

remote_agent.query(input={
    "messages": [
        ("user", "What is the exchange rate from US dollars to SEK today?"),
    ]
})

Let's deploy our agent and you can see we are specifying some dependencies. You may run
into some dependencies issues with module versioning. So something to take note

remote_agent = agent_engines.create(
    agent,
    requirements=[
        "google-cloud-aiplatform[agent_engines,langchain]==1.140.0",
        "pydantic==2.12.3",
        "cloudpickle==3.1.2"
    ]
)


After the deployment you might want to test it out

agent.query(input={"messages": [
    ("user", "What is the exchange rate from US dollars to SEK today?"),
]})


Troubleshooting

If you run into an error like this here - you might need to use a different region that
supports the model.

"404 Publisher Model
`projects/project-xxxxxxx/locations/asia-southeast1/publishers/
google/models/gemini-2.5-flash-lite` was not found or your project
does not have access to it"




Comments

Popular posts from this blog

vllm : Failed to infer device type

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

android studio kotlin source is null error