From 0c09f1aaca147ea1db126f4a9a091a699ae12ef0 Mon Sep 17 00:00:00 2001 From: Karim Iskakov Date: Tue, 1 Aug 2023 18:41:31 +0300 Subject: [PATCH] Add OpenAI API Base to config --- README.md | 1 + bot/config.py | 1 + bot/openai_utils.py | 7 ++++++- config/config.example.yml | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d677661..e1b807a 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ If you want to add payments to your bot and create profitable business – write - *9 Mar 2023*: Now you can easily create your own Chat Modes by editing `config/chat_modes.yml` - *8 Mar 2023*: Added voice message recognition with [OpenAI Whisper API](https://openai.com/blog/introducing-chatgpt-and-whisper-apis). Record a voice message and ChatGPT will answer you! - *2 Mar 2023*: Added support of [ChatGPT API](https://platform.openai.com/docs/guides/chat/introduction). +- *1 Aug 2023*: Added OpenAI API Base to config (useful while using OpenAI-compatible API like [LocalAI](https://github.com/go-skynet/LocalAI)) ## Bot commands - `/retry` – Regenerate last bot answer diff --git a/bot/config.py b/bot/config.py index 9c5de4c..dae169e 100644 --- a/bot/config.py +++ b/bot/config.py @@ -14,6 +14,7 @@ config_env = dotenv.dotenv_values(config_dir / "config.env") # config parameters telegram_token = config_yaml["telegram_token"] openai_api_key = config_yaml["openai_api_key"] +openai_api_base = config_yaml.get("openai_api_base", None) allowed_telegram_usernames = config_yaml["allowed_telegram_usernames"] new_dialog_timeout = config_yaml["new_dialog_timeout"] enable_message_streaming = config_yaml.get("enable_message_streaming", True) diff --git a/bot/openai_utils.py b/bot/openai_utils.py index 5a68302..611faf4 100644 --- a/bot/openai_utils.py +++ b/bot/openai_utils.py @@ -2,7 +2,12 @@ import config import tiktoken import openai + + +# setup openai openai.api_key = config.openai_api_key +if config.openai_api_base is not None: + openai.api_base = config.openai_api_base OPENAI_COMPLETION_OPTIONS = { @@ -151,7 +156,7 @@ class ChatGPT: tokens_per_name = -1 # if there's a name, the role is omitted elif model == "gpt-3.5-turbo": tokens_per_message = 4 - tokens_per_name = -1 + tokens_per_name = -1 elif model == "gpt-4": tokens_per_message = 3 tokens_per_name = 1 diff --git a/config/config.example.yml b/config/config.example.yml index 2af9698..5da7493 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -1,5 +1,6 @@ telegram_token: "" openai_api_key: "" +openai_api_base: null # leave null to use default api base or you can put your own base url here allowed_telegram_usernames: [] # if empty, the bot is available to anyone. pass a username string to allow it and/or user ids as positive integers and/or channel ids as negative integers new_dialog_timeout: 600 # new dialog starts after timeout (in seconds) return_n_generated_images: 1