|
18 | 18 | search_vectors as _search_vectors, |
19 | 19 | get_chunk_text as _get_chunk_text, |
20 | 20 | ) |
21 | | -from .openai import get_embedding_for_text, call_coding_api |
| 21 | +from .openai import call_coding_api |
| 22 | +from .embedding_client import EmbeddingClient |
22 | 23 | from llama_index.core import Document |
23 | 24 | from utils.logger import get_logger |
24 | 25 | from utils import compute_file_hash, chunk_text, norm, cosine |
|
59 | 60 |
|
60 | 61 | logger = get_logger(__name__) |
61 | 62 |
|
| 63 | +# Initialize EmbeddingClient for structured logging and retry logic |
| 64 | +_embedding_client = EmbeddingClient() |
62 | 65 |
|
63 | | -def _get_embedding_with_semaphore(semaphore: threading.Semaphore, text: str, model: Optional[str] = None): |
| 66 | + |
| 67 | +def _get_embedding_with_semaphore(semaphore: threading.Semaphore, text: str, file_path: str = "<unknown>", chunk_index: int = 0, model: Optional[str] = None): |
64 | 68 | """ |
65 | 69 | Wrapper to acquire semaphore inside executor task to avoid deadlock. |
66 | 70 | The semaphore is acquired in the worker thread, not the main thread. |
| 71 | + Now uses EmbeddingClient for better logging and error handling. |
67 | 72 | """ |
68 | 73 | semaphore.acquire() |
69 | 74 | try: |
70 | | - return get_embedding_for_text(text, model) |
| 75 | + # Use the embedding client with enhanced logging |
| 76 | + return _embedding_client.embed_text(text, file_path=file_path, chunk_index=chunk_index) |
71 | 77 | finally: |
72 | 78 | semaphore.release() |
73 | 79 |
|
@@ -192,7 +198,7 @@ def _process_file_sync( |
192 | 198 | for idx, chunk_doc in batch: |
193 | 199 | # Submit task to executor; semaphore will be acquired inside the worker |
194 | 200 | embedding_start_time = time.time() |
195 | | - future = _EXECUTOR.submit(_get_embedding_with_semaphore, semaphore, chunk_doc.text, embedding_model) |
| 201 | + future = _EXECUTOR.submit(_get_embedding_with_semaphore, semaphore, chunk_doc.text, rel_path, idx, embedding_model) |
196 | 202 | embedding_futures.append((idx, chunk_doc, future, embedding_start_time)) |
197 | 203 |
|
198 | 204 | # Wait for batch to complete and store results |
@@ -434,7 +440,7 @@ def search_semantic(query: str, database_path: str, top_k: int = 5): |
434 | 440 | Uses sqlite-vector's vector_full_scan to retrieve best-matching chunks and returns |
435 | 441 | a list of {file_id, path, chunk_index, score}. |
436 | 442 | """ |
437 | | - q_emb = get_embedding_for_text(query) |
| 443 | + q_emb = _embedding_client.embed_text(query, file_path="<query>", chunk_index=0) |
438 | 444 | if not q_emb: |
439 | 445 | return [] |
440 | 446 |
|
|
0 commit comments