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