Added the possibility to configure SQLITE db path

Now, the SQLITE db path for the user API keys can be configured through the USER_KEY_DB_PATH env variable

Signed-off-by: Alessandro Bellia <casildaserrano@yahoo.it>
Alessandro Bellia 2 years ago
parent bec4e3c40a
commit 91e84dddf6
No known key found for this signature in database
GPG Key ID: 9C4D7AA47CD637D0

@ -178,7 +178,9 @@ USER_INPUT_API_KEYS="True"
Then, restart the bot, and it will set up the system for everyone to input their own API keys.
The bot will use SQLite to store API keys for the users, each user's key will be saved with a USER_ID <> API_KEY mapping in SQLite, and will be persistent across restarts. All the data will be saved in a file called `user_key_db.sqlite` in the current working directory of the bot.
The bot will use SQLite to store API keys for the users, each user's key will be saved with a USER_ID <> API_KEY mapping in SQLite, and will be persistent across restarts. All the data will be saved in a file called `user_key_db.sqlite` in the current working directory of the bot (by default).
You can configure the location of the sqlite database by changing the `USER_KEY_DB_PATH` variable in your `.env` file (check the `sample.env` file for reference).
With this feature enabled, any attempt to use a GPT3 or DALL-E command without a valid API key set for the user will pop up the following modal for them to enter their API key:
<img src="https://i.imgur.com/ZDScoWk.png"/>
@ -239,6 +241,8 @@ GPT_ROLES="openai,gpt"
WELCOME_MESSAGE="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaging with you!" # This is a fallback message if gpt3 fails to generate a welcome message.
# This is the channel that auto-moderation alerts will be sent to
MODERATIONS_ALERT_CHANNEL="977697652147892304"
# User API key db path configuration. This is where the user API keys will be stored.
USER_KEY_DB_PATH = user_key_db.sqlite
```
**Permissions**

@ -41,7 +41,18 @@ if USER_INPUT_API_KEYS:
print(
"This server was configured to enforce user input API keys. Doing the required database setup now"
)
USER_KEY_DB = SqliteDict("user_key_db.sqlite")
# Get USER_KEY_DB from the environment variable
USER_KEY_DB_PATH = EnvService.get_user_key_db_path()
# Check if USER_KEY_DB_PATH is valid
if not USER_KEY_DB_PATH:
print("No user key database path was provided. Defaulting to user_key_db.sqlite")
USER_KEY_DB_PATH = "user_key_db.sqlite"
else:
# append "user_key_db.sqlite" to USER_KEY_DB_PATH if it doesn't already end with .sqlite
if not USER_KEY_DB_PATH.match("*.sqlite"):
# append "user_key_db.sqlite" to USER_KEY_DB_PATH
USER_KEY_DB_PATH = USER_KEY_DB_PATH / "user_key_db.sqlite"
USER_KEY_DB = SqliteDict(USER_KEY_DB_PATH)
print("Retrieved/created the user key database")

@ -1,6 +1,8 @@
import os
import sys
from pathlib import Path
from typing import Union
from dotenv import load_dotenv
@ -193,3 +195,14 @@ class EnvService:
return False
except:
return False
@staticmethod
def get_user_key_db_path() -> Union[Path, None]:
try:
user_key_db_path = os.getenv("USER_KEY_DB_PATH")
if user_key_db_path is None:
return None
else:
return Path(user_key_db_path)
except:
return None

@ -18,4 +18,7 @@ WELCOME_MESSAGE = "Hi There! Welcome to our Discord server. We hope you'll enjoy
USER_INPUT_API_KEYS="False" # If True, users must use their own API keys for OpenAI. If False, the bot will use the API key in the .env file.
# Moderations Service alert channel, this is where moderation alerts will be sent as a default if enabled
MODERATIONS_ALERT_CHANNEL = "977697652147892304"
MODERATIONS_ALERT_CHANNEL = "977697652147892304"
# User API key db path configuration. This is where the user API keys will be stored.
USER_KEY_DB_PATH = user_key_db.sqlite
Loading…
Cancel
Save