This article walks you through how to create and expose an AI-powered API using Google Cloud’s Vertex AI and Apigee. You’ll build a simple backend in Python that leverages Vertex AI’s PaLM model, then proxy that backend using Apigee to manage, secure, and monitor the API.
Ideal for developers looking to embed generative AI into real-world systems without reinventing the wheel.
A GCP project with Vertex AI, Cloud Functions, and Apigee enabled
IAM permissions for Vertex AI and Cloud Functions
Basic Python knowledge
gcloud CLI installed and authenticated
We’ll use a Cloud Function to connect to the PaLM API and return a generated response.
Install Vertex AI SDK:
pip install google-cloud-aiplatform
Python Code (main.py):
from flask import jsonify
from flask import Request
from vertexai.language_models import TextGenerationModel
import vertexai
def palm_generate(request: Request):
request_json = request.get_json()
prompt = request_json.get("prompt", "")
vertexai.init(project="your-project-id", location="us-central1")
model = TextGenerationModel.from_pretrained("text-bison")
response = model.predict(prompt, max_output_tokens=256)
return jsonify({"completion": response.text})
Reminder: replace your-project-id and us-central1 with your Google Cloud Project ID as well as the targeted Google Cloud Region identifier.
requirements.txt:
Flask==2.2.2
google-cloud-aiplatform
Deploy Cloud Function:
gcloud functions deploy palm-generate \
--runtime=python310 \
--trigger-http \
--allow-unauthenticated \
--entry-point=palm_generate \
--region=us-central1
Use curl or Postman:
curl -X POST https://REGION-PROJECT.cloudfunctions.net/palm-generate \
-H "Content-Type: application/json" \
-d '{"prompt": "What are three key takeaways from the Agile manifesto?"}'
You should see a text response from PaLM!
Log in to Apigee UI, go to Develop > API Proxies, and click +Proxy.
Proxy Type: Reverse Proxy
Name: ai-text-generator
Existing backend: URL of your Cloud Function
Base Path: /ai-text-generator
Click Next, then Add CORS Policy and enable OAuth2 if needed.
Under Policies > PreFlow, insert:
<VerifyAPIKey name="Verify-API-Key"/>
And attach your key validation. Reminder: requires creating the API key.
Choose your environment (test/prod), click Deploy, and grab your Apigee endpoint URL.
curl -X POST https://your-apigee-endpoint/ai-text-generator \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR-API-KEY" \
-d '{"prompt": "Summarize the benefits of serverless architecture"}'
You now have a fully managed, secured, AI-powered API.
In Apigee:
Spike Arrest Policy:
<SpikeArrest name="RateLimit">
<Rate>10ps</Rate>
</SpikeArrest>
Monitoring: Use Apigee’s analytics dashboard to view call volume, latency, and errors.
Client → Apigee Proxy → Cloud Function → Vertex AI → Response
Congratulations! You’ve now built your first AI-powered API using Vertex AI and Apigee. From here, you can expand to:
Multi-agent orchestration with additional endpoints
Logging and tracing with Cloud Trace
User access control with Apigee OAuth2