An LLM lab: a free local model, embeddings and a test harness

Run a small language model on your own computer with Ollama, call it from Python, add embeddings for search, and set up a tiny test harness so you can repeat the course's assistant against a real model without paying or sending data anywhere.

  • Time: 45 to 60 minutes
  • Cost: free software, your own computer

What you need

The labs in this course use no real model, so that you need no key and no account. A real model is still the best way to see the course’s ideas working. You can run one on your own computer, for free, and nothing you type leaves it.

You need:

  • The Python environment from the data lab guide.
  • Ollama, a free program that downloads and runs open language models locally. Other local runners exist; choose one from a source you trust.
  • A small model. Small models run on ordinary laptops. Quality, size and licences change often, so check the model’s page for its licence and requirements before you download it.

A computer with a graphics card runs models much faster, but a small model works on a processor alone, only more slowly.

Set it up

  1. Install Ollama from its official website and start it. On most systems it runs a small local service.
  2. In a terminal, download a small model. Pick one that fits your memory, for example a model listed as 1 to 4 billion parameters:
ollama pull <model-name>
ollama run <model-name>

Type a question, check you get an answer, and exit.

  1. In your project folder switch on the virtual environment and install a small HTTP library:
pip install requests
  1. Call the local model from Python. Ollama listens on your own computer, and its address and fields are described in its documentation:
import requests
r = requests.post("http://localhost:11434/api/generate",
                  json={"model": "<model-name>", "prompt": "Say hello in one short sentence.", "stream": False})
print(r.json()["response"])

Check that it works

Reproduce each course case with a real model:

  • Tokens: many runners report the number of tokens used in each response. Compare it with your own count from the first case and see how close the rough rule was.
  • Reply parser: ask the model for JSON with a city and an ETA. Run the same prompt twenty times and count how many replies your parse_reply accepts. Real models misbehave in the ways the case showed.
  • Search: install an embedding model through the same runner and compare embedding search with your TF-IDF results on the same questions, using hit rate and MRR.
  • Assistant: put your retrieval, your threshold and your grounding check in front of the model and run your golden set.

If your results are in the same range as the course’s and you can say why they differ, your lab works.

Practise safely

  • Everything here runs locally, so the data stays on your machine. If you move to a hosted service, never send confidential or personal data you are not allowed to share, and check its terms and any free-tier limits.
  • Never put a secret or key in a notebook or in Git. Read keys from an environment variable and keep them out of your repository.
  • Treat model output as untrusted text: validate it, never run it with eval, and never let it run tools without the checks you built in this course.
  • Test with harmless prompt-injection examples of your own on your own system, never on a service you do not own.
  • Record the model name and version, the settings (such as temperature) and the date with every experiment, because results change between models and versions.

Go further

  • Add a temperature of 0 to make answers more repeatable, and compare with a higher temperature on your golden set.
  • Build a small evaluation script that loads your golden set, runs two prompts and prints accuracy, regressions and tokens, and run it before every change.
  • Try the same prompt on two different sizes of model and measure what the larger one buys you.
  • Add citation: make the assistant return the id of the passage it used, and check that the id exists and the passage supports the answer.
  • Read the documentation of the runner and of any libraries you use, and keep a short log of what worked, what did not and what you changed.

Courses that use this lab

Other lab guides

Software changes quickly. Menu names and version numbers may differ a little from what you see; the ideas and the check at the end of each section stay the same. Always download from the project's own website, and read the licence terms for your situation.