LangChain in Action: Redefining Customer Experiences Through LLMs
LangChain significantly enhances the capabilities of LLMs, making them even more powerful and efficient in performing a wide range of tasks.
Join the DZone community and get the full member experience.
Join For FreeIn our previous blog post, “Unraveling LLMs' Full Potential, One Token at a Time with Open-source Framework,” we delved into the innovative strides made by LangChain in reshaping the capabilities of Large Language Models (LLMs). LangChain, as an open-source framework, equips LLMs with the capacity to provide nuanced and expert-level responses, a feat that has the potential to redefine how we interact with information.
However, in this installment, we’re diving even deeper into the practical applications of LangChain, particularly in the realm of customer support—a domain where personalized and informed interactions are paramount.
So, let’s dive in!
Direct Answer Generation
LangChain's capabilities provide a significant edge to applications requiring advanced question-answering systems based on Large Language Models. The framework's architecture is engineered to facilitate efficient querying of data and retrieving precise answers to those queries.
Implementing this intricate functionality is surprisingly straightforward, often requiring only a few lines of code for the default settings. For example, consider the process of querying a blog using LangChain:
from langchain.document_loaders import WebBaseLoader
from langchain.indexes import VectorstoreIndexCreator
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
index = VectorstoreIndexCreator().from_loaders([loader])
Now you can proceed to ask questions:
index.query("What is Task Decomposition?")
' Task decomposition is a technique used to break down complex tasks into smaller and simpler steps. It can be done using LLM with simple prompting, task-specific instructions, or human inputs. Tree of Thoughts (Yao et al. 2023) is an example of a task decomposition technique that explores multiple reasoning possibilities at each step and generates multiple thoughts per step, creating a tree structure.'
The above code showcases how LangChain automatically tokenizes textual data and stores them as vectors in the database. So when you ask an LLM a question, say, “What are the current customer support trends?” it gets converted to vectors, and LangChain looks for the context body that might contain the answer in the context vector database.
This will enable LangChain to find the paragraph most similar to your query, which is then sent to an LLM for generating a precise response.
Answer Summary and Title Generation
Beyond direct answer provision, LangChain's capabilities extend to generating article summaries and associated titles. This functionality proves invaluable in expediting the creation of new knowledge bases (KBs) by leveraging existing comments or discussions on specific cases.
You can combine two chains, one for generating the title and one for summarizing and combining them with a prompt, and voila! You’ve got yourself a ready-to-go title and summary generator!
Chatbots
Incorporating LangChain into LLM-powered chatbots enables them to yield impressive results.
As chatbots are one of the central LLM use cases, their core features involve long-running conversations with access to information that users want to know about.
Memory and retrieval are some of the core components of a chatbot, allowing it to remember past interactions and provide up-to-date, domain-specific information.
Intent Suggestion
The fusion of LangChain and LLMs empowers the generation of contextually rich responses. This includes the novel approach of intent suggestion. By utilizing chains and offering examples, LangChain can provide nuanced information based on the provided context.
For example, we can provide utterances for a given intent like:
“Login Issue” (intent) = [“i can't login”, “help with logging in”, “cannot login”]
This method enhances user-centric responses by enabling the system to generate relevant utterances and information based on user intents.
Stopwords
This is similar to the intent suggested, but instead of asking for intents or utterances, you can ask the LLM to return stopwords based on the examples provided. A simple prompt can yield a collection of stopwords that align with the given context.
Here’s an example of LLM Chain for generating KB title and body based on a given case subject:
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
llm = OpenAI(temperature=.7)
# 1st LLMChain to generate article title
template = """
Generate a relevant title for an article that can solve a case which has the subject: {case_subject}
"""
prompt_template = PromptTemplate(input_variables=["case_subject"], template=template)
title_chain = LLMChain(llm=llm, prompt=prompt_template)
# 2nd LLMChain to generate article body based on generated article title
template = """
Generate a short summary for an article that is titled : {article_title}
"""
prompt_template = PromptTemplate(input_variables=["article_title"], template=template)
summary_chain = LLMChain(llm=llm, prompt=prompt_template)
# This is the overall chain where we run these two chains in sequence.
from langchain.chains import SimpleSequentialChain
overall_chain = SimpleSequentialChain(chains=[title_chain, summary_chain], verbose=True)
review = overall_chain.run("facing python flask issue")
print(review)
LLM Chain for generating a summary based on an input doc, where everything except for the instructions remains the same. We only need one chain this time!
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
llm = OpenAI(temperature=.7)
# Only LLMChain to generate doc summary
template = """
Generate a relevant summary for given doc: {doc_text}
"""
prompt_template = PromptTemplate(input_variables=["doc_text"], template=template)
summary_chain = LLMChain(llm=llm, prompt=prompt_template)
summary = summary_chain.run("this is an example document.")
print(summary)
In conclusion, LangChain is a robust framework that has the capability to transform LLM functions. By harnessing the capabilities of this open-source framework, many organizations are redefining the very nature of customer support.
Intrigued by the possibilities? Stay tuned as we continue to unveil the potential of LangChain and its impact on reshaping customer interactions.
Opinions expressed by DZone contributors are their own.
Comments