@@ -15,36 +15,68 @@ def prepare_env():
1515 If it doesn't exist, creates it and returns the .env.local file path.
1616 """
1717 if not os .path .exists ('.llmstack' ) and not os .path .exists (os .path .join (os .path .expanduser ('~' ), '.llmstack' )):
18+ # Create .llmstack dir in user's home dir
1819 os .mkdir (os .path .join (os .path .expanduser ('~' ), '.llmstack' ))
1920
20- # Copy .env.local file from installed package to ~/.llmstack/.env.local
21+ if not os .path .exists ('.llmstack/config' ) and not os .path .exists (os .path .join (os .path .expanduser ('~' ), '.llmstack/config' )):
22+ # Copy config.toml file from installed package to ~/.llmstack/config
2123 import shutil
22- shutil .copyfile (os .path .join (os .path .dirname (__file__ ), '.env.local' ), os .path .join (
23- os .path .expanduser ('~' ), '.llmstack' , '.env.local' ))
24+ shutil .copyfile (os .path .join (os .path .dirname (__file__ ), 'config.toml' ), os .path .join (
25+ os .path .expanduser ('~' ), '.llmstack' , 'config' ))
26+
27+ # Given this is the first time the user is running llmstack, we should
28+ # ask the user for secret key, cipher_key_salt, database_password and save it in the config file
29+ import toml
30+ import secrets
31+ config_path = os .path .join (
32+ os .path .expanduser ('~' ), '.llmstack' , 'config' )
33+ config = {}
34+ with open (config_path ) as f :
35+ config = toml .load (f )
36+ config ['llmstack' ]['secret_key' ] = secrets .token_urlsafe (32 )
37+ config ['llmstack' ]['cipher_key_salt' ] = secrets .token_urlsafe (32 )
38+ config ['llmstack' ]['database_password' ] = secrets .token_urlsafe (32 )
39+ # Ask the user for admin username, email and password
40+ sys .stdout .write (
41+ 'It looks like you are running LLMStack for the first time. Please provide the following information:\n \n ' )
42+
43+ config ['llmstack' ]['admin_username' ] = input (
44+ 'Enter admin username: (default: admin)' ) or 'admin'
45+ config ['llmstack' ]['admin_email' ] = input (
46+ 'Enter admin email: ' ) or ''
47+ config ['llmstack' ]['admin_password' ] = input (
48+ 'Enter admin password: (default: promptly) ' ) or 'promptly'
49+ config ['llmstack' ]['default_openai_api_key' ] = input (
50+ 'Enter default OpenAI API key: (Leave empty to configure in settings later) ' ) or ''
51+ with open (config_path , 'w' ) as f :
52+ toml .dump (config , f )
2453
2554 # Chdir to .llmstack
2655 if not os .path .exists ('.llmstack' ) and os .path .exists (os .path .join (os .path .expanduser ('~' ), '.llmstack' )):
2756 os .chdir (os .path .join (os .path .expanduser ('~' ), '.llmstack' ))
2857 elif os .path .exists ('.llmstack' ):
2958 os .chdir ('.llmstack' )
3059
31- # Throw error if .env.local file doesn't exist
32- if not os .path .exists ('.env.local ' ):
60+ # Throw error if config file doesn't exist
61+ if not os .path .exists ('config ' ):
3362 sys .exit (
34- 'ERROR: .env.local file not found. Please create one in ~/.llmstack/.env.local ' )
63+ 'ERROR: config file not found. Please create one in ~/.llmstack/config ' )
3564
36- return os .path .join ('.env.local ' )
65+ return os .path .join ('config ' )
3766
3867
3968def main ():
4069 """Main entry point for the application script"""
4170
42- # Get .env.local file path
71+ # Get config file path
4372 env_path = prepare_env ()
4473
45- # Load environment variables from .env.local file
46- from dotenv import load_dotenv
47- load_dotenv (env_path )
74+ # Load environment variables from config under [llmstack] section
75+ import toml
76+ with open (env_path ) as f :
77+ config = toml .load (f )
78+ for key in config ['llmstack' ]:
79+ os .environ [key .upper ()] = str (config ['llmstack' ][key ])
4880
4981 if len (sys .argv ) > 1 and sys .argv [1 ] == 'runserver' :
5082 print ('Starting LLMStack' )
0 commit comments