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.
This commit is contained in:
Paul Gauthier 2023-06-10 06:40:15 -07:00
parent 806805c0b1
commit 3bef60ecf0
2 changed files with 9 additions and 3 deletions

View file

@ -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()

View file

@ -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",