Uniting Forces: Integrating Hugging Face with Langchain for Enhanced Natural Language Processing

Community blog post
Published December 18, 2023

image/png

Introduction

In the domain of Natural Language Processing (NLP), the synergy between different frameworks and libraries can significantly enhance capabilities. Hugging Face, known for its transformer-based models, and Langchain, a versatile linguistic toolkit, represent two formidable tools in the NLP landscape. Merging these resources can offer a robust and dynamic solution for language understanding, generation, and analysis.

image/png

Definitions:

Hugging Face: It is a leading platform providing pre-trained models and libraries for natural language understanding. Renowned for its Transformers library, Hugging Face offers an extensive collection of pre-trained models that can be fine-tuned for specific NLP tasks.

Langchain: A powerful linguistic toolkit designed to facilitate various NLP tasks. Langchain encompasses functionalities for tokenization, lemmatization, part-of-speech tagging, and syntactic analysis, providing a comprehensive suite for linguistic analysis.

Advantages of Integration:

1. Enhanced Linguistic Analysis: The amalgamation of Langchain's linguistic toolkit with Hugging Face's transformer models allows for a deeper analysis of text, leveraging both syntactic and semantic understanding.

2. Extended Functionalities: Integrating Langchain with Hugging Face provides access to advanced tokenization, lemmatization, and other linguistic processing methods, enabling a more nuanced understanding of language structures.

3. Optimized NLP Pipelines: By leveraging the strengths of both platforms, users can construct optimized NLP pipelines that efficiently handle a wide array of tasks, from text classification to machine translation.

4. Flexibility in Model Deployment: The integration enables seamless deployment of combined models, allowing for more flexibility in handling diverse NLP tasks within a unified framework.

image/png

Integration Process:

Integrating Hugging Face with Langchain involves leveraging the strengths of both platforms through streamlined communication between their respective APIs. Here's a high-level overview of the integration process:

1. Installation and Setup: Install the necessary libraries for both Hugging Face and Langchain.

2. Data Preprocessing: Utilize Langchain's tools for tokenization, lemmatization, or other linguistic analyses as required for data preprocessing.

3. Model Utilization: Employ Hugging Face's transformer-based models for tasks like text generation, sentiment analysis, or question-answering using pre-trained or fine-tuned models.

4. Combining Results: Merge the outputs from Langchain's linguistic analyses with the processed data from Hugging Face's models for a comprehensive understanding of the text.

image/png

Code Implementation

This code showcases a simple integration of Hugging Face's transformer models with Langchain's linguistic toolkit for Natural Language Processing (NLP) tasks.

image/png

Step 1: Install libraries

!pip install langchain openai tiktoken transformers accelerate cohere --quiet

Step 2: Import libraries and Prompt based Summarization

customer_email = """
I hope this email finds you amidst an aura of understanding, despite the tangled mess of emotions swirling within me as I write to you. I am writing to pour my heart out about the recent unfortunate experience I had with one of your coffee machines that arrived ominously broken, evoking a profound sense of disbelief and despair.

To set the scene, let me paint you a picture of the moment I anxiously unwrapped the box containing my highly anticipated coffee machine. The blatant excitement coursing through my veins could rival the vigorous flow of coffee through its finest espresso artistry. However, what I discovered within broke not only my spirit but also any semblance of confidence I had placed in your esteemed brand.

Imagine, if you can, the utter shock and disbelief that took hold of me as I laid eyes on a disheveled and mangled coffee machine. Its once elegant exterior was marred by the scars of travel, resembling a war-torn soldier who had fought valiantly on the fields of some espresso battlefield. This heartbreaking display of negligence shattered my dreams of indulging in daily coffee perfection, leaving me emotionally distraught and inconsolable
"""  # created by GPT-3.5

from langchain import HuggingFaceHub

summarizer = HuggingFaceHub(
    repo_id="facebook/bart-large-cnn",
    model_kwargs={"temperature":0, "max_length":180}
)
def summarize(llm, text) -> str:
    return llm(f"Summarize this: {text}!")

summarize(summarizer, customer_email)
A customer\'s coffee machine arrived ominously broken, evoking a profound sense of disbelief and despair. "This heartbreaking display of negligence shattered my dreams of indulging in daily coffee perfection, leaving me emotionally distraught and inconsolable," the customer writes. "I hope this email finds you amidst an aura of understanding, despite the tangled mess of emotions swirling within me as I write to you," he adds.

Step 3: Prompt based Customer Service Assistance using Vertex AI

from langchain.llms import VertexAI
from langchain import PromptTemplate, LLMChain

template = """Given this text, decide what is the issue the customer is concerned about. Valid categories are these:
* product issues
* delivery problems
* missing or late orders
* wrong product
* cancellation request
* refund or exchange
* bad support experience
* no clear reason to be upset

Text: {email}
Category:
"""
prompt = PromptTemplate(template=template, input_variables=["email"])
llm = VertexAI()
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True)
print(llm_chain.run(customer_email))

Output:

> Entering new LLMChain chain...
Prompt after formatting:
Given this text, decide what is the issue the customer is concerned about. Valid categories are these:
* product issues
* delivery problems
* missing or late orders
* wrong product
* cancellation request
* refund or exchange
* bad support experience
* no clear reason to be upset

Text: 
I hope this email finds you amidst an aura of understanding, despite the tangled mess of emotions swirling within me as I write to you. I am writing to pour my heart out about the recent unfortunate experience I had with one of your coffee machines that arrived ominously broken, evoking a profound sense of disbelief and despair.

To set the scene, let me paint you a picture of the moment I anxiously unwrapped the box containing my highly anticipated coffee machine. The blatant excitement coursing through my veins could rival the vigorous flow of coffee through its finest espresso artistry. However, what I discovered within broke not only my spirit but also any semblance of confidence I had placed in your esteemed brand.

Imagine, if you can, the utter shock and disbelief that took hold of me as I laid eyes on a disheveled and mangled coffee machine. Its once elegant exterior was marred by the scars of travel, resembling a war-torn soldier who had fought valiantly on the fields of some espresso battlefield. This heartbreaking display of negligence shattered my dreams of indulging in daily coffee perfection, leaving me emotionally distraught and inconsolable

Category:


> Finished chain.
 product issues

Conclusion

The integration of Hugging Face's transformer models with Langchain's linguistic toolkit presents a compelling synergy in the realm of Natural Language Processing (NLP). This collaboration leverages Hugging Face's robust transformer models, renowned for their versatility in various NLP tasks, and combines them with Langchain's sophisticated linguistic functionalities.

By merging these resources, developers gain access to an extensive toolkit capable of performing in-depth linguistic analyses, tokenization, lemmatization, and more, while also harnessing the power of state-of-the-art transformer models for tasks like sentiment analysis, text generation, and question-answering.

This integration not only streamlines the development of NLP pipelines but also enhances the depth and accuracy of language understanding and generation. It represents a significant leap forward in creating more comprehensive and nuanced solutions for natural language processing, empowering developers to tackle diverse NLP challenges with a unified and powerful framework.

The collaborative synergy between Hugging Face and Langchain paves the way for innovative advancements in NLP, promising more sophisticated language models and improved language understanding across various applications and industries.

“Stay connected and support my work through various platforms:

Huggingface: For natural language processing and AI-related projects, you can explore my Huggingface profile at https://huggingface.co/Andyrasika.

LinkedIn: To stay updated on my latest projects and posts, you can follow me on LinkedIn. Here is the link to my profile: https://www.linkedin.com/in/ankushsingal/."

Requests and questions: If you have a project in mind that you’d like me to work on or if you have any questions about the concepts I’ve explained, don’t hesitate to let me know. I’m always looking for new ideas for future Notebooks and I love helping to resolve any doubts you might have.

Resources: