From ff1abb6aa7a88c936e8081790a338c05bd6a3961 Mon Sep 17 00:00:00 2001 From: David Adams Date: Sat, 10 Aug 2024 19:47:13 +1000 Subject: [PATCH] add ollama support and documentation Signed-off-by: David Adams --- README.md | 3 ++- please.sh | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 90a548e..09f48f5 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,8 @@ You may then: - `-l` or `--legacy` will use the GPT3.5 AI model instead of GPT4 (in case you don't have API access to GPT4) - `--debug` will display additional output - `-a` or `--api-key` will store your API key in the local keychain -- `-m` or `--model` will query ChatGPT with the specified model +- `-m` or `--model` will query your API provider with the specified model +- `-o or `--ollama` will set the API provider to Ollama - `-v` or `--version` will show the current version - `-h` or `--help` will show the help message ``` diff --git a/please.sh b/please.sh index 0fa091e..ca663dc 100755 --- a/please.sh +++ b/please.sh @@ -2,6 +2,8 @@ set -uo pipefail +api_service=${PLEASE_API_SERVICE:-'openai'} + model=${PLEASE_OPENAI_CHAT_MODEL:-'gpt-4-turbo'} options=("[I] Invoke" "[C] Copy to clipboard" "[Q] Ask a question" "[A] Abort" ) number_of_options=${#options[@]} @@ -23,8 +25,10 @@ questionMark="\x1B[31m?\x1B[0m" checkMark="\x1B[31m\xE2\x9C\x93\x1B[0m" openai_api_base=${PLEASE_OPENAI_API_BASE:-${OPENAI_API_BASE:-${OPENAI_URL:-"https://api.openai.com"}}} +ollama_api_base="http://localhost:11434" openai_api_version=${PLEASE_OPENAI_API_VERSION:-${OPENAI_API_VERSION:-"v1"}} openai_invocation_url=${openai_api_base}/${openai_api_version} +ollama_invocation_url=${ollama_api_base}/${openai_api_version} fail_msg="echo 'I do not know. Please rephrase your question.'" @@ -50,14 +54,22 @@ check_args() { exit 0 ;; -m|--model) - if [ -n "$2" ] && [ "${2:0:1}" != "-" ] && [ "${2:0:3}" == "gpt" ]; then + if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then + if [ "${api_service}" = "openai" ] && [ "${2:0:3}" != "gpt" ]; then + echo "Error: --model requires a gpt model" + exit 1 + fi model="$2" shift 2 else - echo "Error: --model requires a gpt model" + echo "Error: --model requires a valid model" exit 1 fi ;; + -o|--ollama) + api_service="ollama" + shift + ;; -v|--version) display_version exit 0 @@ -220,7 +232,9 @@ explain_command() { } perform_openai_request() { - completions_url="${openai_invocation_url}/chat/completions" + invocation_url=${openai_invocation_url} + [ "${api_service}" != "openai" ] && invocation_url=${ollama_invocation_url} + completions_url="${invocation_url}/chat/completions" IFS=$'\n' read -r -d '' -a result < <(curl "${completions_url}" \ -s -w "\n%{http_code}" \ -H "Content-Type: application/json" \ @@ -467,7 +481,11 @@ function main() { fi check_args "${input[@]}" - check_key + if [ "${api_service}" = "openai" ]; then + check_key + else + OPENAI_API_KEY="ollama" + fi get_command if [ "${explain}" -eq 1 ]; then @@ -488,4 +506,4 @@ function main() { # Only call main if the script is not being sourced if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" -fi \ No newline at end of file +fi