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.

Revisions

  1. sroecker revised this gist Nov 15, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions p.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/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 ""
  2. sroecker revised this gist Nov 15, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions p.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@ if [ "$#" -eq 0 ]; then
    echo " Requirements: PERPLEXITY_API, defined; jq and curl, installed; bash, version 3 or higher."
    exit 1
    fi
    #!/usr/bin/env bash

    function p() {
    local json_request
    @@ -32,7 +31,8 @@ function p() {
    --data "$json_request")
    #echo $json_response # uncomment to debug

    echo "$json_response" | jq '.choices[0].message.content'
    content=$(echo "$json_response" | jq '.choices[0].message.content')
    echo -e $content
    echo "Citations:"
    echo "$json_response" | jq '.citations[]' | tr -d '"' | nl
    }
  3. sroecker revised this gist Nov 15, 2024. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions p.sh
    Original file line number Diff line number Diff line change
    @@ -6,21 +6,22 @@ if [ "$#" -eq 0 ]; then
    echo " Requirements: PERPLEXITY_API, defined; jq and curl, installed; bash, version 3 or higher."
    exit 1
    fi
    #!/usr/bin/env bash

    function p() {
    local json_request
    json_request=$(jq -n \
    --arg content "$*" \
    '{
    "model": "pplx-7b-online",
    "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 \
    @@ -29,8 +30,10 @@ function p() {
    --header "authorization: Bearer $PERPLEXITY_API" \
    --header 'content-type: application/json' \
    --data "$json_request")
    # echo $json_response # uncomment to debug
    #echo $json_response # uncomment to debug

    echo "$json_response" | jq --raw-output .choices[0].message.content
    echo "$json_response" | jq '.choices[0].message.content'
    echo "Citations:"
    echo "$json_response" | jq '.citations[]' | tr -d '"' | nl
    }
    p "$*"
  4. @algal algal created this gist Jan 22, 2024.
    36 changes: 36 additions & 0 deletions p.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/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 "$*"