Skip to content

Commit 9d1d433

Browse files
committed
Create Singleton Pattern for Config
1 parent 721fdd2 commit 9d1d433

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ https://github.com/OthersideAI/self-operating-computer/assets/42594239/9e8abc96-
3939

4040

4141
## Run `Self-Operating Computer`
42+
4243
1. **Install the project**
4344
```
4445
pip install self-operating-computer
@@ -78,7 +79,6 @@ cd self-operating-computer
7879
./run.sh
7980
```
8081

81-
8282
## Using `operate` Modes
8383

8484
### Multimodal Models `-m`

operate/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@ class Config:
1616
google_api_key (str): API key for Google.
1717
"""
1818

19+
_instance = None
20+
21+
def __new__(cls):
22+
if cls._instance is None:
23+
cls._instance = super(Config, cls).__new__(cls)
24+
# Put any initialization here
25+
return cls._instance
26+
1927
def __init__(self):
2028
load_dotenv()
2129
self.verbose = False
30+
self.openai_api_key = (
31+
None # instance variables are backups in case saving to a `.env` fails
32+
)
33+
self.google_api_key = (
34+
None # instance variables are backups in case saving to a `.env` fails
35+
)
2236

2337
def initialize_openai(self):
2438
client = OpenAI()
@@ -64,6 +78,10 @@ def prompt_and_save_api_key(self, key_name, key_description):
6478
sys.exit("Operation cancelled by user.")
6579

6680
if key_value:
81+
if key_name == "OPENAI_API_KEY":
82+
self.openai_api_key = key_value
83+
elif key_name == "GOOGLE_API_KEY":
84+
self.google_api_key = key_value
6785
self.save_api_key_to_env(key_name, key_value)
6886
load_dotenv() # Reload environment variables
6987
# Update the instance attribute with the new key

operate/models/apis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636

3737
# Load configuration
38-
VERBOSE = Config().verbose
3938
config = Config()
39+
VERBOSE = config
4040

4141

4242
async def get_next_action(model, messages, objective, session_id):

0 commit comments

Comments
 (0)