Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When starting for the first time, GPTcmd generates an error if the api_key client option is not defined. #1

Closed
rperez030 opened this issue Dec 1, 2024 · 3 comments

Comments

@rperez030
Copy link

If I try running GPTcmd without defining the Open AI API key first, I get the following error.


➜  ~ gptcmd
Traceback (most recent call last):
  File "/opt/homebrew/bin/gptcmd", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/cli.py", line 1273, in main
    shell = Gptcmd(config=config)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/cli.py", line 73, in __init__
    self.config = config or ConfigManager.from_toml()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 136, in from_toml
    return cls(tomllib.load(fin))
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 81, in __init__
    self.accounts = self._configure_accounts(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 153, in _configure_accounts
    res[name] = Account(name=name, provider=provider.from_config(conf))
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/llm/openai.py", line 47, in from_config
    client = openai.OpenAI(**client_opts)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/_client.py", line 105, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
, in __init__
    self.config = config or ConfigManager.from_toml()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 136, in from_toml
    return cls(tomllib.load(fin))
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 81, in __init__
    self.accounts = self._configure_accounts(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/config.py", line 153, in _configure_accounts
    res[name] = Account(name=name, provider=provider.from_config(conf))
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/gptcmd/llm/openai.py", line 47, in from_config
    client = openai.OpenAI(**client_opts)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/_client.py", line 105, in __init__
    raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable


@codeofdusk
Copy link
Owner

This is expected behaviour, though granted the error could be much clearer. I think it makes sense to (either or both):

  • Catch OpenAIError when initializing a client in gptcmd.llm.OpenAI.from_config and raise a ConfigError from it, which would hide the traceback. Potentially write a Gptcmd-specific message (though I think the default exception message is probably fine).
  • When starting Gptcmd, if the config can't be read (i.e. if ConfigError is raised when instantiating a ConfigManager), prompt the user whether they'd like to edit their config. If so, use a default editor since we can't read the user's choice from their config (which, in practice, means either whatever is defined in the EDITOR environment variable or a platform-specific best guess).

@rperez030
Copy link
Author

I think the second option, prompting the user to edit their configuration, would be more helpful.

@codeofdusk
Copy link
Owner

I've included the first solution (for now) in the 2.0.2 release. Thanks @rperez030!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants