mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-10 19:57:17 +03:00
Add spacy-llm docs to website (#12782)
* initial commit * update for v0.4.0 * Apply suggestions from code review * Fix formatting * Apply suggestions from code review * Update website/docs/api/large-language-models.mdx * Update website/docs/api/large-language-models.mdx * update usage page * Apply suggestions from review * Apply suggestions from review * fix links * fix relative links * Apply suggestions from code review Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * Apply suggestions from review * Add section on Llama 2. Format. --------- Co-authored-by: Raphael Mitsch <r.mitsch@outlook.com> Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
This commit is contained in:
parent
1d216a7ea6
commit
e2b89012a2
1488
website/docs/api/large-language-models.mdx
Normal file
1488
website/docs/api/large-language-models.mdx
Normal file
File diff suppressed because it is too large
Load Diff
512
website/docs/usage/large-language-models.mdx
Normal file
512
website/docs/usage/large-language-models.mdx
Normal file
|
@ -0,0 +1,512 @@
|
||||||
|
---
|
||||||
|
title: Large Language Models
|
||||||
|
teaser: Integrating LLMs into structured NLP pipelines
|
||||||
|
menu:
|
||||||
|
- ['Motivation', 'motivation']
|
||||||
|
- ['Install', 'install']
|
||||||
|
- ['Usage', 'usage']
|
||||||
|
- ['Logging', 'logging']
|
||||||
|
- ['API', 'api']
|
||||||
|
- ['Tasks', 'tasks']
|
||||||
|
- ['Models', 'models']
|
||||||
|
---
|
||||||
|
|
||||||
|
[The spacy-llm package](https://github.com/explosion/spacy-llm) integrates Large
|
||||||
|
Language Models (LLMs) into spaCy pipelines, featuring a modular system for
|
||||||
|
**fast prototyping** and **prompting**, and turning unstructured responses into
|
||||||
|
**robust outputs** for various NLP tasks, **no training data** required.
|
||||||
|
|
||||||
|
- Serializable `llm` **component** to integrate prompts into your pipeline
|
||||||
|
- **Modular functions** to define the [**task**](#tasks) (prompting and parsing)
|
||||||
|
and [**model**](#models) (model to use)
|
||||||
|
- Support for **hosted APIs** and self-hosted **open-source models**
|
||||||
|
- Integration with [`LangChain`](https://github.com/hwchase17/langchain)
|
||||||
|
- Access to
|
||||||
|
**[OpenAI API](https://platform.openai.com/docs/api-reference/introduction)**,
|
||||||
|
including GPT-4 and various GPT-3 models
|
||||||
|
- Built-in support for various **open-source** models hosted on
|
||||||
|
[Hugging Face](https://huggingface.co/)
|
||||||
|
- Usage examples for standard NLP tasks such as **Named Entity Recognition** and
|
||||||
|
**Text Classification**
|
||||||
|
- Easy implementation of **your own functions** via the
|
||||||
|
[registry](/api/top-level#registry) for custom prompting, parsing and model
|
||||||
|
integrations
|
||||||
|
|
||||||
|
## Motivation {id="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.
|
||||||
|
|
||||||
|
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 {id="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](/usage).
|
||||||
|
|
||||||
|
> ⚠️ This package is still experimental and it is possible that changes made to
|
||||||
|
> the interface will be breaking in minor version updates.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m pip install spacy-llm
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage {id="usage"}
|
||||||
|
|
||||||
|
The task and the model have to be supplied to the `llm` pipeline component using
|
||||||
|
the [config system](/api/data-formats#config). This package provides various
|
||||||
|
built-in functionality, as detailed in the [API](#-api) documentation.
|
||||||
|
|
||||||
|
### Example 1: Add a text classifier using a GPT-3 model from OpenAI {id="example-1"}
|
||||||
|
|
||||||
|
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 [OpenAI](/api/large-language-models#gpt-3-5) section.
|
||||||
|
|
||||||
|
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.v2"
|
||||||
|
labels = ["COMPLIMENT", "INSULT"]
|
||||||
|
|
||||||
|
[components.llm.model]
|
||||||
|
@llm_models = "spacy.GPT-3-5.v1"
|
||||||
|
config = {"temperature": 0.3}
|
||||||
|
```
|
||||||
|
|
||||||
|
Now run:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from spacy_llm.util import assemble
|
||||||
|
|
||||||
|
nlp = assemble("config.cfg")
|
||||||
|
doc = nlp("You look gorgeous!")
|
||||||
|
print(doc.cats)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 2: Add NER using an open-source model through Hugging Face {id="example-2"}
|
||||||
|
|
||||||
|
To run this example, ensure that you have a GPU enabled, and `transformers`,
|
||||||
|
`torch` and CUDA installed. For more background information, see the
|
||||||
|
[DollyHF](/api/large-language-models#dolly) section.
|
||||||
|
|
||||||
|
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/ner_dolly)):
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[nlp]
|
||||||
|
lang = "en"
|
||||||
|
pipeline = ["llm"]
|
||||||
|
|
||||||
|
[components]
|
||||||
|
|
||||||
|
[components.llm]
|
||||||
|
factory = "llm"
|
||||||
|
|
||||||
|
[components.llm.task]
|
||||||
|
@llm_tasks = "spacy.NER.v2"
|
||||||
|
labels = ["PERSON", "ORGANISATION", "LOCATION"]
|
||||||
|
|
||||||
|
[components.llm.model]
|
||||||
|
@llm_models = "spacy.Dolly.v1"
|
||||||
|
# For better performance, use dolly-v2-12b instead
|
||||||
|
name = "dolly-v2-3b"
|
||||||
|
```
|
||||||
|
|
||||||
|
Now run:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from spacy_llm.util import assemble
|
||||||
|
|
||||||
|
nlp = assemble("config.cfg")
|
||||||
|
doc = nlp("Jack and Jill rode up the hill in Les Deux Alpes")
|
||||||
|
print([(ent.text, ent.label_) for ent in doc.ents])
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that Hugging Face will download the `"databricks/dolly-v2-3b"` model the
|
||||||
|
first time you use it. You can
|
||||||
|
[define the cached directory](https://huggingface.co/docs/huggingface_hub/main/en/guides/manage-cache)
|
||||||
|
by setting the environmental variable `HF_HOME`. Also, you can upgrade the model
|
||||||
|
to be `"databricks/dolly-v2-12b"` for better performance.
|
||||||
|
|
||||||
|
### Example 3: Create the component directly in Python {id="example-3"}
|
||||||
|
|
||||||
|
The `llm` component behaves as any other component does, so adding it to an
|
||||||
|
existing pipeline follows the same pattern:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import spacy
|
||||||
|
|
||||||
|
nlp = spacy.blank("en")
|
||||||
|
nlp.add_pipe(
|
||||||
|
"llm",
|
||||||
|
config={
|
||||||
|
"task": {
|
||||||
|
"@llm_tasks": "spacy.NER.v2",
|
||||||
|
"labels": ["PERSON", "ORGANISATION", "LOCATION"]
|
||||||
|
},
|
||||||
|
"model": {
|
||||||
|
"@llm_models": "spacy.gpt-3.5.v1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
nlp.initialize()
|
||||||
|
doc = nlp("Jack and Jill rode up the hill in Les Deux Alpes")
|
||||||
|
print([(ent.text, ent.label_) for ent in doc.ents])
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that for efficient usage of resources, typically you would use
|
||||||
|
[`nlp.pipe(docs)`](/api/language#pipe) with a batch, instead of calling
|
||||||
|
`nlp(doc)` with a single document.
|
||||||
|
|
||||||
|
### Example 4: Implement your own custom task {id="example-4"}
|
||||||
|
|
||||||
|
To write a [`task`](#tasks), you need to implement two functions:
|
||||||
|
`generate_prompts` that takes a list of [`Doc`](/api/doc) objects and transforms
|
||||||
|
them into a list of prompts, and `parse_responses` that transforms the LLM
|
||||||
|
outputs into annotations on the [`Doc`](/api/doc), e.g. entity spans, text
|
||||||
|
categories and more.
|
||||||
|
|
||||||
|
To register your custom task, decorate a factory function using the
|
||||||
|
`spacy_llm.registry.llm_tasks` decorator with a custom name that you can refer
|
||||||
|
to in your config.
|
||||||
|
|
||||||
|
> 📖 For more details, see the
|
||||||
|
> [**usage example on writing your own task**](https://github.com/explosion/spacy-llm/tree/main/usage_examples#writing-your-own-task)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from typing import Iterable, List
|
||||||
|
from spacy.tokens import Doc
|
||||||
|
from spacy_llm.registry import registry
|
||||||
|
from spacy_llm.util import split_labels
|
||||||
|
|
||||||
|
|
||||||
|
@registry.llm_tasks("my_namespace.MyTask.v1")
|
||||||
|
def make_my_task(labels: str, my_other_config_val: float) -> "MyTask":
|
||||||
|
labels_list = split_labels(labels)
|
||||||
|
return MyTask(labels=labels_list, my_other_config_val=my_other_config_val)
|
||||||
|
|
||||||
|
|
||||||
|
class MyTask:
|
||||||
|
def __init__(self, labels: List[str], my_other_config_val: float):
|
||||||
|
...
|
||||||
|
|
||||||
|
def generate_prompts(self, docs: Iterable[Doc]) -> Iterable[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
def parse_responses(
|
||||||
|
self, docs: Iterable[Doc], responses: Iterable[str]
|
||||||
|
) -> Iterable[Doc]:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# config.cfg (excerpt)
|
||||||
|
[components.llm.task]
|
||||||
|
@llm_tasks = "my_namespace.MyTask.v1"
|
||||||
|
labels = LABEL1,LABEL2,LABEL3
|
||||||
|
my_other_config_val = 0.3
|
||||||
|
```
|
||||||
|
|
||||||
|
## Logging {id="logging"}
|
||||||
|
|
||||||
|
spacy-llm has a built-in logger that can log the prompt sent to the LLM as well
|
||||||
|
as its raw response. This logger uses the debug level and by default has a
|
||||||
|
`logging.NullHandler()` configured.
|
||||||
|
|
||||||
|
In order to use this logger, you can setup a simple handler like this:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import logging
|
||||||
|
import spacy_llm
|
||||||
|
|
||||||
|
|
||||||
|
spacy_llm.logger.addHandler(logging.StreamHandler())
|
||||||
|
spacy_llm.logger.setLevel(logging.DEBUG)
|
||||||
|
```
|
||||||
|
|
||||||
|
> NOTE: Any `logging` handler will work here so you probably want to use some
|
||||||
|
> sort of rotating `FileHandler` as the generated prompts can be quite long,
|
||||||
|
> especially for tasks with few-shot examples.
|
||||||
|
|
||||||
|
Then when using the pipeline you'll be able to view the prompt and response.
|
||||||
|
|
||||||
|
E.g. with the config and code from [Example 1](#example-1) above:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from spacy_llm.util import assemble
|
||||||
|
|
||||||
|
|
||||||
|
nlp = assemble("config.cfg")
|
||||||
|
doc = nlp("You look gorgeous!")
|
||||||
|
print(doc.cats)
|
||||||
|
```
|
||||||
|
|
||||||
|
You will see `logging` output similar to:
|
||||||
|
|
||||||
|
```
|
||||||
|
Generated prompt for doc: You look gorgeous!
|
||||||
|
|
||||||
|
You are an expert Text Classification system. Your task is to accept Text as input
|
||||||
|
and provide a category for the text based on the predefined labels.
|
||||||
|
|
||||||
|
Classify the text below to any of the following labels: COMPLIMENT, INSULT
|
||||||
|
The task is non-exclusive, so you can provide more than one label as long as
|
||||||
|
they're comma-delimited. For example: Label1, Label2, Label3.
|
||||||
|
Do not put any other text in your answer, only one or more of the provided labels with nothing before or after.
|
||||||
|
If the text cannot be classified into any of the provided labels, answer `==NONE==`.
|
||||||
|
|
||||||
|
Here is the text that needs classification
|
||||||
|
|
||||||
|
|
||||||
|
Text:
|
||||||
|
'''
|
||||||
|
You look gorgeous!
|
||||||
|
'''
|
||||||
|
|
||||||
|
Model response for doc: You look gorgeous!
|
||||||
|
COMPLIMENT
|
||||||
|
```
|
||||||
|
|
||||||
|
`print(doc.cats)` to standard output should look like:
|
||||||
|
|
||||||
|
```
|
||||||
|
{'COMPLIMENT': 1.0, 'INSULT': 0.0}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API {id="api"}
|
||||||
|
|
||||||
|
`spacy-llm` exposes a `llm` factory with
|
||||||
|
[configurable settings](/api/large-language-models#config).
|
||||||
|
|
||||||
|
An `llm` component is defined by two main settings:
|
||||||
|
|
||||||
|
- A [**task**](#tasks), defining the prompt to send to the LLM as well as the
|
||||||
|
functionality to parse the resulting response back into structured fields on
|
||||||
|
the [Doc](/api/doc) objects.
|
||||||
|
- A [**model**](#models) defining the model to use and how to connect to it.
|
||||||
|
Note that `spacy-llm` supports both access to external APIs (such as OpenAI)
|
||||||
|
as well as access to self-hosted open-source LLMs (such as using Dolly through
|
||||||
|
Hugging Face).
|
||||||
|
|
||||||
|
Moreover, `spacy-llm` exposes a customizable [**caching**](#cache) functionality
|
||||||
|
to avoid running the same document through an LLM service (be it local or
|
||||||
|
through a REST API) more than once.
|
||||||
|
|
||||||
|
Finally, you can choose to save a stringified version of LLM prompts/responses
|
||||||
|
within the `Doc.user_data["llm_io"]` attribute by setting `save_io` to `True`.
|
||||||
|
`Doc.user_data["llm_io"]` is a dictionary containing one entry for every LLM
|
||||||
|
component within the `nlp` pipeline. Each entry is itself a dictionary, with two
|
||||||
|
keys: `prompt` and `response`.
|
||||||
|
|
||||||
|
A note on `validate_types`: by default, `spacy-llm` checks whether the
|
||||||
|
signatures of the `model` and `task` callables are consistent with each other
|
||||||
|
and emits a warning if they don't. `validate_types` can be set to `False` if you
|
||||||
|
want to disable this behavior.
|
||||||
|
|
||||||
|
### Tasks {id="tasks"}
|
||||||
|
|
||||||
|
A _task_ defines an NLP problem or question, that will be sent to the LLM via a
|
||||||
|
prompt. Further, the task defines how to parse the LLM's responses back into
|
||||||
|
structured information. All tasks are registered in the `llm_tasks` registry.
|
||||||
|
|
||||||
|
Practically speaking, a task should adhere to the `Protocol` `LLMTask` defined
|
||||||
|
in [`ty.py`](https://github.com/explosion/spacy-llm/blob/main/spacy_llm/ty.py).
|
||||||
|
It needs to define a `generate_prompts` function and a `parse_responses`
|
||||||
|
function.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| [`task.generate_prompts`](/api/large-language-models#task-generate-prompts) | Takes a collection of documents, and returns a collection of "prompts", which can be of type `Any`. |
|
||||||
|
| [`task.parse_responses`](/api/large-language-models#task-parse-responses) | Takes a collection of LLM responses and the original documents, parses the responses into structured information, and sets the annotations on the documents. |
|
||||||
|
|
||||||
|
Moreover, the task may define an optional [`scorer` method](/api/scorer#score).
|
||||||
|
It should accept an iterable of `Example`s as input and return a score
|
||||||
|
dictionary. If the `scorer` method is defined, `spacy-llm` will call it to
|
||||||
|
evaluate the component.
|
||||||
|
|
||||||
|
| Component | Description |
|
||||||
|
| ----------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| [`spacy.Summarization.v1`](/api/large-language-models#summarization-v1) | The summarization task prompts the model for a concise summary of the provided text. |
|
||||||
|
| [`spacy.NER.v2`](/api/large-language-models#ner-v2) | The built-in NER task supports both zero-shot and few-shot prompting. This version also supports explicitly defining the provided labels with custom descriptions. |
|
||||||
|
| [`spacy.NER.v1`](/api/large-language-models#ner-v1) | The original version of the built-in NER task supports both zero-shot and few-shot prompting. |
|
||||||
|
| [`spacy.SpanCat.v2`](/api/large-language-models#spancat-v2) | The built-in SpanCat task is a simple adaptation of the NER task to support overlapping entities and store its annotations in `doc.spans`. |
|
||||||
|
| [`spacy.SpanCat.v1`](/api/large-language-models#spancat-v1) | The original version of the built-in SpanCat task is a simple adaptation of the v1 NER task to support overlapping entities and store its annotations in `doc.spans`. |
|
||||||
|
| [`spacy.TextCat.v3`](/api/large-language-models#textcat-v3) | Version 3 (the most recent) of the built-in TextCat task supports both zero-shot and few-shot prompting. It allows setting definitions of labels. |
|
||||||
|
| [`spacy.TextCat.v2`](/api/large-language-models#textcat-v2) | Version 2 of the built-in TextCat task supports both zero-shot and few-shot prompting and includes an improved prompt template. |
|
||||||
|
| [`spacy.TextCat.v1`](/api/large-language-models#textcat-v1) | Version 1 of the built-in TextCat task supports both zero-shot and few-shot prompting. |
|
||||||
|
| [`spacy.REL.v1`](/api/large-language-models#rel-v1) | The built-in REL task supports both zero-shot and few-shot prompting. It relies on an upstream NER component for entities extraction. |
|
||||||
|
| [`spacy.Lemma.v1`](/api/large-language-models#lemma-v1) | The `Lemma.v1` task lemmatizes the provided text and updates the `lemma_` attribute in the doc's tokens accordingly. |
|
||||||
|
| [`spacy.Sentiment.v1`](/api/large-language-models#sentiment-v1) | Performs sentiment analysis on provided texts. |
|
||||||
|
| [`spacy.NoOp.v1`](/api/large-language-models#noop-v1) | This task is only useful for testing - it tells the LLM to do nothing, and does not set any fields on the `docs`. |
|
||||||
|
|
||||||
|
#### Providing examples for few-shot prompts {id="few-shot-prompts"}
|
||||||
|
|
||||||
|
All built-in tasks support few-shot prompts, i. e. including examples in a
|
||||||
|
prompt. Examples can be supplied in two ways: (1) as a separate file containing
|
||||||
|
only examples or (2) by initializing `llm` with a `get_examples()` callback
|
||||||
|
(like any other pipeline component).
|
||||||
|
|
||||||
|
##### (1) Few-shot example file
|
||||||
|
|
||||||
|
A file containing examples for few-shot prompting can be configured like this:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[components.llm.task]
|
||||||
|
@llm_tasks = "spacy.NER.v2"
|
||||||
|
labels = PERSON,ORGANISATION,LOCATION
|
||||||
|
[components.llm.task.examples]
|
||||||
|
@misc = "spacy.FewShotReader.v1"
|
||||||
|
path = "ner_examples.yml"
|
||||||
|
```
|
||||||
|
|
||||||
|
The supplied file has to conform to the format expected by the required task
|
||||||
|
(see the task documentation further down).
|
||||||
|
|
||||||
|
##### (2) Initializing the `llm` component with a `get_examples()` callback
|
||||||
|
|
||||||
|
Alternatively, you can initialize your `nlp` pipeline by providing a
|
||||||
|
`get_examples` callback for [`nlp.initialize`](/api/language#initialize) and
|
||||||
|
setting `n_prompt_examples` to a positive number to automatically fetch a few
|
||||||
|
examples for few-shot learning. Set `n_prompt_examples` to `-1` to use all
|
||||||
|
examples as part of the few-shot learning prompt.
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[initialize.components.llm]
|
||||||
|
n_prompt_examples = 3
|
||||||
|
```
|
||||||
|
|
||||||
|
### Model {id="models"}
|
||||||
|
|
||||||
|
A _model_ defines which LLM model to query, and how to query it. It can be a
|
||||||
|
simple function taking a collection of prompts (consistent with the output type
|
||||||
|
of `task.generate_prompts()`) and returning a collection of responses
|
||||||
|
(consistent with the expected input of `parse_responses`). Generally speaking,
|
||||||
|
it's a function of type `Callable[[Iterable[Any]], Iterable[Any]]`, but specific
|
||||||
|
implementations can have other signatures, like
|
||||||
|
`Callable[[Iterable[str]], Iterable[str]]`.
|
||||||
|
|
||||||
|
All built-in models are registered in `llm_models`. If no model is specified,
|
||||||
|
the repo currently connects to the `OpenAI` API by default using REST, and
|
||||||
|
accesses the `"gpt-3.5-turbo"` model.
|
||||||
|
|
||||||
|
Currently three different approaches to use LLMs are supported:
|
||||||
|
|
||||||
|
1. `spacy-llm`s native REST interface. This is the default for all hosted models
|
||||||
|
(e. g. OpenAI, Cohere, Anthropic, ...).
|
||||||
|
2. A HuggingFace integration that allows to run a limited set of HF models
|
||||||
|
locally.
|
||||||
|
3. A LangChain integration that allows to run any model supported by LangChain
|
||||||
|
(hosted or locally).
|
||||||
|
|
||||||
|
Approaches 1. and 2 are the default for hosted model and local models,
|
||||||
|
respectively. Alternatively you can use LangChain to access hosted or local
|
||||||
|
models by specifying one of the models registered with the `langchain.` prefix.
|
||||||
|
|
||||||
|
<Infobox>
|
||||||
|
_Why LangChain if there are also are a native REST and a HuggingFace interface? When should I use what?_
|
||||||
|
|
||||||
|
Third-party libraries like `langchain` focus on prompt management, integration
|
||||||
|
of many different LLM APIs, and other related features such as conversational
|
||||||
|
memory or agents. `spacy-llm` on the other hand emphasizes features we consider
|
||||||
|
useful in the context of NLP pipelines utilizing LLMs to process documents
|
||||||
|
(mostly) independent from each other. It makes sense that the feature sets of
|
||||||
|
such third-party libraries and `spacy-llm` aren't identical - and users might
|
||||||
|
want to take advantage of features not available in `spacy-llm`.
|
||||||
|
|
||||||
|
The advantage of implementing our own REST and HuggingFace integrations is that
|
||||||
|
we can ensure a larger degree of stability and robustness, as we can guarantee
|
||||||
|
backwards-compatibility and more smoothly integrated error handling.
|
||||||
|
|
||||||
|
If however there are features or APIs not natively covered by `spacy-llm`, it's
|
||||||
|
trivial to utilize LangChain to cover this - and easy to customize the prompting
|
||||||
|
mechanism, if so required.
|
||||||
|
|
||||||
|
</Infobox>
|
||||||
|
|
||||||
|
<Infobox variant="warning">
|
||||||
|
Note that when using hosted services, you have to ensure that the [proper API
|
||||||
|
keys](/api/large-language-models#api-keys) are set as environment variables as described by the corresponding
|
||||||
|
provider's documentation.
|
||||||
|
|
||||||
|
</Infobox>
|
||||||
|
|
||||||
|
| Component | Description |
|
||||||
|
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ |
|
||||||
|
| [`spacy.GPT-4.v1`](/api/large-language-models#gpt-4) | OpenAI’s `gpt-4` model family. |
|
||||||
|
| [`spacy.GPT-3-5.v1`](/api/large-language-models#gpt-3-5) | OpenAI’s `gpt-3-5` model family. |
|
||||||
|
| [`spacy.Text-Davinci.v1`](/api/large-language-models#text-davinci) | OpenAI’s `text-davinci` model family. |
|
||||||
|
| [`spacy.Code-Davinci.v1`](/api/large-language-models#code-davinci) | OpenAI’s `code-davinci` model family. |
|
||||||
|
| [`spacy.Text-Curie.v1`](/api/large-language-models#text-curie) | OpenAI’s `text-curie` model family. |
|
||||||
|
| [`spacy.Text-Babbage.v1`](/api/large-language-models#text-babbage) | OpenAI’s `text-babbage` model family. |
|
||||||
|
| [`spacy.Text-Ada.v1`](/api/large-language-models#text-ada) | OpenAI’s `text-ada` model family. |
|
||||||
|
| [`spacy.Davinci.v1`](/api/large-language-models#davinci) | OpenAI’s `davinci` model family. |
|
||||||
|
| [`spacy.Curie.v1`](/api/large-language-models#curie) | OpenAI’s `curie` model family. |
|
||||||
|
| [`spacy.Babbage.v1`](/api/large-language-models#babbage) | OpenAI’s `babbage` model family. |
|
||||||
|
| [`spacy.Ada.v1`](/api/large-language-models#ada) | OpenAI’s `ada` model family. |
|
||||||
|
| [`spacy.Command.v1`](/api/large-language-models#command) | Cohere’s `command` model family. |
|
||||||
|
| [`spacy.Claude-1.v1`](/api/large-language-models#claude-1) | Anthropic’s `claude-1` model family. |
|
||||||
|
| [`spacy.Claude-instant-1.v1`](/api/large-language-models#claude-instant-1) | Anthropic’s `claude-instant-1` model family. |
|
||||||
|
| [`spacy.Claude-instant-1-1.v1`](/api/large-language-models#claude-instant-1-1) | Anthropic’s `claude-instant-1.1` model family. |
|
||||||
|
| [`spacy.Claude-1-0.v1`](/api/large-language-models#claude-1-0) | Anthropic’s `claude-1.0` model family. |
|
||||||
|
| [`spacy.Claude-1-2.v1`](/api/large-language-models#claude-1-2) | Anthropic’s `claude-1.2` model family. |
|
||||||
|
| [`spacy.Claude-1-3.v1`](/api/large-language-models#claude-1-3) | Anthropic’s `claude-1.3` model family. |
|
||||||
|
| [`spacy.Dolly.v1`](/api/large-language-models#dolly) | Dolly models through [Databricks](https://huggingface.co/databricks) on HuggingFace. |
|
||||||
|
| [`spacy.Falcon.v1`](/api/large-language-models#falcon) | Falcon model through HuggingFace. |
|
||||||
|
| [`spacy.StableLM.v1`](/api/large-language-models#stablelm) | StableLM model through HuggingFace. |
|
||||||
|
| [`spacy.OpenLLaMA.v1`](/api/large-language-models#openllama) | OpenLLaMA model through HuggingFace. |
|
||||||
|
| [LangChain models](/api/large-language-models#langchain-models) | LangChain models for API retrieval. |
|
||||||
|
|
||||||
|
### Cache {id="cache"}
|
||||||
|
|
||||||
|
Interacting with LLMs, either through an external API or a local instance, is
|
||||||
|
costly. Since developing an NLP pipeline generally means a lot of exploration
|
||||||
|
and prototyping, `spacy-llm` implements a built-in
|
||||||
|
[cache](/api/large-language-models#cache) to avoid reprocessing the same
|
||||||
|
documents at each run that keeps batches of documents stored on disk.
|
||||||
|
|
||||||
|
### Various functions {id="various-functions"}
|
||||||
|
|
||||||
|
| Component | Description |
|
||||||
|
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| [`spacy.FewShotReader.v1`](/api/large-language-models#fewshotreader-v1) | This function is registered in spaCy's `misc` registry, and reads in examples from a `.yml`, `.yaml`, `.json` or `.jsonl` file. It uses [`srsly`](https://github.com/explosion/srsly) to read in these files and parses them depending on the file extension. |
|
||||||
|
| [`spacy.FileReader.v1`](/api/large-language-models#filereader-v1) | This function is registered in spaCy's `misc` registry, and reads a file provided to the `path` to return a `str` representation of its contents. This function is typically used to read [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) files containing the prompt template. |
|
||||||
|
| [Normalizer functions](/api/large-language-models#normalizer-functions) | These functions provide simple normalizations for string comparisons, e.g. between a list of specified labels and a label given in the raw text of the LLM response. |
|
|
@ -26,16 +26,19 @@
|
||||||
{ "text": "Processing Pipelines", "url": "/usage/processing-pipelines" },
|
{ "text": "Processing Pipelines", "url": "/usage/processing-pipelines" },
|
||||||
{
|
{
|
||||||
"text": "Embeddings & Transformers",
|
"text": "Embeddings & Transformers",
|
||||||
"url": "/usage/embeddings-transformers",
|
"url": "/usage/embeddings-transformers"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "Large Language Models",
|
||||||
|
"url": "/usage/large-language-models",
|
||||||
"tag": "new"
|
"tag": "new"
|
||||||
},
|
},
|
||||||
{ "text": "Training Models", "url": "/usage/training", "tag": "new" },
|
{ "text": "Training Models", "url": "/usage/training" },
|
||||||
{
|
{
|
||||||
"text": "Layers & Model Architectures",
|
"text": "Layers & Model Architectures",
|
||||||
"url": "/usage/layers-architectures",
|
"url": "/usage/layers-architectures"
|
||||||
"tag": "new"
|
|
||||||
},
|
},
|
||||||
{ "text": "spaCy Projects", "url": "/usage/projects", "tag": "new" },
|
{ "text": "spaCy Projects", "url": "/usage/projects" },
|
||||||
{ "text": "Saving & Loading", "url": "/usage/saving-loading" },
|
{ "text": "Saving & Loading", "url": "/usage/saving-loading" },
|
||||||
{ "text": "Visualizers", "url": "/usage/visualizers" }
|
{ "text": "Visualizers", "url": "/usage/visualizers" }
|
||||||
]
|
]
|
||||||
|
@ -102,6 +105,7 @@
|
||||||
{ "text": "EntityLinker", "url": "/api/entitylinker" },
|
{ "text": "EntityLinker", "url": "/api/entitylinker" },
|
||||||
{ "text": "EntityRecognizer", "url": "/api/entityrecognizer" },
|
{ "text": "EntityRecognizer", "url": "/api/entityrecognizer" },
|
||||||
{ "text": "EntityRuler", "url": "/api/entityruler" },
|
{ "text": "EntityRuler", "url": "/api/entityruler" },
|
||||||
|
{ "text": "Large Language Models", "url": "/api/large-language-models" },
|
||||||
{ "text": "Lemmatizer", "url": "/api/lemmatizer" },
|
{ "text": "Lemmatizer", "url": "/api/lemmatizer" },
|
||||||
{ "text": "Morphologizer", "url": "/api/morphologizer" },
|
{ "text": "Morphologizer", "url": "/api/morphologizer" },
|
||||||
{ "text": "SentenceRecognizer", "url": "/api/sentencerecognizer" },
|
{ "text": "SentenceRecognizer", "url": "/api/sentencerecognizer" },
|
||||||
|
|
|
@ -106,50 +106,21 @@ const Landing = () => {
|
||||||
|
|
||||||
<LandingBannerGrid>
|
<LandingBannerGrid>
|
||||||
<LandingBanner
|
<LandingBanner
|
||||||
to="https://explosion.ai/custom-solutions"
|
label="NEW"
|
||||||
|
title="Large Language Models: Integrating LLMs into structured NLP pipelines"
|
||||||
|
to="/usage/large-language-models"
|
||||||
button="Learn more"
|
button="Learn more"
|
||||||
background="#E4F4F9"
|
|
||||||
color="#1e1935"
|
|
||||||
small
|
small
|
||||||
>
|
>
|
||||||
<p>
|
<p>
|
||||||
<Link to="https://explosion.ai/custom-solutions" hidden>
|
<Link to="https://github.com/explosion/spacy-llm">
|
||||||
<ImageFill
|
The spacy-llm package
|
||||||
image={tailoredPipelinesImage}
|
</Link>{' '}
|
||||||
alt="spaCy Tailored Pipelines"
|
integrates Large Language Models (LLMs) into spaCy, featuring a modular
|
||||||
/>
|
system for <strong>fast prototyping</strong> and <strong>prompting</strong>,
|
||||||
</Link>
|
and turning unstructured responses into <strong>robust outputs</strong> for
|
||||||
|
various NLP tasks, <strong>no training data</strong> required.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
|
||||||
<strong>
|
|
||||||
Get a custom spaCy pipeline, tailor-made for your NLP problem by
|
|
||||||
spaCy's core developers.
|
|
||||||
</strong>
|
|
||||||
</p>
|
|
||||||
<Ul>
|
|
||||||
<Li emoji="🔥">
|
|
||||||
<strong>Streamlined.</strong> Nobody knows spaCy better than we do. Send
|
|
||||||
us your pipeline requirements and we'll be ready to start producing
|
|
||||||
your solution in no time at all.
|
|
||||||
</Li>
|
|
||||||
<Li emoji="🐿 ">
|
|
||||||
<strong>Production ready.</strong> spaCy pipelines are robust and easy
|
|
||||||
to deploy. You'll get a complete spaCy project folder which is
|
|
||||||
ready to <InlineCode>spacy project run</InlineCode>.
|
|
||||||
</Li>
|
|
||||||
<Li emoji="🔮">
|
|
||||||
<strong>Predictable.</strong> You'll know exactly what you're
|
|
||||||
going to get and what it's going to cost. We quote fees up-front,
|
|
||||||
let you try before you buy, and don't charge for over-runs at our
|
|
||||||
end — all the risk is on us.
|
|
||||||
</Li>
|
|
||||||
<Li emoji="🛠">
|
|
||||||
<strong>Maintainable.</strong> spaCy is an industry standard, and
|
|
||||||
we'll deliver your pipeline with full code, data, tests and
|
|
||||||
documentation, so your team can retrain, update and extend the solution
|
|
||||||
as your requirements change.
|
|
||||||
</Li>
|
|
||||||
</Ul>
|
|
||||||
</LandingBanner>
|
</LandingBanner>
|
||||||
|
|
||||||
<LandingBanner
|
<LandingBanner
|
||||||
|
@ -240,21 +211,50 @@ const Landing = () => {
|
||||||
|
|
||||||
<LandingBannerGrid>
|
<LandingBannerGrid>
|
||||||
<LandingBanner
|
<LandingBanner
|
||||||
label="New in v3.0"
|
to="https://explosion.ai/custom-solutions"
|
||||||
title="Transformer-based pipelines, new training system, project templates & more"
|
button="Learn more"
|
||||||
to="/usage/v3"
|
background="#E4F4F9"
|
||||||
button="See what's new"
|
color="#1e1935"
|
||||||
small
|
small
|
||||||
>
|
>
|
||||||
<p>
|
<p>
|
||||||
spaCy v3.0 features all new <strong>transformer-based pipelines</strong>{' '}
|
<Link to="https://explosion.ai/custom-solutions" noLinkLayout>
|
||||||
that bring spaCy's accuracy right up to the current{' '}
|
<ImageFill
|
||||||
<strong>state-of-the-art</strong>. You can use any pretrained transformer to
|
image={tailoredPipelinesImage}
|
||||||
train your own pipelines, and even share one transformer between multiple
|
alt="spaCy Tailored Pipelines"
|
||||||
components with <strong>multi-task learning</strong>. Training is now fully
|
/>
|
||||||
configurable and extensible, and you can define your own custom models using{' '}
|
</Link>
|
||||||
<strong>PyTorch</strong>, <strong>TensorFlow</strong> and other frameworks.
|
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<strong>
|
||||||
|
Get a custom spaCy pipeline, tailor-made for your NLP problem by
|
||||||
|
spaCy's core developers.
|
||||||
|
</strong>
|
||||||
|
</p>
|
||||||
|
<Ul>
|
||||||
|
<Li emoji="🔥">
|
||||||
|
<strong>Streamlined.</strong> Nobody knows spaCy better than we do. Send
|
||||||
|
us your pipeline requirements and we'll be ready to start producing
|
||||||
|
your solution in no time at all.
|
||||||
|
</Li>
|
||||||
|
<Li emoji="🐿 ">
|
||||||
|
<strong>Production ready.</strong> spaCy pipelines are robust and easy
|
||||||
|
to deploy. You'll get a complete spaCy project folder which is
|
||||||
|
ready to <InlineCode>spacy project run</InlineCode>.
|
||||||
|
</Li>
|
||||||
|
<Li emoji="🔮">
|
||||||
|
<strong>Predictable.</strong> You'll know exactly what you're
|
||||||
|
going to get and what it's going to cost. We quote fees up-front,
|
||||||
|
let you try before you buy, and don't charge for over-runs at our
|
||||||
|
end — all the risk is on us.
|
||||||
|
</Li>
|
||||||
|
<Li emoji="🛠">
|
||||||
|
<strong>Maintainable.</strong> spaCy is an industry standard, and
|
||||||
|
we'll deliver your pipeline with full code, data, tests and
|
||||||
|
documentation, so your team can retrain, update and extend the solution
|
||||||
|
as your requirements change.
|
||||||
|
</Li>
|
||||||
|
</Ul>
|
||||||
</LandingBanner>
|
</LandingBanner>
|
||||||
<LandingBanner
|
<LandingBanner
|
||||||
to="https://course.spacy.io"
|
to="https://course.spacy.io"
|
||||||
|
@ -264,7 +264,7 @@ const Landing = () => {
|
||||||
small
|
small
|
||||||
>
|
>
|
||||||
<p>
|
<p>
|
||||||
<Link to="https://course.spacy.io" hidden>
|
<Link to="https://course.spacy.io" noLinkLayout>
|
||||||
<ImageFill
|
<ImageFill
|
||||||
image={courseImage}
|
image={courseImage}
|
||||||
alt="Advanced NLP with spaCy: A free online course"
|
alt="Advanced NLP with spaCy: A free online course"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user