Run Llama 3.1 locally using LangChain
Table of Contents
Introduction
This tutorial will guide you through the process of running Meta's Llama 3.1 locally using LangChain. The Llama 3.1 model comes in various sizes (405B, 70B, and 8B) and offers impressive capabilities such as multilingual support, complex reasoning, and code generation. By following this guide, you'll learn how to set up and utilize this powerful open-source AI model on your local machine.
Step 1: Set Up Your Environment
Before running Llama 3.1, ensure your environment is ready. Follow these steps:
-
Install Required Software
- Ensure Python is installed (preferably Python 3.8 or later).
- Install virtual environment tools (optional but recommended).
- Install necessary libraries:
pip install langchain transformers torch
-
Clone the GitHub Repository
- Open your terminal and run:
git clone https://github.com/AarohiSingla/Generative_AI.git cd Generative_AI/L-6
- Open your terminal and run:
-
Set Up a Virtual Environment (Optional)
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
- Create a virtual environment:
Step 2: Download the Llama 3.1 Model
To use the Llama 3.1 model, you need to download it. Here's how:
-
Choose the Model Version
- Decide which version of Llama 3.1 you want to download (405B, 70B, or 8B).
- The 405B model is the most powerful but requires substantial system resources.
-
Download the Model
- Use the following command to download the model weights:
python -m transformers-cli download meta/llama-3.1-<model_size>
- Replace
<model_size>
with405B
,70B
, or8B
as per your choice.
- Use the following command to download the model weights:
Step 3: Configure LangChain
Now that you have the model, configure LangChain to work with it.
-
Import Necessary Libraries
- In your Python script, start by importing the required modules:
from langchain.llms import Llama
- In your Python script, start by importing the required modules:
-
Initialize the Model
- Load the model in your script:
model = Llama(model_name="llama-3.1-<model_size>")
- Load the model in your script:
Step 4: Run Your First Query
With everything set up, you can now run queries against the Llama model.
-
Create a Query Function
- Write a simple function to send queries to the model:
def query_llama(prompt): response = model(prompt) return response
- Write a simple function to send queries to the model:
-
Test the Model
- Call the function with a sample prompt:
prompt = "What are the benefits of using Llama 3.1?" print(query_llama(prompt))
- Call the function with a sample prompt:
Conclusion
You have successfully set up and run Llama 3.1 locally using LangChain. Key points to remember include:
- Ensure your environment is properly configured.
- Choose the model size based on your system's capabilities.
- Use the provided code snippets to streamline your interaction with the model.
Next steps could involve experimenting with different prompts or integrating the model into larger applications. Enjoy exploring the capabilities of Llama 3.1!