chatgpt_api.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # exo provides an API that aims to be a drop-in replacements for the ChatGPT-API.
  2. # This example shows how you can use the API first without streaming and second with streaming.
  3. # This works the same in a single-node set up and in a multi-node setup.
  4. # You need to start exo before running this by running `python3 main.py`.
  5. API_ENDPOINT="http://${API_ENDPOINT:-$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1):52415}"
  6. MODEL="llama-3.1-8b"
  7. PROMPT="What is the meaning of exo?"
  8. TEMPERATURE=0.7
  9. echo ""
  10. echo ""
  11. echo "--- Output without streaming:"
  12. echo ""
  13. curl "${API_ENDPOINT}/v1/chat/completions" --silent \
  14. -H "Content-Type: application/json" \
  15. -d '{
  16. "model": "'"${MODEL}"'",
  17. "messages": [{"role": "user", "content": "'"${PROMPT}"'"}],
  18. "temperature": '"${TEMPERATURE}"'
  19. }'
  20. echo ""
  21. echo ""
  22. echo "--- Output with streaming:"
  23. echo ""
  24. curl "${API_ENDPOINT}/v1/chat/completions" --silent \
  25. -H "Content-Type: application/json" \
  26. -d '{
  27. "model": "'"${MODEL}"'",
  28. "messages": [{"role": "user", "content": "'"${PROMPT}"'"}],
  29. "temperature": '"${TEMPERATURE}"',
  30. "stream": true
  31. }' | while read -r line; do
  32. if [[ $line == data:* ]]; then
  33. content=$(echo "$line" | sed 's/^data: //')
  34. echo "$content" | jq -r '.choices[].delta.content' --unbuffered | tr -d '\n'
  35. fi
  36. done