Advertisement
bitsnaps

bash script to query openai

Aug 27th, 2023 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.44 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# -eq 0 ]; then
  4.   echo "Usage: \$0 <query>"
  5.   exit 1
  6. fi
  7.  
  8. query="$1"
  9.  
  10. OPENAI_API_KEY="your_openai_api_key_here"
  11.  
  12. # Query with a model (models: gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613)
  13. #curl -s -X POST https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"model\":\"gpt-4-0613\", \"messages\": [{\"role\":\"user\",\"content\":\"$query\"}]}"
  14.  
  15. curl -s -X POST https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"model\":\"gpt-3.5-turbo-16k-0613\", \"messages\": [{\"role\":\"user\",\"content\":\"$query\"}]}"
  16.  
  17. # Prompt for completion (models: text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001)
  18. #curl -s -X POST https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"model\":\"text-davinci-003\",\"prompt\":\"$query\"}"
  19.  
  20. # Prompt for completion (max_tokens is optional)
  21. #curl -s -X POST https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"model\":\"text-davinci-003\",\"prompt\":\"$query\",\"max_tokens\": 2048}"
  22.  
  23. # Engines are deprecated
  24. #curl -s -X POST https://api.openai.com/v1/engines/text-davinci-003/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"prompt\": \"$query\", \"max_tokens\": 2048}"
  25.  
  26. # List of files
  27. # curl https://api.openai.com/v1/files -H "Authorization: Bearer $OPENAI_API_KEY"
  28.  
  29. # List all available models
  30. # curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"
  31.  
  32. # Retrieve basic information about a model
  33. # curl https://api.openai.com/v1/models/text-davinci-003 -H "Authorization: Bearer $OPENAI_API_KEY"
  34.  
  35. # Edits (Given a prompt and an instruction, the model will return an edited version of the prompt, models: text-davinci-edit-001, code-davinci-edit-001)
  36. # curl -s -X POST https://api.openai.com/v1/edits -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model":"code-davinci-edit-001","input":"print(hello world in C++)", "instruction":"complete and fix the code"}'
  37.  
  38. # More details at: https://platform.openai.com/docs/api-reference/edits
  39. # and: https://platform.openai.com/docs/models/gpt-3-5
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement