James Edmunds commited on
Commit
5909095
·
1 Parent(s): e0d8c55

feat: add detailed OpenAI API error logging in generation

Browse files
Files changed (1) hide show
  1. src/generator/generator.py +34 -7
src/generator/generator.py CHANGED
@@ -201,11 +201,16 @@ class LyricGenerator:
201
  chat_history = []
202
 
203
  try:
 
 
 
204
  # Get source documents with scores first
 
205
  docs_and_scores = self.vector_store.similarity_search_with_score(
206
  prompt,
207
  k=20
208
  )
 
209
 
210
  # Sort by similarity (convert distance to similarity)
211
  docs_and_scores.sort(key=lambda x: x[1], reverse=False)
@@ -218,15 +223,36 @@ class LyricGenerator:
218
  'artist': doc.metadata['artist'],
219
  'song': doc.metadata['song_title'],
220
  'similarity': similarity,
221
- # First 200 chars
222
- 'content': doc.page_content[:200] + "..."
 
 
 
 
 
 
 
223
  })
 
224
 
225
- # Generate response using invoke
226
- response = self.qa_chain.invoke({
227
- "question": prompt,
228
- "chat_history": chat_history
229
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
  # Add detailed context to response
232
  response["source_documents_with_scores"] = docs_and_scores
@@ -235,4 +261,5 @@ class LyricGenerator:
235
  return response
236
 
237
  except Exception as e:
 
238
  raise RuntimeError(f"Failed to generate lyrics: {str(e)}")
 
201
  chat_history = []
202
 
203
  try:
204
+ print("Starting lyrics generation process...")
205
+ print(f"Using OpenAI model: {Settings.LLM_MODEL}")
206
+
207
  # Get source documents with scores first
208
+ print("Searching for similar documents...")
209
  docs_and_scores = self.vector_store.similarity_search_with_score(
210
  prompt,
211
  k=20
212
  )
213
+ print(f"Found {len(docs_and_scores)} similar documents")
214
 
215
  # Sort by similarity (convert distance to similarity)
216
  docs_and_scores.sort(key=lambda x: x[1], reverse=False)
 
223
  'artist': doc.metadata['artist'],
224
  'song': doc.metadata['song_title'],
225
  'similarity': similarity,
226
+ 'content': doc.page_content[:200] + "..." # First 200 chars
227
+ })
228
+
229
+ try:
230
+ print("Attempting OpenAI API call...")
231
+ # Generate response using invoke
232
+ response = self.qa_chain.invoke({
233
+ "question": prompt,
234
+ "chat_history": chat_history
235
  })
236
+ print("Successfully generated response from OpenAI")
237
 
238
+ except Exception as e:
239
+ error_msg = str(e)
240
+ print(f"OpenAI API error details: {error_msg}")
241
+ if "401" in error_msg:
242
+ raise RuntimeError(
243
+ "OpenAI API authentication failed. Please verify the API key."
244
+ )
245
+ elif "429" in error_msg:
246
+ raise RuntimeError(
247
+ "OpenAI API rate limit exceeded. Please try again in a moment."
248
+ )
249
+ elif "connect" in error_msg.lower():
250
+ raise RuntimeError(
251
+ "Connection to OpenAI failed. This might be a temporary issue. "
252
+ "Please try again."
253
+ )
254
+ else:
255
+ raise RuntimeError(f"OpenAI API error: {error_msg}")
256
 
257
  # Add detailed context to response
258
  response["source_documents_with_scores"] = docs_and_scores
 
261
  return response
262
 
263
  except Exception as e:
264
+ print(f"Error in generate_lyrics: {str(e)}")
265
  raise RuntimeError(f"Failed to generate lyrics: {str(e)}")