Skip to content

Commit d67b0c5

Browse files
authored
Merge pull request #78 from NAG-DevOps/ollama-example
add Ollama example
2 parents 71f1783 + e87093c commit d67b0c5

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

src/llm-examples/ollama/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Ollama on HPC (Speed) Cluster
2+
3+
Ollama is an open-source software tool that simplifies running large language models (LLMs) directly on your local machine.
4+
5+
#### References:
6+
- [Ollama](https://ollama.com)
7+
- [Ollama GitHub](https://github.com/ollama/ollama)
8+
9+
## Prerequisites
10+
Before starting, ensure you have [access](https://nag-devops.github.io/speed-hpc/#requesting-access) to the HPC (Speed) cluster.
11+
12+
## Instructions
13+
* Clone Speed Github repository
14+
```shell
15+
git clone --depth=1 https://github.com/NAG-DevOps/speed-hpc.git
16+
```
17+
18+
* Navigate to ollama directory in `src/llm-examples`
19+
20+
* Run `start_ollama.sh`
21+
```shell
22+
sbatch start_ollama.sh
23+
```
24+
25+
The script will:
26+
- Request required resources
27+
- Download Ollama tarball and extract it
28+
- Add Ollama to user's path and setup environment variables
29+
30+
```shell
31+
setenv PATH /speed-scratch/$USER/ollama/bin:$PATH
32+
```
33+
34+
- Start Ollama server with `ollama serve`
35+
- Print the ssh command to connect to the server.
36+
37+
Note: The server is set to run for 3 hours (adjust if needed)
38+
39+
* Open a new terminal window and paste the ssh command to connect to the speed node the server is running on. The command will look like:
40+
```shell
41+
ssh -L XXXXX:speed-XX:XXXXX <ENCSusername>@speed.encs.concordia.ca -t ssh speed-XX
42+
```
43+
44+
* Navigate to ollama directory and do a sanity check
45+
```shell
46+
setenv PATH /speed-scratch/$USER/ollama/bin:$PATH
47+
ollama -v
48+
```
49+
50+
* Run the `run_ollama.sh` script, replace speed-XX with the name of the node the server is running on
51+
```shell
52+
sbatch -w speed-XX run_ollama.sh
53+
```
54+
55+
The script will:
56+
- Request required resources
57+
- Set environment variables
58+
- Pull a model to run (in this case it's llama3.2)
59+
- Create a python environment to run `ollama_demo.py`
60+
- Run `ollama_demo.py` which interact with the model
61+
62+
Optional:
63+
1. Check if the server is running, replace XXXXX with the port number
64+
```shell
65+
curl http://localhost:XXXXX/api/tags
66+
```
67+
68+
2. Run a model with a prompt
69+
```shell
70+
curl -sS http://localhost:56781/api/generate -H "Content-Type: application/json" -d '{"model": "llama3.2","prompt": "why is the sky blue?","stream": false}' | jq -r '.response'
71+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import ollama
2+
import os
3+
from pathlib import Path
4+
5+
user = os.getenv("USER")
6+
host_file = Path(f"/speed-scratch/{user}/ollama/.ollama_host")
7+
8+
ollama_host = host_file.read_text().strip()
9+
10+
client = ollama.Client(host=ollama_host)
11+
response = client.chat(
12+
model='llama3.2',
13+
messages=[{
14+
'role': 'user',
15+
'content': (
16+
'What popular operating system, launched in 1991, '
17+
'also has its own mascot, Tux the penguin?'
18+
)
19+
}]
20+
)
21+
22+
print(f"[Client connected to {ollama_host}]")
23+
print(response["message"]["content"])
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/encs/bin/tcsh
2+
3+
#SBATCH --job-name=ollama-client
4+
#SBATCH --mem=50G
5+
#SBATCH --ntasks=1
6+
#SBATCH --cpus-per-task=4
7+
#SBATCH --mail-type=ALL
8+
#SBATCH --output=ollama-%J.out
9+
10+
set ODIR = /speed-scratch/$USER/ollama
11+
setenv PATH /speed-scratch/$USER/ollama/bin:$PATH
12+
setenv OLLAMA_MODELS $ODIR/models
13+
setenv OLLAMA_HOST `cat /speed-scratch/$USER/ollama/.ollama_host`
14+
15+
# Sanity check
16+
ollama -v
17+
18+
# Pull a model
19+
ollama pull llama3.2
20+
21+
# Create a python environment
22+
setenv ENV_DIR /speed-scratch/$USER/envs/python-env
23+
24+
if ( ! -d $ENV_DIR ) then
25+
echo "Creating python environment..."
26+
mkdir -p $ENV_DIR/{tmp,pkgs,cache}
27+
28+
setenv TMP $ENV_DIR/tmp
29+
setenv TMPDIR $ENV_DIR/tmp
30+
setenv PIP_CACHE_DIR $ENV_DIR/cache
31+
32+
python3 -m venv $ENV_DIR
33+
else
34+
echo "Python environment already exists."
35+
endif
36+
37+
source $ENV_DIR/bin/activate.csh
38+
pip install -U pip ollama
39+
40+
python ollama_demo.py
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/encs/bin/tcsh
2+
3+
#SBATCH --job-name=ollama-server
4+
#SBATCH --mem=50G
5+
#SBATCH --gpus=1
6+
#SBATCH --ntasks=1
7+
#SBATCH --cpus-per-task=4
8+
#SBATCH --mail-type=ALL
9+
#SBATCH --output=ollama-%J.out
10+
#SBATCH --time=03:00:00 ## Adjust based on your needs
11+
12+
set ODIR = /speed-scratch/$USER/ollama
13+
mkdir -p $ODIR && cd $ODIR
14+
15+
# Download Ollama tarball and extract it once
16+
if ( ! -x $ODIR/bin/ollama ) then
17+
echo "Downloading Ollama..."
18+
curl -LO https://ollama.com/download/ollama-linux-amd64.tgz
19+
tar -xzf ollama-linux-amd64.tgz
20+
endif
21+
22+
# Add ollama to your PATH and set models directory
23+
setenv PATH $ODIR/bin:$PATH
24+
setenv OLLAMA_MODELS $ODIR/models
25+
mkdir -p $OLLAMA_MODELS
26+
27+
# Ollama by default listens on 127.0.0.1:11434, the port however can be overwritten
28+
set PORT = `python -c 'import socket,sys; s=socket.socket(); s.bind(("",0)); print(s.getsockname()[1]); s.close()'`
29+
setenv OLLAMA_HOST 127.0.0.1:$PORT
30+
echo "http://localhost:$PORT" >! ${ODIR}/.ollama_host
31+
32+
# Print connection instructions
33+
set NODE = `hostname -s`
34+
set USER = `whoami`
35+
echo ""
36+
echo "===================================================="
37+
echo " Ollama server will start on $NODE"
38+
echo "===================================================="
39+
echo "To connect from your laptop, open a new terminal and run:"
40+
41+
echo ""
42+
echo " ssh -L ${PORT}:${NODE}:${PORT} ${USER}@speed.encs.concordia.ca -t ssh $NODE"
43+
echo ""
44+
echo "Once connected, set your environment variables:"
45+
echo " setenv PATH ${ODIR}/bin:$PATH"
46+
echo " setenv OLLAMA_HOST http://localhost:${PORT}"
47+
echo " setenv OLLAMA_MODELS ${ODIR}/models"
48+
echo "===================================================="
49+
echo ""
50+
51+
# Start server
52+
srun ollama serve

0 commit comments

Comments
 (0)