How to Integrate Large Language Models (LLMs) into spaCy

Mar 19, 2022 | Data Science

Welcome to your guide for incorporating Large Language Models into spaCy, making your natural language processing tasks faster and more efficient! With the spicy package spacy-llm, you can effectively turn unstructured responses into meaningful outputs without any training data. Let’s dive into its features, installation, and usage!

Feature Highlights

  • Serializable LLM component to integrate prompts into your spaCy pipeline.
  • Modular functions to define tasks (prompting and parsing) and models.
  • Supports various LLM APIs including:
  • Compatible with open-source LLMs hosted on Hugging Face like Falcon and Dolly.
  • Integration with LangChain models and features.
  • Supports tasks like Named Entity Recognition, Text Classification, Lemmatization, Sentiment Analysis, and more!

Installation

To install spacy-llm, run the following command in the same virtual environment as your spaCy installation:

python -m pip install spacy-llm

Note that this package is experimental, and updates may break compatibility in minor versions.

Quickstart Guide

Let’s get started with a simple example of text classification using OpenAI’s GPT model.

1. In Python Code

Begin by importing the necessary libraries and creating your processing pipeline:

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)

2. Using a Configuration File

If you want to control parameters more meticulously, use a config file:

[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

Run the following to utilize this configuration:

from spacy_llm.util import assemble
nlp = assemble("config.cfg")
doc = nlp("You look gorgeous!")
print(doc.cats)

Understanding the Concept with an Analogy

Imagine you are a chef in a restaurant. You have basic ingredients (words and sentences) and recipes (NLU tasks) to create delicious dishes (meaningful outputs). Using traditional methods of cooking requires you to follow each step strictly, often leading to longer wait times (supervised learning).

Now, with the advent of LLMs, it’s like having an experienced sous-chef who can improvise based on cut vegetables and available spices (contextual understanding). You don’t need exhaustive manuals or previous examples; you just provide the ingredients, and the LLM can create dishes that cater to customer’s unique tastes (custom NLP tasks). This blend allows you to produce exquisite gourmet meals quickly and adaptively, whether by sticking to traditional recipes or adding flair with the LLM’s creativity!

Troubleshooting Ideas

If you encounter issues when using spacy-llm, consider the following suggestions:

  • Ensure spaCy is correctly installed in your environment.
  • Verify that your API keys are set as environmental variables for the LLMs.
  • Check the compatibility of models and your tasks with the current version of spacy-llm.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Ongoing Work

Future enhancements will focus on:

  • Adding more example tasks
  • Supporting a broader range of models
  • Providing extensive examples and tutorials

Conclusion

With spacy-llm, you harness the synergy of structured NLP with the dynamism brought by Large Language Models. The ability to integrate various components allows customization and enhancement of your NLP tasks like never before. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox