Skip to content

Commit

Permalink
add ollama support and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
David Adams authored and Dadams2 committed Aug 10, 2024
1 parent c405b13 commit f07836f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
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"
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

0 comments on commit f07836f

Please sign in to comment.