chat.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. if [ $# -lt 2 ]; then
  3. echo "Usage: bash chat_completion.sh <model_name> <max_tokens> [<temperature>] [<stop_sequence>]"
  4. exit 1
  5. fi
  6. ENDPOINT="http://localhost:8000/v1/chat/completions"
  7. MODEL_NAME="$1"
  8. MAX_TOKENS="$2"
  9. TEMPERATURE="${3:-1.0}" # Default temperature is 1.0 if not provided
  10. STOP_SEQUENCE="${4:-\\n###}" # Default stop sequence is '\n###' if not provided
  11. echo "Enter your conversation ('q' to quit):"
  12. CONVERSATION=""
  13. while true; do
  14. read -p "You: " USER_INPUT
  15. if [ "$USER_INPUT" == "q" ]; then
  16. echo "Exiting..."
  17. exit 0
  18. fi
  19. # Append user input to the conversation
  20. CONVERSATION="$CONVERSATION\n### Human: $USER_INPUT"
  21. DATA=$(cat <<EOF
  22. {
  23. "model": "$MODEL_NAME",
  24. "messages": [
  25. {"role": "system", "content": "You are a helpful assistant."},
  26. {"role": "user", "content": "$CONVERSATION"}
  27. ],
  28. "max_tokens": $MAX_TOKENS,
  29. "temperature": $TEMPERATURE,
  30. "stop": ["$STOP_SEQUENCE"]
  31. }
  32. EOF
  33. )
  34. RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" \
  35. -d "$DATA" \
  36. $ENDPOINT)
  37. AI_REPLY=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
  38. # Remove any generated text after the stop sequence
  39. AI_REPLY=$(echo "$AI_REPLY" | sed -n "/$STOP_SEQUENCE/q;p")
  40. echo -e "\033[1;35mBot:\033[0m \033[1;32m$AI_REPLY\033[0m"
  41. done