archive2024

Llama 3 8B with Ollama on my own machine

Meta released Llama 3 in 8B and 70B sizes. How to run the 8B model with Ollama, call it from code, and what hardware you really need.

3 min read#llm#ollama#local-llm

Meta released Llama 3 on 18 April 2024 in two sizes, 8B and 70B parameters, each as a base model and an instruction-tuned model. Both have an 8K token context window. Ollama added the models on the same day.

I had been using Llama 2 7B for small text jobs, like summarizing logs, and it was often not good enough. So I tried the new 8B model this week. These are the setup steps and my impressions so far.

Hardware

The default llama3 tag in Ollama is the 8B instruct model, quantized to 4 bits. The download is about 4.7 GB. Plan for roughly 6 to 8 GB of memory: a GPU with 8 GB VRAM, an Apple Silicon Mac with 16 GB, or a CPU with 16 GB RAM if you can accept slow answers. The 70B model needs 40 GB and more, which I do not have. These are rough numbers, and a longer context adds memory too.

Install and run

On Linux:

curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama3
ollama run llama3 "Explain MQTT retained messages in three sentences."

On macOS, install the app from ollama.com. The ollama command works the same way.

List the downloaded models:

ollama list

Call it from code

Ollama listens on http://127.0.0.1:11434. The native chat endpoint:

curl http://127.0.0.1:11434/api/chat -d '{
  "model": "llama3",
  "messages": [{"role": "user", "content": "Give me a cron line for every 15 minutes."}],
  "stream": false
}'

Ollama also has an OpenAI-compatible endpoint at /v1/chat/completions. This is useful because many libraries already speak that format:

# pip install openai
from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:11434/v1", api_key="ollama")  # key is ignored
resp = client.chat.completions.create(
    model="llama3",
    messages=[
        {"role": "system", "content": "Answer briefly."},
        {"role": "user", "content": "What does 'docker compose down -v' remove?"},
    ],
    temperature=0.2,
)
print(resp.choices[0].message.content)

With this, you can switch between a local model and a hosted API by changing base_url and model.

First impressions

It follows instructions much better than Llama 2 7B. For short summaries of logs and notes, rewriting text, simple classification, extraction into JSON, and explaining shell commands, it is good enough for me.

Long documents are a problem, because 8K tokens is not much. I split the input. It also invents version numbers and API names with full confidence, so I do not use it for facts I cannot check. I have not tried it on larger code changes.

A few settings that helped. For extraction and classification I use a temperature between 0 and 0.3, so the output is stable. Ollama supports "format": "json" in the request, but I still parse and validate the result on my side. By default Ollama unloads a model after a few minutes idle, and the next request is slow; the keep_alive parameter changes that.

One more thing: Llama 3 uses the Meta Llama 3 Community License, not an OSI open source license. For my own tools that is fine. I would read it again before putting it in a product.