Skip to content

Commit

Permalink
fix(rephraser): escape double quotes for JSON in request
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Jan 26, 2024
1 parent 02794fa commit 553a337
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
50 changes: 45 additions & 5 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
<string>⭐️</string>
<key>connections</key>
<dict>
<key>197CBF3E-ACE7-46FC-B02C-FA08363B1BA7</key>
<array>
<dict>
<key>destinationuid</key>
<string>B34DBBCE-C220-4020-BDB7-DFAB47719F7A</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>322E5C7E-0088-48C7-8510-ABB6F13FAF6B</key>
<array>
<dict>
Expand Down Expand Up @@ -142,7 +155,7 @@
<array>
<dict>
<key>destinationuid</key>
<string>B34DBBCE-C220-4020-BDB7-DFAB47719F7A</string>
<string>197CBF3E-ACE7-46FC-B02C-FA08363B1BA7</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
Expand Down Expand Up @@ -501,6 +514,23 @@
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>matchmode</key>
<integer>0</integer>
<key>matchstring</key>
<string>'</string>
<key>replacestring</key>
<string>"</string>
</dict>
<key>type</key>
<string>alfred.workflow.utility.replace</string>
<key>uid</key>
<string>197CBF3E-ACE7-46FC-B02C-FA08363B1BA7</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -560,9 +590,8 @@ The workflow offers four [hotkeys](https://www.alfredapp.com/help/workflows/trig
1. Autocorrect the word under the cursor.
2. Suggest synonyms for the word under the cursor.
3. Rephrase the selected text via ChatGPT, improving its language (requires
OpenAI API key).
4. Same as 3, but use Markdown markup to show the changes:
3. Rephrase the selected text via ChatGPT, improving its language (requires OpenAI API key).
4. Same as 3, but uses Markdown markup to show the changes:
+ Additions are displayed as `==highlights==`.
+ Deletions are displayed as `~~strikethroughs~~`.
+ There is a workflow configuration to alternatively format the changes as [Critic Markup](https://github.com/CriticMarkup/CriticMarkup-toolkit).
Expand All @@ -572,6 +601,17 @@ The workflow offers four [hotkeys](https://www.alfredapp.com/help/workflows/trig
Created by [Chris Grieser](https://chris-grieser.de/).</string>
<key>uidata</key>
<dict>
<key>197CBF3E-ACE7-46FC-B02C-FA08363B1BA7</key>
<dict>
<key>colorindex</key>
<integer>8</integer>
<key>note</key>
<string>single quotes back to double quotes</string>
<key>xpos</key>
<real>625</real>
<key>ypos</key>
<real>405</real>
</dict>
<key>322E5C7E-0088-48C7-8510-ABB6F13FAF6B</key>
<dict>
<key>colorindex</key>
Expand Down Expand Up @@ -761,7 +801,7 @@ DOUBLE CLICK TO EDIT</string>
<true/>
</dict>
<key>description</key>
<string>OpenAI API Key. If empty, will look through `.zshenv` for a variable `$OPENAI_API_KEY`.</string>
<string>OpenAI API Key. If empty, will use $OPENAI_API_KEY exported in .zshenv.</string>
<key>label</key>
<string>Rephrasing</string>
<key>type</key>
Expand Down
19 changes: 14 additions & 5 deletions scripts/rephrase-selection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#───────────────────────────────────────────────────────────────────────────────

selection="$*"
the_prompt="$static_prompt$selection" # WARN `$prompt` is reserved variable in zsh
cache="$alfred_workflow_cache"
mkdir -p "$cache"
model="gpt-3.5-turbo" # https://platform.openai.com/docs/models/gpt-3
Expand All @@ -20,23 +19,33 @@ if [[ -z "$apikey" ]]; then
return 1
fi

# WARN `$prompt` is reserved variable in zsh
# escape quotes in prompt for JSON
the_prompt=$(echo "$static_prompt $selection" | sed -e "s/\"/'/g")

# OPENAI API CALL
# DOCS https://platform.openai.com/docs/api-reference/making-requests
response=$(curl --silent https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $apikey" \
-d "{ \"model\": \"$model\", \"messages\": [{\"role\": \"user\", \"content\": \"$the_prompt\"}], \"temperature\": $temperature }" |
grep '"content"' | cut -d'"' -f4) # doing this avoids jq dependency
-d "{ \"model\": \"$model\", \"messages\": [{\"role\": \"user\", \"content\": \"$the_prompt\"}], \"temperature\": $temperature }")

if grep -q '"error"' ; then
# doing this avoids jq dependency
text="ERROR: $(echo "$response" | grep '"message"' | cut -d'"' -f4)"
else
text=$(echo "$response" | grep '"content"' | cut -d'"' -f4)
fi

#───────────────────────────────────────────────────────────────────────────────

if [[ "$output_type" == "plain" ]]; then
echo -n "$response"
echo -n "$text"
exit 0
fi

echo "$selection" >"$cache/selection.txt"
echo "$response" >"$cache/rephrased.txt"
echo "$text" >"$cache/rephrased.txt"

# https://unix.stackexchange.com/questions/677764/show-differences-in-strings
diff=$(git diff --word-diff "$cache/selection.txt" "$cache/rephrased.txt" |
Expand Down

0 comments on commit 553a337

Please sign in to comment.