How to Deploy Phi-4 on Alibaba Cloud PAI for Ad Copy
Nobody needs GPT-5-class reasoning to write "Save 20% this weekend only." If your marketing team is burning frontier-model spend on high-volume, low-complexity tasks like ad copy variants, product descriptions, and headline A/B tests, you're paying for capability you don't use. Microsoft's Phi-4 is a 14-billion-parameter, MIT-licensed model that's plenty sharp for short-form marketing text, and you can self-host it. This walks through deploying Phi-4 on Alibaba Cloud PAI-EAS, useful if your team is already on Alibaba Cloud for APAC data residency and doesn't want ad copy prompts leaving the region.
By the end you'll have a running Phi-4 endpoint behind PAI's Elastic Algorithm Service (EAS), callable with an OpenAI-compatible API, generating ad copy on demand.
1. Stage the Phi-4 weights in OSS
PAI-EAS reads model files from Object Storage Service (OSS), not from Hugging Face directly. Pull the weights locally, then push them to a bucket in the same region as your PAI workspace (pick an APAC region like ap-southeast-1 if data residency is the point of this exercise).
# download the weights
pip install -U "huggingface_hub[cli]"
huggingface-cli download microsoft/phi-4 --local-dir ./phi-4-weights
# push them to OSS (configure ossutil with your AccessKey first: ossutil config)
ossutil cp -r ./phi-4-weights oss://<your-bucket>/models/phi-4/
Phi-4 is a dense decoder-only transformer with a 16K-token context window, which is more than enough for ad copy prompts plus a product brief and a few examples.
2. Pick a serving image with vLLM
PAI-EAS's console ships prebuilt vLLM images you can select at deploy time (for example, tags like vllm:0.11.2-mows0.5.1 show up in the image picker). vLLM serves Phi-4 out of the box since it uses the Phi3ForCausalLM architecture that vLLM already supports, so you don't need a custom Dockerfile unless you want one.
3. Deploy Phi-4 as a custom PAI-EAS service
In the PAI console, go to EAS > Deploy Service > Custom Deployment and choose Image-based Deployment. Configure:
- Image: the vLLM image from step 2 (or your own, pushed to Container Registry)
- Mount storage: OSS URI
oss://<your-bucket>/models/phi-4/, mount path/mnt/data - Command: the vLLM startup command pointing at the mounted weights
- Resource: a GPU instance type with enough VRAM. Phi-4 at bf16 needs roughly 30 GB, so pick an A100-class instance (Alibaba's
gn7efamily); if you want to squeeze it onto a single 24 GBgn7icard, deploy an AWQ or GPTQ quantized build instead.
vllm serve /mnt/data/phi-4 \
--port 8000 \
--served-model-name phi-4-ads \
--max-model-len 16384
Deployment takes a few minutes to reach "Running" status. Grab the service's endpoint URL and token from the service details page.
4. Test the endpoint with a marketing prompt
EAS exposes an OpenAI-compatible /v1/chat/completions route through vLLM, so you can hit it directly.
curl http://<your-service-id>.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/<service-name>/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: <your-eas-token>" \
-X POST \
-d '{
"model": "phi-4-ads",
"messages": [{"role": "user", "content": "Write 3 short Google Ads headlines (max 30 chars each) for a waterproof hiking backpack, urgent tone, no exclamation marks"}],
"max_tokens": 200
}'
If you get three tight headlines back, the deployment works.
5. Wire it into a batch ad-copy script
For real use you'll loop this over a product catalog, not call curl by hand.
import csv
from openai import OpenAI
client = OpenAI(
base_url="http://<your-service-id>.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/<service-name>/v1",
api_key="<your-eas-token>",
)
with open("products.csv") as f:
for row in csv.DictReader(f):
resp = client.chat.completions.create(
model="phi-4-ads",
messages=[{
"role": "user",
"content": f"Write 3 ad headlines (max 30 chars) for {row['name']}: {row['description']}"
}],
max_tokens=150,
temperature=0.8,
)
print(row["name"], "->", resp.choices[0].message.content)
Because the OpenAI Python SDK just needs a compatible base URL, this drops into any pipeline you'd otherwise point at a hosted model.
Gotchas
- Region matters for residency. Keep the OSS bucket and the EAS service in the same APAC region as your workspace, or you've defeated the point of self-hosting.
- GPU quota isn't instant. Request the GPU instance family you need before your deployment deadline; approval isn't always same-day.
- VRAM math bites. A 14B model at full precision needs around 30 GB. Undersize the GPU and the service will crash on load, not just run slow.
- EAS bills per instance-hour, running or idle. Scale the instance count down with
eascmd modify <service_name> -Dmetadata.instance=0(or through the console) between batch jobs if traffic is bursty. - 16K context fills up fast if you're stuffing in a full brand style guide plus a product feed. Keep prompts tight or summarize the brief first.
A self-hosted Phi-4 endpoint won't write your brand strategy, but it'll turn out hundreds of ad copy variants for a fraction of what a frontier model API costs per token, and it keeps the prompts and outputs inside the region your compliance team already signed off on.