James Edmunds commited on
Commit ·
6911b3d
1
Parent(s): 33731b6
more fixes to HF run
Browse files- src/generator/generator.py +34 -5
src/generator/generator.py
CHANGED
|
@@ -47,15 +47,22 @@ class LyricGenerator:
|
|
| 47 |
|
| 48 |
@retry(
|
| 49 |
retry=retry_if_exception_type((APIConnectionError, RateLimitError)),
|
| 50 |
-
wait=wait_exponential(multiplier=
|
| 51 |
-
stop=stop_after_attempt(
|
| 52 |
)
|
| 53 |
def _create_embeddings_with_retry(self):
|
| 54 |
"""Create OpenAI embeddings with retry logic"""
|
| 55 |
try:
|
| 56 |
return OpenAIEmbeddings(
|
| 57 |
openai_api_key=Settings.OPENAI_API_KEY,
|
| 58 |
-
timeout=60 # Increase timeout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error creating embeddings: {type(e).__name__}: {str(e)}")
|
|
@@ -310,16 +317,26 @@ class LyricGenerator:
|
|
| 310 |
|
| 311 |
@retry(
|
| 312 |
retry=retry_if_exception_type((APIConnectionError, RateLimitError)),
|
| 313 |
-
wait=wait_exponential(multiplier=
|
| 314 |
-
stop=stop_after_attempt(
|
| 315 |
)
|
| 316 |
def _similarity_search_with_retry(self, query: str, k: int = 5):
|
| 317 |
"""Perform similarity search with retry logic"""
|
| 318 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
return self.vector_store.similarity_search_with_score(
|
| 320 |
query,
|
| 321 |
k=k
|
| 322 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
except Exception as e:
|
| 324 |
print(f"Similarity search error: {type(e).__name__}: {str(e)}")
|
| 325 |
raise
|
|
@@ -434,3 +451,15 @@ class LyricGenerator:
|
|
| 434 |
|
| 435 |
except Exception as e:
|
| 436 |
print(f"Warning: Could not fully examine SQLite database: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
@retry(
|
| 49 |
retry=retry_if_exception_type((APIConnectionError, RateLimitError)),
|
| 50 |
+
wait=wait_exponential(multiplier=2, min=4, max=60),
|
| 51 |
+
stop=stop_after_attempt(10)
|
| 52 |
)
|
| 53 |
def _create_embeddings_with_retry(self):
|
| 54 |
"""Create OpenAI embeddings with retry logic"""
|
| 55 |
try:
|
| 56 |
return OpenAIEmbeddings(
|
| 57 |
openai_api_key=Settings.OPENAI_API_KEY,
|
| 58 |
+
timeout=60, # Increase timeout
|
| 59 |
+
max_retries=5, # Add max retries
|
| 60 |
+
request_timeout=30, # Add request timeout
|
| 61 |
+
http_client_kwargs={ # Add HTTP client settings
|
| 62 |
+
'timeout': 30.0,
|
| 63 |
+
'verify': True,
|
| 64 |
+
'pool_timeout': 30.0
|
| 65 |
+
}
|
| 66 |
)
|
| 67 |
except Exception as e:
|
| 68 |
print(f"Error creating embeddings: {type(e).__name__}: {str(e)}")
|
|
|
|
| 317 |
|
| 318 |
@retry(
|
| 319 |
retry=retry_if_exception_type((APIConnectionError, RateLimitError)),
|
| 320 |
+
wait=wait_exponential(multiplier=2, min=4, max=60),
|
| 321 |
+
stop=stop_after_attempt(10)
|
| 322 |
)
|
| 323 |
def _similarity_search_with_retry(self, query: str, k: int = 5):
|
| 324 |
"""Perform similarity search with retry logic"""
|
| 325 |
try:
|
| 326 |
+
# First verify OpenAI connection
|
| 327 |
+
test_embedding = self.embeddings.embed_query("test")
|
| 328 |
+
if not test_embedding:
|
| 329 |
+
raise RuntimeError("Empty response from OpenAI")
|
| 330 |
+
|
| 331 |
+
# Then do the actual search
|
| 332 |
return self.vector_store.similarity_search_with_score(
|
| 333 |
query,
|
| 334 |
k=k
|
| 335 |
)
|
| 336 |
+
except APIConnectionError as e:
|
| 337 |
+
print(f"OpenAI API Connection Error: {str(e)}")
|
| 338 |
+
print("Retrying...")
|
| 339 |
+
raise # Retry
|
| 340 |
except Exception as e:
|
| 341 |
print(f"Similarity search error: {type(e).__name__}: {str(e)}")
|
| 342 |
raise
|
|
|
|
| 451 |
|
| 452 |
except Exception as e:
|
| 453 |
print(f"Warning: Could not fully examine SQLite database: {e}")
|
| 454 |
+
|
| 455 |
+
def _verify_openai_connection(self):
|
| 456 |
+
"""Verify OpenAI API connection"""
|
| 457 |
+
try:
|
| 458 |
+
print("Verifying OpenAI API connection...")
|
| 459 |
+
test_embedding = self.embeddings.embed_query("test")
|
| 460 |
+
if test_embedding:
|
| 461 |
+
print("OpenAI API connection verified")
|
| 462 |
+
return True
|
| 463 |
+
except Exception as e:
|
| 464 |
+
print(f"OpenAI API connection test failed: {type(e).__name__}: {str(e)}")
|
| 465 |
+
return False
|