#Speaking to ChatGPT via Bash (SOLVED)

19 messages · Page 1 of 1 (latest)

frail juniper
#

ChatGPT provided me this script so I could speak to it via my terminal. Unfortunately however I am getting null nonsensical responses and I'm at a loss for how to troubleshoot it.

#!/bin/bash

# Replace YOUR_API_KEY with your OpenAI API key
API_KEY="MY_API_KEY"

# Replace R2 with the name you want to call me
BOT_NAME="R2"

# Replace ENGINE_NAME with the name of the GPT-3 engine you want to use
ENGINE_NAME="davinci"

# Get user input from command line arguments
USER_INPUT="$@"
echo $USER_INPUT

# Send a request to the OpenAI API with cURL
RESPONSE=$(curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $API_KEY" \
     -d '{"prompt": "'"$BOT_NAME $USER_INPUT"'", "temperature": 0.7, "max_tokens": 150, "stop": "\n"}' \
     https://api.openai.com/v1/engines/$ENGINE_NAME/completions)

# Extract the response from the JSON result
echo "$RESPONSE"
MESSAGE=$(echo $RESPONSE | jq -r '.choices[0].text')

# Display the response in the terminal window
echo "--------------- ChatGPT Response:"
echo $MESSAGE
frail juniper
#

So, I worked with ChatGPT last night to do a bunch of troubleshooting and I'm no longer getting null response. Instead I am now getting nonsensical responses. I find it very weird. It's almost as if I"m getting responses to other peoples queries but at the same time it's just occasionally just raw punctuation. Here is a sample of responses:

hello
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   326  100   251  100    75    101     30  0:00:02  0:00:02 --:--:--   131
{"id":"cmpl-6yRFtRDCSuSdVI3fc5RDDZLCMXvVY","object":"text_completion","created":1679862469,"model":"davinci","choices":[{"text":".","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}
--------------- ChatGPT Response:
.
hello R2 I would like to speak to you
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   395    0   288  100   107     76     28  0:00:03  0:00:03 --:--:--   105
{"id":"cmpl-6yRIsuB7OfNL9EfYHGxAZLJUInelx","object":"text_completion","created":1679862654,"model":"davinci","choices":[{"text":", but I don’t know your password.","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":11,"total_tokens":23}}
--------------- ChatGPT Response:
, but I don’t know your password.
you do not need my password r2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   611  100   511  100   100     70     13  0:00:07  0:00:07 --:--:--   124
{"id":"cmpl-6yRJMht5TJeHKW9VqNR9mt9ihdrIo","object":"text_completion","created":1679862684,"model":"davinci","choices":[{"text":" c:\\>cd \\robocopy c:\\robocopy\\robocopy.log 1 /COPY:DATSOFF /XJ /XA:SH /R:0 /W:0 /MT:64 /FP /TEE /LOG+:robocopy.log /NP /NFL /NDL /NDD /TS /ETA /XJD /XAEW /XC /FFT /XA:L /XF /TSC /XJD /XRA /ETA /XA:0 /XA:1 /XA:2 /XA:3 /V /NJS /FPF /NC /NDD /TSC /XJE /","index":0,"logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":10,"completion_tokens":150,"total_tokens":160}}
--------------- ChatGPT Response:
c:\>cd \robocopy c:\robocopy\robocopy.log 1 /COPY:DATSOFF /XJ /XA:SH /R:0 /W:0 /MT:64 /FP /TEE /LOG+:robocopy.log /NP /NFL /NDL /NDD /TS /ETA /XJD /XAEW /XC /FFT /XA:L /XF /TSC /XJD /XRA /ETA /XA:0 /XA:1 /XA:2 /XA:3 /V /NJS /FPF /NC /NDD /TSC /XJE /
#

Speaking to ChatGPT via Bash - "nonsensical" responses

lethal thunder
frail juniper
#

Thanks @lethal thunder. I'll take a look. Still somewhat junior but having fun with it. It was an interesting night lol... Some of our troubleshooting was a bit circular but over all I'm impressed.

frail juniper
#

Never could find the documentation you mentioned @lethal thunder and my experience isn’t sufficient to know what you were referring to.

lethal thunder
#

You're using engine which is no longer supported

frail juniper
#

Ok cool thank you for that and the link

#

I’ll update this thread when I get it working so others can use the info when done.

uncut mesa
#

Davinci’s not the best one to use for chatting. Use chatgpt-3.5-turbo instead. It’s got extra training, and is also cheaper 👍

frail juniper
#

I’m more interested in coding and technical features than conversation.

uncut mesa
#

Ok don’t listen then 👍 I hope you find another way to fix your problem.

frail juniper
# uncut mesa Ok don’t listen then 👍 I hope you find another way to fix your problem.

Seems like we've had a misunderstanding. I only said that because when I saw the list of versions returned by the API when I was tooling around that there was versions for coding. I wasn't trying to tell you I wasn't going to use it more clarifying my intent on what I'm going to be using this for. While things are out of via the gpt I do find it a useful tool for explaining basic things about languages. Was hoping for your input on whether or not you'd still recommend the same one. I haven't actually had a chance until now to sit down and play with this since Teemu sent the link.

#

So apologies for the misunderstanding. I'll try to be a bit more clear next time.

frail juniper
#

So I've got working code finally thank you both for the advice.

#!/bin/bash
# Dependencies
# apt-get install pandoc
# apt-get install espeak

# Colors
teal="\033[36m"
purple="\033[35m"
nocolor="\033[0m"

# API Key used to access openai.
API_KEY="REPLACEMEWITHYOURSDUH"

session_id=""
running=true
while $running; do
        echo -e "${purple}Input Text:${nocolor} \c"
        read input
        if [[ $input == "exit" ]]; then
                exit
        fi

        # Send a request to the OpenAI API with cURL
        RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer $API_KEY" \
          -d '{
             "model": "gpt-3.5-turbo",
             "messages": [{"role": "user", "content": "'"$input"'"}],
             "temperature": 0.7
           }')

        # Extract the response from the JSON result
        #echo "$RESPONSE"
        MESSAGE=$(echo $RESPONSE | jq -r '.choices[0].message.content')

        # Use pandoc to convert the markdown text to formatted text output
        formatted_text=$(echo "$MESSAGE" | pandoc)

        # Use pandoc to convert the formatted text to teal using ANSI escape codes
        teal_text="$(echo "$formatted_text" | pandoc -f html -t plain)"
        teal_text="${teal}${teal_text}${nocolor}"

        # Display the teal formatted text
        echo -e "$teal_text"

        # Use espeak to read the message aloud using Text To Speech.
        espeak -v f2 "$MESSAGE"
done

My next step is to go through and make it so each time I loop it continues the session so context is remembered. Assuming the API even allows me to do that (i'm not able to pay atm if it's a premium feature)

Also I used espeak and pandoc for this to polish it. It does work without those.

#

Speaking to ChatGPT via Bash (SOLVED)

uncut mesa
frail juniper
#

It’s good, it happens. Would have helped if I wasn’t dropping a curt reply from mobile in the middle of public 😂