Embed wiki nodes with the configured embedding provider.
(
user_id: str,
node_ids: Optional[List[str]] = None,
*,
force: bool = False,
limit: int = 500,
)
| 350 | |
| 351 | |
| 352 | def embed_nodes_for_user( |
| 353 | user_id: str, |
| 354 | node_ids: Optional[List[str]] = None, |
| 355 | *, |
| 356 | force: bool = False, |
| 357 | limit: int = 500, |
| 358 | ) -> Dict[str, Any]: |
| 359 | """Embed wiki nodes with the configured embedding provider.""" |
| 360 | init_wiki_schema() |
| 361 | from paperflow.providers import build_embedding_provider |
| 362 | |
| 363 | params: List[Any] = [user_id] |
| 364 | where = "WHERE user_id = ?" |
| 365 | if node_ids: |
| 366 | placeholders = ",".join("?" for _ in node_ids) |
| 367 | where += f" AND node_id IN ({placeholders})" |
| 368 | params.extend(node_ids) |
| 369 | if not force: |
| 370 | where += " AND embedding IS NULL" |
| 371 | params.append(max(1, int(limit))) |
| 372 | |
| 373 | conn = db_ops.get_connection() |
| 374 | rows = conn.execute( |
| 375 | f""" |
| 376 | SELECT * FROM wiki_nodes |
| 377 | {where} |
| 378 | ORDER BY updated_at DESC, id DESC |
| 379 | LIMIT ? |
| 380 | """, |
| 381 | params, |
| 382 | ).fetchall() |
| 383 | if not rows: |
| 384 | conn.close() |
| 385 | return {"embedded": 0, "model": None} |
| 386 | |
| 387 | nodes = _rows_to_nodes(rows) |
| 388 | provider = build_embedding_provider() |
| 389 | texts = [_node_embedding_text(node) for node in nodes] |
| 390 | vectors = provider.embed_batch(texts) |
| 391 | model_name = f"{provider.name}:{provider.model}" |
| 392 | for node, vector in zip(nodes, vectors): |
| 393 | conn.execute( |
| 394 | """ |
| 395 | UPDATE wiki_nodes |
| 396 | SET embedding = ?, embedding_model = ?, updated_at = ? |
| 397 | WHERE user_id = ? AND node_id = ? |
| 398 | """, |
| 399 | (_vector_to_blob(vector), model_name, _now(), user_id, node["node_id"]), |
| 400 | ) |
| 401 | conn.commit() |
| 402 | conn.close() |
| 403 | return {"embedded": len(nodes), "model": model_name} |
| 404 | |
| 405 | |
| 406 | def upsert_edge( |
nothing calls this directly
no test coverage detected