fixed bug.
This commit is contained in:
parent
687c5cce3f
commit
0497fadab8
95
askChatGPT
Normal file → Executable file
95
askChatGPT
Normal file → Executable file
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# askChatGPT - A simple Bash client for OpenAI Chat API
|
# askChatGPT - A simple Bash client for OpenAI Chat API
|
||||||
# Requires: curl, jq
|
# Requires: curl, jq
|
||||||
# Env var: OPENAI_API_KEY must be set
|
# Env var: OPENAI_API_KEY must be set
|
||||||
@ -10,6 +9,7 @@ MODEL="gpt-4o-mini"
|
|||||||
PREPEND_TEXT=""
|
PREPEND_TEXT=""
|
||||||
CONTENT_FILE=""
|
CONTENT_FILE=""
|
||||||
LIST_MODELS=0
|
LIST_MODELS=0
|
||||||
|
declare -a PROMPT_WORDS=() # <-- important: initialize the array
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
@ -17,15 +17,10 @@ Usage: $0 [options] [prompt text]
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
-model <name> Set the OpenAI model (default: $MODEL)
|
-model <name> Set the OpenAI model (default: $MODEL)
|
||||||
--list-models List available models from OpenAI
|
--list-models List available models from OpenAI
|
||||||
-prepend <text> Text to prepend before the main prompt
|
-prepend <text> Text to prepend before the main prompt
|
||||||
-content-from-file <file> Read prompt content from file
|
-content-from-file <file> Read prompt content from file
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
|
|
||||||
Examples:
|
|
||||||
$0 "Write a haiku about Bash scripting"
|
|
||||||
$0 -model gpt-4o -prepend "Explain like I'm five:" "What is quantum physics?"
|
|
||||||
$0 -content-from-file notes.txt
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,32 +28,25 @@ EOF
|
|||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-model)
|
-model)
|
||||||
MODEL="$2"
|
MODEL="$2"; shift 2 ;;
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
--list-models)
|
--list-models)
|
||||||
LIST_MODELS=1
|
LIST_MODELS=1; shift ;;
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-prepend)
|
-prepend)
|
||||||
PREPEND_TEXT="$2"
|
PREPEND_TEXT="$2"; shift 2 ;;
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-content-from-file)
|
-content-from-file)
|
||||||
CONTENT_FILE="$2"
|
CONTENT_FILE="$2"; shift 2 ;;
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-h|--help)
|
-h|--help)
|
||||||
usage
|
usage; exit 0 ;;
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
PROMPT_WORDS+=("$1")
|
PROMPT_WORDS+=("$1"); shift ;;
|
||||||
shift
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Check deps
|
||||||
|
for dep in curl jq; do
|
||||||
|
command -v "$dep" >/dev/null 2>&1 || { echo "Error: '$dep' is required."; exit 1; }
|
||||||
|
done
|
||||||
|
|
||||||
# Check API key
|
# Check API key
|
||||||
if [[ -z "${OPENAI_API_KEY:-}" ]]; then
|
if [[ -z "${OPENAI_API_KEY:-}" ]]; then
|
||||||
echo "Error: OPENAI_API_KEY is not set."
|
echo "Error: OPENAI_API_KEY is not set."
|
||||||
@ -74,23 +62,15 @@ if [[ "$LIST_MODELS" -eq 1 ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Build prompt
|
# Build prompt
|
||||||
PROMPT=""
|
PROMPT_MAIN=""
|
||||||
if [[ -n "$PREPEND_TEXT" ]]; then
|
|
||||||
PROMPT+="$PREPEND_TEXT "
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$CONTENT_FILE" ]]; then
|
if [[ -n "$CONTENT_FILE" ]]; then
|
||||||
if [[ ! -f "$CONTENT_FILE" ]]; then
|
[[ -f "$CONTENT_FILE" ]] || { echo "Error: file '$CONTENT_FILE' not found."; exit 1; }
|
||||||
echo "Error: file '$CONTENT_FILE' not found."
|
PROMPT_MAIN="$(<"$CONTENT_FILE")"
|
||||||
exit 1
|
elif (( ${#PROMPT_WORDS[@]} > 0 )); then
|
||||||
fi
|
PROMPT_MAIN="${PROMPT_WORDS[*]}"
|
||||||
PROMPT+="$(<"$CONTENT_FILE")"
|
|
||||||
elif [[ ${#PROMPT_WORDS[@]:-0} -gt 0 ]]; then
|
|
||||||
PROMPT+="${PROMPT_WORDS[*]}"
|
|
||||||
else
|
else
|
||||||
# Read from stdin if nothing else provided
|
|
||||||
if ! [ -t 0 ]; then
|
if ! [ -t 0 ]; then
|
||||||
PROMPT+="$(cat)"
|
PROMPT_MAIN="$(cat)"
|
||||||
else
|
else
|
||||||
echo "Error: No prompt provided."
|
echo "Error: No prompt provided."
|
||||||
usage
|
usage
|
||||||
@ -98,19 +78,36 @@ else
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
PROMPT_FULL=""
|
||||||
|
if [[ -n "$PREPEND_TEXT" ]]; then
|
||||||
|
PROMPT_FULL+="$PREPEND_TEXT"$'\n\n'
|
||||||
|
fi
|
||||||
|
PROMPT_FULL+="$PROMPT_MAIN"
|
||||||
|
|
||||||
|
# JSON-escape content safely
|
||||||
|
JSON_CONTENT=$(printf '%s' "$PROMPT_FULL" | jq -Rs .)
|
||||||
|
|
||||||
# Send request
|
# Send request
|
||||||
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
|
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
||||||
-d @- <<EOF
|
-d "$(cat <<EOF
|
||||||
{
|
{
|
||||||
"model": "$MODEL",
|
"model": "$MODEL",
|
||||||
"messages": [
|
"messages": [
|
||||||
{"role": "user", "content": "$PROMPT"}
|
{"role": "user", "content": $JSON_CONTENT}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
)
|
)")
|
||||||
|
|
||||||
# Extract and print answer
|
# Extract and print answer (or error)
|
||||||
echo "$RESPONSE" | jq -r '.choices[0].message.content // "Error: No content in response"'
|
echo "$RESPONSE" | jq -r '
|
||||||
|
if .choices and .choices[0].message.content then
|
||||||
|
.choices[0].message.content
|
||||||
|
elif .error and .error.message then
|
||||||
|
"API Error: " + .error.message
|
||||||
|
else
|
||||||
|
"Error: No content in response"
|
||||||
|
end
|
||||||
|
'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user