From 3bef60ecf06f3729e0fb076bd7253d0655a88e22 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 10 Jun 2023 06:40:15 -0700 Subject: [PATCH 1/3] aider: Added option to set OpenAI API base endpoint. # Context: USER: is it possible to add OPEN_AI_BASE or openai.api_base endpoint default: openai.api_base = "https://api.openai.com/v1/chat/completions" ASSISTANT: Yes, you can add an option to set the OpenAI API base endpoint. Here's how to do it: 1. Add a new argument to the argument parser in `aider/main.py`. 2. Set the `openai.api_base` in the `Coder` class in `aider/coder.py`. Here are the changes you need to make: ```python aider/main.py <<<<<<< ORIGINAL parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) ======= parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", default="https://api.openai.com/v1/chat/completions", help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1/chat/completions)", ) >>>>>>> UPDATED ``` ```python aider/coder.py <<<<<<< ORIGINAL if openai_api_key: openai.api_key = openai_api_key else: raise MissingAPIKeyError("No OpenAI API key provided.") ======= if not openai_api_key: raise MissingAPIKeyError("No OpenAI API key provided.") openai.api_key = openai_api_key openai.api_base = args.openai_api_base >>>>>>> UPDATED ``` Now you can use the `--openai-api-base` option to set the OpenAI API base endpoint when running the script. --- aider/coder.py | 6 +++--- aider/main.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index 9fc78c3be..b105c4715 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -51,10 +51,10 @@ class Coder: verbose=False, openai_api_key=None, ): - if openai_api_key: - openai.api_key = openai_api_key - else: + if not openai_api_key: raise MissingAPIKeyError("No OpenAI API key provided.") + openai.api_key = openai_api_key + openai.api_base = args.openai_api_base self.verbose = verbose self.abs_fnames = set() diff --git a/aider/main.py b/aider/main.py index 3bba81d57..14ea79bb1 100644 --- a/aider/main.py +++ b/aider/main.py @@ -151,6 +151,12 @@ def main(args=None, input=None, output=None): help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) + parser.add_argument( + "--openai-api-base", + metavar="OPENAI_API_BASE", + default="https://api.openai.com/v1/chat/completions", + help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1/chat/completions)", + ) parser.add_argument( "--dry-run", action="store_true", From 3e7f791112b6ac3e7c6ad3d384f3fb5b0af5af7a Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 10 Jun 2023 10:40:54 -0700 Subject: [PATCH 2/3] aider: Added support for passing `openai_api_base` into the Coder class. # Context: USER: we need to pass `openai_api_base` into Coder ASSISTANT: To pass `openai_api_base` into the Coder class, you need to modify the `aider/main.py` file and update the Coder class instantiation. Here's the change you need to make: ```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, map_tokens=args.map_tokens, verbose=args.verbose, openai_api_key=args.openai_api_key, ) ======= 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, map_tokens=args.map_tokens, verbose=args.verbose, openai_api_key=args.openai_api_key, openai_api_base=args.openai_api_base, ) >>>>>>> UPDATED ``` Now, you also need to update the `__init__` method of the Coder class in the `aider/coder.py` file to accept the `openai_api_base` argument and use it: ```python aider/coder.py <<<<<<< ORIGINAL def __init__( self, io, main_model=models.GPT4.name, fnames=None, pretty=True, show_diffs=False, auto_commits=True, dirty_commits=True, dry_run=False, map_tokens=1024, verbose=False, openai_api_key=None, ): if not openai_api_key: raise MissingAPIKeyError("No OpenAI API key provided.") openai.api_key = openai_api_key openai.api_base = args.openai_api_base ======= def __init__( self, io, main_model=models.GPT4.name, fnames=None, pretty=True, show_diffs=False, auto_commits=True, dirty_commits=True, dry_run=False, map_tokens=1024, verbose=False, openai_api_key=None, openai_api_base=None, ): if not openai_api_key: raise MissingAPIKeyError("No OpenAI API key provided.") openai.api_key = openai_api_key openai.api_base = openai_api_base >>>>>>> UPDATED ``` These changes will allow you to pass the `openai_api_base` value into the Coder class and use it. --- aider/coder.py | 3 ++- aider/main.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/aider/coder.py b/aider/coder.py index b105c4715..99ae29e28 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -50,11 +50,12 @@ class Coder: map_tokens=1024, verbose=False, openai_api_key=None, + openai_api_base=None, ): if not openai_api_key: raise MissingAPIKeyError("No OpenAI API key provided.") openai.api_key = openai_api_key - openai.api_base = args.openai_api_base + openai.api_base = openai_api_base self.verbose = verbose self.abs_fnames = set() diff --git a/aider/main.py b/aider/main.py index 14ea79bb1..505e0f908 100644 --- a/aider/main.py +++ b/aider/main.py @@ -227,6 +227,7 @@ def main(args=None, input=None, output=None): map_tokens=args.map_tokens, verbose=args.verbose, openai_api_key=args.openai_api_key, + openai_api_base=args.openai_api_base, ) if args.dirty_commits: From 193901ee437fd8a3b5dd3878c7b96718fc8d2afc Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 10 Jun 2023 10:50:45 -0700 Subject: [PATCH 3/3] Changed OpenAI API base endpoint to https://api.openai.com/v1. --- aider/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aider/main.py b/aider/main.py index 505e0f908..8cc34ac12 100644 --- a/aider/main.py +++ b/aider/main.py @@ -154,8 +154,8 @@ def main(args=None, input=None, output=None): parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", - default="https://api.openai.com/v1/chat/completions", - help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1/chat/completions)", + default="https://api.openai.com/v1", + help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1)", ) parser.add_argument( "--dry-run",