spacy-llm


Namespacy-llm JSON
Version 0.7.1 PyPI version JSON
download
home_page
SummaryIntegrating LLMs into structured NLP pipelines
upload_time2024-01-29 13:45:12
maintainer
docs_urlNone
authorExplosion
requires_python>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <a href="https://explosion.ai"><img src="https://explosion.ai/assets/img/logo.svg" width="125" height="125" align="right" /></a>
<a href="https://explosion.ai"><img src="assets/logo.png" width="125" height="125" align="left" style="margin-right:30px" /></a>

<h1 align="center">
<span style="font: bold 38pt'Courier New';">spacy-llm</span>
<br>Structured NLP with LLMs
</h1>
<br><br>

[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/explosion/spacy-llm/test.yml?branch=main)](https://github.com/explosion/spacy-llm/actions/workflows/test.yml)
[![pypi Version](https://img.shields.io/pypi/v/spacy-llm.svg?style=flat-square&logo=pypi&logoColor=white)](https://pypi.org/project/spacy-llm/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/ambv/black)

This package integrates Large Language Models (LLMs) into [spaCy](https://spacy.io), 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 `llm` **component** to integrate prompts into your spaCy pipeline
- **Modular functions** to define the [**task**](https://spacy.io/api/large-language-models#tasks) (prompting and parsing) and [**model**](https://spacy.io/api/large-language-models#models)
- Interfaces with the APIs of
  - **[OpenAI](https://platform.openai.com/docs/api-reference/)**
  - **[Cohere](https://docs.cohere.com/reference/generate)**
  - **[Anthropic](https://docs.anthropic.com/claude/reference/)**
  - **[Google PaLM](https://ai.google/discover/palm2/)**
  - **[Microsoft Azure AI](https://azure.microsoft.com/en-us/solutions/ai)**
- Supports open-source LLMs hosted on Hugging Face 🤗:
  - **[Falcon](https://huggingface.co/tiiuae)**
  - **[Dolly](https://huggingface.co/databricks)**
  - **[Llama 2](https://huggingface.co/meta-llama)**
  - **[OpenLLaMA](https://huggingface.co/openlm-research)**
  - **[StableLM](https://huggingface.co/stabilityai)**
  - **[Mistral](https://huggingface.co/mistralai)**
- Integration with [LangChain](https://github.com/hwchase17/langchain) 🦜️🔗 - all `langchain` models and features can be used in `spacy-llm`
- Tasks available out of the box:
  - [Named Entity Recognition](https://spacy.io/api/large-language-models#ner)
  - [Text classification](https://spacy.io/api/large-language-models#textcat)
  - [Lemmatization](https://spacy.io/api/large-language-models#lemma)
  - [Relationship extraction](https://spacy.io/api/large-language-models#rel)
  - [Sentiment analysis](https://spacy.io/api/large-language-models#sentiment)
  - [Span categorization](https://spacy.io/api/large-language-models#spancat)
  - [Summarization](https://spacy.io/api/large-language-models#summarization)
  - [Entity linking](https://spacy.io/api/large-language-models#nel)
  - [Translation](https://spacy.io/api/large-language-models#translation)
  - [Raw prompt execution for maximum flexibility](https://spacy.io/api/large-language-models#raw)
  - Soon:
    - Semantic role labeling
- Easy implementation of **your own functions** via [spaCy's registry](https://spacy.io/api/top-level#registry) for custom prompting, parsing and model integrations. For an example, see [here](https://spacy.io/usage/large-language-models#example-4).
- [Map-reduce approach](https://spacy.io/api/large-language-models#task-sharding) 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](https://spacy.io) 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](https://spacy.io/usage).

```bash
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](https://spacy.io/api/large-language-models#api-keys).

### In Python code

To do some quick experiments, from 0.5.0 onwards you can run:

```python
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](https://spacy.io/api/data-formats#config).
To start, create a config file `config.cfg` containing at least the following (or see the
full example
[here](https://github.com/explosion/spacy-llm/tree/main/usage_examples/textcat_openai)):

```ini
[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:

```python
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](https://github.com/explosion/spacy-llm/discussions).
Bug reports can be filed on the [spaCy issue tracker](https://github.com/explosion/spacy-llm/issues). Thank you!

## Migration guides

Please refer to our [migration guide](migration_guide.md).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "spacy-llm",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Explosion",
    "author_email": "contact@explosion.ai",
    "download_url": "https://files.pythonhosted.org/packages/6e/5f/6f7f7eb672a82909f07dd45673d69c7baa594d427d30046657fac7cd58b8/spacy-llm-0.7.1.tar.gz",
    "platform": null,
    "description": "<a href=\"https://explosion.ai\"><img src=\"https://explosion.ai/assets/img/logo.svg\" width=\"125\" height=\"125\" align=\"right\" /></a>\n<a href=\"https://explosion.ai\"><img src=\"assets/logo.png\" width=\"125\" height=\"125\" align=\"left\" style=\"margin-right:30px\" /></a>\n\n<h1 align=\"center\">\n<span style=\"font: bold 38pt'Courier New';\">spacy-llm</span>\n<br>Structured NLP with LLMs\n</h1>\n<br><br>\n\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/explosion/spacy-llm/test.yml?branch=main)](https://github.com/explosion/spacy-llm/actions/workflows/test.yml)\n[![pypi Version](https://img.shields.io/pypi/v/spacy-llm.svg?style=flat-square&logo=pypi&logoColor=white)](https://pypi.org/project/spacy-llm/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/ambv/black)\n\nThis package integrates Large Language Models (LLMs) into [spaCy](https://spacy.io), featuring a modular system for **fast prototyping** and **prompting**, and turning unstructured responses into **robust outputs** for various NLP tasks, **no training data** required.\n\n## Feature Highlight\n\n- Serializable `llm` **component** to integrate prompts into your spaCy pipeline\n- **Modular functions** to define the [**task**](https://spacy.io/api/large-language-models#tasks) (prompting and parsing) and [**model**](https://spacy.io/api/large-language-models#models)\n- Interfaces with the APIs of\n  - **[OpenAI](https://platform.openai.com/docs/api-reference/)**\n  - **[Cohere](https://docs.cohere.com/reference/generate)**\n  - **[Anthropic](https://docs.anthropic.com/claude/reference/)**\n  - **[Google PaLM](https://ai.google/discover/palm2/)**\n  - **[Microsoft Azure AI](https://azure.microsoft.com/en-us/solutions/ai)**\n- Supports open-source LLMs hosted on Hugging Face \ud83e\udd17:\n  - **[Falcon](https://huggingface.co/tiiuae)**\n  - **[Dolly](https://huggingface.co/databricks)**\n  - **[Llama 2](https://huggingface.co/meta-llama)**\n  - **[OpenLLaMA](https://huggingface.co/openlm-research)**\n  - **[StableLM](https://huggingface.co/stabilityai)**\n  - **[Mistral](https://huggingface.co/mistralai)**\n- Integration with [LangChain](https://github.com/hwchase17/langchain) \ud83e\udd9c\ufe0f\ud83d\udd17 - all `langchain` models and features can be used in `spacy-llm`\n- Tasks available out of the box:\n  - [Named Entity Recognition](https://spacy.io/api/large-language-models#ner)\n  - [Text classification](https://spacy.io/api/large-language-models#textcat)\n  - [Lemmatization](https://spacy.io/api/large-language-models#lemma)\n  - [Relationship extraction](https://spacy.io/api/large-language-models#rel)\n  - [Sentiment analysis](https://spacy.io/api/large-language-models#sentiment)\n  - [Span categorization](https://spacy.io/api/large-language-models#spancat)\n  - [Summarization](https://spacy.io/api/large-language-models#summarization)\n  - [Entity linking](https://spacy.io/api/large-language-models#nel)\n  - [Translation](https://spacy.io/api/large-language-models#translation)\n  - [Raw prompt execution for maximum flexibility](https://spacy.io/api/large-language-models#raw)\n  - Soon:\n    - Semantic role labeling\n- Easy implementation of **your own functions** via [spaCy's registry](https://spacy.io/api/top-level#registry) for custom prompting, parsing and model integrations. For an example, see [here](https://spacy.io/usage/large-language-models#example-4).\n- [Map-reduce approach](https://spacy.io/api/large-language-models#task-sharding) for splitting prompts too long for LLM's context window and fusing the results back together\n\n## \ud83e\udde0 Motivation\n\nLarge 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.\n\n[spaCy](https://spacy.io) 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.\n\nSupervised 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.\n\n`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.\n\nOf 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.\n\n## \u23f3 Install\n\n`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](https://spacy.io/usage).\n\n```bash\npython -m pip install spacy-llm\n```\n\n> \u26a0\ufe0f This package is still experimental and it is possible that changes made to the interface will be breaking in minor version updates.\n\n## \ud83d\udc0d Quickstart\n\nLet's run some text classification using a GPT model from OpenAI. \n\nCreate a new API key from openai.com or fetch an existing one, and ensure the\nkeys are set as environmental variables. For more background information, see\nthe documentation around setting [API keys](https://spacy.io/api/large-language-models#api-keys).\n\n### In Python code\n\nTo do some quick experiments, from 0.5.0 onwards you can run:\n\n```python\nimport spacy\n\nnlp = spacy.blank(\"en\")\nllm = nlp.add_pipe(\"llm_textcat\")\nllm.add_label(\"INSULT\")\nllm.add_label(\"COMPLIMENT\")\ndoc = nlp(\"You look gorgeous!\")\nprint(doc.cats)\n# {\"COMPLIMENT\": 1.0, \"INSULT\": 0.0}\n```\n\nBy using the `llm_textcat` factory, the latest version of the built-in textcat task is used, \nas well as the default GPT-3-5 model from OpenAI.\n\n### Using a config file\n\nTo control the various parameters of the `llm` pipeline, we can use \n[spaCy's config system](https://spacy.io/api/data-formats#config).\nTo start, create a config file `config.cfg` containing at least the following (or see the\nfull example\n[here](https://github.com/explosion/spacy-llm/tree/main/usage_examples/textcat_openai)):\n\n```ini\n[nlp]\nlang = \"en\"\npipeline = [\"llm\"]\n\n[components]\n\n[components.llm]\nfactory = \"llm\"\n\n[components.llm.task]\n@llm_tasks = \"spacy.TextCat.v3\"\nlabels = [\"COMPLIMENT\", \"INSULT\"]\n\n[components.llm.model]\n@llm_models = \"spacy.GPT-4.v2\"\n```\n\nNow run:\n\n```python\nfrom spacy_llm.util import assemble\n\nnlp = assemble(\"config.cfg\")\ndoc = nlp(\"You look gorgeous!\")\nprint(doc.cats)\n# {\"COMPLIMENT\": 1.0, \"INSULT\": 0.0}\n```\n\nThat's it! There's a lot of other features - prompt templating, more tasks, logging etc. For more information on how to\nuse those, check out https://spacy.io/api/large-language-models.\n\n\n## \ud83d\ude80 Ongoing work\n\nIn the near future, we will\n\n- Add more example tasks\n- Support a broader range of models\n- Provide more example use-cases and tutorials\n\nPRs are always welcome!\n\n## \ud83d\udcdd\ufe0f Reporting issues\n\nIf you have questions regarding the usage of `spacy-llm`, or want to give us feedback after giving it a spin, please use\nthe [discussion board](https://github.com/explosion/spacy-llm/discussions).\nBug reports can be filed on the [spaCy issue tracker](https://github.com/explosion/spacy-llm/issues). Thank you!\n\n## Migration guides\n\nPlease refer to our [migration guide](migration_guide.md).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Integrating LLMs into structured NLP pipelines",
    "version": "0.7.1",
    "project_urls": {
        "Release notes": "https://github.com/explosion/spacy-llm/releases",
        "Source": "https://github.com/explosion/spacy-llm"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "23dc19b155fd5cdc4b2d2306b79043491a11f42df8e71277ac3cad1a5110b94e",
                "md5": "9098d203c35a4d9d9889b2736291e3f3",
                "sha256": "20ee79dae645edc64a38ee15e7f725c38358188f73ac426ab043b145c1be52c4"
            },
            "downloads": -1,
            "filename": "spacy_llm-0.7.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9098d203c35a4d9d9889b2736291e3f3",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.7",
            "size": 255915,
            "upload_time": "2024-01-29T13:45:09",
            "upload_time_iso_8601": "2024-01-29T13:45:09.794229Z",
            "url": "https://files.pythonhosted.org/packages/23/dc/19b155fd5cdc4b2d2306b79043491a11f42df8e71277ac3cad1a5110b94e/spacy_llm-0.7.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6e5f6f7f7eb672a82909f07dd45673d69c7baa594d427d30046657fac7cd58b8",
                "md5": "dd180017231368a0b90e03d5ea5ac533",
                "sha256": "c59fe8fbf71aee1bc4243488ecdb7b7e9b3df81877d5e7ac661b899b658b73d6"
            },
            "downloads": -1,
            "filename": "spacy-llm-0.7.1.tar.gz",
            "has_sig": false,
            "md5_digest": "dd180017231368a0b90e03d5ea5ac533",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 149795,
            "upload_time": "2024-01-29T13:45:12",
            "upload_time_iso_8601": "2024-01-29T13:45:12.268819Z",
            "url": "https://files.pythonhosted.org/packages/6e/5f/6f7f7eb672a82909f07dd45673d69c7baa594d427d30046657fac7cd58b8/spacy-llm-0.7.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-29 13:45:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "explosion",
    "github_project": "spacy-llm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "spacy-llm"
}
        
Elapsed time: 0.19756s