From abf73d00a2e90a556388973f916cb24385539508 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 27 May 2023 06:17:11 -0700 Subject: [PATCH] aider: Added `--openai-api-key` argument to the command-line parser, passed it to the `Coder` class, and set the `openai.api_key` accordingly. # Context: USER: add a new arg --openai-api-key. pass it to Coder ASSISTANT: I will add a new argument `--openai-api-key` and pass it to the `Coder` class. Here are the changes: ```python aider/main.py <<<<<<< ORIGINAL parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable dirty commit of changes", ) ======= parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable dirty commit of changes", ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", ) >>>>>>> UPDATED ``` ```python aider/main.py <<<<<<< ORIGINAL coder = Coder( io, main_model=args.model, fnames=args.files, pretty=args.pretty, show_diffs=args.show_diffs, auto_commits=args.auto_commits, dirty_commits=args.dirty_commits, dry_run=args.dry_run, use_ctags=args.ctags, verbose=args.verbose, ) ======= coder = Coder( io, main_model=args.model, fnames=args.files, pretty=args.pretty, show_diffs=args.show_diffs, auto_commits=args.auto_commits, dirty_commits=args.dirty_commits, dry_run=args.dry_run, use_ctags=args.ctags, verbose=args.verbose, openai_api_key=args.openai_api_key, ) >>>>>>> UPDATED ``` ```python aider/coder.py <<<<<<< ORIGINAL openai.api_key = os.getenv("OPENAI_API_KEY") class Coder: abs_fnames = None repo = None last_aider_commit_hash = None last_asked_for_commit_time = 0 def __init__( self, io, main_model="gpt-4", fnames=None, pretty=True, show_diffs=False, auto_commits=True, dirty_commits=True, dry_run=False, use_ctags=False, verbose=False, ): ======= class Coder: abs_fnames = None repo = None last_aider_commit_hash = None last_asked_for_commit_time = 0 def __init__( self, io, main_model="gpt-4", fnames=None, pretty=True, show_diffs=False, auto_commits=True, dirty_commits=True, dry_run=False, use_ctags=False, verbose=False, openai_api_key=None, ): if openai_api_key: openai.api_key = openai_api_key else: openai.api_key = os.getenv("OPENAI_API_KEY") >>>>>>> UPDATED ``` These changes add the `--openai-api-key` argument to the command-line parser, pass it to the `Coder` class, and set the `openai.api_key` accordingly. --- aider/coder.py | 8 +++++--- aider/main.py | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index f23fd6dfc..1890d2d4a 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -20,9 +20,6 @@ from aider import prompts from aider.commands import Commands from aider.repomap import RepoMap -openai.api_key = os.getenv("OPENAI_API_KEY") - - class Coder: abs_fnames = None repo = None @@ -41,7 +38,12 @@ class Coder: dry_run=False, use_ctags=False, verbose=False, + openai_api_key=None, ): + if openai_api_key: + openai.api_key = openai_api_key + else: + openai.api_key = os.getenv("OPENAI_API_KEY") self.verbose = verbose self.abs_fnames = set() self.cur_messages = [] diff --git a/aider/main.py b/aider/main.py index 20a461fa4..ba91e86eb 100644 --- a/aider/main.py +++ b/aider/main.py @@ -127,6 +127,11 @@ def main(args=None, input=None, output=None): dest="dirty_commits", help="Disable dirty commit of changes", ) + parser.add_argument( + "--openai-api-key", + metavar="OPENAI_API_KEY", + help="Specify the OpenAI API key", + ) parser.add_argument( "--dry-run", action="store_true", @@ -194,6 +199,7 @@ def main(args=None, input=None, output=None): dry_run=args.dry_run, use_ctags=args.ctags, verbose=args.verbose, + openai_api_key=args.openai_api_key, ) if args.dirty_commits: coder.commit(ask=True, prefix="wip: ", which="repo_files")