spacy-llm: Integrating LLMs into structured NLP pipelines.

Pankaj Pandey
2 min readMay 21, 2023

--

LLM Functionality visual

It is crucial for everyone, especially machine learning developers, to be familiar with Spacy, the NLP tool widely utilized in the field. Recently, Spacy has introduced a new library called spacy-llm, which focuses on llm integration.

spacy-llm is a Python package that integrates Large Language Models (LLMs) into spaCy, a popular Natural Language Processing (NLP) library. spacy-llm allows user to use LLMs to power a variety of NLP tasks, including text classification, named entity recognition and question answering.

spacy-llm is a powerful tool for anyone who wants to use LLMs to improve their NLP applications. It is easy to use and can be integrated with any spaCy pipeline.

spacy-llm works by first converting your NLP task into a prompt that can be understood by an LLM. The prompt is then sent to the LLM, which generates a response. The response is then parsed by spacy-llm and the results are added to the spaCy Doc object.

spacy-llm supports a variety of LLMs, including GPT-3, Jurassic-1 Jumbo and Megatron-Turing NLG. It also supports a variety of NLP tasks, including text classification, named entity recognition and question answering.

To use spacy-llm, you first need to install it. You can do this with the following command:

pip install spacy-llm

Once spacy-llm is installed, you can create a new spaCy pipeline and add the LLM component. You can do this with the following code:

nlp = spacy.blank("en")
nlp.add_pipe("llm")

Now you can use the LLM to power your NLP tasks. For example, you can use the LLM to classify text with the following code:

doc = nlp("This is a blog post about NLP.")

labels = ["news", "blog", "article"]

prediction = nlp.predict(doc, labels)

print(prediction)

This will print the following output:

blog

You can also use the LLM to recognize named entities with the following code:

doc = nlp("Bard is a large language model from Google AI.")

ents = doc.ents

for ent in ents:

print(ent.text, ent.label_)

This will print the following output:

Bard PERSON
Google AI ORGANIZATION

spacy-llm is a powerful tool that can be used to improve a variety of NLP applications. It is easy to use and can be integrated with any spaCy pipeline.

Code Example:

The following code shows how to use spacy-llm to classify text:

import spacy

nlp = spacy.blank("en")
nlp.add_pipe("llm")

doc = nlp("This is a blog post about NLP.")

labels = ["news", "blog", "article"]

prediction = nlp.predict(doc, labels)

print(prediction)

The following code shows how to use spacy-llm to recognize named entities:

import spacy

nlp = spacy.blank("en")
nlp.add_pipe("llm")

doc = nlp("Bard is a large language model from Google AI.")

ents = doc.ents

for ent in ents:

print(ent.text, ent.label_)

For more info please visit.

https://github.com/explosion/spacy-llm

--

--

Pankaj Pandey

Expert in software technologies with proficiency in multiple languages, experienced in Generative AI, NLP, Bigdata, and application development.