Skip to content

Instantly share code, notes, and snippets.

@sroecker
Forked from algal/p.sh
Last active November 15, 2024 14:10
Show Gist options
  • Save sroecker/d92a080601ed2863aa0f285a08610eef to your computer and use it in GitHub Desktop.
Save sroecker/d92a080601ed2863aa0f285a08610eef to your computer and use it in GitHub Desktop.
bash script to query perplexity.ai
#!/usr/bin/env bash
# based off of https://gist.github.com/rauchg/c5f0b1dc245ad95c593de8336aa382ac?permalink_comment_id=4842642#gistcomment-4842642
# further adapted from https://gist.github.com/algal/5e815ffd371d57d3a6a37056db1bd281
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": "llama-3.1-sonar-small-128k-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
content=$(echo "$json_response" | jq '.choices[0].message.content')
echo -e $content
echo "Citations:"
echo "$json_response" | jq '.citations[]' | tr -d '"' | nl
}
p "$*"
@sroecker
Copy link
Author

Added the new sonar small model and output of citations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment