From dda89aac2c19f038657d393289f1d2793866deeb Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Sat, 25 Mar 2023 07:04:41 +0200 Subject: [PATCH 1/7] Added azure support --- scripts/zsh_setup_azure.sh | 148 +++++++++++++++++++++++++++++++++++++ src/codex_query.py | 5 +- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100755 scripts/zsh_setup_azure.sh diff --git a/scripts/zsh_setup_azure.sh b/scripts/zsh_setup_azure.sh new file mode 100755 index 0000000..8c6d81f --- /dev/null +++ b/scripts/zsh_setup_azure.sh @@ -0,0 +1,148 @@ +#!/bin/zsh +# +# A shell script to setup Codex CLI for zsh +# +# You can pass the following arguments to the script: +# -o: Your OpenAI organization id. +# -k: Your OpenAI API key. +# -e: The OpenAI engine id that provides access to a model. +# +# For example: +# ./zsh_setup.sh -o -k -e +# +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 TEST=$(curl -s 'https://jarvis-openai.openai.azure.com/' -H "api-key: $secret" -w '%{http_code}') + local TEST=$(curl -s 'https://jarvis-openai-eastus.openai.azure.com/openai/deployments/code-davinci/completions?api-version=2022-12-01' \ + -H "Content-Type: application/json" \ + -H "api-key: $secret" \ + -d '{ + "prompt": "Explain what the below SQL query does. Also answer why someone might be interested in this time period, and why a company might be interested in this SQL query.\n\nDetails: Temperature = 0.8; MaxTokens = 150+; Stop sequences: #\nSELECT c.customer_id\nFROM Customers c\nJOIN Streaming s\nON c.customer_id = s.customer_id\nWHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'\nAND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)\nGROUP BY c.customer_id\nHAVING SUM(s.watch_minutes) > 50 * 60\n\nExplanation:", + "max_tokens": 250, + "temperature": 0.7, + "frequency_penalty": 0, + "presence_penalty": 0, + "top_p": 1, + "best_of": 1, + "stop": null +}' -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 + echo "OK ***" +} + +# Append settings and 'Ctrl + G' binding in .zshrc +configureZsh() +{ + # Remove previous settings + sed -i '' '/### Codex CLI setup - start/,/### Codex CLI setup - end/d' $zshrcPath + echo "Removed previous settings in $zshrcPath if present" + + # Update the latest settings + echo "### Codex CLI setup - start" >> $zshrcPath + echo "export CODEX_CLI_PATH=$CODEX_CLI_PATH" >> $zshrcPath + echo "source \"\$CODEX_CLI_PATH/scripts/zsh_plugin.zsh\"" >> $zshrcPath + echo "bindkey '^G' create_completion" >> $zshrcPath + echo "### Codex CLI setup - end" >> $zshrcPath + + echo "Added settings in $zshrcPath" +} + +# Store API key and other settings in `openaiapirc` +configureApp() +{ + echo "[openai]" > $openAIConfigPath + echo "organization_id=$orgId" >> $openAIConfigPath + echo "secret_key=$secret" >> $openAIConfigPath + echo "engine=$engineId" >> $openAIConfigPath + echo "type=$type" >> $openAIConfigPath + + echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" + + # Change file mode of codex_query.py to allow execution + chmod +x "$CODEX_CLI_PATH/src/codex_query.py" + echo "Allow execution of $CODEX_CLI_PATH/src/codex_query.py" +} + +# Start installation +# Use zparseopts to parse parameters +zmodload zsh/zutil +zparseopts -E -D -- \ + o:=o_orgId \ + e:=o_engineId \ + k:=o_key \ + t:=o_type \ + b:=o_baseUrl + +if (( ${+o_orgId[2]} )); then + orgId=${o_orgId[2]} +else + echo -n 'OpenAI Organization Id: '; read orgId +fi + +if (( ${+o_engineId[2]} )); then + engineId=${o_engineId[2]} +else + echo -n 'OpenAI Engine Id: '; read engineId +fi + +if (( ${+o_key[2]} )); then + secret=${o_key[2]} +else + # Prompt user for OpenAI access key + read -rs 'secret?OpenAI access key:' + echo -e "\n" +fi + +if (( ${+o_type[2]} )); then + type=${o_type[2]} +else + echo -n 'API type: '; read type +fi + +if (( ${+o_baseUrl[2]} )); then + baseUrl=${o_baseUrl[2]} +else + echo -n 'API base url: '; read baseUrl +fi + +# Detect Codex CLI folder path +CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" +echo "CODEX_CLI_PATH is $CODEX_CLI_PATH" + +validateSettings + +openAIConfigPath="$CODEX_CLI_PATH/src/openaiapirc" +zshrcPath="$HOME/.zshrc" + +configureZsh +configureApp + +echo -e "*** Setup complete! ***\n"; + +echo "***********************************************" +echo "Open a new zsh terminal, type '#' followed by" +echo "your natural language command and hit Ctrl + G!" +echo "***********************************************" \ No newline at end of file diff --git a/src/codex_query.py b/src/codex_query.py index ade2a9a..4e54860 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -58,7 +58,10 @@ def initialize(): config.read(API_KEYS_LOCATION) openai.api_key = config['openai']['secret_key'].strip('"').strip("'") - openai.organization = config['openai']['organization_id'].strip('"').strip("'") + openai.api_version = "2022-12-01" + #openai.organization = config['openai']['organization_id'].strip('"').strip("'") + openai.api_type = "azure" + openai.api_base = "https://jarvis-openai.openai.azure.com/" ENGINE = config['openai']['engine'].strip('"').strip("'") prompt_config = { From 7ab5def524aea5b86f2c7a84c96e340b6056a6cc Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Sat, 25 Mar 2023 08:29:47 +0200 Subject: [PATCH 2/7] Disabled sensitivity check for now --- scripts/zsh_setup_azure.sh | 73 ++++++++++++++------------------------ src/codex_query.py | 26 ++++++++++---- 2 files changed, 47 insertions(+), 52 deletions(-) diff --git a/scripts/zsh_setup_azure.sh b/scripts/zsh_setup_azure.sh index 8c6d81f..2d4de58 100755 --- a/scripts/zsh_setup_azure.sh +++ b/scripts/zsh_setup_azure.sh @@ -3,12 +3,11 @@ # A shell script to setup Codex CLI for zsh # # You can pass the following arguments to the script: -# -o: Your OpenAI organization id. -# -k: Your OpenAI API key. +# -k: Your Azure OpenAI API key. # -e: The OpenAI engine id that provides access to a model. # # For example: -# ./zsh_setup.sh -o -k -e +# ./zsh_setup.sh -k -e # set -e @@ -16,21 +15,19 @@ set -e 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 TEST=$(curl -s 'https://jarvis-openai.openai.azure.com/' -H "api-key: $secret" -w '%{http_code}') - local TEST=$(curl -s 'https://jarvis-openai-eastus.openai.azure.com/openai/deployments/code-davinci/completions?api-version=2022-12-01' \ - -H "Content-Type: application/json" \ - -H "api-key: $secret" \ - -d '{ - "prompt": "Explain what the below SQL query does. Also answer why someone might be interested in this time period, and why a company might be interested in this SQL query.\n\nDetails: Temperature = 0.8; MaxTokens = 150+; Stop sequences: #\nSELECT c.customer_id\nFROM Customers c\nJOIN Streaming s\nON c.customer_id = s.customer_id\nWHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'\nAND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)\nGROUP BY c.customer_id\nHAVING SUM(s.watch_minutes) > 50 * 60\n\nExplanation:", - "max_tokens": 250, - "temperature": 0.7, - "frequency_penalty": 0, - "presence_penalty": 0, - "top_p": 1, - "best_of": 1, - "stop": null -}' -w '%{http_code}') + local TEST=$(curl -s $baseUrl"openai/deployments/$engineId/completions?api-version=$apiVersion" \ + -H "Content-Type: application/json" \ + -H "api-key: $secret" \ + -d '{ + "prompt": "This is a test", + "max_tokens": 250, + "temperature": 0.7, + "frequency_penalty": 0, + "presence_penalty": 0, + "top_p": 1, + "best_of": 1, + "stop": null + }' -w '%{http_code}') local STATUS_CODE=$(echo "$TEST"|tail -n 1) if [ $STATUS_CODE -ne 200 ]; then echo "ERROR [$STATUS_CODE]" @@ -41,15 +38,6 @@ validateSettings() 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 echo "OK ***" } @@ -74,10 +62,10 @@ configureZsh() configureApp() { echo "[openai]" > $openAIConfigPath - echo "organization_id=$orgId" >> $openAIConfigPath echo "secret_key=$secret" >> $openAIConfigPath echo "engine=$engineId" >> $openAIConfigPath - echo "type=$type" >> $openAIConfigPath + echo "base_url=$baseUrl" >> $openAIConfigPath + echo "api_version=$apiVersion" >> $openAIConfigPath echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" @@ -90,17 +78,10 @@ configureApp() # Use zparseopts to parse parameters zmodload zsh/zutil zparseopts -E -D -- \ - o:=o_orgId \ e:=o_engineId \ k:=o_key \ - t:=o_type \ - b:=o_baseUrl - -if (( ${+o_orgId[2]} )); then - orgId=${o_orgId[2]} -else - echo -n 'OpenAI Organization Id: '; read orgId -fi + b:=o_baseUrl \ + a:=o_apiVersion if (( ${+o_engineId[2]} )); then engineId=${o_engineId[2]} @@ -111,23 +92,23 @@ fi if (( ${+o_key[2]} )); then secret=${o_key[2]} else - # Prompt user for OpenAI access key - read -rs 'secret?OpenAI access key:' + # Prompt user for Azure OpenAI access key + read -rs 'secret?Azure OpenAI access key:' echo -e "\n" fi -if (( ${+o_type[2]} )); then - type=${o_type[2]} -else - echo -n 'API type: '; read type -fi - if (( ${+o_baseUrl[2]} )); then baseUrl=${o_baseUrl[2]} else echo -n 'API base url: '; read baseUrl fi +if (( ${+o_apiVersion[2]} )); then + apiVersion=${o_apiVersion[2]} +else + echo -n 'API version: '; read apiVersion +fi + # Detect Codex CLI folder path CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" echo "CODEX_CLI_PATH is $CODEX_CLI_PATH" diff --git a/src/codex_query.py b/src/codex_query.py index 4e54860..97c4d00 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -33,6 +33,8 @@ # organization= # secret_key= # engine= +# api_version= +# base_url= def create_template_ini_file(): """ If the ini file does not exist create it and add secret_key @@ -44,6 +46,8 @@ def create_template_ini_file(): print('# organization_id=') print('# secret_key=\n') print('# engine=') + print('# api_version=') + print('# base_url=\n') sys.exit(1) def initialize(): @@ -58,10 +62,15 @@ def initialize(): config.read(API_KEYS_LOCATION) openai.api_key = config['openai']['secret_key'].strip('"').strip("'") - openai.api_version = "2022-12-01" - #openai.organization = config['openai']['organization_id'].strip('"').strip("'") - openai.api_type = "azure" - openai.api_base = "https://jarvis-openai.openai.azure.com/" + base_url = config['openai']['base_url'].strip('"').strip("'") + if base_url == "": + openai.organization = config['openai']['organization_id'].strip('"').strip("'") + else: + if DEBUG_MODE: + print("Using Azure OpenAI") + openai.api_type = "azure" + openai.api_version = config['openai']['api_version'].strip('"').strip("'") + openai.api_base = config['openai']['base_url'].strip('"').strip("'") ENGINE = config['openai']['engine'].strip('"').strip("'") prompt_config = { @@ -204,11 +213,16 @@ 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="#") + 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 (False): #s_sensitive_content(user_query + '\n' + completion_all): print("\n# Sensitive content detected, response has been redacted") else: print(completion_all) From fd848140dc81e620dc61fa788b4a6f2b34711b81 Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Sat, 25 Mar 2023 08:36:45 +0200 Subject: [PATCH 3/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f5cddd..007b818 100644 --- a/README.md +++ b/README.md @@ -137,4 +137,4 @@ You might have access to different [OpenAI engines](https://beta.openai.com/docs ``` ### Can I run the sample on Azure? -The sample code can be currently be used with Codex on OpenAI’s API. In the coming months, the sample will be updated so you can also use it with the [Azure OpenAI Service](https://aka.ms/azure-openai). +In order to use this with [Azure OpenAI Service](https://aka.ms/azure-openai), you can run zsh_setup_azure.sh. Bash and powershell will be included in future. From e783175c5261e02eabc03a05266fe23dca09ed25 Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Thu, 11 May 2023 12:16:01 +0200 Subject: [PATCH 4/7] simplified setup --- scripts/zsh_setup.sh | 92 +++++++++++++++++++++++--- scripts/zsh_setup_azure.sh | 129 ------------------------------------- src/codex_query.py | 35 ++++++---- 3 files changed, 103 insertions(+), 153 deletions(-) delete mode 100755 scripts/zsh_setup_azure.sh diff --git a/scripts/zsh_setup.sh b/scripts/zsh_setup.sh index 312fc0f..df5b9c7 100755 --- a/scripts/zsh_setup.sh +++ b/scripts/zsh_setup.sh @@ -39,11 +39,45 @@ validateSettings() echo "OK ***" } +# Call Azure OpenAI API with the given settings to verify everythin is in order +validateAzureSettings() +{ + echo -n "*** Testing Open AI access... " + local TEST=$(curl -s $apiBase"openai/deployments/$modelDeployment/completions?api-version=$apiVersion" \ + -H "Content-Type: application/json" \ + -H "api-key: $secret" \ + -d '{ + "prompt": "This is a test", + "max_tokens": 250, + "temperature": 0.7, + "frequency_penalty": 0, + "presence_penalty": 0, + "top_p": 1, + "best_of": 1, + "stop": null + }' -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 Azure OpenAI API key" + echo "and Endpoint URL." + echo "*************" + + exit 1 + fi + echo "OK ***" +} + # Append settings and 'Ctrl + G' binding in .zshrc configureZsh() { # Remove previous settings - sed -i '' '/### Codex CLI setup - start/,/### Codex CLI setup - end/d' $zshrcPath + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' '/### Codex CLI setup - start/,/### Codex CLI setup - end/d' $zshrcPath + else + sed -i -e '/### Codex CLI setup - start/,/### Codex CLI setup - end/d' $zshrcPath + fi echo "Removed previous settings in $zshrcPath if present" # Update the latest settings @@ -63,6 +97,11 @@ configureApp() echo "organization_id=$orgId" >> $openAIConfigPath echo "secret_key=$secret" >> $openAIConfigPath echo "engine=$engineId" >> $openAIConfigPath + # Azure specific settings + echo "model_deployment=$modelDeployment" >> $openAIConfigPath + echo "api_base=$apiBase" >> $openAIConfigPath + echo "api_version=$apiVersion" >> $openAIConfigPath + echo "api_type=$apiType" >> $openAIConfigPath echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" @@ -77,18 +116,47 @@ zmodload zsh/zutil zparseopts -E -D -- \ o:=o_orgId \ e:=o_engineId \ - k:=o_key - -if (( ${+o_orgId[2]} )); then - orgId=${o_orgId[2]} + k:=o_key \ + a:=o_apiType \ + m:=o_modelDeployment \ + p:=o_apiBase \ + v:=o_apiVersion + +if (( ${+o_apiType[2]} )); then + apiType=${o_apiType[2]} else - echo -n 'OpenAI Organization Id: '; read orgId + echo -n 'API type (openapi/azure):'; read apiType fi -if (( ${+o_engineId[2]} )); then - engineId=${o_engineId[2]} +# Check for Azure API settings if apiType is azure +if [[ "$apiType" == "azure" ]]; then + if (( ${+o_modelDeployment[2]} )); then + modelDeployment=${o_modelDeployment[2]} + else + echo -n 'Azure Model Deployment: '; read modelDeployment + fi + if (( ${+o_apiBase[2]} )); then + apiBase=${o_apiBase[2]} + else + echo -n 'Azure API Endpoint: '; read apiBase + fi + if (( ${+o_apiVersion[2]} )); then + apiVersion=${o_apiVersion[2]} + else + echo -n 'Azure API Version: '; read apiVersion + fi else - echo -n 'OpenAI Engine Id: '; read engineId + if (( ${+o_orgId[2]} )); then + orgId=${o_orgId[2]} + else + echo -n 'OpenAI Organization Id: '; read orgId + fi + + if (( ${+o_engineId[2]} )); then + engineId=${o_engineId[2]} + else + echo -n 'OpenAI Engine Id: '; read engineId + fi fi if (( ${+o_key[2]} )); then @@ -103,7 +171,11 @@ fi CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" echo "CODEX_CLI_PATH is $CODEX_CLI_PATH" -validateSettings +if [[ "$useAzure" == "y" ]]; then + validateAzureSettings +else + validateSettings +fi openAIConfigPath="$CODEX_CLI_PATH/src/openaiapirc" zshrcPath="$HOME/.zshrc" diff --git a/scripts/zsh_setup_azure.sh b/scripts/zsh_setup_azure.sh deleted file mode 100755 index 2d4de58..0000000 --- a/scripts/zsh_setup_azure.sh +++ /dev/null @@ -1,129 +0,0 @@ -#!/bin/zsh -# -# A shell script to setup Codex CLI for zsh -# -# You can pass the following arguments to the script: -# -k: Your Azure OpenAI API key. -# -e: The OpenAI engine id that provides access to a model. -# -# For example: -# ./zsh_setup.sh -k -e -# -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 $baseUrl"openai/deployments/$engineId/completions?api-version=$apiVersion" \ - -H "Content-Type: application/json" \ - -H "api-key: $secret" \ - -d '{ - "prompt": "This is a test", - "max_tokens": 250, - "temperature": 0.7, - "frequency_penalty": 0, - "presence_penalty": 0, - "top_p": 1, - "best_of": 1, - "stop": null - }' -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 - echo "OK ***" -} - -# Append settings and 'Ctrl + G' binding in .zshrc -configureZsh() -{ - # Remove previous settings - sed -i '' '/### Codex CLI setup - start/,/### Codex CLI setup - end/d' $zshrcPath - echo "Removed previous settings in $zshrcPath if present" - - # Update the latest settings - echo "### Codex CLI setup - start" >> $zshrcPath - echo "export CODEX_CLI_PATH=$CODEX_CLI_PATH" >> $zshrcPath - echo "source \"\$CODEX_CLI_PATH/scripts/zsh_plugin.zsh\"" >> $zshrcPath - echo "bindkey '^G' create_completion" >> $zshrcPath - echo "### Codex CLI setup - end" >> $zshrcPath - - echo "Added settings in $zshrcPath" -} - -# Store API key and other settings in `openaiapirc` -configureApp() -{ - echo "[openai]" > $openAIConfigPath - echo "secret_key=$secret" >> $openAIConfigPath - echo "engine=$engineId" >> $openAIConfigPath - echo "base_url=$baseUrl" >> $openAIConfigPath - echo "api_version=$apiVersion" >> $openAIConfigPath - - echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" - - # Change file mode of codex_query.py to allow execution - chmod +x "$CODEX_CLI_PATH/src/codex_query.py" - echo "Allow execution of $CODEX_CLI_PATH/src/codex_query.py" -} - -# Start installation -# Use zparseopts to parse parameters -zmodload zsh/zutil -zparseopts -E -D -- \ - e:=o_engineId \ - k:=o_key \ - b:=o_baseUrl \ - a:=o_apiVersion - -if (( ${+o_engineId[2]} )); then - engineId=${o_engineId[2]} -else - echo -n 'OpenAI Engine Id: '; read engineId -fi - -if (( ${+o_key[2]} )); then - secret=${o_key[2]} -else - # Prompt user for Azure OpenAI access key - read -rs 'secret?Azure OpenAI access key:' - echo -e "\n" -fi - -if (( ${+o_baseUrl[2]} )); then - baseUrl=${o_baseUrl[2]} -else - echo -n 'API base url: '; read baseUrl -fi - -if (( ${+o_apiVersion[2]} )); then - apiVersion=${o_apiVersion[2]} -else - echo -n 'API version: '; read apiVersion -fi - -# Detect Codex CLI folder path -CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" -echo "CODEX_CLI_PATH is $CODEX_CLI_PATH" - -validateSettings - -openAIConfigPath="$CODEX_CLI_PATH/src/openaiapirc" -zshrcPath="$HOME/.zshrc" - -configureZsh -configureApp - -echo -e "*** Setup complete! ***\n"; - -echo "***********************************************" -echo "Open a new zsh terminal, type '#' followed by" -echo "your natural language command and hit Ctrl + G!" -echo "***********************************************" \ No newline at end of file diff --git a/src/codex_query.py b/src/codex_query.py index 97c4d00..7e30c4c 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -33,8 +33,11 @@ # organization= # secret_key= # engine= +# use_azure= +# model_deployment= # api_version= -# base_url= +# api_base= + def create_template_ini_file(): """ If the ini file does not exist create it and add secret_key @@ -43,11 +46,13 @@ def create_template_ini_file(): print('# Please create a file at {} and add your secret key'.format(API_KEYS_LOCATION)) print('# The format is:\n') print('# [openai]') - print('# organization_id=') + print('# organization_id=\n') print('# secret_key=\n') - print('# engine=') - print('# api_version=') - print('# base_url=\n') + print('# engine=\n') + print('# use_azure=\n') + print('# model_deployment=\n') + print('# api_version=\n') + print('# api_base=\n') sys.exit(1) def initialize(): @@ -62,16 +67,18 @@ def initialize(): config.read(API_KEYS_LOCATION) openai.api_key = config['openai']['secret_key'].strip('"').strip("'") - base_url = config['openai']['base_url'].strip('"').strip("'") - if base_url == "": - openai.organization = config['openai']['organization_id'].strip('"').strip("'") - else: - if DEBUG_MODE: - print("Using Azure OpenAI") - openai.api_type = "azure" + openai.api_type = config['openai']['api_type'].strip('"').strip("'") + if openai.api_type == "azure": + openai.api_base = config['openai']['api_base'].strip('"').strip("'") openai.api_version = config['openai']['api_version'].strip('"').strip("'") - openai.api_base = config['openai']['base_url'].strip('"').strip("'") - ENGINE = config['openai']['engine'].strip('"').strip("'") + ENGINE = config['openai']['model_deployment'].strip('"').strip("'") + print("Using Azure API") + print("API Base: " + openai.api_base) + print("API Version: " + openai.api_version) + print("Model Deployment: " + ENGINE) + else: + openai.organization = config['openai']['organization_id'].strip('"').strip("'") + ENGINE = config['openai']['engine'].strip('"').strip("'") prompt_config = { 'engine': ENGINE, From c9e08ffc0dcdc835e904a4c986180cd154f537c4 Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Thu, 11 May 2023 12:28:17 +0200 Subject: [PATCH 5/7] removed debug prints --- src/codex_query.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/codex_query.py b/src/codex_query.py index 7e30c4c..96c0c6f 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -72,10 +72,6 @@ def initialize(): openai.api_base = config['openai']['api_base'].strip('"').strip("'") openai.api_version = config['openai']['api_version'].strip('"').strip("'") ENGINE = config['openai']['model_deployment'].strip('"').strip("'") - print("Using Azure API") - print("API Base: " + openai.api_base) - print("API Version: " + openai.api_version) - print("Model Deployment: " + ENGINE) else: openai.organization = config['openai']['organization_id'].strip('"').strip("'") ENGINE = config['openai']['engine'].strip('"').strip("'") From e09374e384f4b149e9f93ff1bd6153ea051cf2f1 Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Thu, 11 May 2023 13:39:21 +0200 Subject: [PATCH 6/7] bash and Zsh enabled for Azure --- scripts/bash_setup.sh | 79 ++++++++++++++++++++++++++++++++++++++----- scripts/zsh_setup.sh | 6 +++- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index acf228c..53bda51 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -10,9 +10,13 @@ Help for Codex CLI Bash setup script Usage: source bash_setup.sh [optional parameters] - -o orgId Set the OpenAI organization id. - -k apiKey Set the OpenAI API key. - -e engineId Set the OpenAI engine id. + -o orgId Set the OpenAI organization id. + -k apiKey Set the OpenAI API key. + -e engineId Set the OpenAI engine id. + -a apiType Set the API type (openapi/azure). + -m modelDeployment Set the Azure model deployment. + -p apiBase Set the Azure API endpoint. + -v apiVersion Set the Azure API version. -d Print some system information for debugging. -h Print this help content. @@ -29,6 +33,10 @@ readParameters() -o ) shift; ORG_ID=$1 ;; -k ) shift; SECRET_KEY=$1 ;; -e ) shift; ENGINE_ID=$1 ;; + -a ) shift; API_TYPE=$1 ;; + -m ) shift; MODEL_DEPLOYMENT=$1 ;; + -p ) shift; API_BASE=$1 ;; + -v ) shift; API_VERSION=$1 ;; -d ) systemInfo exitScript ;; @@ -44,15 +52,30 @@ readParameters() askSettings() { echo "*** Starting Codex CLI bash setup ***" - if [ -z "$ORG_ID" ]; then - echo -n 'OpenAI Organization Id: '; read ORG_ID + if [ -z "$API_TYPE" ]; then + echo -n 'OpenAI API type (openapi/azure): '; read API_TYPE + fi + if [ "$API_TYPE" == "azure" ]; then + if [ -z "$MODEL_DEPLOYMENT" ]; then + echo -n 'Azure model deployment: '; read MODEL_DEPLOYMENT + fi + if [ -z "$API_BASE" ]; then + echo -n 'Azure API endpoint: '; read API_BASE + fi + if [ -z "$API_VERSION" ]; then + echo -n 'Azure API version: '; read API_VERSION + fi + else + if [ -z "$ORG_ID" ]; then + echo -n 'OpenAI Organization Id: '; read ORG_ID + fi + if [ -z "$ENGINE_ID" ]; then + echo -n 'OpenAI Engine Id: '; read ENGINE_ID + fi fi if [ -z "$SECRET_KEY" ]; then echo -n 'OpenAI API key: '; read -s SECRET_KEY; echo fi - if [ -z "$ENGINE_ID" ]; then - echo -n 'OpenAI Engine Id: '; read ENGINE_ID - fi } # Call OpenAI API with the given settings to verify everythin is in order @@ -82,6 +105,36 @@ validateSettings() echo "OK ***" } +# Call Azure OpenAI API with the given settings to verify everythin is in order +validateAzureSettings() +{ + echo -n "*** Testing Open AI access... " + local TEST=$(curl -s $API_BASE "openai/deployments/$MODEL_DEPLOYMENT /completions?api-version=$API_VERSION" \ + -H "Content-Type: application/json" \ + -H "api-key: $SECRET_KEY" \ + -d '{ + "prompt": "This is a test", + "max_tokens": 250, + "temperature": 0.7, + "frequency_penalty": 0, + "presence_penalty": 0, + "top_p": 1, + "best_of": 1, + "stop": null + }' -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 Azure OpenAI API key" + echo "and Endpoint URL." + echo "*************" + + exit 1 + fi + echo "OK ***" +} + # Store API key and other settings in `openaiapirc` configureApp() { @@ -90,6 +143,10 @@ configureApp() echo "organization_id=$ORG_ID" >> $OPENAI_RC_FILE echo "secret_key=$SECRET_KEY" >> $OPENAI_RC_FILE echo "engine=$ENGINE_ID" >> $OPENAI_RC_FILE + echo "api_type=$API_TYPE" >> $OPENAI_RC_FILE + echo "model_deployment=$MODEL_DEPLOYMENT" >> $OPENAI_RC_FILE + echo "api_base=$API_BASE" >> $OPENAI_RC_FILE + echo "api_version=$API_VERSION" >> $OPENAI_RC_FILE chmod +x "$CODEX_CLI_PATH/src/codex_query.py" } @@ -173,7 +230,11 @@ BASH_RC_FILE="$HOME/.codexclirc" # Start installation readParameters $* askSettings -validateSettings +if [ "$API_TYPE" == "azure" ]; then + validateAzureSettings +else + validateSettings +fi configureApp configureBash enableApp diff --git a/scripts/zsh_setup.sh b/scripts/zsh_setup.sh index df5b9c7..2998db4 100755 --- a/scripts/zsh_setup.sh +++ b/scripts/zsh_setup.sh @@ -6,9 +6,13 @@ # -o: Your OpenAI organization id. # -k: Your OpenAI API key. # -e: The OpenAI engine id that provides access to a model. +# -a: The API type (openapi/azure). +# -m: The Azure model deployment. +# -p: The Azure API endpoint. +# -v: The Azure API version. # # For example: -# ./zsh_setup.sh -o -k -e +# ./zsh_setup.sh -o -k -e -a -m -p -v # set -e From c2c29bf5e9f0159b3ab9de25dfd733736b3949d9 Mon Sep 17 00:00:00 2001 From: Yusuf Kaka Date: Thu, 11 May 2023 14:40:13 +0200 Subject: [PATCH 7/7] cleaned up variables --- README.md | 4 ++-- scripts/bash_setup.sh | 26 ++++++++++++-------------- scripts/zsh_setup.sh | 30 +++++++++++------------------- src/codex_query.py | 5 +---- 4 files changed, 26 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 007b818..7162b53 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Codex CLI - Natural Language Command Line Interface -This project uses [GPT-3 Codex](https://openai.com/blog/openai-codex/) to convert natural language commands into commands in PowerShell, Z shell and Bash. +This project uses [GPT-3 Codex](https://openai.com/blog/openai-codex/) or custom Azure deployed models to convert natural language commands into commands in PowerShell, Z shell and Bash. ![Codex Cli GIF](codex_cli.gif) @@ -121,7 +121,7 @@ You might have access to different [OpenAI engines](https://beta.openai.com/docs ``` curl https://api.openai.com/v1/engines \ -H 'Authorization: Bearer YOUR_API_KEY' \ - -H 'OpenAI-Organization: YOUR_ORG_ID' + -H 'OpenAI-Organization: YOUR_ORG_ID' ``` * PowerShell diff --git a/scripts/bash_setup.sh b/scripts/bash_setup.sh index 53bda51..0d4b00d 100755 --- a/scripts/bash_setup.sh +++ b/scripts/bash_setup.sh @@ -12,9 +12,8 @@ Usage: source bash_setup.sh [optional parameters] -o orgId Set the OpenAI organization id. -k apiKey Set the OpenAI API key. - -e engineId Set the OpenAI engine id. + -e engineId Set the OpenAI engine id or Azure model deployment. -a apiType Set the API type (openapi/azure). - -m modelDeployment Set the Azure model deployment. -p apiBase Set the Azure API endpoint. -v apiVersion Set the Azure API version. -d Print some system information for debugging. @@ -56,9 +55,6 @@ askSettings() echo -n 'OpenAI API type (openapi/azure): '; read API_TYPE fi if [ "$API_TYPE" == "azure" ]; then - if [ -z "$MODEL_DEPLOYMENT" ]; then - echo -n 'Azure model deployment: '; read MODEL_DEPLOYMENT - fi if [ -z "$API_BASE" ]; then echo -n 'Azure API endpoint: '; read API_BASE fi @@ -69,9 +65,9 @@ askSettings() if [ -z "$ORG_ID" ]; then echo -n 'OpenAI Organization Id: '; read ORG_ID fi - if [ -z "$ENGINE_ID" ]; then - echo -n 'OpenAI Engine Id: '; read ENGINE_ID - fi + fi + if [ -z "$ENGINE_ID" ]; then + echo -n 'OpenAI Engine Id or Azure model deployment: '; read ENGINE_ID fi if [ -z "$SECRET_KEY" ]; then echo -n 'OpenAI API key: '; read -s SECRET_KEY; echo @@ -109,7 +105,7 @@ validateSettings() validateAzureSettings() { echo -n "*** Testing Open AI access... " - local TEST=$(curl -s $API_BASE "openai/deployments/$MODEL_DEPLOYMENT /completions?api-version=$API_VERSION" \ + local TEST=$(curl -s $API_BASE "openai/deployments/$ENGINE_ID /completions?api-version=$API_VERSION" \ -H "Content-Type: application/json" \ -H "api-key: $SECRET_KEY" \ -d '{ @@ -140,13 +136,15 @@ configureApp() { echo "*** Configuring application [$OPENAI_RC_FILE] ***" echo '[openai]' > $OPENAI_RC_FILE - echo "organization_id=$ORG_ID" >> $OPENAI_RC_FILE echo "secret_key=$SECRET_KEY" >> $OPENAI_RC_FILE echo "engine=$ENGINE_ID" >> $OPENAI_RC_FILE - echo "api_type=$API_TYPE" >> $OPENAI_RC_FILE - echo "model_deployment=$MODEL_DEPLOYMENT" >> $OPENAI_RC_FILE - echo "api_base=$API_BASE" >> $OPENAI_RC_FILE - echo "api_version=$API_VERSION" >> $OPENAI_RC_FILE + if [ "$API_TYPE" == "azure" ]; then + echo "api_type=$API_TYPE" >> $OPENAI_RC_FILE + echo "api_base=$API_BASE" >> $OPENAI_RC_FILE + echo "api_version=$API_VERSION" >> $OPENAI_RC_FILE + else + echo "organization_id=$ORG_ID" >> $OPENAI_RC_FILE + fi chmod +x "$CODEX_CLI_PATH/src/codex_query.py" } diff --git a/scripts/zsh_setup.sh b/scripts/zsh_setup.sh index 2998db4..0e3bcaa 100755 --- a/scripts/zsh_setup.sh +++ b/scripts/zsh_setup.sh @@ -47,7 +47,7 @@ validateSettings() validateAzureSettings() { echo -n "*** Testing Open AI access... " - local TEST=$(curl -s $apiBase"openai/deployments/$modelDeployment/completions?api-version=$apiVersion" \ + local TEST=$(curl -s $apiBase"openai/deployments/$engineId/completions?api-version=$apiVersion" \ -H "Content-Type: application/json" \ -H "api-key: $secret" \ -d '{ @@ -98,15 +98,15 @@ configureZsh() configureApp() { echo "[openai]" > $openAIConfigPath - echo "organization_id=$orgId" >> $openAIConfigPath echo "secret_key=$secret" >> $openAIConfigPath echo "engine=$engineId" >> $openAIConfigPath # Azure specific settings - echo "model_deployment=$modelDeployment" >> $openAIConfigPath - echo "api_base=$apiBase" >> $openAIConfigPath - echo "api_version=$apiVersion" >> $openAIConfigPath - echo "api_type=$apiType" >> $openAIConfigPath - + if [[ "$apiType" == "azure" ]]; then + echo "api_base=$apiBase" >> $openAIConfigPath + echo "api_version=$apiVersion" >> $openAIConfigPath + echo "api_type=$apiType" >> $openAIConfigPath + else + echo "organization_id=$orgId" >> $openAIConfigPath echo "Updated OpenAI configuration file ($openAIConfigPath) with secrets" # Change file mode of codex_query.py to allow execution @@ -122,7 +122,6 @@ zparseopts -E -D -- \ e:=o_engineId \ k:=o_key \ a:=o_apiType \ - m:=o_modelDeployment \ p:=o_apiBase \ v:=o_apiVersion @@ -134,11 +133,6 @@ fi # Check for Azure API settings if apiType is azure if [[ "$apiType" == "azure" ]]; then - if (( ${+o_modelDeployment[2]} )); then - modelDeployment=${o_modelDeployment[2]} - else - echo -n 'Azure Model Deployment: '; read modelDeployment - fi if (( ${+o_apiBase[2]} )); then apiBase=${o_apiBase[2]} else @@ -155,14 +149,12 @@ else else echo -n 'OpenAI Organization Id: '; read orgId fi - - if (( ${+o_engineId[2]} )); then +fi +if (( ${+o_engineId[2]} )); then engineId=${o_engineId[2]} else - echo -n 'OpenAI Engine Id: '; read engineId + echo -n 'OpenAI Engine Id or Azure model deployment: '; read engineId fi -fi - if (( ${+o_key[2]} )); then secret=${o_key[2]} else @@ -175,7 +167,7 @@ fi CODEX_CLI_PATH="$( cd "$( dirname "$0" )" && cd .. && pwd )" echo "CODEX_CLI_PATH is $CODEX_CLI_PATH" -if [[ "$useAzure" == "y" ]]; then +if [[ "$apiType" == "azure" ]]; then validateAzureSettings else validateSettings diff --git a/src/codex_query.py b/src/codex_query.py index 96c0c6f..910d0fc 100755 --- a/src/codex_query.py +++ b/src/codex_query.py @@ -34,7 +34,6 @@ # secret_key= # engine= # use_azure= -# model_deployment= # api_version= # api_base= @@ -50,7 +49,6 @@ def create_template_ini_file(): print('# secret_key=\n') print('# engine=\n') print('# use_azure=\n') - print('# model_deployment=\n') print('# api_version=\n') print('# api_base=\n') sys.exit(1) @@ -71,10 +69,9 @@ def initialize(): if openai.api_type == "azure": openai.api_base = config['openai']['api_base'].strip('"').strip("'") openai.api_version = config['openai']['api_version'].strip('"').strip("'") - ENGINE = config['openai']['model_deployment'].strip('"').strip("'") else: openai.organization = config['openai']['organization_id'].strip('"').strip("'") - ENGINE = config['openai']['engine'].strip('"').strip("'") + ENGINE = config['openai']['engine'].strip('"').strip("'") prompt_config = { 'engine': ENGINE,