
The Hugging Face text generation inference is a production-ready Docker container that allows you to deploy and interact with Large Language Models (LLMs). For instance, you can use this container to run an API that exposes Llama 2 models programmatically.
Llama 2 is a collection of fine-tuned text models that you can use for natural language processing tasks.
In this guide, you are to implement a Hugging Face text generation Inference API on Vultr Cloud GPU.
Before you begin:
Deploy a new Ubuntu 22.04 A100 Vultr Cloud GPU Server with at least:
Create a non-root user with sudo rights and switch to the account.
Install JupyterLab.
After setting up a Docker container in this guide, you'll code some Python scripts to use the text generation inference API. These Python scripts rely on some Python libraries. Run the commands below to install the libraries.
Hugging Face allows you to download and run a Docker container with all the necessary settings to access LLMs. Follow the steps below to download and run the Docker container:
Initialize the following variables. Replace $HF_TOKEN with your Hugging Face user access token. The meta-llama/Llama-2-7b-chat-hf is the model you will use to generate text.
Run the following command to download and run the Hugging Face container.
Wait for the command above to finish, and then check the Docker logs to ensure the container listens for incoming requests.
The following output shows that the API is listening to all IP addresses.
Click Python 3 ipykernel under Notebook. This opens a new notebook where you can type and run the Python codes in the following sections.
Every LLM understands a specific template, format, or prompt to ensure optimal output. Designing the correct format is called prompt engineering. While most LLMs are very flexible in handling such prompts, you may need some experimentation before perfecting crafting better prompts.
The Hugging Face LLM uses the following prompt template:
In the following sections, you'll programmatically use the above template using some Python code to prompt the LLM.
In this step, you'll query the LLM using a default prompt to answer a query.
Run the following code on your Jupyter Notebook.
You should see the following output:
In a zero-shot prompt, you should give the model a prompt that describes how you want it to respond.
Execute the following Python code to ask the model to describe the trend of electronic sales.
Output:
Change the user_prompt to the following text and review the response.
Output:
In this prompt, the model requires a sample input text and the corresponding out to generate other samples.
Run the following Python code to ask the model to translate an English text to French.
Output:
In this strategy, the model accepts a few examples of the tasks that you want it to do.
Run the following code to prompt the model to complete proverbs.
Output:
When dealing with LLMs, tokens are numerical representations of words or characters. Tokens form the basic unit an LLM uses to process and generate output.
For instance, the Llama 2 model can accept a maximum of 4096 tokens.
Run the following Python code to understand how LLMs generate and handle tokens.
Output:
When running the Hugging Face text generation inference, you can change some model's parameters. For instance, to tweak the model parameters when running the text generation inference container, follow the steps below:
Initialize the container's variables.
Run the Docker container. The max-input-length parameter declares the maximum number of tokens the LLM can accept. The max-total-tokens parameter defines the total tokens the LLM generates, including the input token.
Wait for the docker container to load, then run the following Python code on your Jupyter notebook.
Review the following output and note how your model behaves after tweaking the different API parameters.
In the previous code, the chat_completion function accepts the following parameters:
length: Represents the max_new_tokens parameter for the model.temperature: Defines how creative the output will be for the model. The higher the temperature, the more the model randomizes the output. The default value is none.repetition_penalty: Instructs the model on how to deal with repetitive sequences.top_p: Limits the LLMs choices and prevents nonsensical outputs and unwanted diversity.top_k: Makes the model more focused and consistent.In this guide, you've implemented a text generation Inference API on Vultr Cloud GPU. You've run a Hugging Face Docker container that exposes an API that generates content based on some prompts. Towards the end, you've learned how to tweak the model parameters to improve output.
0 Comments
Be the first to comment and share your perspective with the community.