From 3bef60ecf06f3729e0fb076bd7253d0655a88e22 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 10 Jun 2023 06:40:15 -0700 Subject: [PATCH] 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",