Giter Site home page Giter Site logo

0xacx / chatgpt-shell-cli Goto Github PK

View Code? Open in Web Editor NEW
1.0K 26.0 150.0 170 KB

Simple shell script to use OpenAI's ChatGPT and DALL-E from the terminal. No Python or JS required.

Home Page: https://gptshell.cc

License: MIT License

Shell 100.00%
chatbot chatgpt chatgpt-api chatgpt-api-wrapper terminal shell shell-script bash zsh cli

chatgpt-shell-cli's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chatgpt-shell-cli's Issues

Cannot prompt api key

Hello !
I've seen your repo and wanted to use it in a Ubuntu Multipass vm but I basically cannot prompt my api key so nothing want to start when I try starting it and there's error.
Could you help me ?
Thanks :)

recognize rate-limit and wait

would be great if the tool could understand when a rate limit is hit and wait for the suggested seconds (seems to be always 20s) and try again.
maybe allow max retires in the cmd line.

Cheers

our request to Open AI API failed: 
Rate limit reached for default-gpt-3.5-turbo in organization org-OtZ--removed--  on requests per min. 
Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. 
Please add a payment method to your account to increase your rate limit. 
Visit https://platform.openai.com/account/billing to add a payment method.

I have a few of these errors. Am I using a character that I'm supposed to escape or something? If so, which one?

❱chat
Welcome to chatgpt. You can quit with 'exit' or 'q'.

Enter a prompt:
in bash how do you split a long line


chatgpt

In Bash, you can split a long line by using the backslash (\) character at the end of a line to indicate that the
command continues on the next line. Alternatively, you can use parentheses () or curly braces {} to group commands
together and split them onto multiple lines.


Enter a prompt:
demonstrate

Your request to Open AI API failed: invalid_request_error
We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what wassent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to [email protected] and include any relevant code you'd like help with.)
1

❱chat
Welcome to chatgpt. You can quit with 'exit' or 'q'.

Enter a prompt:
in bash how do you split a long line


chatgpt

In Bash, you can split a long line using the backslash (\) character at the end of the line, indicating that the
command continues on the next line. For example:

echo "This is a very long \
line that needs to be split \
into multiple lines for clarity."

Alternatively, you can also use parentheses to group commands and split them across multiple lines. For example:

( command1 arg1 arg2 \
&& command2 arg1 arg2 \
&& command3 arg1 arg2 )


Enter a prompt:
"can you use parentheses () or curly braces {} to group commands together and split them onto multiple lines? if so please demonstrate."

Your request to Open AI API failed: invalid_request_error
We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what wassent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to [email protected] and include any relevant code you'd like help with.)
1

Newlines are not saved in history

❯ ./repos/bin/chatGPT-shell-cli/chatgpt.sh
Welcome to chatgpt. You can quit with 'exit'.

Enter a prompt:
write any python function

chatgpt Here's an example of a Python function that takes two arguments (a and b) and returns their sum:

def add_numbers(a, b):
    sum = a + b
    return sum

Once defined, you can call this function with two numbers as arguments:

result = add_numbers(5, 7)
print(result)

This would output 12 because the function adds 5 and 7 together.

Enter a prompt:
history

16/03/2023 20:58 write any python function
Here's an example of a Python function that takes two arguments (a and b) and returns their sum: def add_numbers(a, b): sum = a + b return sum Once defined, you can call this function with two numbers as arguments: result = add_numbers(5, 7) print(result) This would output 12 because the function adds 5 and 7 together.

[bug] runtime error

After commit 752d7b3 I get the following error

❯ ~/repos/bin/chatGPT-shell-cli/chatgpt.sh -p "hello"
goroutine 1 [running]:
runtime/debug.Stack(0x46, 0x0, 0x0)
/usr/lib/go-1.16/src/runtime/debug/stack.go:24 +0x9f
runtime/debug.PrintStack()
/usr/lib/go-1.16/src/runtime/debug/stack.go:16 +0x25
github.com/charmbracelet/bubbletea.(*Program).Start.func2(0xc00058de80)
/root/go/pkg/mod/github.com/charmbracelet/[email protected]/tea.go:294 +0xd3
panic(0xc3ae40, 0x11e48e0)
/usr/lib/go-1.16/src/runtime/panic.go:965 +0x1b9
strings.Repeat(0xdf0bc0, 0x1, 0xffffffffffffffff, 0xc3ae40, 0xc00025b980)
/usr/lib/go-1.16/src/strings/strings.go:529 +0x5e5
github.com/charmbracelet/bubbles/viewport.Model.View(0x0, 0x0, 0x0, 0x0, 0x1, 0xc0002daa80, 0xa6, 0xa6, 0x1619d80, 0x4)
/root/go/pkg/mod/github.com/charmbracelet/[email protected]/viewport/viewport.go:361 +0x225
github.com/charmbracelet/glow/ui.pagerModel.View(0xc00030fba0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc0002daa80, 0xa6, 0xa6, 0x0, ...)
/home/ed/gh/glow/ui/pager.go:337 +0x8a
github.com/charmbracelet/glow/ui.model.View(0xc00030fba0, 0x1, 0x0, 0x0, 0x0, 0xc00030fba0, 0x0, 0x0, 0x1619d80, 0x4, ...)
/home/ed/gh/glow/ui/ui.go:444 +0x79
github.com/charmbracelet/bubbletea.(*Program).Start(0xc00058de80, 0x0, 0x0)
/root/go/pkg/mod/github.com/charmbracelet/[email protected]/tea.go:419 +0x72c
main.runTUI(0x0, 0x0, 0x0, 0x0, 0x0)
/home/ed/gh/glow/main.go:327 +0x293
main.execute(0x161d540, 0x166b630, 0x0, 0x0, 0x0, 0x0)
/home/ed/gh/glow/main.go:190 +0x1d6
github.com/spf13/cobra.(*Command).execute(0x161d540, 0xc000020210, 0x0, 0x0, 0x161d540, 0xc000020210)
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:852 +0x472
github.com/spf13/cobra.(*Command).ExecuteC(0x161d540, 0x0, 0x15c6b08, 0xc000028118)
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:960 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:897
main.main()
/home/ed/gh/glow/main.go:337 +0x2d

Caught panic:

strings: negative Repeat count

Restoring terminal...

Thanks for using Glow!

"open" is not working

I see that this application is developed by a Mac user and for Mac users, so there's some problems with it running on GNU/Linux.
So in line 263 you have the command "open".
In GNU/Linux there is no such command, but its counterpart is "xdg-open".
You can easily make the script check the system and then use "xdg-open" or "open" depending on the system.

Cannot login to OpenAI

❯ chatgpt

             Provide a prompt for ChatGPT, or type /help or ? to list commands.

1> /login
Enter username or email: [email protected]
AttributeError("'NoneType' object has no attribute 'password'")```
1>

Obviously I'm trying to use it, tried installing from AUR and from pip.

uname -a:

Linux mjolnir 6.2.8-zen1-1-zen

python version

❯ python --version
Python 3.10.10

zsh shell.

Issue with escaping of newlines

$ chatgpt -p 'How many newlines in the following string: "
>
> "'

  There are no newlines in the string "  ". It consists of two spaces.

Indeed literal newlines are converted into spaces the actual message sent is:
{"role": "user", "content": "How many newlines in the following string: \" \""}

Current escaping has other limitations, I'm making a PR.

Can not install

here is my terminal output:

paul@laptop ~ % curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash
Password:
zsh: done                   curl -sS  | 
zsh: suspended (tty input)  sudo -E bash
paul@laptop ~ % 

root requirement

Why is root needed?
Would be best to stick to user space and request the user to add PATH to bash_profile

Add interactive command execution feature?

Would you consider to add an interactive command execution feature.

This feature should allow users to input a command prompt, and then receive a suggestion for a command to execute based on their input. The application should then ask the user whether they would like to execute the suggested command.

I tried something like this:

Enter a prompt:
show current folder contents

chatgpt  Execute 'ls'? (Yes/No, Enter=Yes)
chatgpt.sh  install.sh  LICENSE  README.md  test

This feature will make the application more intuitive and user-friendly, and will help users who are not familiar with command-line interfaces to use the application more easily.

uninstall instructions

Hi could I suggest an uninstall script or instructions to undo everything the install script did?

thanks

ChatGPT-shell-cli output doesn't register new lines

For example, If I input the following into ChatGPT web

write a poem:

Beneath the starry sky so bright,
I stand alone in the quiet night.
The moon above shines down on me,
And in its light, my thoughts run free.

The gentle breeze that stirs the trees,
Whispers secrets to the leaves.
The crickets' song a symphony,
That fills the air with melody.

The world around is hushed and still,
As if in awe of nature's thrill.
The beauty here is plain to see,
And in this moment, I am free.

So let my heart and spirit soar,
On wings of wonder evermore.
For in this peaceful, quiet place,
I find my soul has found its grace.

The same in ChatGPT-shell-cli:

Enter a prompt:
write a poem

chatgpt about a treeA tree so tall and greenIt stands so proud and tallIt's leaves so green and freshA sight to behold for allIt's branches reach so highTo the sky, so grand and blueThe trunk so strong and sturdyA sight so old and trueIt's roots so deep and hiddenThe secret to its strengthIt stands through wind and weatherSo tall and vast and lengthA tree so strong and gracefulIt's beauty so sublimeIt stands like a symbolof hope throughout all time

Is it possible for new lines to be used? and what about code blocks?

Help this: Welcome to chatgpt.. Your request to Open AI API failed: invalid_request_error

Please help solve this error in step by step explanation, after got the key:

$ chatgpt
Welcome to chatgpt. You can quit with 'exit' or 'q'.

Enter a prompt:
tesla
Your request to Open AI API failed: invalid_request_error
You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)

jq: error (at <stdin>:8): Cannot iterate over null (null)

Getting the following jq error:

[dave@void ~]$ chatgpt
Welcome to chatgpt. You can quit with 'exit'.

Enter a prompt:
write a hello world script in brainfuck
jq: error (at <stdin>:8): Cannot iterate over null (null)

chatgpt

Enter a prompt:

Newline characters not being printed correctly

When chatgpt responds by giving advice on how to add a new line to some Python code, it doesn't return the correct character...

e.g.

image

Compared to it's response on the web chat where it does this correctly:

image

Installation improvments: Add .zshrc to install script, avoid overwriting $PATH if /usr/local/bin already found

I made two PRs to solve issues I ran into while installing on my machine. Basically, the install script does not check for .zshrc, which is actually the place to put anything related to interactive sessions, such as this package. I do not have a .zprofile on m debian machine, and this is common.

I also did not want /usr/local/bin prepended to my path. It is already therem but is located after conda / mambaforge bin for virtual environment purposes, which is the standard conda install. So the install script should not prepend it.

I made two PRs that solve the issues. #63 and #64

Hope they are useful. Let me know if you have any questions.

Random chrashes

I have several "bugs"

First the most important one:
Your request to Open AI API failed: server_error
The server had an error while processing your request. Sorry about that!

This happens about every 10 th time.

Second. Image creation

  1. I changed the line nr 50 from "open" to $BROWSER
  2. even so my terminal can do imcat it does not start because it is not a iTerm.app (i guess). So the it would be nicer if possible to change the if statement

Issue with backslashes

chatgpt -p 'Hey here is a backslash \ '
Your request to Open AI API failed: invalid_request_error

This is related #96 and will be solved together.

Some suggestions

1. Don't mess with profiles

Changing user/system profiles not a good idea and can create a mess, use own config instead

Example install.sh:

if [[ $EUID -eq 0 ]]; then
    mkdir -p /etc/chatgpt/chatgpt.txt
    echo "OPENAI_KEY=$key" > /etc/chatgpt/chatgpt.conf
else
    mkdir -p "$HOME/.config/chatgpt/chatgpt.conf"
    echo "OPENAI_KEY=$key" > "$HOME/.config/chatgpt/chatgpt.conf"
fi

Example chatgpt.sh:

if [[ -f "$HOME/.config/chatgpt/chatgpt.conf" ]]; then
    . "$HOME/.config/chatgpt/chatgpt.conf"
elif [[ -f "/etc/chatgpt/chatgpt.conf" ]]; then
  . "/etc/chatgpt/chatgpt.conf"
fi

# If OPENAI_KEY empty warn user
if [ -z $OPENAI_KEY ]; then
    echo "OPENAI_KEY not set, please add to your env or config file"
fi

2. Allow user to install on $HOME

if [[ $EUID -eq 0 ]]; then
   CHATGPT_BIN="/usr/local/bin/"
else
   CHATGPT_BIN="$HOME/local/bin"
   # Warn user about PATH
   echo 'Add $HOME/local/bin to your PATH environment'
   echo 'Example: PATH=$PATH:$HOME/local/bin'  # Don't use double-quote to not expand $PATH
fi

# Create path of not exist
mkdir -p "$CHATGPT_BIN"

# Then install to CHATGPT_BIN
curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/chatgpt.sh -o "$CHATGPT_BIN/chatgpt"

# Same with imgcat
curl -sS https://iterm2.com/utilities/imgcat -o "$CHATGPT_BIN/imgcat"

Table output support is not very good, hope it can be optimized

Table output support is not very good, hope it can be optimized

咖啡种类 咖啡 牛奶 其他
拿铁 浓缩咖啡 牛奶 可选 -
卡布奇诺 浓缩咖啡 牛奶 可选 泡沫奶
美式咖啡 浓缩咖啡 可选 可选 -
摩卡咖啡 浓缩咖啡 牛奶 巧克力 -
香草拿铁 浓缩咖啡 牛奶 香草糖浆 -
卡布奇诺玛奇朵 浓缩咖啡 牛奶 可选 一层巧克力酱和泡沫奶
冰咖啡 浓缩咖啡 可选 可选 冰块
浓缩咖啡 浓缩咖啡 可选 可选 -

注意,这个表格只是一个示例,并不代表所有咖啡店都会使用相同的成分。成分可能因品牌、制作方式、地区和个人口味而异。

Default for the $COLUMNS

Some shells don't have the COLUMNS defined in certain cases.

This makes fold error: fold: invalid number of columns: ‘’

A sane default would be appreciated. Either really big or a standard width 80.

Simply changing $COLUMNS -> ${COLUMNS:-80} would be an improvement.

Why bash? wouldn't sh be enough?

The script is still small enough to migrate to /bin/sh without much effort, leading to a more portable script. I would suggest you do so.

jq: error (at <stdin>:8): Cannot iterate over null (null)

I cloned the repo, ran the install.sh, and added my api key.

When I try to enter anything after the prompt, I receive this error message:

"jq: error (at :8): Cannot iterate over null (null)"

I am running Ubuntu 22.10 and I have curl and jq already installed.

Bug - fold: illegal width value when using pipe mode

Reported on discord:

Running chatgpt in pipe mode:
cat /test_playbook.yml | ./chatgpt.sh --init-prompt-from-file ../prompts/documentation_instructions_and_format.txt
results in
fold: illegal width value

Assumption: The $COLUMNS variable probably is not initialized or updated properly when using pipe mode.

Raw mode similar to jq -r

Just filing this here even though we spoke about it in the PR.

A raw mode similar to jq -r, pass the question via arguments and have the answer returned to stdout.

Proposal: Avoid passing the API KEY as ENV VAR

PROBLEM
Defining OPENAI_KEY as an env var, makes your chatgpt KEY available to any program.

SOLUTION
If instead we store the key in ~/chatgpt-shell-cli.apikey, the user will be able to manage file permissions.

Optionally, having a --update-key options could be a user friendly way to set the api key.

Install error on PopOS!/Ubuntu for sed option

First off this is a Great tool! Thank you for developing and sharing this!
I've seen an error for sed option during install on pop-os/ubuntu, so raising this issue. Could be an easy fix(?)

direster@pop-os:~$ curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash
sed: invalid option -- 'h'
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
Installed chatgpt script to /usr/local/bin/chatgpt
OpenAI key and chatgpt path added to ~/.profile
Installation complete

[BUG] We could not parse the JSON body of your request

The command I run:
~/repos/bin/chatGPT-shell-cli/chatgpt.sh -m gpt-4 --init-prompt-from-file ~pr/tutor

The init file is json:
https://raw.githubusercontent.com/JushBJJ/Mr.-Ranedeer-AI-Tutor/main/Mr_Ranedeer.json

The full error:
Your request to Open AI API failed: invalid_request_error
We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)

Free API key !=Free access to API

Thanks for creating the script.
However, I am failing with
image

because I do not have a pay OpenAI account it tells me

me@localhost:~$ chatgpt.sh
Welcome to chatgpt. You can quit with 'exit' or 'q'.

Enter a prompt:
what is the radius of earth

Your request to Open AI API failed: insufficient_quota
You exceeded your current quota, please check your plan and billing details.

LaTeX doesn't work well with shell.

When chatGPT replies mathematical expression in $\LaTeX$ , some symbles will be transferred to what I don't want. Mainly due to it functioning as an escape character.

Cases:

1. The missing "\f"

Enter a prompt:
What is the Maclaurin formula of $\arcsin x$ ?

chatgpt The Maclaurin series formula for $arcsin(x)$ is:

$arcsin(x)=\sum_{n=0}^{\infty}
rac{(2n)!}{4^n(n!)^2(2n+1)}x^{2n+1}$

While "\frac" is the correct response.

2. The missing "\t"

Enter a prompt:
$\theta=3x+2$ ,what is $x$?

chatgpt Without additional information or an equation to solve for a specific value of $heta$, it is not possible to determine the value of $x$ from the given equation $ heta = 3x + 2$.

While "\theta" is the correct response.

Additionally, I tried to make chatGPT answer in single quotation marks. But that does not work at all either.

Multi-line input in interactive mode

Currently, interactive mode only allows for single-line input. This can be
inconvenient when working with longer prompts, especially when copy/pasting code blocks from a code editor. Therefore, I suggest considering allowing multi-line input in interactive mode, perhaps by a distinction between Enter and Shift+Enter.

I believe this enhancement would make the CLI more user-friendly.
Thank you for your consideration.

Ciaran

insufficient_quota

$ chatgpt
Welcome to chatgpt. You can quit with 'exit'.

Enter a prompt:
hello
Your request to Open AI API failed: insufficient_quota
You exceeded your current quota, please check your plan and billing details.

Not a bug, just showing off

It works correctly on Termux, on Android. I manually installed chatgpt.sh on ~/.local/bin and added the path to .bashrc or .zshrc
Screenshot_20230322_023547_Termux

some escaping issue?

Observe the return pi line, where it seems a * was expanded into the contents of my home directory.

chatgpt Sure! Here's an implementation of the Leibniz formula for calculating pi in Python:

```python
def calculate_pi():
 pi = 0
 sign = 1
 denominator = 1
 precision = 1e-10 # 10 decimal places

 while True:
 term = sign / denominator
 if abs(term) < precision:
 break
 pi += term
 sign = -sign
 denominator += 2

 return pi Applications Desktop Documents Downloads   Dropbox (Work) Dropbox (Personal) Library Movies Music Pictures Public PycharmProjects X-Plane 11 bin dotfiles go myapi sendKindle venvs 4

print(calculate_pi())

This algorithm uses an infinite series to calculate pi, adding and subtracting terms alternately to converge on the value. The precision variable determines how close the result needs to be to the true value of pi, and the loop continues until the absolute value of the next term is smaller than that precision. The result is then multiplied by 4 since the series actually calculates pi/4.

Enter a prompt:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.