From 0329ef2b886e16269d4c285ed21f7b6e2fe1ebd4 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Thu, 6 Jul 2023 10:04:43 +0530 Subject: [PATCH 01/11] Add support for Azure Open AI --- scripts/bash_setup.sh | 9 ++++++++- src/codex_query.py | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index acf228c..f1ae736 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -15,6 +15,7 @@ Usage: source bash_setup.sh [optional parameters] -e engineId Set the OpenAI engine id. -d Print some system information for debugging. -h Print this help content. + -a Use Azure Open AI To uninstall Codex CLI use bash_cleanup.sh. For more information visit https://github.com/microsoft/Codex-CLI @@ -29,6 +30,7 @@ readParameters() -o ) shift; ORG_ID=$1 ;; -k ) shift; SECRET_KEY=$1 ;; -e ) shift; ENGINE_ID=$1 ;; + -a ) shift; USE_AZURE=$1 ;; -d ) systemInfo exitScript ;; @@ -90,6 +92,9 @@ configureApp() echo "organization_id=$ORG_ID" >> $OPENAI_RC_FILE echo "secret_key=$SECRET_KEY" >> $OPENAI_RC_FILE echo "engine=$ENGINE_ID" >> $OPENAI_RC_FILE + if [ -n "$USE_AZURE" ]; then + echo "use_azure=$USE_AZURE" >> $OPENAI_RC_FILE + fi chmod +x "$CODEX_CLI_PATH/src/codex_query.py" } @@ -173,7 +178,9 @@ BASH_RC_FILE="$HOME/.codexclirc" # Start installation readParameters $* askSettings -validateSettings +if ![ -z $USE_AZURE ]; then + validateSettings +fi configureApp configureBash enableApp diff --git a/src/codex_query.py b/src/codex_query.py index ade2a9a..ea9b152 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -57,10 +57,20 @@ def initialize(): config = configparser.ConfigParser() config.read(API_KEYS_LOCATION) + """ + Check if Azure Open AI is to be used + If so get the version and set base URL based on the organization + """ + if 'use_azure' in config['openai']: + print ('set azure type') + openai.api_type = "azure" + openai.api_base = config['openai']['organization_id'].strip('"').strip("'") + openai.api_version = config['openai']['use_azure'].strip('"').strip("'") + else: + openai.organization = config['openai']['organization_id'].strip('"').strip("'") openai.api_key = config['openai']['secret_key'].strip('"').strip("'") - openai.organization = config['openai']['organization_id'].strip('"').strip("'") ENGINE = config['openai']['engine'].strip('"').strip("'") - + print ('ENGINE = ', ENGINE) prompt_config = { 'engine': ENGINE, 'temperature': TEMPERATURE, @@ -143,6 +153,8 @@ def get_query(prompt_file): entry = input("prompt: ") + '\n' else: entry = sys.stdin.read() + + print('entry = ', entry) # first we check if the input is a command command_result, prompt_file = get_command_result(entry, prompt_file) @@ -171,10 +183,9 @@ def detect_shell(): if __name__ == '__main__': detect_shell() prompt_file = initialize() - + print('prompt_file.config[engine] = ', prompt_file.config['engine']) try: user_query, prompt_file = get_query(prompt_file) - config = prompt_file.config if prompt_file else { 'engine': ENGINE, 'temperature': TEMPERATURE, @@ -183,6 +194,7 @@ def detect_shell(): 'multi_turn': MULTI_TURN, 'token_count': 0 } + print ('config[engine] = ', config['engine']) # use query prefix to prime Codex for correct scripting language prefix = "" @@ -201,7 +213,9 @@ def detect_shell(): codex_query = prefix + prompt_file.read_prompt_file(user_query) + user_query # get the response from codex - response = openai.Completion.create(engine=config['engine'], prompt=codex_query, temperature=config['temperature'], max_tokens=config['max_tokens'], stop="#") + print('engine = ', config['engine']) + # response = openai.Completion.create(engine=config['engine'], prompt=codex_query, temperature=config['temperature'], max_tokens=config['max_tokens'], stop="#") + response = openai.Completion.create(engine=config['engine'], prompt=codex_query, stop="#") completion_all = response['choices'][0]['text'] From 708f4b3737001c0e6c038e4458654a03272bd5f4 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Thu, 6 Jul 2023 12:12:39 +0530 Subject: [PATCH 02/11] remove accidental ! --- scripts/bash_setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index f1ae736..64cc689 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -178,7 +178,7 @@ BASH_RC_FILE="$HOME/.codexclirc" # Start installation readParameters $* askSettings -if ![ -z $USE_AZURE ]; then +if [ -z $USE_AZURE ]; then validateSettings fi configureApp From 00e723511a2de2597814b90daa751557c385fa3f Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Thu, 6 Jul 2023 12:26:25 +0530 Subject: [PATCH 03/11] add more debugging troubleshooting --- src/codex_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codex_query.py b/src/codex_query.py index ea9b152..ead41a6 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -213,7 +213,7 @@ def detect_shell(): codex_query = prefix + prompt_file.read_prompt_file(user_query) + user_query # get the response from codex - print('engine = ', config['engine']) + print('engine :', config['engine'], openai.api_type, openai.api_key, openai.api_base, openai.api_version) # response = openai.Completion.create(engine=config['engine'], prompt=codex_query, temperature=config['temperature'], max_tokens=config['max_tokens'], stop="#") response = openai.Completion.create(engine=config['engine'], prompt=codex_query, stop="#") From c5c8768a577600b6f4fd7b1500bd3a1533baab29 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Sat, 8 Jul 2023 10:45:59 +0530 Subject: [PATCH 04/11] add working standalone python script --- azure_completion.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 azure_completion.py diff --git a/azure_completion.py b/azure_completion.py new file mode 100644 index 0000000..6b915db --- /dev/null +++ b/azure_completion.py @@ -0,0 +1,11 @@ +import openai +openai.api_type = "azure" +openai.api_base = "https://gavsgpt-openai.openai.azure.com/" +openai.api_version = "2022-12-01" +openai.api_key = "" +engine = "test-code" +print(engine, openai.api_type, openai.api_key, openai.api_base, openai.api_version, openai.organization) +codex_query="What is OpenAI?" +response = openai.Completion.create(engine=engine, prompt=codex_query, stop="#") +print (response) +print (response['choices'][0]['text']) From fc5d4803f891e4e29632b06fdde1cbb7f557a4b6 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Tue, 11 Jul 2023 13:45:09 +0530 Subject: [PATCH 05/11] Add Azure Open AI validation --- scripts/bash_setup.sh | 61 ++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index 64cc689..e18b222 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -57,29 +57,44 @@ askSettings() fi } -# Call OpenAI API with the given settings to verify everythin is in order +# Call (Azure) OpenAI API with the given settings to verify everythin is in order validateSettings() { - echo -n "*** Testing Open AI access... " - local TEST=$(curl -s 'https://api.openai.com/v1/engines' -H "Authorization: Bearer $SECRET_KEY" -H "OpenAI-Organization: $ORG_ID" -w '%{http_code}') - local STATUS_CODE=$(echo "$TEST"|tail -n 1) - if [ $STATUS_CODE -ne 200 ]; then - echo "ERROR [$STATUS_CODE]" - echo "Failed to access OpenAI API, result: $STATUS_CODE" - echo "Please check your OpenAI API key (https://beta.openai.com/account/api-keys)" - echo "and Organization ID (https://beta.openai.com/account/org-settings)." - echo "*************" - exitScript - return - fi - local ENGINE_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$ENGINE_ID\"") - if [ -z "$ENGINE_FOUND" ]; then - echo "ERROR" - echo "Cannot find OpenAI engine: $ENGINE_ID" - echo "Please check the OpenAI engine id (https://beta.openai.com/docs/engines/codex-series-private-beta)." - echo "*************" - exitScript - return + if [ -n "USE_AZURE" ]; then + echo -n "*** Testing Azure Open AI access... " + URL="${ORG_ID}openai/models?api-version=${USE_AZURE}" + local TEST=$(curl -s $URL -H "api-key: $SECRET_KEY" -w '%{http_code}') + local STATUS_CODE=$(echo "$TEST"|tail -n 1 | sed s'/}//g') + if [ $STATUS_CODE -ne 200 ]; then + echo "ERROR [$STATUS_CODE]" + echo "Failed to access Azure OpenAI API, result: $STATUS_CODE" + echo "Please check your Azure OpenAI Endpoint and API key (https://portal.azure.com)" + echo "*************" + exitScript + return + fi + else + echo -n "*** Testing Open AI access... " + local TEST=$(curl -s 'https://api.openai.com/v1/engines' -H "Authorization: Bearer $SECRET_KEY" -H "OpenAI-Organization: $ORG_ID" -w '%{http_code}') + local STATUS_CODE=$(echo "$TEST"|tail -n 1) + if [ $STATUS_CODE -ne 200 ]; then + echo "ERROR [$STATUS_CODE]" + echo "Failed to access OpenAI API, result: $STATUS_CODE" + echo "Please check your OpenAI API key (https://beta.openai.com/account/api-keys)" + echo "and Organization ID (https://beta.openai.com/account/org-settings)." + echo "*************" + exitScript + return + fi + local ENGINE_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$ENGINE_ID\"") + if [ -z "$ENGINE_FOUND" ]; then + echo "ERROR" + echo "Cannot find OpenAI engine: $ENGINE_ID" + echo "Please check the OpenAI engine id (https://beta.openai.com/docs/engines/codex-series-private-beta)." + echo "*************" + exitScript + return + fi fi echo "OK ***" } @@ -178,9 +193,7 @@ BASH_RC_FILE="$HOME/.codexclirc" # Start installation readParameters $* askSettings -if [ -z $USE_AZURE ]; then - validateSettings -fi +validateSettings configureApp configureBash enableApp From b5e0f6818eb0ccee9cd62974269924cbab329d0f Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Tue, 11 Jul 2023 13:56:14 +0530 Subject: [PATCH 06/11] Validate Azure Open AI deployment engine --- scripts/bash_setup.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index e18b222..000eabb 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -60,9 +60,9 @@ askSettings() # Call (Azure) OpenAI API with the given settings to verify everythin is in order validateSettings() { - if [ -n "USE_AZURE" ]; then + if [ -n "USE_AZURE" ]; then echo -n "*** Testing Azure Open AI access... " - URL="${ORG_ID}openai/models?api-version=${USE_AZURE}" + URL="${ORG_ID}openai/deployments?api-version=${USE_AZURE}" local TEST=$(curl -s $URL -H "api-key: $SECRET_KEY" -w '%{http_code}') local STATUS_CODE=$(echo "$TEST"|tail -n 1 | sed s'/}//g') if [ $STATUS_CODE -ne 200 ]; then @@ -73,6 +73,15 @@ validateSettings() exitScript return fi + local DEPLOYMENT_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$ENGINE_ID\"") + if [ -z "$DEPLOYMENT_FOUND" ]; then + echo "ERROR" + echo "Cannot find Azure OpenAI deployment engine: $ENGINE_ID" + echo "Please check the Azure OpenAI deployment engine id (hhttps://portal.azure.com)." + echo "*************" + exitScript + return + fi else echo -n "*** Testing Open AI access... " local TEST=$(curl -s 'https://api.openai.com/v1/engines' -H "Authorization: Bearer $SECRET_KEY" -H "OpenAI-Organization: $ORG_ID" -w '%{http_code}') From cd2edd89985b560ed36a239283274c4ba9108008 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Tue, 11 Jul 2023 17:09:14 +0530 Subject: [PATCH 07/11] cleanup USE_AZURE environment variable --- scripts/bash_setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index 000eabb..0028647 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -178,7 +178,7 @@ systemInfo() # Remove variables and functions from the environment, in case the script was sourced cleanupEnv() { - unset ORG_ID SECRET_KEY ENGINE_ID SOURCED OPENAI_RC_FILE BASH_RC_FILE + unset ORG_ID SECRET_KEY ENGINE_ID USE_AZURE SOURCED OPENAI_RC_FILE BASH_RC_FILE unset -f askSettings validateSettings configureApp configureBash enableApp readParameters } From 3604407dba7a301cbbe6820a29ef2dce63204694 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Wed, 12 Jul 2023 10:46:32 +0530 Subject: [PATCH 08/11] move azure_completion.py to test folder and branch --- azure_completion.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 azure_completion.py diff --git a/azure_completion.py b/azure_completion.py deleted file mode 100644 index 6b915db..0000000 --- a/azure_completion.py +++ /dev/null @@ -1,11 +0,0 @@ -import openai -openai.api_type = "azure" -openai.api_base = "https://gavsgpt-openai.openai.azure.com/" -openai.api_version = "2022-12-01" -openai.api_key = "" -engine = "test-code" -print(engine, openai.api_type, openai.api_key, openai.api_base, openai.api_version, openai.organization) -codex_query="What is OpenAI?" -response = openai.Completion.create(engine=engine, prompt=codex_query, stop="#") -print (response) -print (response['choices'][0]['text']) From 0b8cc22f591b20e4d55c905d4195ba00dc309997 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Thu, 13 Jul 2023 17:38:37 +0530 Subject: [PATCH 09/11] complete Azure OpenAI support --- src/codex_query.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/codex_query.py b/src/codex_query.py index ead41a6..27b6b33 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -62,7 +62,6 @@ def initialize(): If so get the version and set base URL based on the organization """ if 'use_azure' in config['openai']: - print ('set azure type') openai.api_type = "azure" openai.api_base = config['openai']['organization_id'].strip('"').strip("'") openai.api_version = config['openai']['use_azure'].strip('"').strip("'") @@ -70,7 +69,6 @@ def initialize(): openai.organization = config['openai']['organization_id'].strip('"').strip("'") openai.api_key = config['openai']['secret_key'].strip('"').strip("'") ENGINE = config['openai']['engine'].strip('"').strip("'") - print ('ENGINE = ', ENGINE) prompt_config = { 'engine': ENGINE, 'temperature': TEMPERATURE, @@ -154,7 +152,6 @@ def get_query(prompt_file): else: entry = sys.stdin.read() - print('entry = ', entry) # first we check if the input is a command command_result, prompt_file = get_command_result(entry, prompt_file) @@ -183,7 +180,6 @@ def detect_shell(): if __name__ == '__main__': detect_shell() prompt_file = initialize() - print('prompt_file.config[engine] = ', prompt_file.config['engine']) try: user_query, prompt_file = get_query(prompt_file) config = prompt_file.config if prompt_file else { @@ -194,7 +190,6 @@ def detect_shell(): 'multi_turn': MULTI_TURN, 'token_count': 0 } - print ('config[engine] = ', config['engine']) # use query prefix to prime Codex for correct scripting language prefix = "" @@ -211,15 +206,13 @@ def detect_shell(): prefix = '#' + config['shell'] + '\n\n' codex_query = prefix + prompt_file.read_prompt_file(user_query) + user_query - + # get the response from codex - print('engine :', config['engine'], openai.api_type, openai.api_key, openai.api_base, openai.api_version) - # response = openai.Completion.create(engine=config['engine'], prompt=codex_query, temperature=config['temperature'], max_tokens=config['max_tokens'], stop="#") - response = openai.Completion.create(engine=config['engine'], prompt=codex_query, stop="#") - + response = openai.Completion.create(engine=config['engine'], prompt=codex_query, temperature=config['temperature'], max_tokens=config['max_tokens'], stop="#") + completion_all = response['choices'][0]['text'] - if is_sensitive_content(user_query + '\n' + completion_all): + if openai.api_type == "" and is_sensitive_content(user_query + '\n' + completion_all): print("\n# Sensitive content detected, response has been redacted") else: print(completion_all) From 26b29d950a1a5872417fd1b6440638edec91ea38 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Fri, 14 Jul 2023 10:10:33 +0530 Subject: [PATCH 10/11] Update scripts/bash_setup.sh Co-authored-by: Amol Pawar <89975882+amolpawarap@users.noreply.github.com> --- scripts/bash_setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index 0028647..7f0f5fc 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -77,7 +77,7 @@ validateSettings() if [ -z "$DEPLOYMENT_FOUND" ]; then echo "ERROR" echo "Cannot find Azure OpenAI deployment engine: $ENGINE_ID" - echo "Please check the Azure OpenAI deployment engine id (hhttps://portal.azure.com)." + echo "Please check the Azure OpenAI deployment engine id (https://portal.azure.com)." echo "*************" exitScript return From bc0a2824281ccb12326118ad7e18bfbc507a2943 Mon Sep 17 00:00:00 2001 From: Sameer Mahajan Date: Fri, 4 Aug 2023 12:40:59 +0530 Subject: [PATCH 11/11] add Azure Open AI support for Mac / zsh --- scripts/zsh_setup.sh | 75 +++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/scripts/zsh_setup.sh b/scripts/zsh_setup.sh index 312fc0f..b9fc578 100755 --- a/scripts/zsh_setup.sh +++ b/scripts/zsh_setup.sh @@ -6,6 +6,7 @@ # -o: Your OpenAI organization id. # -k: Your OpenAI API key. # -e: The OpenAI engine id that provides access to a model. +# -a: Use Azure Open AI. # # For example: # ./zsh_setup.sh -o -k -e @@ -15,26 +16,50 @@ set -e # Call OpenAI API with the given settings to verify everythin is in order validateSettings() { - echo -n "*** Testing Open AI access... " - local TEST=$(curl -s 'https://api.openai.com/v1/engines' -H "Authorization: Bearer $secret" -H "OpenAI-Organization: $orgId" -w '%{http_code}') - local STATUS_CODE=$(echo "$TEST"|tail -n 1) - if [ $STATUS_CODE -ne 200 ]; then - echo "ERROR [$STATUS_CODE]" - echo "Failed to access OpenAI API, result: $STATUS_CODE" - echo "Please check your OpenAI API key (https://beta.openai.com/account/api-keys)" - echo "and Organization ID (https://beta.openai.com/account/org-settings)." - echo "*************" - - exit 1 - fi - local ENGINE_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$engineId\"") - if [ -z "$ENGINE_FOUND" ]; then - echo "ERROR" - echo "Cannot find OpenAI engine: $engineId" - echo "Please check the OpenAI engine id (https://beta.openai.com/docs/engines/codex-series-private-beta)." - echo "*************" - - exit 1 + if (( ${use_azure} )); then + echo -n "*** Testing Azure Open AI access... " + URL="$orgId/openai/deployments?api-version=$use_azure" + local TEST=$(curl -s $URL -H "api-key: $secret" -w '%{http_code}') + local STATUS_CODE=$(echo "$TEST"|tail -n 1 | sed s'/}//g') + if [ $STATUS_CODE -ne 200 ]; then + echo "ERROR [$STATUS_CODE]" + echo "Failed to access Azure OpenAI API, result: $STATUS_CODE" + echo "Please check your Azure OpenAI Endpoint and API key (https://portal.azure.com)" + echo "*************" + + exit 1 + fi + local DEPLOYMENT_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$engineId\"") + if [ -z "$DEPLOYMENT_FOUND" ]; then + echo "ERROR" + echo "Cannot find Azure OpenAI deployment engine: $engineId" + echo "Please check the Azure OpenAI deployment engine id (https://portal.azure.com)." + echo "*************" + + exit 1 + fi + else + echo -n "*** Testing Open AI access... " + local TEST=$(curl -s 'https://api.openai.com/v1/engines' -H "Authorization: Bearer $secret" -H "OpenAI-Organization: $orgId" -w '%{http_code}') + local STATUS_CODE=$(echo "$TEST"|tail -n 1) + if [ $STATUS_CODE -ne 200 ]; then + echo "ERROR [$STATUS_CODE]" + echo "Failed to access OpenAI API, result: $STATUS_CODE" + echo "Please check your OpenAI API key (https://beta.openai.com/account/api-keys)" + echo "and Organization ID (https://beta.openai.com/account/org-settings)." + echo "*************" + + exit 1 + fi + local ENGINE_FOUND=$(echo "$TEST"|grep '"id"'|grep "\"$engineId\"") + if [ -z "$ENGINE_FOUND" ]; then + echo "ERROR" + echo "Cannot find OpenAI engine: $engineId" + echo "Please check the OpenAI engine id (https://beta.openai.com/docs/engines/codex-series-private-beta)." + echo "*************" + + exit 1 + fi fi echo "OK ***" } @@ -63,6 +88,9 @@ configureApp() echo "organization_id=$orgId" >> $openAIConfigPath echo "secret_key=$secret" >> $openAIConfigPath echo "engine=$engineId" >> $openAIConfigPath + if (( ${use_azure} )); then + echo "use_azure=$use_azure" >> $openAIConfigPath + fi echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" @@ -77,7 +105,8 @@ zmodload zsh/zutil zparseopts -E -D -- \ o:=o_orgId \ e:=o_engineId \ - k:=o_key + k:=o_key \ + a:=o_azure if (( ${+o_orgId[2]} )); then orgId=${o_orgId[2]} @@ -99,6 +128,10 @@ else echo -e "\n" fi +if (( ${+o_azure[2]} )); then + use_azure=${o_azure[2]} +fi + # Detect Codex CLI folder path CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" echo "CODEX_CLI_PATH is $CODEX_CLI_PATH"