From 2236a93efccb2aa8d907225c182d54ffd2e90e11 Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Tue, 31 Oct 2023 09:12:39 -0700 Subject: [PATCH 1/5] docs: add examples using bash to compare models Signed-off-by: Matt Williams --- examples/bash-comparemodels/README.md | 9 ++++ examples/bash-comparemodels/bulkquestions.sh | 3 ++ examples/bash-comparemodels/comparemodels.sh | 56 ++++++++++++++++++++ examples/bash-comparemodels/sourcequestions | 7 +++ 4 files changed, 75 insertions(+) create mode 100644 examples/bash-comparemodels/README.md create mode 100755 examples/bash-comparemodels/bulkquestions.sh create mode 100755 examples/bash-comparemodels/comparemodels.sh create mode 100644 examples/bash-comparemodels/sourcequestions diff --git a/examples/bash-comparemodels/README.md b/examples/bash-comparemodels/README.md new file mode 100644 index 00000000..870c1259 --- /dev/null +++ b/examples/bash-comparemodels/README.md @@ -0,0 +1,9 @@ +# Bash Shell examples + +When calling `ollama`, you can pass it a file to run all the prompts in the file, one after the other. This concept is used in two examples + +## Bulk Questions +`bulkquestions.sh` is a script that runs all the questions in `sourcequestions` using the llama2 model and outputs the answers. + +## Compare Models +`comparemodels.sh` is a script that runs all the questions in `sourcequestions` using any 4 models you choose that you have already pulled from the registry or have created locally. \ No newline at end of file diff --git a/examples/bash-comparemodels/bulkquestions.sh b/examples/bash-comparemodels/bulkquestions.sh new file mode 100755 index 00000000..0c522020 --- /dev/null +++ b/examples/bash-comparemodels/bulkquestions.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +ollama run llama2 < sourcequestions diff --git a/examples/bash-comparemodels/comparemodels.sh b/examples/bash-comparemodels/comparemodels.sh new file mode 100755 index 00000000..2acd225c --- /dev/null +++ b/examples/bash-comparemodels/comparemodels.sh @@ -0,0 +1,56 @@ +#! /usr/bin/env bash +NUMBEROFCHOICES=4 +CHOICES=$(ollama list | awk '{print $1}') +SELECTIONS=() +declare -a SUMS=() + +echo "Select $NUMBEROFCHOICES models to compare:" +select ITEM in $CHOICES; do + if [[ -n $ITEM ]]; then + echo "You have selected $ITEM" + SELECTIONS+=("$ITEM") + ((COUNT++)) + if [[ $COUNT -eq $NUMBEROFCHOICES ]]; then + break + fi + else + echo "Invalid selection" + fi +done + +for ITEM in "${SELECTIONS[@]}"; do + echo "--------------------------------------------------------------" + echo "Loading the model $ITEM into memory" + ollama run "$ITEM" "" + echo "--------------------------------------------------------------" + echo "Running the questions through the model $ITEM" + COMMAND_OUTPUT=$(ollama run "$ITEM" --verbose < sourcequestions 2>&1| tee /dev/stderr) + + SUM=$(echo "$COMMAND_OUTPUT" | awk ' + /eval duration:/ { + value = $3 + if (index(value, "ms") > 0) { + gsub("ms", "", value) + value /= 1000 + } else { + gsub("s", "", value) + } + sum += value + } + END { print sum }') + + + SUMS+=("All questions for $ITEM completed in $SUM seconds") +done + +echo "" +echo "--------------------------------------------------------------" +echo -e "Sums of eval durations for each run:" +for val in "${SUMS[@]}"; do + echo "$val" +done + +echo "--------------------------------------------------------------" +echo "Comparison complete. Now you can decide" +echo "which model is best." +echo "--------------------------------------------------------------" \ No newline at end of file diff --git a/examples/bash-comparemodels/sourcequestions b/examples/bash-comparemodels/sourcequestions new file mode 100644 index 00000000..90004c07 --- /dev/null +++ b/examples/bash-comparemodels/sourcequestions @@ -0,0 +1,7 @@ +Why is the sky blue +What is a black hole +Explain the big bang theory like I am 5? +What is the quickest way to win a game of Monopoly with 3 others? +Why does a vacuum bottle keep my coffee hot and my milkshake cold? +What is the difference between a meteor, a meteorite, and a meteoroid? +Create an array with 5 items and print to the console. Do this in Python, C#, Typescript, and Rust. \ No newline at end of file From 45123017563c459d698b6221965661b02c783885 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 31 Oct 2023 13:25:36 -0400 Subject: [PATCH 2/5] Update README.md --- examples/bash-comparemodels/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/bash-comparemodels/README.md b/examples/bash-comparemodels/README.md index 870c1259..01b21ba0 100644 --- a/examples/bash-comparemodels/README.md +++ b/examples/bash-comparemodels/README.md @@ -6,4 +6,4 @@ When calling `ollama`, you can pass it a file to run all the prompts in the file `bulkquestions.sh` is a script that runs all the questions in `sourcequestions` using the llama2 model and outputs the answers. ## Compare Models -`comparemodels.sh` is a script that runs all the questions in `sourcequestions` using any 4 models you choose that you have already pulled from the registry or have created locally. \ No newline at end of file +`comparemodels.sh` is a script that runs all the questions in `sourcequestions` using any 4 models you choose that you have already pulled from the Ollama library or have created locally. From 57579250603dfbdd3d716bfdb9bff53759824ddd Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Tue, 31 Oct 2023 11:52:01 -0700 Subject: [PATCH 3/5] add a gif Signed-off-by: Matt Williams --- examples/bash-comparemodels/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/bash-comparemodels/README.md b/examples/bash-comparemodels/README.md index 01b21ba0..b33863c3 100644 --- a/examples/bash-comparemodels/README.md +++ b/examples/bash-comparemodels/README.md @@ -1,5 +1,7 @@ # Bash Shell examples +![Example Gif](https://uce9aa94fbc06b088ca05a92fe37.previews.dropboxusercontent.com/p/thumb/ACF0g3yrdu5-tvuw59Wil8B5bwuLkvWFFQNrYJzEkqvJnv4WfyuqcTGfXhXDfqfbemi5jGr9-bccO8r5VZxXrAeU1l_Plq99HCqV6b10thwwlaQCNbkXkw4YSF0YlYu-wu5A6Vn2SlrdcfiwTl6et-m7CPYx8ad2jSZXcPEozDUqXqB-f_zZNskASYzWwQko9n6UjMKx6qt54FYvIiW6n3ZiNVlM0GGt91FAA2Y0zD23aBlOlIAN8wH7qLznS2rZsn1n_7ukJMwegcEVud_XNPbG8Hn_13NtwkVsf4uWThknUpslNRmxWisqlRCaxZY71Me9wz3puH3nlpxtNlwoNAvQcXf0S4u_r1WLx22KwWqmvYFU41X2j_1Kum8amUrAv_5WVnOL6ctWnrbV4fauYfT9ClwgmLAtLoHwaQSXo2R2Kut_QIAkFIDAyMj9Fe9Ifj0/p.gif) + When calling `ollama`, you can pass it a file to run all the prompts in the file, one after the other. This concept is used in two examples ## Bulk Questions From 80362fedce7d6e0a12e9ab5de66edaa0a1f2b741 Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Tue, 31 Oct 2023 12:40:46 -0700 Subject: [PATCH 4/5] better readme Signed-off-by: Matt Williams --- examples/bash-comparemodels/README.md | 10 ++-------- examples/bash-comparemodels/bulkquestions.sh | 1 + examples/bash-comparemodels/comparemodels.sh | 10 +++++++++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/bash-comparemodels/README.md b/examples/bash-comparemodels/README.md index b33863c3..ad56aa7d 100644 --- a/examples/bash-comparemodels/README.md +++ b/examples/bash-comparemodels/README.md @@ -1,11 +1,5 @@ # Bash Shell examples -![Example Gif](https://uce9aa94fbc06b088ca05a92fe37.previews.dropboxusercontent.com/p/thumb/ACF0g3yrdu5-tvuw59Wil8B5bwuLkvWFFQNrYJzEkqvJnv4WfyuqcTGfXhXDfqfbemi5jGr9-bccO8r5VZxXrAeU1l_Plq99HCqV6b10thwwlaQCNbkXkw4YSF0YlYu-wu5A6Vn2SlrdcfiwTl6et-m7CPYx8ad2jSZXcPEozDUqXqB-f_zZNskASYzWwQko9n6UjMKx6qt54FYvIiW6n3ZiNVlM0GGt91FAA2Y0zD23aBlOlIAN8wH7qLznS2rZsn1n_7ukJMwegcEVud_XNPbG8Hn_13NtwkVsf4uWThknUpslNRmxWisqlRCaxZY71Me9wz3puH3nlpxtNlwoNAvQcXf0S4u_r1WLx22KwWqmvYFU41X2j_1Kum8amUrAv_5WVnOL6ctWnrbV4fauYfT9ClwgmLAtLoHwaQSXo2R2Kut_QIAkFIDAyMj9Fe9Ifj0/p.gif) +When you review the examples on this site, it is possible to think that making use of AI with Ollama will be hard. You need an orchestrator, and vector database, complicated infrastructure, and more. But that is not always the case. Ollama is designed to be easy to use, and to be used in any environment. -When calling `ollama`, you can pass it a file to run all the prompts in the file, one after the other. This concept is used in two examples - -## Bulk Questions -`bulkquestions.sh` is a script that runs all the questions in `sourcequestions` using the llama2 model and outputs the answers. - -## Compare Models -`comparemodels.sh` is a script that runs all the questions in `sourcequestions` using any 4 models you choose that you have already pulled from the Ollama library or have created locally. +The two examples here show how to list the models and query them from a simple bash script. diff --git a/examples/bash-comparemodels/bulkquestions.sh b/examples/bash-comparemodels/bulkquestions.sh index 0c522020..b0e4a585 100755 --- a/examples/bash-comparemodels/bulkquestions.sh +++ b/examples/bash-comparemodels/bulkquestions.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash +# Run a model and pass in a text file of questions ollama run llama2 < sourcequestions diff --git a/examples/bash-comparemodels/comparemodels.sh b/examples/bash-comparemodels/comparemodels.sh index 2acd225c..fa5520d7 100755 --- a/examples/bash-comparemodels/comparemodels.sh +++ b/examples/bash-comparemodels/comparemodels.sh @@ -1,9 +1,14 @@ #! /usr/bin/env bash +# Compare multiple models by running them with the same questions + NUMBEROFCHOICES=4 -CHOICES=$(ollama list | awk '{print $1}') SELECTIONS=() declare -a SUMS=() +# Get the list of models +CHOICES=$(ollama list | awk '{print $1}') + +# Select which models to run as a comparison echo "Select $NUMBEROFCHOICES models to compare:" select ITEM in $CHOICES; do if [[ -n $ITEM ]]; then @@ -18,6 +23,7 @@ select ITEM in $CHOICES; do fi done +# Loop through each of the selected models for ITEM in "${SELECTIONS[@]}"; do echo "--------------------------------------------------------------" echo "Loading the model $ITEM into memory" @@ -26,6 +32,8 @@ for ITEM in "${SELECTIONS[@]}"; do echo "Running the questions through the model $ITEM" COMMAND_OUTPUT=$(ollama run "$ITEM" --verbose < sourcequestions 2>&1| tee /dev/stderr) + # eval duration is sometimes listed in seconds and sometimes in milliseconds. + # Add up the values for each model SUM=$(echo "$COMMAND_OUTPUT" | awk ' /eval duration:/ { value = $3 From f31961637fde5642bd8fac54319d970e305e8dfc Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 1 Nov 2023 12:20:55 -0400 Subject: [PATCH 5/5] Update README.md --- examples/bash-comparemodels/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/bash-comparemodels/README.md b/examples/bash-comparemodels/README.md index b33863c3..01b21ba0 100644 --- a/examples/bash-comparemodels/README.md +++ b/examples/bash-comparemodels/README.md @@ -1,7 +1,5 @@ # Bash Shell examples -![Example Gif](https://uce9aa94fbc06b088ca05a92fe37.previews.dropboxusercontent.com/p/thumb/ACF0g3yrdu5-tvuw59Wil8B5bwuLkvWFFQNrYJzEkqvJnv4WfyuqcTGfXhXDfqfbemi5jGr9-bccO8r5VZxXrAeU1l_Plq99HCqV6b10thwwlaQCNbkXkw4YSF0YlYu-wu5A6Vn2SlrdcfiwTl6et-m7CPYx8ad2jSZXcPEozDUqXqB-f_zZNskASYzWwQko9n6UjMKx6qt54FYvIiW6n3ZiNVlM0GGt91FAA2Y0zD23aBlOlIAN8wH7qLznS2rZsn1n_7ukJMwegcEVud_XNPbG8Hn_13NtwkVsf4uWThknUpslNRmxWisqlRCaxZY71Me9wz3puH3nlpxtNlwoNAvQcXf0S4u_r1WLx22KwWqmvYFU41X2j_1Kum8amUrAv_5WVnOL6ctWnrbV4fauYfT9ClwgmLAtLoHwaQSXo2R2Kut_QIAkFIDAyMj9Fe9Ifj0/p.gif) - When calling `ollama`, you can pass it a file to run all the prompts in the file, one after the other. This concept is used in two examples ## Bulk Questions