包仍属实验性,小版本升级可能有破坏性接口变更;调用云端 API 需先设置对应环境变量中的 API key;使用 Hugging Face 开源模型需另行下载模型权重
python -m pip install spacy-llmREADME 未说明显存要求;支持 Hugging Face 上的开源模型(Falcon、Dolly、Llama 2、OpenLLaMA、StableLM、Mistral),也可通过 API 调用云端模型
把大模型接入 spaCy 流水线,用提示词做结构化 NLP,无需训练数据
把大语言模型接入 spaCy 的结构化 NLP 流水线,用提示词完成命名实体识别等任务,无需训练即可复用现有流程。
核心能力
- 提供可序列化的 llm 组件接入 spaCy 流水线
- 任务与模型解耦,可自定义提示与解析函数
- 内置 NER、文本分类、关系抽取、摘要等任务
- 支持 OpenAI、Cohere、Anthropic、PaLM、Azure 等 API
- 用 map-reduce 分片处理超长提示并融合结果
容易踩的坑
- release-v0.7.4v0.7.4: Python 3.14 support, Pydantic v2 migration, and updated dependencies最新正式版2026-03-24 · 2026-03-24 22:33 UTC
Generated notes (2026-03-24 23:31)
Python 3.14 support, Pydantic v2 migration, and updated dependencies
Features
- Add support for Python 3.13 and 3.14 (Python 3.9 dropped; minimum is now 3.10)
- Migrate from Pydantic v1 to Pydantic v2 native API
- Update LangChain integration to require langchain >= 1.0
Fixes
- Fix compatibility with spaCy 3.8.13 by requiring confection >= 1.3.3
- Filter spurious pydantic v1 deprecation warning from langchain-core on Python 3.14
Other
- Update LangChain import to be lazy-loaded for Python 3.14 compatibility
- Update dev dependencies: langchain 1.x, openai 1.x, drop stale pins
- release-v0.7.3v0.7.3: Sandbox Jinja to prevent code execution from untrusted configs2025-01-13 · 2025-01-13 12:11 UTC
查看更新说明
The
jinjatemplating library allows arbitrary code execution unless run within a sandbox. This could allow arbitrary code execution if an untrusted config file were used to load the pipeline. - v0.7.2v0.7.2: Support for Python 3.12 and a Torch-related bugfix2024-05-17 · 2024-05-17 09:36 UTC
查看更新说明
✨ New features and improvements
- Support for Python 3.12 (#466).
🔴 Bug fixes
- Ensure that Torch is not imported or used unless it is available (#467).
⚠️ Backwards incompatibilities
No backwards incompatibilities w.r.t. v0.7.1.
👥 Contributors
@honnibal, @ines, @magdaaniol, @svlandeg
- v0.7.1v0.7.1: Update `langchain` integration; fix EL task, model endpoints2024-01-29 · 2024-01-29 13:46 UTC
查看更新说明
🔴 Bug fixes
- Remove misguided check for fixed endpoints in OpenAI REST implementation (#429)
- Fix bug in EL task if non-highlighted entities are in text (#375)
- Update
langchainintegration to supportlangchain>= 0.1, < 0.2 (#433)
⚠️ Backwards incompatibilities
No backwards incompatibilities w.r.t. v0.7.0.
📖 Documentation and examples
- Link task mentions in readme to docs (#421)
👥 Contributors
@honnibal, @ines, @magdaaniol, @rmitsch, @svlandeg
- v0.7.0v0.7.0: Supporting arbitrarily long docs and various new tasks2024-01-19 · 2024-01-19 16:27 UTC
查看更新说明
✨ New features and improvements
- NEW: Support arbitrarily long docs leveraging map-reduce and document sharding. Read more here (#332)
- NEW: New tasks -
EntityLinkingTask: use LLMs to link entity mentions in texts to entities in a custom knowledge base. Read more here (#241)TranslationTask: translate documents from and to arbitrary languages. Read more here (#396)RawTask: prompt models with only doc content, no prompt scaffolding, for a maximum of flexibility. Read more here (#395)
- Allow custom endpoints for all models to support usage of fine-tuned models (#390)
🔴 Bug fixes
- Fix issues with previously incomplete integration of LangChain objects (#417)
⚠️ Backwards incompatibilities
No backwards incompatibilities w.r.t. v0.6.x.
📖 Documentation and examples
- Descriptions of
EntityLinkingTask,TranslationTaskandRawTask(https://github.com/explosion/spaCy/pull/12988, https://github.com/explosion/spaCy/pull/13183, https://github.com/explosion/spaCy/pull/13180) - Introduction to and description of doc sharding/map-reduce for the support of infinitely long docs (https://github.com/explosion/spaCy/pull/13214)
👥 Contributors
@honnibal, @ines, @rmitsch, @svlandeg
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
This package integrates Large Language Models (LLMs) into spaCy, featuring a modular system for fast prototyping and prompting, and turning unstructured responses into robust outputs for various NLP tasks, no training data required.
Feature Highlight
- Serializable
llmcomponent to integrate prompts into your spaCy pipeline - Modular functions to define the task (prompting and parsing) and model
- Interfaces with the APIs of
- Supports open-source LLMs hosted on Hugging Face 🤗:
- Integration with LangChain 🦜️🔗 - all
langchainmodels and features can be used inspacy-llm - Tasks available out of the box:
- Easy implementation of your own functions via spaCy's registry for custom prompting, parsing and model integrations. For an example, see here.
- Map-reduce approach for splitting prompts too long for LLM's context window and fusing the results back together
🧠 Motivation
Large Language Models (LLMs) feature powerful natural language understanding capabilities. With only a few (and sometimes no) examples, an LLM can be prompted to perform custom NLP tasks such as text categorization, named entity recognition, coreference resolution, information extraction and more.
spaCy is a well-established library for building systems that need to work with language in various ways. spaCy's built-in components are generally powered by supervised learning or rule-based approaches.
Supervised learning is much worse than LLM prompting for prototyping, but for many tasks it's much better for production. A transformer model that runs comfortably on a single GPU is extremely powerful, and it's likely to be a better choice for any task for which you have a well-defined output. You train the model with anything from a few hundred to a few thousand labelled examples, and it will learn to do exactly that. Efficiency, reliability and control are all better with supervised learning, and accuracy will generally be higher than LLM prompting as well.
spacy-llm lets you have the best of both worlds. You can quickly initialize a pipeline with components powered by LLM prompts, and freely mix in components powered by other approaches. As your project progresses, you can look at replacing some or all of the LLM-powered components as you require.
Of course, there can be components in your system for which the power of an LLM is fully justified. If you want a system that can synthesize information from multiple documents in subtle ways and generate a nuanced summary for you, bigger is better. However, even if your production system needs an LLM for some of the task, that doesn't mean you need an LLM for all of it. Maybe you want to use a cheap text classification model to help you find the texts to summarize, or maybe you want to add a rule-based system to sanity check the output of the summary. These before-and-after tasks are much easier with a mature and well-thought-out library, which is exactly what spaCy provides.
⏳ Install
spacy-llm will be installed automatically in future spaCy versions. For now, you can run the following in the same virtual environment where you already have spacy installed.
python -m pip install spacy-llm
⚠️ This package is still experimental and it is possible that changes made to the interface will be breaking in minor version updates.
🐍 Quickstart
Let's run some text classification using a GPT model from OpenAI.
Create a new API key from openai.com or fetch an existing one, and ensure the keys are set as environmental variables. For more background information, see the documentation around setting API keys.
In Python code
To do some quick experiments, from 0.5.0 onwards you can run:
import spacy
nlp = spacy.blank("en")
llm = nlp.add_pipe("llm_textcat")
llm.add_label("INSULT")
llm.add_label("COMPLIMENT")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}
By using the llm_textcat factory, the latest version of the built-in textcat task is used,
as well as the default GPT-3-5 model from OpenAI.
Using a config file
To control the various parameters of the llm pipeline, we can use
spaCy's config system.
To start, create a config file config.cfg containing at least the following (or see the
full example
here):
[nlp]
lang = "en"
pipeline = ["llm"]
[components]
[components.llm]
factory = "llm"
[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["COMPLIMENT", "INSULT"]
[components.llm.model]
@llm_models = "spacy.GPT-4.v2"
Now run:
from spacy_llm.util import assemble
nlp = assemble("config.cfg")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}
That's it! There's a lot of other features - prompt templating, more tasks, logging etc. For more information on how to use those, check out https://spacy.io/api/large-language-models.
🚀 Ongoing work
In the near future, we will
- Add more example tasks
- Support a broader range of models
- Provide more example use-cases and tutorials
PRs are always welcome!
📝️ Reporting issues
If you have questions regarding the usage of spacy-llm, or want to give us feedback after giving it a spin, please use
the discussion board.
Bug reports can be filed on the spaCy issue tracker. Thank you!
Migration guides
Please refer to our migration guide.