Phi4 Multimodal Instruct - OpenAI endpoint and Docker Image for self-hosting
Python
40
4 commits
updated Mar 2, 2025
This project enables you to self-host an OpenAI-compatible chat completion API endpoint powered by the microsoft/Phi-4-multimodal-instruct model. Deployed via a FastAPI server within a Docker container, this solution supports multimodal inputs—text, images, and audio—and leverages NVIDIA GPU acceleration for efficient inference.
Tested on AWS on G6 instance
Before you begin, ensure the following are installed on your system:
Follow these steps to build and run the self-hosted endpoint.
git clone <repository-url>
cd <repository-directory>
The provided Dockerfile sets up the environment with CUDA, Python, PyTorch, and required libraries. Build the image with:
docker build -t phi4-server .
This command creates an image named phi4-server with:
flash-attn installed.server.py) copied into the container.Use the docker-compose.yml file to launch the service:
docker compose up -d
This command:
phi4-server container in detached mode (-d).8000 on the host to 8000 in the container.hf_cache) for caching model weights at /app/hf_cache.The API will be available at http://localhost:8000.
Interact with the endpoint using the OpenAI Python client or any HTTP client. Below is an example demonstrating a request with text and an image.
import openai
import base64
# Function to encode an image to base64
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Replace with the path to your image
image_path = "image.png"
image_base64 = encode_image(image_path)
# Initialize the OpenAI client
client = openai.OpenAI(
base_url="http://localhost:8000/v1",
api_key="test", # Use a real API key if authentication is enabled
)
# Send a multimodal request
response = client.chat.completions.create(
model="Phi4",
messages=[{
"role": "user",
"content": [{
"type": "text",
"text": "Describe this image."
}, {
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{image_base64}"
}
}],
}],
max_completion_tokens=512,
)
print(response.choices[0].message.content)
"type": "text" in the "content" list."type": "image_url"."type": "input_audio" or "type": "audio_url" for audio inputs.Here’s how the system operates behind the scenes.
nvidia/cuda:12.4.1-devel-ubuntu22.04, providing CUDA support.flash-attn (version 2.7.4.post1) for optimized attention mechanisms.HF_HOME=/app/hf_cache: Sets the cache directory for Hugging Face model weights.PYTHONUNBUFFERED=1: Ensures Python output is unbuffered for real-time logging.server.py script (assumed to contain the API logic) is launched with Uvicorn:
uvicorn server:app --host 0.0.0.0 --port 8000 --workers 4
/v1/chat/completions endpoint (assumed based on compatibility).microsoft/Phi-4-multimodal-instruct model from Hugging Face, configured to run on the GPU.flash-attn for efficient attention computation, reducing memory and compute overhead./app/hf_cache, mapped to the hf_cache Docker volume.docker-compose.yml file reserves GPU resources:
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
docker run --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi).hf_cache volume is populated. If not, ensure internet access and sufficient disk space."type" fields for inputs).Feel free to fork this repository, submit issues, or contribute pull requests to enhance the project!
This project is released under the MIT License.
Phi4 Multimodal Instruct - OpenAI endpoint and Docker Image for self-hosting
Python
40
4 commits
updated Mar 2, 2025
This project enables you to self-host an OpenAI-compatible chat completion API endpoint powered by the microsoft/Phi-4-multimodal-instruct model. Deployed via a FastAPI server within a Docker container, this solution supports multimodal inputs—text, images, and audio—and leverages NVIDIA GPU acceleration for efficient inference.
Tested on AWS on G6 instance
Before you begin, ensure the following are installed on your system:
Follow these steps to build and run the self-hosted endpoint.
git clone <repository-url>
cd <repository-directory>
The provided Dockerfile sets up the environment with CUDA, Python, PyTorch, and required libraries. Build the image with:
docker build -t phi4-server .
This command creates an image named phi4-server with:
flash-attn installed.server.py) copied into the container.Use the docker-compose.yml file to launch the service:
docker compose up -d
This command:
phi4-server container in detached mode (-d).8000 on the host to 8000 in the container.hf_cache) for caching model weights at /app/hf_cache.The API will be available at http://localhost:8000.
Interact with the endpoint using the OpenAI Python client or any HTTP client. Below is an example demonstrating a request with text and an image.
import openai
import base64
# Function to encode an image to base64
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Replace with the path to your image
image_path = "image.png"
image_base64 = encode_image(image_path)
# Initialize the OpenAI client
client = openai.OpenAI(
base_url="http://localhost:8000/v1",
api_key="test", # Use a real API key if authentication is enabled
)
# Send a multimodal request
response = client.chat.completions.create(
model="Phi4",
messages=[{
"role": "user",
"content": [{
"type": "text",
"text": "Describe this image."
}, {
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{image_base64}"
}
}],
}],
max_completion_tokens=512,
)
print(response.choices[0].message.content)
"type": "text" in the "content" list."type": "image_url"."type": "input_audio" or "type": "audio_url" for audio inputs.Here’s how the system operates behind the scenes.
nvidia/cuda:12.4.1-devel-ubuntu22.04, providing CUDA support.flash-attn (version 2.7.4.post1) for optimized attention mechanisms.HF_HOME=/app/hf_cache: Sets the cache directory for Hugging Face model weights.PYTHONUNBUFFERED=1: Ensures Python output is unbuffered for real-time logging.server.py script (assumed to contain the API logic) is launched with Uvicorn:
uvicorn server:app --host 0.0.0.0 --port 8000 --workers 4
/v1/chat/completions endpoint (assumed based on compatibility).microsoft/Phi-4-multimodal-instruct model from Hugging Face, configured to run on the GPU.flash-attn for efficient attention computation, reducing memory and compute overhead./app/hf_cache, mapped to the hf_cache Docker volume.docker-compose.yml file reserves GPU resources:
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
docker run --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi).hf_cache volume is populated. If not, ensure internet access and sufficient disk space."type" fields for inputs).Feel free to fork this repository, submit issues, or contribute pull requests to enhance the project!
This project is released under the MIT License.