Skip to content

Instantly share code, notes, and snippets.

@woodongwong
Created April 9, 2023 07:19
Show Gist options
  • Select an option

  • Save woodongwong/7d186f164982ce483198bd384a005d72 to your computer and use it in GitHub Desktop.

Select an option

Save woodongwong/7d186f164982ce483198bd384a005d72 to your computer and use it in GitHub Desktop.

Revisions

  1. woodongwong renamed this gist Apr 9, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. woodongwong created this gist Apr 9, 2023.
    27 changes: 27 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/env sh

    OPENAI_API_KEY="sk-xxx"

    request_params=$(
    cat <<EOF
    {
    "model": "gpt-3.5-turbo",
    "stream": true,
    "messages": [
    {
    "role": "user",
    "content": "$1"
    }
    ]
    }
    EOF
    )

    curl --header "Authorization: Bearer $OPENAI_API_KEY" \
    --header 'Content-Type: application/json' \
    --data "$request_params" -N -s 'https://api.openai.com/v1/chat/completions' |
    awk -F 'data: ' '{print $2; system("")}' |
    jq --unbuffered -R 'fromjson?' |
    jq --unbuffered -r '(.choices[].delta.content)? | select(. != null)' |
    awk '{printf "%s", $0}'
    echo