用途:semantic-grep 提供 jina-grep 风格的语义搜索,但以 Python 模块在进程内运行,而非外部 CLI。它把查询与语料分块用 gemini-embedding-2 嵌入,按余弦相似度排序,返回 grep 格式输出。适合“哪些文件讨论了概念 X”这类 X 未必逐字出现的检索。
主要能力:核心函数 semantic_grep(query, corpus, , top_k=10, threshold=None, granularity="paragraph", include=".txt", model="gemini-embedding-2", dim=768, task="text"),corpus 可为文件、目录或预分块列表,返回含 path、line、text、score 的 Match 列表。辅助函数有 load_corpus、embed_batch、format_grep。支持 text/code 两种任务类型,dim 可选 128/768/1536/3072。
使用方式:凭据通过 proxy.env 配置 CF_ACCOUNT_ID、CF_GATEWAY_ID、CF_API_TOKEN,或回退到 GOOGLE_API_KEY / GEMINI_API_KEY。依赖仅 requests 与 numpy。示例:import sys; sys.path.insert(0, '/mnt/skills/user/semantic-grep/scripts'); from semantic_grep import semantic_grep, format_grep; results = semantic_grep("error handling under load", "/path/to/notes", top_k=5, granularity="paragraph"); print(format_grep(results))。推荐混合检索:先用 rg 做精确/正则预筛,再用 semantic_grep 对候选重排。
适用场景:概念/改写匹配、日志或笔记的模糊检索、代码的自然语言查询。若查询是命名实体或关键词,应先用 grep/rg。
限制:无持久索引,每次调用重新嵌入语料,适合约 1K 块以内;token 预算按字符数近似;批量中单个文本失败会导致整批失败;语料规模无内存上限;批组串行执行;无 CLI 封装。定价为 $0.15 / 1M 输入 token。