Serve LLMs Locally in Python: vLLM with an OpenAI-Compatible API

Professor Py: AI Engineering · Intermediate ·🧠 Large Language Models ·4mo ago

Key Takeaways

This video demonstrates how to serve LLMs locally in Python using vLLM with an OpenAI-compatible API, allowing for predictable latency, cost control, and privacy for chat workloads. The video covers building a local chat service with a simple endpoint, efficient batching, token streaming, and request timeouts using the OpenAI Python client and FastAPI.

Full Transcript

run your own LLM API without breaking your app. This is the pattern you'll be able to implement by the end, which starts a local chat service and serves a batched streaming request via a simple endpoint. I'm Professor Pi, teaching AI engineering and LLM systems with simple Python. Serving local models with VLLM solves a practical problem. You want predictable latency, cost control, and privacy. You also want to avoid remote API limits when you deploy an app that needs many short chats. Success means a local endpoint that accepts requests, batches work efficiently, streams tokens back, and refuses dangerous or slow queries. VLM gives strong batching and streaming performance for models on your machines. And the OpenAI compatible interface makes integration approachable at a high level. VLM acts like a local LLM server. You talk to it using familiar client calls. It groups similar requests, runs them on GPU efficiently, and can stream partial outputs. The strengths are lower per request cost and better throughput on multi-GPU rigs. A weakness is that you must manage the server and monitor GPU memory. We will build a small local chat service. It will make a single test call, expose a post endpoint, show batching under load, stream tokens incrementally, and add a guard that times out long requests. That gives a realistic pipeline you can drop into a prototype and scale later. Now, we will warm up with a single chat call to the local model. This snippet sends a simple chat completion request to a locally running VLM server using the OpenAI Python client. It first configures the client to point at VLLM's OpenAI compatible base URL storing a reusable model ID for clarity. The variable msg holds a compact user prompt. The callclient.hat.comp completions.create Create executes the request with temperature set to zero and a fixed C. So responses are repeatable. The response object includes usage details and the code extracts total tokens to quantify compute consumed. Reducing max tokens caps response length and often lowers latency and cost. The printed token count confirms the end-to-end call and lets you gauge per request cost. Next, we'll expose that capability as a tiny web API so other services can call your local model. This example creates a minimal fast API endpoint that forwards requests to the VLM OpenAI compatible server. The app variable holds the fast API instance. The function chat takes a query string Q then calls client.hat.comp completions.create create with model ID and a user message. Returning the models text in a simple JSON shape using app.add_appi route registers the handler at / chat as a post route. The final expression counts matching routes to verify the endpoint was added. Increasing max tokens allows longer replies but may raise latency and cost. The printed route count confirms your API surface is live and correctly wired to VLLM. Now we will simulate multiple users to show how VLLM batches requests under load. This code fires three concurrent chat requests so VLM can batch them efficiently under load. The list prompts hold short readable questions. A thread pool executor schedules simultaneous calls to client.comp completions.create each using the same model ID temperature zero and a fixed seed for deterministic outputs. Once the future's resolved, the code measures each reply length by taking the content from choices and computing its character count. The variable best captures the largest reply. A quick comparison that reflects different prompt scopes. Increasing max workers ops concurrency and can improve throughput on multi-GPU servers. The printed maximum length highlights which prompt yielded the most verbose answer. Next, we will switch to streaming so responses arrive token by token. This snippet demonstrates token streaming from VLLLM using the OpenAI client stream equals true chat completion. It requests a short deterministic response. So the incremental chunks are predictable. The variable stream yields chat completion chunk events each carrying a delta with a tokensized piece of content. The loop checks that choices exist and that delta.content content is present before incrementing the chunk counter. Using temperature zero with a seed ensures the same chunking across runs, which simplifies testing and monitoring. Reducing max tokens limits total emitted chunks and speeds responses. The printed chunk count verifies that streaming worked and shows how many increments were delivered. Finally, we'll harden the endpoint with simple validation and a timeout to keep the API responsive. This snippet adds a defensive fast API route that validates inputs and enforces a request timeout to VLM. The set band lists disallowed terms and the guarded function rejects any query containing them, returning allowed false. If the query is acceptable, clientwith options timeout equals 5.0 creates a short-lived client configuration that limits how long the chat.comp completions.create call can run. This pattern keeps your API responsive under slow or overloaded backends. Lowering max tokens further constrains cost and latency while tightening timeout cuts longtail stalls. The printed boolean confirms the request passed the guard and would proceed to VLLLM safely under a timeout. Mapping this toy flow to a real app is straightforward. The same endpoint can sit behind an authentication layer. The streaming handler can feed a websocket or server sent events and the guard plus timeout pattern protects user experience while your GPU cluster stes. Recap. It is a local chat API wired to VLLM through an OpenAI compatible client. Use it when you need lower latency, batching, and token streaming from onrem models. Caveat, you must operate the server and monitor GPU resources. Next step, tune max tokens and the client timeout while measuring end-to-end latency and token costs to find the best trade-off for your users. If short practical AI engineering helps, subscribe and watch the AI engineering

Original Description

Run your own LLM API without breaking your app — build a local vLLM server to gain predictable latency, cost control, and privacy for chat workloads. Follow a practical pipeline: single test call, FastAPI POST endpoint, efficient batching, token streaming, and request timeouts using the OpenAI Python client. Subscribe for short, practical AI engineering and LLM systems tutorials. #vLLM #FastAPI #OpenAI #LLM #Python #AIEngineering #MLOps
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to build a local chat service using vLLM and OpenAI-compatible API, covering key concepts such as batching, token streaming, and request timeouts. By following this tutorial, viewers can create a scalable and efficient LLM server for their chat workloads.

Key Takeaways
  1. Configure the OpenAI Python client to point to the vLLM server
  2. Create a FastAPI endpoint to forward requests to the vLLM server
  3. Simulate multiple users to demonstrate batching under load
  4. Implement token streaming to receive incremental responses
  5. Add input validation and request timeouts to harden the endpoint
💡 Using a local LLM server with vLLM and OpenAI-compatible API can provide predictable latency, cost control, and privacy for chat workloads, while also allowing for efficient batching and token streaming.

Related Reads

Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →