Skip to content

Commit

Permalink
api key errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dayesouza committed Apr 11, 2024
1 parent 19029d6 commit 0f5fc03
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/pages/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def main():
if secret_input and secret_input != secret:
secrets_handler.write_secret(key, secret_input)
st.rerun()
elif get_key_env() == '':
elif get_key_env() == '' and len(secret) == 0:
st.warning("No OpenAI key found in the environment. Please insert one above.")
elif not secret_input and not secret:
st.info("Using key from the environment.")
Expand Down
6 changes: 6 additions & 0 deletions app/util/AI_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ def generate_text_from_message_list(messages, placeholder=None, prefix='', model
placeholder.markdown(prefix + response, unsafe_allow_html=True)
except Exception as e:
print(f'Error generating from message list: {e}')
if '401' and 'invalid_api_key' in str(e):
if placeholder is not None:
placeholder.error(f'Error generating OpenAI response. Your key is invalid.')
else:
if placeholder is not None:
placeholder.error(f'Error generating OpenAI response.')
return response
26 changes: 19 additions & 7 deletions app/util/Embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ def encode_all(self, texts):
batch = new_texts[i:i+2000]
batch_texts = [x[1] for x in batch]
list_all_embeddings = []
embeddings = [x.embedding for x in openai.client().embeddings.create(input = batch_texts, model=self.model).data]
for j, (ix, text) in enumerate(batch):
hsh = hash(text)
list_all_embeddings.append((hsh, embeddings[j]))
final_embeddings[ix] = np.array(embeddings[j])
self.connection.insert_multiple_into_embeddings(list_all_embeddings)
try:
embeddings = [x.embedding for x in openai.client().embeddings.create(input = batch_texts, model=self.model).data]
for j, (ix, text) in enumerate(batch):
hsh = hash(text)
list_all_embeddings.append((hsh, embeddings[j]))
final_embeddings[ix] = np.array(embeddings[j])
self.connection.insert_multiple_into_embeddings(list_all_embeddings)
except Exception as e:
print(f'Error embedding batch: {e}')
if '401' and 'invalid_api_key' in str(e):
st.error(f'Error generating OpenAI response. Your key is invalid.')
else:
st.error(f'Error generating OpenAI response.')
st.stop()

pb.empty()
return np.array(final_embeddings)
Expand All @@ -76,7 +84,11 @@ def encode(self, text, auto_save = True):
if auto_save:
self.connection.insert_into_embeddings(hsh, embedding, self.username)
return embedding
except:
except Exception as e:
if '401' and 'invalid_api_key' in str(e):
st.error(f'Error generating OpenAI response. Your key is invalid.')
else:
st.error(f'Error generating OpenAI response.')
print(f'Error embedding text: {text}')
return None

Expand Down

0 comments on commit 0f5fc03

Please sign in to comment.