#!/usr/bin/env python3 """embed-knowledge Skill CLI。库实现安装为 muse_embed,这里只做入参与出错的壳。""" import pathlib import sys import click import psycopg import requests from muse_db import connect from muse_embed import EmbeddingOwnershipConflict, _run_bulk, _session, embed_texts _EVIDENCE = pathlib.Path(__file__).resolve().parents[2] / "record-run-evidence" / "scripts" if str(_EVIDENCE) not in sys.path: sys.path.insert(0, str(_EVIDENCE)) @click.command() @click.option("--work-id", type=int, help="限定作品/拆书批次;拆书默认按 source_id,章后抽卡按 draft.work_id") @click.option("--source-type", type=str, help="来源类型;chapter_extract 按作品的 draft.work_id 筛选") @click.option("--limit", type=click.IntRange(min=0), default=0, help="最多处理条数(0=不限)") @click.option("--probe", help="自由文本试嵌(打印维度与前 5 维,不落库)") def main(work_id, source_type, limit, probe): sess = _session() if probe: vecs, bad = embed_texts(sess, [probe]) if bad: raise click.ClickException("试嵌失败") v = vecs[0] click.echo(f"维度={len(v)} 前5维={[round(x, 4) for x in v[:5]]}") return with connect() as conn: _run_bulk(conn, sess, work_id, limit, source_type) from lesson_registry import propose_lesson_dedup # noqa: WPS433 run_id = f"embed-{work_id or 0}-{source_type or 'all'}-{limit}" lesson = propose_lesson_dedup( kind="win", title="知识草稿嵌入批次完成", detail={ "skill": "embed-knowledge", "work_id": work_id, "source_type": source_type, "limit": limit, }, work_id=work_id if isinstance(work_id, int) else None, run_id=run_id, creator="embed-knowledge", ) click.echo(f"嵌入完成 lesson={lesson.get('lesson_id')}") if __name__ == "__main__": try: main() except (EmbeddingOwnershipConflict, psycopg.Error, requests.RequestException) as e: click.echo(f"[错误] {type(e).__name__}: {e}", err=True) sys.exit(1)