Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,8 +59,19 @@ def load_ohamlab_knowledge():
|
|
| 59 |
return chunks
|
| 60 |
|
| 61 |
# ---------------------------
|
| 62 |
-
# 3. Generate or Load Embeddings (with Cache)
|
| 63 |
# ---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def get_embeddings_with_cache():
|
| 65 |
"""Generate or load cached embeddings for OhamLab knowledge."""
|
| 66 |
if os.path.exists(CACHE_PATH):
|
|
@@ -77,16 +88,12 @@ def get_embeddings_with_cache():
|
|
| 77 |
chunks = load_ohamlab_knowledge()
|
| 78 |
texts = [c["text"] for c in chunks]
|
| 79 |
print(f"📘 Generating embeddings for {len(texts)} OhamLab chunks...")
|
|
|
|
| 80 |
all_embs = []
|
| 81 |
for i in range(0, len(texts), 50):
|
| 82 |
batch = texts[i:i + 50]
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
embs = [d.embedding for d in res.data]
|
| 86 |
-
all_embs.extend(embs)
|
| 87 |
-
except Exception as e:
|
| 88 |
-
print(f"⚠️ Embedding batch failed ({i}): {e}")
|
| 89 |
-
all_embs.extend([[0.0] * 1536] * len(batch)) # fallback
|
| 90 |
time.sleep(0.5)
|
| 91 |
|
| 92 |
data = [{"text": t, "embedding": e} for t, e in zip(texts, all_embs)]
|
|
@@ -103,7 +110,7 @@ OHAMLAB_TEXTS, OHAMLAB_EMBS = get_embeddings_with_cache()
|
|
| 103 |
def retrieve_knowledge(query, top_k=3):
|
| 104 |
"""Retrieve top-k most relevant text snippets from markdown knowledge bank."""
|
| 105 |
try:
|
| 106 |
-
q_emb =
|
| 107 |
sims = np.dot(OHAMLAB_EMBS, q_emb) / (
|
| 108 |
np.linalg.norm(OHAMLAB_EMBS, axis=1) * np.linalg.norm(q_emb)
|
| 109 |
)
|
|
|
|
| 59 |
return chunks
|
| 60 |
|
| 61 |
# ---------------------------
|
| 62 |
+
# 3. Generate or Load Embeddings (with Cache & Retry)
|
| 63 |
# ---------------------------
|
| 64 |
+
def create_embeddings_with_retry(texts, retries=3, delay=2):
|
| 65 |
+
"""Generate embeddings with retries on failure."""
|
| 66 |
+
for attempt in range(retries):
|
| 67 |
+
try:
|
| 68 |
+
res = client.embeddings.create(model=EMBED_MODEL, input=texts)
|
| 69 |
+
return [d.embedding for d in res.data]
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"⚠️ Embedding attempt {attempt+1} failed: {e}")
|
| 72 |
+
time.sleep(delay)
|
| 73 |
+
raise RuntimeError("❌ Failed to generate embeddings after retries.")
|
| 74 |
+
|
| 75 |
def get_embeddings_with_cache():
|
| 76 |
"""Generate or load cached embeddings for OhamLab knowledge."""
|
| 77 |
if os.path.exists(CACHE_PATH):
|
|
|
|
| 88 |
chunks = load_ohamlab_knowledge()
|
| 89 |
texts = [c["text"] for c in chunks]
|
| 90 |
print(f"📘 Generating embeddings for {len(texts)} OhamLab chunks...")
|
| 91 |
+
|
| 92 |
all_embs = []
|
| 93 |
for i in range(0, len(texts), 50):
|
| 94 |
batch = texts[i:i + 50]
|
| 95 |
+
embs = create_embeddings_with_retry(batch)
|
| 96 |
+
all_embs.extend(embs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
time.sleep(0.5)
|
| 98 |
|
| 99 |
data = [{"text": t, "embedding": e} for t, e in zip(texts, all_embs)]
|
|
|
|
| 110 |
def retrieve_knowledge(query, top_k=3):
|
| 111 |
"""Retrieve top-k most relevant text snippets from markdown knowledge bank."""
|
| 112 |
try:
|
| 113 |
+
q_emb = create_embeddings_with_retry([query])[0]
|
| 114 |
sims = np.dot(OHAMLAB_EMBS, q_emb) / (
|
| 115 |
np.linalg.norm(OHAMLAB_EMBS, axis=1) * np.linalg.norm(q_emb)
|
| 116 |
)
|