本镜像用于构建LinTO的NLP服务:关键词提取(Keyphrase Extraction),基于https://github.com/linto-ai/linto-platform-nlp-core%E5%BC%80%E5%8F%91%E3%80%82%E5%8F%AF%E4%B8%8Ehttps://github.com/linto-ai/linto-platform-stack%E4%B8%80%E8%B5%B7%E9%83%A8%E7%BD%B2%EF%BC%8C%E4%B9%9F%E5%8F%AF%E7%8B%AC%E7%AB%8B%E9%83%A8%E7%BD%B2%EF%BC%88%E8%AF%A6%E8%A7%81%E4%B8%8B%E6%96%87%E5%BC%80%E5%8F%91%E9%83%A8%E5%88%86%EF%BC%89%E3%80%82
LinTO的NLP服务采用spaCy的核心设计理念:组件与管道。组件(位于components/目录下)与服务解耦,可轻松复用至其他spaCy项目,组件通过管道组织以实现特定NLP任务。
本服务支持两种启动方式:REST API和Celery任务,且均支持GPU与CPU模式。
安装依赖工具
确保已安装:
下载模型文件
将模型文件下载至主机的./assets目录(可自定义路径):
bashcd linto-platform-nlp-keyphrase-extraction/ bash scripts/download_models.sh
配置环境变量
复制默认环境变量配置并修改:
bashcp .envdefault .env
| 环境变量 | 描述 | 默认值 |
|---|---|---|
APP_LANG | 应用支持的语言列表(空格分隔) | fr en |
ASSETS_PATH_ON_HOST | 主机上资产文件夹路径 | ./assets |
ASSETS_PATH_IN_CONTAINER | 容器内模型文件挂载路径 | /app/assets |
LM_MAP | 语言与对应模型的JSON映射字符串 | {"fr":"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2","en":"sentence-transformers/all-MiniLM-L6-v2"} |
SERVICE_MODE | 服务模式,可选"http"(REST API)或"task"(Celery任务) | "http" |
CONCURRENCY | 最大并发请求数 | 1 |
USE_GPU | 是否使用GPU,可选"True"或"False" | True |
SERVICE_NAME | 微服务名称 | kpe |
SERVICES_BROKER | 微服务通信的 broker 服务器URL | "redis://localhost:6379" |
BROKER_PASS | broker服务器访问密码 | None |
使用Docker命令构建:
bashsudo docker build --tag lintoai/linto-platform-nlp-keyphrase-extraction:latest .
或使用Docker Compose构建:
bashsudo docker-compose build
GPU模式
bashsudo docker run --gpus all \ --rm -p 80:80 \ -v $PWD/assets:/app/assets:ro \ --env-file .env \ lintoai/linto-platform-nlp-keyphrase-extraction:latest
CPU模式
--gpus all参数.env文件中设置USE_GPU=False或使用Docker Compose运行:
bashsudo docker-compose up
docker-compose.yml中移除runtime: nvidia配置.env文件中设置USE_GPU=False当SERVICE_MODE=http时,服务启动后可通过http://localhost/docs或http://localhost/redoc访问交互式API文档。
| 语言代码({lang}) | 模型 | 大小 |
|---|---|---|
en | sentence-transformers/all-MiniLM-L6-v2 | 80 MB |
fr | sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 | 418 MB |
http://localhost/kpe/{lang},其中{lang}为语言代码(如en或fr)。
请求示例
json{ "articles": [ { "text": "Apple Inc. is an American multinational technology company that specializes in consumer electronics, computer software and online services." }, { "text": "Unsupervised learning is a type of machine learning in which the algorithm is not provided with any pre-assigned labels or scores for the training data. As a result, unsupervised learning algorithms must first self-discover any naturally occurring patterns in that training data set." } ] }
响应示例
json{ "kpe": [ { "text": "Apple Inc. is an American multinational technology company that specializes in consumer electronics, computer software and online services.", "keyphrases": [ { "text": "apple", "score": 0.6539 }, { "text": "inc", "score": 0.3941 }, { "text": "company", "score": 0.2985 }, { "text": "multinational", "score": 0.2635 }, { "text": "electronics", "score": 0.2143 } ] }, { "text": "Unsupervised learning is a type of machine learning in which the algorithm is not provided with any pre-assigned labels or scores for the training data. As a result, unsupervised learning algorithms must first self-discover any naturally occurring patterns in that training data set.", "keyphrases": [ { "text": "unsupervised", "score": 0.6663 }, { "text": "learning", "score": 0.3155 }, { "text": "algorithms", "score": 0.3128 }, { "text": "algorithm", "score": 0.2494 }, { "text": "patterns", "score": 0.2476 } ] } ] }
本服务基于https://github.com/MaartenGr/KeyBERT%E5%B0%81%E8%A3%85%EF%BC%8C%E6%94%AF%E6%8C%81%E4%BB%A5%E4%B8%8B%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0%EF%BC%9A
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| candidates | List[str] | null | 用于提取的候选关键词/关键短语列表,而非从文档中提取 |
| diversity | Float | 0.5 | 当use_mmr=True时,结果的多样性(0-1之间) |
| keyphrase_ngram_range | Tuple[int, int] | [1,1] | 提取的关键词/关键短语的词长范围 |
| min_df | int | 1 | 当提取多个文档的关键词时,词在所有文档中的最小出现频率 |
| nr_candidates | int | 20 | 当use_maxsum=True时,考虑的候选词数量 |
| seed_keywords | List[str] | null | 引导关键词提取的种子词,通过调整相似度引导提取方向 |
| stop_words | Union[str, List[str]] | null | 需从文档中移除的停用词 |
| top_n | int | 5 | 返回的top N关键词/关键短语数量 |
| use_maxsum | bool | false | 是否使用最大和相似度选择关键词/关键短语 |
| use_mmr | bool | false | 是否使用最大边际相关性(MMR)选择关键词/关键短语 |
运行时配置示例
可在API请求中通过component_cfg参数自定义配置:
json{ "articles": [ { "text": "Unsupervised learning is a type of machine learning in which the algorithm is not provided with any pre-assigned labels or scores for the training data." } ], "component_cfg": { "kpe": {"keyphrase_ngram_range": [2,2], "top_n": 1} } }
响应:
json{ "kpe": [ { "text": "Unsupervised learning is a type of machine learning in which the algorithm is not provided with any pre-assigned labels or scores for the training data.", "keyphrases": [ { "text": "unsupervised learning", "score": 0.7252 } ] } ] }
本地安装Redis并启动:
bashredis-server --protected-mode no --bind 0.0.0.0 --loglevel debug
配置.env文件:
SERVICE_MODE=taskSERVICES_BROKER=redis://172.17.0.1:6379(172.17.0.1为Docker默认网关)启动容器(使用上述docker run或docker-compose up命令)
在本地运行以下Python脚本调用Celery任务:
pythonfrom celery import Celery celery = Celery(broker='redis://localhost:6379/0', backend='redis://localhost:6379/1') r = celery.send_task( 'kpe_task', ( 'en', [ "Apple Inc. is an American multinational technology company that specializes in consumer electronics, computer software and online services.", "Unsupervised learning is a type of machine learning in which the algorithm is not provided with any pre-assigned labels or scores for the training data." ], {"kpe": {"top_n": 3}} ), queue='kpe') print(r.get())
关于最大和相似度(Max Sum Similarity)、最大边际相关性(MMR)等用于多样化提取结果的高级功能,可参考KeyBERT文档和技术文章了解详细原理。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务