-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathset_openai_api_key.py
95 lines (80 loc) · 3.61 KB
/
set_openai_api_key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Description: This script is used to set the OpenAI API key in the environment.
# Author: harmindesinghnijjar
# Date: 2023-02-20
# Updated: 2023-04-28
# Version: 1.0.0
# Usage: python set_openai_api_key.py
# Import required libraries.
from os import environ
import os
import sys
# Define function to check if OpenAI API key exists
def check_openai_api_key():
openai_api_key = os.environ.get('OPENAI_API_KEY', None)
if openai_api_key is None:
print('ERROR: OPENAI_API_KEY is not set in environment variables')
get_openai_api_key()
else:
print('SUCCESS: OPENAI_API_KEY is set in environment variables')
return openai_api_key
# Define functions to get OpenAI API key from file.
def get_openai_api_key_from_file(filepath):
with open(filepath, 'r') as infile:
return infile.read().strip()
# Define function to get OpenAI API key from environment.
def get_openai_api_key_from_env():
return os.environ['OPENAI_API_KEY']
# Define function to get OpenAI API key from user input.
def get_openai_api_key_from_user():
return input("Please enter your OpenAI API key: ")
# Define function to set OpenAI API key in environment.
def set_openai_api_key(api_key):
# Set the OpenAI API key in the environment variable by using the command line.
os.system(f"setx OPENAI_API_KEY {api_key}")
# Ask user how they want to enter their OpenAI API key.
def get_openai_api_key():
# Run an infinite loop until the user enters a valid choice.
while True:
# Print the menu.
print("Welcome to the Quora Automation OpenAI API key setup script.")
print("How would you like to enter your OpenAI API key?")
print("1. From a file")
print("2. From the environment")
print("3. From the console")
print("4. Exit")
choice = input("Please enter a number: ")
# If the user enters 1, ask them for the filepath to the API key file.
if choice == '1':
filepath = input("Please enter the filepath to your API key file: ")
return get_openai_api_key_from_file(filepath)
# If the user enters 2, get the API key from the environment.
elif choice == '2':
return get_openai_api_key_from_env()
# If the user enters 3, ask them to enter the API key.
elif choice == '3':
return get_openai_api_key_from_user()
# If the user enters 4, exit the script.
elif choice == '4':
exit()
# If the user enters anything else, ask them to try again.
else:
print("Invalid choice. Please try again.")
# Define function to display the OpenAI API key in environmental variables.
def open_env():
print("Your OpenAI API key is: %s" % os.environ['OPENAI_API_KEY'])
# Main program of the script.
if __name__ == '__main__':
# Check if the environment variable is already set.
if "OPENAI_API_KEY" in os.environ:
print("Your OpenAI API key is already set in the environment.")
print("You can now run the quora_automation.py script.")
exit()
# If the environment variable is not set, ask the user how they want to enter their OpenAI API key.
else:
print("Your OpenAI API key is not set in the environment.")
print("Let's set it now.")
api_key = get_openai_api_key()
print("Your OpenAI API key is: %s" % api_key)
set_openai_api_key(api_key)
print("Your OpenAI API key has been set in the environment.")
print("You can now run the quora_automation.py script.")