diff --git a/config.dist.ini b/config.dist.ini index 8a43ed1..91fe066 100644 --- a/config.dist.ini +++ b/config.dist.ini @@ -93,6 +93,13 @@ APIKey = sk-yoursecretkey # # MaxMessages = 20 +# The base URL of the OpenAI API +# +# Setting this allows you to use a self-hosted AI model for chat completions +# using something like https://github.com/abetlen/llama-cpp-python +# +# BaseURL = https://openai.local/v1 + ############################################################################### [WolframAlpha] diff --git a/src/gptbot/classes/bot.py b/src/gptbot/classes/bot.py index e0cf205..4c0b442 100644 --- a/src/gptbot/classes/bot.py +++ b/src/gptbot/classes/bot.py @@ -143,6 +143,10 @@ class GPTBot: bot.max_tokens = config["OpenAI"].getint("MaxTokens", bot.max_tokens) bot.max_messages = config["OpenAI"].getint("MaxMessages", bot.max_messages) + if "BaseURL" in config["OpenAI"]: + bot.chat_api.base_url = config["OpenAI"]["BaseURL"] + bot.image_api = None + # Set up WolframAlpha if "WolframAlpha" in config: bot.calculation_api = WolframAlpha( diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index 3423ba2..fcff3e3 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -29,6 +29,7 @@ class OpenAI: self.api_key = api_key self.chat_model = chat_model or self.chat_model self.logger = logger or Logger() + self.base_url = openai.api_base async def _request_with_retries(self, request: partial, attempts: int = 5, retry_interval: int = 2) -> AsyncGenerator[Any | list | Dict, None]: """Retry a request a set number of times if it fails. @@ -73,7 +74,8 @@ class OpenAI: model=self.chat_model, messages=messages, api_key=self.api_key, - user=user + user=user, + api_base=self.base_url, ) response = await self._request_with_retries(chat_partial) @@ -115,7 +117,8 @@ Only the event_types mentioned above are allowed, you must not respond in any ot model=self.chat_model, messages=messages, api_key=self.api_key, - user=user + user=user, + api_base=self.base_url, ) response = await self._request_with_retries(chat_partial) @@ -147,7 +150,8 @@ Only the event_types mentioned above are allowed, you must not respond in any ot n=1, api_key=self.api_key, size="1024x1024", - user=user + user=user, + api_base=self.base_url, ) response = await self._request_with_retries(image_partial)