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

Revise the prompt for simplicity #43

Merged
merged 1 commit into from
Jul 13, 2024
Merged

Revise the prompt for simplicity #43

merged 1 commit into from
Jul 13, 2024

Conversation

johnlk
Copy link
Contributor

@johnlk johnlk commented Jul 13, 2024

I'm now using HEREDOCs and simplified the code a bit.

@johnlk johnlk self-assigned this Jul 13, 2024
Copy link

Score: 85

Improvements:

  • The script lacks a shebang at the top. It's a good practice to include #!/usr/bin/env bash at the top of the script to ensure that it's run using the correct interpreter.
  • Consider making variable names more descriptive to improve readability. For example, instead of api_url, you could use openai_api_endpoint to better describe what the variable is used for.
  • Add comments to your script to make it easier for others to understand what each part of your code does.
  • The code has inconsistent indentation in the jq block, which might lead to confusion. Maintain consistent indentation to improve readability.
  • Hardcoding the API URL 'https://api.openai.com/v1/chat/completions' might not be a good idea as the URL might change in future and that would require a code change. A better way would be to store it in a configuration file or an environment variable.
  • Some variables are unnecessarily marked as readonly (local -r). Readonly variables are those whose value cannot be changed after they are set. Consider removing readonly declaration if the variables are meant to be changed.
  • Handle the API error more gracefully to inform user about what went wrong. Right now it just logs the error message and terminates the execution. It might be better to display an user friendly error message and suggest corrective actions if any.
#!/usr/bin/env bash

API_URL="https://api.openai.com/v1/chat/completions"

INITIAL_PROMPT=$(cat <<EOF
...
EOF
)

gpt::prompt_model() {
  local git_diff="$1"

  local response
  response=$(curl -sSL \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPEN_AI_API_KEY" \
    -d "$(jq -n \
      --arg model "$GPT_MODEL" \
      --arg prompt "$INITIAL_PROMPT" \
      --arg git_diff "$git_diff" \
      '{
        model: $model,
        messages: [
          {role: "user", content: $prompt},
          {role: "user", content: $git_diff}
        ]
      }'
    )" \
    "$API_URL")

  local error
  error=$(jq -r '.error' <<< "$response")

  if [[ "$error" != "null" ]]; then
    echo "API request to 'api.openai.com' failed: $error, Please check your API_KEY and try again"
    return 1
  fi

  jq -r '.choices[0].message.content' <<< "$response"
}

@johnlk johnlk merged commit 5e4a989 into main Jul 13, 2024
1 check passed
@johnlk johnlk deleted the john/revise-prompt branch July 13, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant