Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Ollama backend #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
26 changes: 22 additions & 4 deletions please.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}
Expand All @@ -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.'"

Expand All @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the "--"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the convention of the previous error message

echo "Error: --model requires a gpt model"

or am I missing something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, nevermind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course you are right. I'm just getting started with work today ;-)

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
Expand Down Expand Up @@ -220,7 +232,9 @@ explain_command() {
}

perform_openai_request() {
IFS=$'\n' read -r -d '' -a result < <(curl "${openai_invocation_url}/chat/completions" \
invocation_url=${openai_invocation_url}
[ "${api_service}" != "openai" ] && invocation_url=${ollama_invocation_url}
IFS=$'\n' read -r -d '' -a result < <(curl "${invocation_url}/chat/completions" \
-s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Accept-Encoding: identity" \
Expand Down Expand Up @@ -466,7 +480,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
Expand Down