-
Notifications
You must be signed in to change notification settings - Fork 16
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
Feature/azure openai #42
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ questionMark="\x1B[31m?\x1B[0m" | |
checkMark="\x1B[31m\xE2\x9C\x93\x1B[0m" | ||
|
||
openai_invocation_url=${OPENAI_URL:-"https://api.openai.com/v1"} | ||
openai_use_azure_endpoint=$(echo "$OPENAI_URL" | grep azure.com >/dev/null && echo 1 || echo 0) | ||
fail_msg="echo 'I do not know. Please rephrase your question.'" | ||
|
||
declare -a qaMessages=() | ||
|
@@ -46,6 +47,10 @@ check_args() { | |
store_api_key | ||
exit 0 | ||
;; | ||
-H|--api-host) | ||
store_api_host | ||
exit 0 | ||
;; | ||
-m|--model) | ||
if [ -n "$2" ] && [ "${2:0:1}" != "-" ] && [ "${2:0:3}" == "gpt" ]; then | ||
model="$2" | ||
|
@@ -82,12 +87,14 @@ function store_api_key() { | |
exit 1 | ||
fi | ||
|
||
echo "The script needs to create or copy the API key. Press Enter to continue..." | ||
read -rs | ||
if [ "${openai_use_azure_endpoint}" -eq 0 ]; then | ||
echo "The script needs to create or copy the API key. Press Enter to continue..." | ||
read -rs | ||
|
||
apiKeyUrl="https://platform.openai.com/account/api-keys" | ||
echo "Opening ${apiKeyUrl} in your browser..." | ||
open "${apiKeyUrl}" || xdg-open "${apiKeyUrl}" | ||
apiKeyUrl="https://platform.openai.com/account/api-keys" | ||
echo "Opening ${apiKeyUrl} in your browser..." | ||
open "${apiKeyUrl}" || xdg-open "${apiKeyUrl}" | ||
fi | ||
|
||
while true; do | ||
echo "Please enter your API key: [Press Ctrl+C to exit]" | ||
|
@@ -109,6 +116,35 @@ function store_api_key() { | |
done | ||
} | ||
|
||
function store_api_host() { | ||
echo "Setting up Azure OpenAPI host." | ||
|
||
while true; do | ||
echo "Please enter your Endpoint: [Press Ctrl+C to exit]" | ||
read -rs endpoint | ||
|
||
if [ -z "$endpoint" ]; then | ||
echo "Endpoint cannot be empty. Please try again." | ||
else | ||
endpoint=${endpoint%/} | ||
break | ||
fi | ||
done | ||
|
||
while true; do | ||
echo "Please enter your Deployment Name: [Press Ctrl+C to exit]" | ||
read -rs deployment_id | ||
|
||
if [ -z "$deployment_id" ]; then | ||
echo "Deployment Name cannot be empty. Please try again." | ||
else | ||
break | ||
fi | ||
done | ||
|
||
export OPENAI_URL="${endpoint}/openai/deployments/${deployment-id}" | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really understand - do you want to set endpoint and deployment ID every time you call please - or at least with every new terminal? Wouldn't that be a lot of work? We could also store these values somewhere on the disk, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Saving to disk sounds like the right thing to do.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, on Linux, we should respect |
||
display_version() { | ||
echo "Please vVERSION_NUMBER" | ||
} | ||
|
@@ -122,6 +158,7 @@ display_help() { | |
echo " -l, --legacy Use GPT 3.5 (in case you do not have GPT4 API access)" | ||
echo " --debug Show debugging output" | ||
echo " -a, --api-key Store your API key in the local keychain" | ||
echo " -H, --api-host Store your Azure OpenAPI host" | ||
echo " -m, --model Specify the exact LLM model for the script" | ||
echo " -v, --version Display version information and exit" | ||
echo " -h, --help Display this help message and exit" | ||
|
@@ -217,10 +254,18 @@ explain_command() { | |
} | ||
|
||
perform_openai_request() { | ||
IFS=$'\n' read -r -d '' -a result < <(curl "${openai_invocation_url}/chat/completions" \ | ||
if [ "${openai_use_azure_endpoint}" -eq 0 ]; then | ||
endpoint="${openai_invocation_url}/chat/completions" | ||
authorization="Authorization: Bearer ${OPENAI_API_KEY}" | ||
else | ||
endpoint="${openai_invocation_url}/chat/completions?api-version=2023-05-15" | ||
authorization="api-key: ${OPENAI_API_KEY}" | ||
fi | ||
|
||
IFS=$'\n' read -r -d '' -a result < <(curl "${endpoint}" \ | ||
-s -w "\n%{http_code}" \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer ${OPENAI_API_KEY}" \ | ||
-H "${authorization}" \ | ||
-d "${payload}" \ | ||
--silent) | ||
debug "Response:\n${result[*]}" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a bit brittle - should we rather set it wenn setting the OPENAI_URL as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather infer the information from the actually used endpoint url rather than keep the 2 settings in sync.
I can change the code to be more robust in a proper
if
block.But if you prefer a setting on it's own (saved to disk), I can do that too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, if we make it a bit more robust.