-
-
Save algal/5e815ffd371d57d3a6a37056db1bd281 to your computer and use it in GitHub Desktop.
bash script to query perplexity.ai
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# based off of https://gist.github.com/rauchg/c5f0b1dc245ad95c593de8336aa382ac?permalink_comment_id=4842642#gistcomment-4842642 | |
if [ "$#" -eq 0 ]; then | |
echo "Usage: $(basename $0) promt_to_send_to_perplexity" | |
echo "" | |
echo " Requirements: PERPLEXITY_API, defined; jq and curl, installed; bash, version 3 or higher." | |
exit 1 | |
fi | |
function p() { | |
local json_request | |
json_request=$(jq -n \ | |
--arg content "$*" \ | |
'{ | |
"model": "pplx-7b-online", | |
"messages": [ | |
{ "role": "system", "content": "Be precise and concise." }, | |
{ "role": "user", "content": $content } | |
], | |
"stream": false | |
}') | |
# echo $json_request # uncomment to debug | |
local json_response | |
json_response=$(curl --silent \ | |
--request POST \ | |
--url https://api.perplexity.ai/chat/completions \ | |
--header 'accept: application/json' \ | |
--header "authorization: Bearer $PERPLEXITY_API" \ | |
--header 'content-type: application/json' \ | |
--data "$json_request") | |
# echo $json_response # uncomment to debug | |
echo "$json_response" | jq --raw-output .choices[0].message.content | |
} | |
p "$*" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment