nova-api/tests/monkeypatch.py

32 lines
895 B
Python
Raw Normal View History

2023-06-23 02:18:28 +02:00
# Credit: @miss_articulate_python on Discord
import configparser
import os
import pathlib
import openai
# creating a config file, so we can store the api key and other settings
config_file = pathlib.Path(__file__).parent / 'config.ini'
config = configparser.ConfigParser()
config.read_dict({
'openai': {
'api_base': 'http://ENDPOINT',
'api_key': '',
'reset_ip_every_request': 'false'
}
})
if config_file.exists():
config.read(config_file)
with open(config_file, 'w', encoding='utf8') as configfile:
config.write(configfile)
# the normal patch that you apply
openai.api_base = config['openai']['api_base']
openai.api_key = config['openai']['api_key']
# many modules lookup these environment variable, so we pre-emptively set them
os.environ['OPENAI_API_KEY'] = config['openai']['api_key']
2023-06-23 12:07:42 +02:00
os.environ['OPENAI_API_BASE'] = config['openai']['api_base']