kardolus / chatgpt-cli

ChatGPT CLI is a versatile tool for interacting with ChatGPT models via OpenAI and Azure, as well as with models from Perplexity AI. It offers streaming, query mode, and history tracking for seamless, context-aware interactions. With extensive configuration options, it’s designed for both users and developers to create a tailored GPT experience.
MIT License
503 stars 34 forks source link

Regarding autocompletion #26

Closed benbenbang closed 7 months ago

benbenbang commented 7 months ago

Hi there,

Since we are using cobra, any plan to use the completion to help with the flags? I can do a PR if it's ok for you :)

Thanks

kardolus commented 7 months ago

Hi @benbenbang.

I think that's a great idea. I have been struggling with auto completion. Please take a look at this branch (and feel free to make adjustments, or to submit your own PR):

https://github.com/kardolus/chatgpt-cli/blob/d2f1804930d0587563354a7d71f5ea612dd37dfc/cmd/chatgpt/main.go#L59

The problem I am having is that once I add 1 command (completion), chatgpt no longer works as intended:

 2024-02-20 09:12:21 ⌚  Guillermos-MacBook-Pro in ~/workspace/chatgpt-cli
± |main {4} U:1 ✗| → ./bin/chatgpt what is the capital of denmark
unknown command "what" for "chatgpt"

 2024-02-20 09:12:52 ⌚  Guillermos-MacBook-Pro in ~/workspace/chatgpt-cli
± |main {4} U:1 ✗| → chatgpt what is the capital of denmark
The capital of Denmark is Copenhagen (Danish: København).

I love Cobra/Viper but also really like to use the CLI without commands :) -- what would be a good workaround for this?

benbenbang commented 7 months ago

Hello @kardolus,

Sure, let me take a look tmw morning

kardolus commented 7 months ago

If we can't get the command to work I suppose we could always set up a script ourselves.

kardolus commented 7 months ago

Here's a completion script just for flag completion: https://github.com/kardolus/chatgpt-cli/blob/completion-script/scripts/completion.sh -- not sure if it works across different shells though. I can confirm it works on GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23).

kardolus commented 7 months ago

Merged it into the main branch: https://github.com/kardolus/chatgpt-cli?tab=readme-ov-file#command-line-autocompletion

I am open to suggestions for something better though :).

benbenbang commented 7 months ago

Long day :D Anyways, we can make it simple or make it "complex" Maybe I am wrong, or say there's no one way to do it. But I can see from the current implementation that you are trying to circuit the command by flags. Hence, the command is not that "official" structure.

I have a proposal, but it might become a project instead of an issue 😅

the idea is mainly to

and the grouping can be something like

$ chatgpt
chatgpt-cli is a cli to...

some other desc 

Enjoy! :)

Chat
  prompt  Run prompt command

Settings Commands
  completion    Generate completion script
  confg  Run config settings command

Other Commands:
  update        Update your chatgpt-cli version
  version       Check your chatgpt-cli version

Use "chatgpt <command> --help" for more information about a given command.

like so

And for the config subcommand, I am thinking we can extend it to

And so on.

I'd like to stop here first to hear your feedback. Otherwise, I still have some idea can be implemented 😆

kardolus commented 7 months ago

Thanks @benbenbang! I think the way you are describing the use of cobra/viper makes total sense. However, I really like the current UX (having the simplicity of chatgpt <message> without a command). Maybe the current flag completion script is the way to go for now? It's not perfect but it works. I have been enjoying flag completion since yesterday :). Great idea! Thank you.

benbenbang commented 7 months ago

Alright, nvm, then.

I will create a PR and keep it as it is, and just add a flag to generate a completion script

benbenbang commented 7 months ago

If you don't mind, I can create another PR to add a makefile to run the test / build the binaries?

benbenbang commented 7 months ago

Thanks @benbenbang! I think the way you are describing the use of cobra/viper makes total sense. However, I really like the current UX (having the simplicity of chatgpt <message> without a command). Maybe the current flag completion script is the way to go for now? It's not perfect but it works. I have been enjoying flag completion since yesterday :). Great idea! Thank you.

My suggestion won't change how you prompt since the default command is the only prompt command. It will be triggered when encountering execute error or use chatgpt prompt xxx

kardolus commented 7 months ago

Thank you! This is great. I like Makefiles, so that sounds good to me. Looking at your PR.