Proposal: Add FastAPI + Ollama (Local AI/LLM Inference) Compose sample - #824
VimalN2005 wants to merge 2 commits into
Conversation
Signed-off-by: Vimal Sahani <vimalsahani2005@gmail.com>
stewartmbofana
left a comment
There was a problem hiding this comment.
Thanks for contributing this sample! Having an Ollama + FastAPI sample in awesome-compose is a great addition for developers looking to run local LLM inference with Docker Compose.
Before this can be merged, please address the following changes:
1. Remove UTF-8 BOM (Byte Order Mark) from files
Several files have been saved with a UTF-8 BOM (\xef\xbb\xbf):
fastapi-ollama/Dockerfilefastapi-ollama/compose.yamlfastapi-ollama/requirements.txtfastapi-ollama/app/main.pyfastapi-ollama/README.md
In particular, the BOM at the beginning of fastapi-ollama/Dockerfile causes Docker BuildKit to fail to recognize the # syntax=docker/dockerfile:1.4 directive, because parser directives must strictly start at byte 0. Please re-save all files as standard UTF-8 (without BOM).
2. Remove --no-cache-dir when using pip cache mount in Dockerfile
In fastapi-ollama/Dockerfile:
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --no-cache-dir -r requirements.txtUsing --mount=type=cache,target=/root/.cache/pip alongside pip install --no-cache-dir is contradictory because --no-cache-dir instructs pip to bypass the cache directory entirely. Please remove --no-cache-dir so pip can utilize the cache mount:
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt3. Streaming error handling & client lifecycle in app/main.py
- Streaming exception handling: In
/generateand/chat, thetry...except httpx.RequestErrorblock does not catch connection or network failures during streaming becausestream_generator()is consumed asynchronously by Starlette after the route returns. - Status code check on stream: If Ollama returns a non-200 status code (e.g. 404 when a model has not yet been pulled),
client.streamdoes not raise an exception, and the error response body is streamed under an HTTP 200 response. Consider verifyingresponse.status_code == 200before yielding chunks. - Client lifecycle: Rather than creating a new
httpx.AsyncClientinside each request, consider managing a shared client via FastAPI'slifespancontext manager (@asynccontextmanager async def lifespan(app: FastAPI): ...) to enable HTTP connection pooling and proper cleanup.
4. GPU Acceleration Note
Most users running Ollama in Docker will be interested in GPU pass-through. It would be very helpful to add a note or commented-out configuration in compose.yaml and fastapi-ollama/README.md showing how to enable Nvidia GPU support (e.g., via deploy.resources.reservations.devices).
5. PR Title & Description
Please update the PR title and description to reflect that this is an implemented Pull Request ready for review rather than an issue proposal.
…eaming lifecycle, add GPU note) Signed-off-by: Vimal Sahani <vimalsahani2005@gmail.com>
|
Hi @stewartmbofana, thank you for the helpful review! I have addressed all the requested changes in the latest commit:
Please let me know if any further adjustments are needed! |
|
Hi @stewartmbofana, friendly ping! All requested changes (BOM removal, streaming lifecycle, GPU notes) were pushed and CI is clean. Could you please take a re-look when you have a moment? Thank you! |
Description
This Pull Request adds a new application sample: FastAPI + Ollama (Local AI/LLM Inference) under
fastapi-ollama/.Architecture & Features
ollama_data:/root/.ollama) and optional NVIDIA GPU acceleration config./generate(streaming & non-streaming),/chat, and/modelsendpoints.README.mdcovering deployment, pulling models (llama3.2:1b), testing endpoints, and enabling GPU acceleration.Signed-off-by: Vimal Sahani vimalsahani2005@gmail.com