From 733a37b900c9cf8e13da261414bf8b2966b26335 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Sun, 13 Oct 2024 13:50:37 -0500 Subject: [PATCH] Add support for custom OpenAI API base URL --- README.md | 12 +++++++++++- cmd/aicommit/main.go | 25 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 18c0a88..d592880 100644 --- a/README.md +++ b/README.md @@ -83,4 +83,14 @@ determine the style guide. It is optional, but if it exists, it will be followed even if the rules there diverge from the norm. If there is no repo style guide, `aicommit` will look for a user style guide -in `~/COMMITS.md`. \ No newline at end of file +in `~/COMMITS.md`. + +## Other Providers + +You may set `OPENAI_BASE_URL` to use other OpenAI compatible APIs with `aicommit`. +So far, I've tested it with [LiteLLM](https://github.com/BerriAI/litellm) across +local models (via ollama) and Anthropic. I have yet to find a local model +that is well-steered by the prompt design here, but the Anthropic Claude 3.5 +commit messages are on par with 4o. My theory for why local models don't work well +is (even the "Instruct" fine-tuned models) have much worse instruction +fine-tuning than the flagship commercial models. \ No newline at end of file diff --git a/cmd/aicommit/main.go b/cmd/aicommit/main.go index f1acc95..dcfb210 100644 --- a/cmd/aicommit/main.go +++ b/cmd/aicommit/main.go @@ -193,12 +193,13 @@ func run(inv *serpent.Invocation, opts runOptions) error { } type runOptions struct { - client *openai.Client - model string - dryRun bool - amend bool - ref string - context []string + client *openai.Client + openAIBaseURL string + model string + dryRun bool + amend bool + ref string + context []string } func main() { @@ -253,7 +254,9 @@ func main() { return nil } - client := openai.NewClient(openAIKey) + oaiConfig := openai.DefaultConfig(openAIKey) + oaiConfig.BaseURL = opts.openAIBaseURL + client := openai.NewClientWithConfig(oaiConfig) opts.client = client if len(inv.Args) > 0 { opts.ref = inv.Args[0] @@ -268,6 +271,14 @@ func main() { Flag: "openai-key", Value: serpent.StringOf(&cliOpenAIKey), }, + { + Name: "openai-base-url", + Description: "The base URL to use for the OpenAI API.", + Env: "OPENAI_BASE_URL", + Flag: "openai-base-url", + Value: serpent.StringOf(&opts.openAIBaseURL), + Default: "https://api.openai.com/v1", + }, { Name: "model", Description: "The model to use, e.g. gpt-4o or gpt-4o-mini.",