Introduction to vLLM
Today we're going to learn how to easily deploy LLMs on servers using VLM. VLM is an easy-to-use and fast Python library that allows us to do LLM inference and serving. We will take a look at how we can use it to easily chat with models locally, how we can deploy any hugging face model with a single command, and even how we can serve our own custom fine-tuned models. So, let us get right into it.
Setting Up the Environment
All right. So, we're going to take a look at VLM in this video today. In particular, we're going to look at three basic use cases. The first one being just using any hugging face model in code with VLM in a simple way. The second one being serving a model locally and using it like an open AI API in the same sort of way. So, with the same interface, with the same API. Uh and the third example is doing the exact same thing but with a custom fine-tuned model. So we get a gguf file with the tokenizer and everything. Uh which could be the result of one of the previous videos I did on fine-tuning models with unsloth. So we take a model like this and we serve it with VLM in a very simple way with a single command. And this can also be run on a server and then used publicly as an API and we can even use an API key for that. So this is a very powerful and easy to use tool.
Let us get right into it. I'm going to go to my tutorial directory here as always and I'm going to initialize an environment. Now, you don't have to work with environments here. You don't have to use UV, which is what I'm going to use. You can just go and say pip or pip 3 install vllm. If you just want to use your basic Python installation, if you want to use a basic virtual environment, you can say python or python 3-m ven.v or in my case, I'm using uv. Uh you can check out my video on UV if you're interested in that. But in my case, I just say uv init and then uv instead of pip install. So uv vlm. But the idea is the same. I have a virtual environment with vlm installed in it.
Local Inference with a Hugging Face Model
You can see that this is the case. If I go ahead and activate this virtual environment. I open up a python shell. I can say import vlm and this works. And this should also work in your case. If it doesn't work, there's a problem with your environment. But we're not going to focus on that now. So let us get started with a very minimal example of using a model from hugging face in code with VLM in a very simple way. So you don't need to use the transformers package. You don't need to set up any tokenizer. You don't need to do something like from pre-trained. You can literally just take any model. For example here this tiny llama 1.1 billion parameter chat model. So a very simple small model. What you do is you copy the handle so to say. You copy this part here. And all you have to do then is you have to open up a Python file. I'm going to call this main.py. I'm going to remove all the code that is here already. And now I'm going to say from vlm import lm and sampling parameters. So sampling parameters basically means how we sample the tokens. Here we can provide the maximum number of tokens, the temperature and so on.
And what I'm going to do here now is I'm just going to say lm is equal to llm and then the handle. So, Tiny Llama, Tiny Llama 1.1B, Chad V 1.0. You can do that with any model provided that you have the necessary hardware to run this on your system. And if you have, for example, limited VRAM, what you can also do is you can say GPU memory utilization and you can specify a percentage. So, 0.7 to only use 70% of the available VRAM. That is to prevent a out of memory exception or error. And by doing that you basically have your LLM. Then you just have to provide the parameters for sampling. So sampling parameters in my case here max tokens is equal to 128. And then I'm going to say also temperature is equal to 0.7.
And now the only thing that we need to do with VLM to get a response is we need to say outputs is equal to llm.generate. And then here as a list the prompt for example what is special about Python programming language question mark and then I also add here my sampling parameters. So params as a positional argument. And the only thing I need to do now is I need to say outputs dot or actually outputs zero do.outputs zero.ext text.cript. Strip is optional. But that is the only thing the only code that we need to get a response from this lm.
Of course, this is going to be executed locally. So, you need to have the system resources, the hardware that is required to run this. But then you can just go ahead and say Python or Python 3 main.py. Now, chances are this is going to break now since I'm recording. If that's the case, I will have to briefly stop recording, run it, show you the output, and yeah, basically continue recording because my OBS, if we take a look at the Nvidia SMI output in my system, actually, it it is kind of fine. Recently, it had something like six GB allocated. So, this should actually even work. But now you can see the VRAM usage is up. Okay, but I still get the out of memory problem. So, let me just stop the recording and see if this helps. All right. So now I had enough VRAMm because I wasn't recording. And you can see I get here seat input explain how Python interpreter works and how it handles strings, numbers and functions. Describe the main data structures and so on. So it doesn't have to be a reasonable output but we do get an output. I think the model is not the most intelligent model. But basically this is the output. This is how we can talk to a model easily with this library.
Serving a Model with an OpenAI-Compatible API
Now this of course just uses a model in code. What if we want to actually serve a model? What if we actually want to deploy a model from hugging face on our server? So, it's running and has the open AAI interface that we can just interact with even using the OpenAI package. In this case, we can just use the simple VLM command. So, I can close this here. Probably I'm going to have to stop recording for this to work again. But essentially, what you do is you say V LLM serve and then you choose the model. So, in our case again, Tiny Llama Tiny Llama 1.1B chat v 1.0 zero then you can still provide the tag GPU_memory utilization is equal or actually not it's equal to uh space then the value for example 0.7 and then you can also provide an API key if you want to protect this if you don't want to answer any response but only the responses that have the correct API key you can say d- API_key and then something like neural 9 key for example model. This will run the model and host it like an OpenAI model. So we can write a Python script that interacts with this hosted model by using the OpenAI library. So let us run this. Let's see if my system breaks. Probably it will. Okay, actually it seems to work. So I'm recording and it's still hosting this uh model.
Interacting with the Served Model
So let us go again to the tutorial directory. Here we're going to again activate the virtual environment. in my case source VN bin activate and then now we're going to also add an additional package. So we're going to install the open AI package which means in your case pip or pip 3 install open AAI if you're using the basic Python installation. In my case here it's going to be UV add uh open AI and then we're going to open up a actually let me just reuse the main py file. I'm going to remove the code from here. I'm going to turn this into a full screen. And now all we need to do here in order to interact with our model that we're hosting with VLM is we need to say from open AAI import open AAI we can initialize the client here by saying open AI and pass an API key which in our case is neural 9 key. Now since we're not using the actual open AAI API we also need to provide a base URL. So I'm going to say here base URL is equal to and in here I'm going to say http col//lohost port what is it port 8000. So we're going to say here http localhost 8000. And now we can use this client like when we're talking to chatgbt. So we can just say here response is equal to client chat completions.create create and we can pass a simple message history.
So we can decide the model that we want to use here is our tiny llama tiny llama 1.1b and then we just say messages is equal to a list of dictionaries and these dictionaries are individual uh messages. First of all the system prompt. So ro system is going to say I don't know let's say content you are a helpful assistant very generic I know and then the user question here will be something like summarize the idea of docker in simple words and then to get the answer we print response dot choice choices zero dot message.content and of course close the brackets. So that's it. Let's run this and let's see what happens here with our running model. Okay, seems like we forgot something here. Of course, it's not localhost 8000, it's localhost 8000/v1. This is important. And now when I run this, we should get an answer. There you go. Docker is a containerization platform that enables developers to create and deploy applications and so on and so forth. So you can see I'm hosting this with VLM and I can access it with Python. Of course, if I host this on an actual server uh on the internet, I can use this as my API if I have some hugging face model that I want to use. I can just deploy it so to say with VLM and use it with a Python script by pinging this endpoint.
Serving a Custom Fine-Tuned GGUF Model
However, the interesting thing is not just hosting hugging face models, but hosting your own custom fine-tuned models. So, I'm not going to show you how to fine-tune models in this video. I have videos where I showed you how to do that on the one hand with Laura directly in Python. On the other hand, also with Unsloth. So, in this case, I'm going to use a model that I fine-tuned with Unsloth. Very, very simple. It's uh similar to the example that we had in the video, just a different model that is a little bit smaller. So I have here this directory gguf model scratch smaller. Uh the naming might be a bit confusing to you. It's not really important. It's just ggf model scratch because I did it from scratch typing the code again and uh smaller because it's a smaller model. It's not really important. If we go into this directory however you will see that there are a couple of files that are relevant. First of all the model that I want to use the gguf file of the quantized 4bit model here. So that is the thing that we actually want to use. However, we also need to use the proper tokenizer uh which is here as a JSON file and we also have the tokenizer config and so on. We also have a chat template. So, all of this is important.
So, we need to make sure that all of this is exported properly. Again, refer to my unslopped video if you're interested in how to fine-tune models. But let's say we have a directory like this one ready to use. We can also deploy such a server uh such a model, such a fine-tuned model with VLM. And how do we do that? quite easily vlm surf as before. Now we point to the gguf file. So in our case here gguf model scratch smaller unsloth and then q4k km gguf. Now in addition to that we need to provide the tokenizer. So I have to say here d- tokenizer and just the directory itself. We don't have to provide a specific file. Then also d-serfed model name. So surf dash model dash name. This is just the name of the model when we select it in our code. So remember we have in our code here we're going to use basically the same code. We have the model that we choose. So the name here is going to be the name that we provide here. Let's say tuned model or tuned tutorial model something like this. Then one thing that I forgot is the chat template. So dash chat dash template here we need to direct it to the ginger file. So gguf model scratch smaller chat template.jinger Ginger we can still provide here the GPU memory is it with underscore or with actually it's with underscore GPU memory utilization let's say 0.7 again and then finally the API key again so API_key neural 9 key and that could be deploying this on a server so I can just run this now this is going to take the file it's going to deploy the model.
Querying the Custom-Tuned Model
And while this is happening I can change my code to work with this new system. The only thing I actually have to do is I have to change the model. Nothing else. The endpoint is the same. I can of course also change the messages, which would make sense in this case because the model is fine-tuned uh to give a specific format based on information about a person. So here we're going to say now tuned tutorial model. And I'm going to remove the system message. And I'm just going to provide a user message that says Mike is a 30year-old programmer. He loves hiking.
Remember the video for onslaught fine-tuning? We did the example where we had a data set where person data is provided and then we get a dictionary as a response. Now, I'm not sure if we're going to get a perfect response here, but it's more like a proof of concept that we can host our own models even if the models are not perfect. That's not the focus here. The focus is we can host a fine-tuned model. All right, so it's running. Let's see if we can get a response. I'm going to run main.py. And you can see I get gender male, job nothing, name Mike and H30. Now programmer would have been nice for job. We can try again. Maybe we get a different output. Now it seems like we get the same thing all the time. But you can see I have a fine-tuned model. It returns a specific format and I can host this easily with VLM on any server and just use it with the OpenAI package in Python.
Conclusion
So that's it for today's video. I hope you enjoyed it and hope you learned something. If so, let me know by hitting a like button and leaving a comment in the comment section down below. And of course, don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free. Other than that, thank you much for watching. See you in the next video and bye.