Spaces:
Paused
Paused
Download experiments/semantic_search.py from dellweb3/vibeCodingP2: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/semantic_search.py
- Command line
-
hf download hf://spaces/dellweb3/vibeCodingP2/experiments/semantic_search.py
-
curl -L -o semantic_search.py https://huggingface.co/spaces/dellweb3/vibeCodingP2/resolve/main/experiments/semantic_search.py
1.47 kB
| from sentence_transformers import SentenceTransformer, util | |
| # 加载支持中英文的 Embedding 模型 | |
| MODEL_NAME = "intfloat/multilingual-e5-small" | |
| print("正在加载语义模型(第一次运行需要下载)...") | |
| model = SentenceTransformer(MODEL_NAME) | |
| # 模拟 ZKCoding 的 Java / ERP / WMS 知识库 | |
| documents = [ | |
| "库存管理系统记录商品入库、出库和剩余库存数量。", | |
| "ERP 采购订单系统负责供应商报价、采购审批和付款流程。", | |
| "Java Spring Boot 可以通过 REST Controller 实现 HTTP API。", | |
| "设备运维系统负责设备巡检、维修工单及故障记录。", | |
| ] | |
| # E5 模型使用 passage: 和 query: 前缀 | |
| passages = ["passage: " + doc for doc in documents] | |
| document_embeddings = model.encode( | |
| passages, | |
| convert_to_tensor=True, | |
| normalize_embeddings=True, | |
| ) | |
| questions = [ | |
| "如何追踪商品的进出及剩余数量?", | |
| "怎样管理供应商的报价和采购流程?", | |
| "Spring Boot 如何编写接口?", | |
| ] | |
| for question in questions: | |
| query_embedding = model.encode( | |
| "query: " + question, | |
| convert_to_tensor=True, | |
| normalize_embeddings=True, | |
| ) | |
| scores = util.cos_sim( | |
| query_embedding, | |
| document_embeddings, | |
| )[0] | |
| best_index = int(scores.argmax()) | |
| print("\n问题:", question) | |
| print("最相关文档:", documents[best_index]) | |
| print("相似度:", round(float(scores[best_index]), 4)) | |