Instructions to use meta-llama/Meta-Llama-3-8B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use meta-llama/Meta-Llama-3-8B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use meta-llama/Meta-Llama-3-8B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "meta-llama/Meta-Llama-3-8B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "meta-llama/Meta-Llama-3-8B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/meta-llama/Meta-Llama-3-8B-Instruct
- SGLang
How to use meta-llama/Meta-Llama-3-8B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "meta-llama/Meta-Llama-3-8B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "meta-llama/Meta-Llama-3-8B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "meta-llama/Meta-Llama-3-8B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "meta-llama/Meta-Llama-3-8B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use meta-llama/Meta-Llama-3-8B-Instruct with Docker Model Runner:
docker model run hf.co/meta-llama/Meta-Llama-3-8B-Instruct
My chatbot stopped working
I use this code at Hugging Faces Spaces, i have Gated Repos Accepted to Meta Llama 3:
import gradio as gr
from huggingface_hub import InferenceClient
Cliente de inferência com modelo de IA pública
client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct") # Modelo gratuito e avançado
Função para processar a conversa
def responder(mensagem, historico):
mensagens = []
if historico is None:
historico = []
for item in historico:
if isinstance(item, list) and len(item) == 2:
user_msg, bot_msg = item
mensagens.append({"role": "user", "content": user_msg})
if bot_msg:
mensagens.append({"role": "assistant", "content": bot_msg})
mensagens.append({"role": "user", "content": mensagem})
resposta = ""
try:
for mensagem in client.chat_completion(
mensagens,
max_tokens=250,
stream=True,
temperature=0.2,
top_p=0.7,
):
if not mensagem or not isinstance(mensagem, dict):
continue
try:
conteudo = mensagem["choices"][0]["delta"].get("content", "")
if conteudo.strip():
resposta += conteudo
yield resposta
except (AttributeError, IndexError, KeyError) as e:
print(f"Erro ao processar mensagem: {e}")
continue
except Exception as e:
print(f"Erro inesperado: {e}")
yield "Ocorreu um erro ao gerar a resposta."
if not resposta.strip():
yield "Nenhuma resposta gerada. Tente novamente."
Interface do chat com labels em português
demo = gr.ChatInterface(
responder,
title="Benjamin – Assistente Virtual da CEaD - IBC. Tire suas dúvidas com a minha inteligência artificial! Minha base de conhecimento vai até 2021, por isso posso não trazer respostas precisas sobre pessoas, conceitos, lugares ou fatos históricos. Se isso acontecer, entre em contato com o mediador do curso para obter ajuda mais adequada.",
textbox=gr.Textbox(label="Campo de mensagem: Digite uma mensagem e depois tecle Enter para enviar"),
type="messages"
)
if name == "main":
demo.launch()
My requirements.txt is:
gradio>=3.41.0
huggingface_hub>=0.17.0