dsmueller commited on
Commit
8e218e4
1 Parent(s): 6a34935

Refactor API key handling in setup.py

Browse files
Files changed (1) hide show
  1. scripts/setup.py +9 -10
scripts/setup.py CHANGED
@@ -102,25 +102,16 @@ def load_sidebar(config_file,
102
  sb_out['keys']={}
103
  if 'llm_source' in sb_out and sb_out['llm_source'] == 'OpenAI':
104
  sb_out['keys']['OPENAI_API_KEY'] = st.sidebar.text_input('OpenAI API Key', type='password')
105
- if sb_out['keys']['OPENAI_API_KEY']=='':
106
- raise Exception('OpenAI API Key is required.')
107
  elif 'query_model' in sb_out and sb_out['query_model'] == 'Openai':
108
  sb_out['keys']['OPENAI_API_KEY'] = st.sidebar.text_input('OpenAI API Key', type='password')
109
- if sb_out['keys']['OPENAI_API_KEY']=='':
110
- raise Exception('OpenAI API Key is required.')
111
  if 'llm_source' in sb_out and sb_out['llm_source']=='Hugging Face':
112
  sb_out['keys']['HUGGINGFACEHUB_API_TOKEN'] = st.sidebar.text_input('Hugging Face API Key', type='password')
113
- if sb_out['keys']['HUGGINGFACEHUB_API_TOKEN']=='':
114
- raise Exception('Hugging Face API Key is required.')
115
  if 'query_model' in sb_out and sb_out['query_model']=='Voyage':
116
  sb_out['keys']['VOYAGE_API_KEY'] = st.sidebar.text_input('Voyage API Key', type='password')
117
- if sb_out['keys']['VOYAGE_API_KEY']=='':
118
- raise Exception('Voyage API Key is required.')
119
  if 'index_type' in sb_out and sb_out['index_type']=='Pinecone':
120
  sb_out['keys']['PINECONE_API_KEY']=st.sidebar.text_input('Pinecone API Key',type='password')
121
- if sb_out['keys']['PINECONE_API_KEY']=='':
122
- raise Exception('Pinecone API Key is required.')
123
  return sb_out
 
124
  def set_secrets(sb):
125
  """
126
  Sets secrets from environment file, or from sidebar if not available.
@@ -131,20 +122,28 @@ def set_secrets(sb):
131
  if not secrets['OPENAI_API_KEY'] and 'keys' in sb and 'OPENAI_API_KEY' in sb['keys']:
132
  secrets['OPENAI_API_KEY'] = sb['keys']['OPENAI_API_KEY']
133
  os.environ['OPENAI_API_KEY'] = secrets['OPENAI_API_KEY']
 
 
134
  openai.api_key = secrets['OPENAI_API_KEY']
135
 
136
  secrets['VOYAGE_API_KEY'] = os.getenv('VOYAGE_API_KEY')
137
  if not secrets['VOYAGE_API_KEY'] and 'keys' in sb and 'VOYAGE_API_KEY' in sb['keys']:
138
  secrets['VOYAGE_API_KEY'] = sb['keys']['VOYAGE_API_KEY']
139
  os.environ['VOYAGE_API_KEY'] = secrets['VOYAGE_API_KEY']
 
 
140
 
141
  secrets['PINECONE_API_KEY'] = os.getenv('PINECONE_API_KEY')
142
  if not secrets['PINECONE_API_KEY'] and 'keys' in sb and 'PINECONE_API_KEY' in sb['keys']:
143
  secrets['PINECONE_API_KEY'] = sb['keys']['PINECONE_API_KEY']
144
  os.environ['PINECONE_API_KEY'] = secrets['PINECONE_API_KEY']
 
 
145
 
146
  secrets['HUGGINGFACEHUB_API_TOKEN'] = os.getenv('HUGGINGFACEHUB_API_TOKEN')
147
  if not secrets['HUGGINGFACEHUB_API_TOKEN'] and 'keys' in sb and 'HUGGINGFACEHUB_API_TOKEN' in sb['keys']:
148
  secrets['HUGGINGFACEHUB_API_TOKEN'] = sb['keys']['HUGGINGFACEHUB_API_TOKEN']
149
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = secrets['HUGGINGFACEHUB_API_TOKEN']
 
 
150
  return secrets
 
102
  sb_out['keys']={}
103
  if 'llm_source' in sb_out and sb_out['llm_source'] == 'OpenAI':
104
  sb_out['keys']['OPENAI_API_KEY'] = st.sidebar.text_input('OpenAI API Key', type='password')
 
 
105
  elif 'query_model' in sb_out and sb_out['query_model'] == 'Openai':
106
  sb_out['keys']['OPENAI_API_KEY'] = st.sidebar.text_input('OpenAI API Key', type='password')
 
 
107
  if 'llm_source' in sb_out and sb_out['llm_source']=='Hugging Face':
108
  sb_out['keys']['HUGGINGFACEHUB_API_TOKEN'] = st.sidebar.text_input('Hugging Face API Key', type='password')
 
 
109
  if 'query_model' in sb_out and sb_out['query_model']=='Voyage':
110
  sb_out['keys']['VOYAGE_API_KEY'] = st.sidebar.text_input('Voyage API Key', type='password')
 
 
111
  if 'index_type' in sb_out and sb_out['index_type']=='Pinecone':
112
  sb_out['keys']['PINECONE_API_KEY']=st.sidebar.text_input('Pinecone API Key',type='password')
 
 
113
  return sb_out
114
+
115
  def set_secrets(sb):
116
  """
117
  Sets secrets from environment file, or from sidebar if not available.
 
122
  if not secrets['OPENAI_API_KEY'] and 'keys' in sb and 'OPENAI_API_KEY' in sb['keys']:
123
  secrets['OPENAI_API_KEY'] = sb['keys']['OPENAI_API_KEY']
124
  os.environ['OPENAI_API_KEY'] = secrets['OPENAI_API_KEY']
125
+ if os.environ['OPENAI_API_KEY']=='':
126
+ raise Exception('OpenAI API Key is required.')
127
  openai.api_key = secrets['OPENAI_API_KEY']
128
 
129
  secrets['VOYAGE_API_KEY'] = os.getenv('VOYAGE_API_KEY')
130
  if not secrets['VOYAGE_API_KEY'] and 'keys' in sb and 'VOYAGE_API_KEY' in sb['keys']:
131
  secrets['VOYAGE_API_KEY'] = sb['keys']['VOYAGE_API_KEY']
132
  os.environ['VOYAGE_API_KEY'] = secrets['VOYAGE_API_KEY']
133
+ if os.environ['VOYAGE_API_KEY']=='':
134
+ raise Exception('Voyage API Key is required.')
135
 
136
  secrets['PINECONE_API_KEY'] = os.getenv('PINECONE_API_KEY')
137
  if not secrets['PINECONE_API_KEY'] and 'keys' in sb and 'PINECONE_API_KEY' in sb['keys']:
138
  secrets['PINECONE_API_KEY'] = sb['keys']['PINECONE_API_KEY']
139
  os.environ['PINECONE_API_KEY'] = secrets['PINECONE_API_KEY']
140
+ if os.environ['PINECONE_API_KEY']=='':
141
+ raise Exception('Pinecone API Key is required.')
142
 
143
  secrets['HUGGINGFACEHUB_API_TOKEN'] = os.getenv('HUGGINGFACEHUB_API_TOKEN')
144
  if not secrets['HUGGINGFACEHUB_API_TOKEN'] and 'keys' in sb and 'HUGGINGFACEHUB_API_TOKEN' in sb['keys']:
145
  secrets['HUGGINGFACEHUB_API_TOKEN'] = sb['keys']['HUGGINGFACEHUB_API_TOKEN']
146
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = secrets['HUGGINGFACEHUB_API_TOKEN']
147
+ if os.environ['HUGGINGFACEHUB_API_TOKEN']=='':
148
+ raise Exception('Hugging Face API Key is required.')
149
  return secrets