James Edmunds commited on
Commit
e10d2ca
·
1 Parent(s): c18d3e8

Trying to fix connection error

Browse files
config/settings.py CHANGED
@@ -13,6 +13,11 @@ class Settings:
13
 
14
  # API Keys
15
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  # Directory Paths
 
13
 
14
  # API Keys
15
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
16
+ if not OPENAI_API_KEY:
17
+ raise ValueError(
18
+ "OpenAI API key not found. Please set OPENAI_API_KEY "
19
+ "environment variable."
20
+ )
21
  HF_TOKEN = os.getenv("HF_TOKEN")
22
 
23
  # Directory Paths
requirements.txt CHANGED
@@ -6,6 +6,7 @@ chromadb>=0.4.22
6
  streamlit==1.41.0
7
  python-dotenv==1.0.1
8
  huggingface-hub>=0.19.4
 
9
  pytest>=7.4.0
10
  black>=23.0.0
11
  flake8>=6.0.0
 
6
  streamlit==1.41.0
7
  python-dotenv==1.0.1
8
  huggingface-hub>=0.19.4
9
+ datasets>=2.16.0
10
  pytest>=7.4.0
11
  black>=23.0.0
12
  flake8>=6.0.0
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,25 @@ 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 +250,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...")
205
+ print(f"OpenAI API Key present: {bool(Settings.OPENAI_API_KEY)}")
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
+ print("Generating response using QA chain...")
230
+ try:
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")
237
+ except Exception as e:
238
+ print(f"Error in QA chain: {str(e)}")
239
+ if "Connection" in str(e):
240
+ raise RuntimeError(
241
+ "OpenAI API connection error. Please check your internet "
242
+ "connection and API key."
243
+ )
244
+ raise
245
 
246
  # Add detailed context to response
247
  response["source_documents_with_scores"] = docs_and_scores
 
250
  return response
251
 
252
  except Exception as e:
253
+ print(f"Error in generate_lyrics: {str(e)}")
254
  raise RuntimeError(f"Failed to generate lyrics: {str(e)}")