Skip to content

Commit e87093c

Browse files
committed
bug fixes and final updates
1 parent 2bb9cd7 commit e87093c

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

src/llm-examples/ollama/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Before starting, ensure you have [access](https://nag-devops.github.io/speed-hpc
3838
3939
* 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:
4040
```shell
41-
ssh -L XXXXX:localhost:XXXXX <ENCSusername>@speed.encs.concordia.ca -t ssh speed-XX
41+
ssh -L XXXXX:speed-XX:XXXXX <ENCSusername>@speed.encs.concordia.ca -t ssh speed-XX
4242
```
4343
4444
* Navigate to ollama directory and do a sanity check
@@ -47,8 +47,25 @@ Before starting, ensure you have [access](https://nag-devops.github.io/speed-hpc
4747
ollama -v
4848
```
4949
50-
* Pull a specific model and run it interactively (optional).
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
5169
```shell
52-
ollama pull llama3.2
53-
echo "What is today" | ollama run llama3.2
54-
```
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+
```

src/llm-examples/ollama/ollama_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
client = ollama.Client(host=ollama_host)
1111
response = client.chat(
12-
model='llama3.1',
12+
model='llama3.2',
1313
messages=[{
1414
'role': 'user',
1515
'content': (
@@ -20,4 +20,4 @@
2020
)
2121

2222
print(f"[Client connected to {ollama_host}]")
23-
print(response["message"]["content"])
23+
print(response["message"]["content"])

src/llm-examples/ollama/run_ollama.sh

100644100755
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#!/encs/bin/tcsh
22

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+
310
set ODIR = /speed-scratch/$USER/ollama
411
setenv PATH /speed-scratch/$USER/ollama/bin:$PATH
512
setenv OLLAMA_MODELS $ODIR/models
6-
setenv OLLAMA_HOST "`cat $ODIR/.ollama_host`"
13+
setenv OLLAMA_HOST `cat /speed-scratch/$USER/ollama/.ollama_host`
714

815
# Sanity check
916
ollama -v
@@ -13,14 +20,21 @@ ollama pull llama3.2
1320

1421
# Create a python environment
1522
setenv ENV_DIR /speed-scratch/$USER/envs/python-env
16-
mkdir -p $ENV_DIR/{tmp,pkgs,cache}
1723

18-
setenv TMP $ENV_DIR/tmp
19-
setenv TMPDIR $ENV_DIR/tmp
20-
setenv PIP_CACHE_DIR $ENV_DIR/cache
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
2136

22-
python3 -m venv $ENV_DIR
2337
source $ENV_DIR/bin/activate.csh
2438
pip install -U pip ollama
2539

26-
python ollama_demo.py
40+
python ollama_demo.py

src/llm-examples/ollama/start_ollama.sh

100644100755
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ setenv PATH $ODIR/bin:$PATH
2424
setenv OLLAMA_MODELS $ODIR/models
2525
mkdir -p $OLLAMA_MODELS
2626

27-
# pull a specific model
28-
ollama pull llama3.2
29-
30-
# Get an available port for the server
27+
# Ollama by default listens on 127.0.0.1:11434, the port however can be overwritten
3128
set PORT = `python -c 'import socket,sys; s=socket.socket(); s.bind(("",0)); print(s.getsockname()[1]); s.close()'`
3229
setenv OLLAMA_HOST 127.0.0.1:$PORT
3330
echo "http://localhost:$PORT" >! ${ODIR}/.ollama_host
@@ -42,7 +39,7 @@ echo "===================================================="
4239
echo "To connect from your laptop, open a new terminal and run:"
4340

4441
echo ""
45-
echo " ssh -L ${PORT}:localhost:${PORT} ${USER}@speed.encs.concordia.ca -t ssh $NODE"
42+
echo " ssh -L ${PORT}:${NODE}:${PORT} ${USER}@speed.encs.concordia.ca -t ssh $NODE"
4643
echo ""
4744
echo "Once connected, set your environment variables:"
4845
echo " setenv PATH ${ODIR}/bin:$PATH"
@@ -52,4 +49,4 @@ echo "===================================================="
5249
echo ""
5350

5451
# Start server
55-
srun ollama serve
52+
srun ollama serve

0 commit comments

Comments
 (0)