avatarYeyu Huang

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7440

Abstract

loaded has quickly generated by tagging chain which could save tons of your content managing workload. After being embedded with a code snippet, your recommendation system is capable of understanding the categories of each input content and dispatching it to a certain group of audiences with relevant preferences close to the tags.</p><figure id="0ed3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Mf4X0-lfg7Wmq83hPsThag.png"><figcaption>Generated output</figcaption></figure><h1 id="30f0">2. Email Classification</h1><p id="80b7">I know every email service provider has a mature toolset to filter, classify and prioritize incoming emails for users. Unfortunately, these tool is mostly based on traditional rule-based filtering and classification techniques, which may not always capture the nuances and context of emails effectively. However, if you want to enhance the email classification capabilities using more advanced techniques like the tagging chain, you can integrate it as a complementary tool alongside the existing tools provided by the email service provider.</p><h2 id="6acb">Prototype</h2><p id="f42d">Assuming you are a sales manager and your email box is full of different various topics of emails from the internal organization to external communities, from key customer complaints to normal user inquiries, without reading, it’s hard for you to prioritize them based on the normal tool before tagging chain step in.</p><p id="5268"><b>Step 1</b>— Import the modules</p><div id="25f1"><pre><span class="hljs-keyword">from</span> langchain.chains <span class="hljs-keyword">import</span> LLMChain, create_tagging_chain_pydantic <span class="hljs-keyword">from</span> langchain.chat_models <span class="hljs-keyword">import</span> ChatOpenAI <span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum <span class="hljs-keyword">from</span> pydantic <span class="hljs-keyword">import</span> BaseModel, Field <span class="hljs-keyword">import</span> os</pre></div><p id="525c"><b>Step 2</b> — Define the source text</p><p id="6925">The fake email has come from Amazon’s engineering director about a complaint about the quality of the product “GuessWhat”. let’s copy it to <code>text_input</code>.</p><div id="f975"><pre>text_input = <span class="hljs-string">""" Subject: Urgent Concerns about the Quality of GuessWhat AI Product

Dear Yeyu,

I am Hulk, the Engineering Director at Amazon. I am writing to express my deep disappointment and frustration with the quality of our GuessWhat AI product. We have identified several critical issues that compromise its performance and accuracy.

We value our longstanding partnership, and it is crucial that we address these concerns promptly. I request your immediate attention to this matter and ask that you assign a team of experts to collaborate with our engineering team. Together, we can identify the root causes and work towards a swift resolution.

Please provide a comprehensive plan outlining the steps you will take to improve the quality of GuessWhat. We need to establish a roadmap for enhancement to meet the highest standards of performance and reliability.

I appreciate your partnership and trust that, by working together, we can overcome these challenges and continue delivering exceptional products to our customers. I look forward to your prompt response.

Sincerely,

Hulk Engineering Director Amazon """</span> </pre></div><p id="7a4b"><b>Step 3</b>— Tag definition by Pydantic class</p><div id="082c"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">EmailFeatures</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): topic: <span class="hljs-built_in">str</span> = Field( ..., enum=[ <span class="hljs-string">"Customer Inquiry"</span>, <span class="hljs-string">"Customer Complaint"</span>, <span class="hljs-string">"Follow-up"</span>, <span class="hljs-string">"Proposal"</span>, <span class="hljs-string">"Demo/Product Presentation"</span>, <span class="hljs-string">"Deal Negotiation"</span>, <span class="hljs-string">"Order Processing"</span>, <span class="hljs-string">"Customer Testimonials/Reviews"</span>, <span class="hljs-string">"Product Information Request"</span>, <span class="hljs-string">"Account Support"</span>, <span class="hljs-string">"Shipping and Delivery"</span>, <span class="hljs-string">"Payment and Billing"</span>, <span class="hljs-string">"Return and Refund"</span>, <span class="hljs-string">"Product Support"</span>, <span class="hljs-string">"Technical Support"</span>, <span class="hljs-string">"Partnership/Cooperation"</span>, <span class="hljs-string">"Event/Conference Invitation"</span>, <span class="hljs-string">"Webinar/Training Registration"</span>, <span class="hljs-string">"Other"</span> ], ) sender_weighs: <span class="hljs-built_in">int</span> = Field( ..., description=<span class="hljs-string">"The importance of sender based on their title and company, score 1-10, 10 means Senior leadership from high ranking company"</span>, ) urgent: <span class="hljs-built_in">int</span> = Field( ..., description=<span class="hljs-string">"Score 1-10, 10 is the highest level of urgency"</span>, ) sentiment: <span class="hljs-built_in">str</span> = Field( ..., description=<span class="hljs-string">"whether the email has a positive, negative, or neutral sentiment."</span>, )</pre></div><p id="2b31">I defined four tags which indicate what type of topic in a very detailed definition, how important is the sender, the sender’s urgency, and his/her sentiment, which helps the receiver to manage their email much more efficiently.</p><p id="5cd3"><b>Step 4 </b>— Create tagging chain and generate tags</p><p id="b336">As usual, simply call <code>create_tagging_chain_pydantic()</code> by loading the tag definition and <code>gpt-3.5-turbo-0613</code> model which has the latest “function calling” update.</p><div id="8bb7"><pre>os.environ[<span class="hljs-string">"OPENAI_API_KEY"</span>] = <span class="hljs-string">"{Your_API_Key}"</span>

llm = ChatOpenAI(temperature=<span class="hljs-number">0</span>, model=<span class="hljs-string">"gpt-3.5-turbo-0613"</span>) tagging_chain = create_tagging_chain_pydantic(EmailFeatures, llm) res = tagging_chain.run(text_input) <span class="hljs-built_in">print</span>(res)</pre></div><p id="6d77">The result shows what I am expecting.</p><figure id="f00d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Fo54x3OPNrDMgM6KhSD9WA.png"><figcaption>Generated output</figcaption></figure><p id="b40a">You are able to integrate the code snippet into any popular email service provider by using their APIs like Gmail API and Azure Outlook API to organize your mailbox for handling email with high efficiency.</p><h1 id="205f">3. Resume Screening</h1><p id="c216">As a management leader or HR specialist, resume screening is so necessary but so frustrating work that wastes 80% of your time reading mismatched resumes. Traditional keyword filtering always falls short in accurately identifying qualified candidates. To alleviate

Options

these challenges and optimize resume screening, the tagging chain is naturally perfect for this task.</p><p id="6eab">Compared to the last two examples, for resume screening, you may be interested in more tags for completely extracting the essential personal details for evaluation. Here is my recommendation for tags:</p><ul><li>Education Background — degree, field of study, institution, certifications</li><li>Professional Experience — job title, industry experience, years, leadership roles</li><li>Publications and Research — papers, conference presentations</li><li>Skills and Expertise — tech skills, language proficiency</li></ul><p id="d343">Since the contents of resumes are so varied, we have to use the <code>Optional</code> type for the schema to tolerate the <code>None</code> value if the resume has not been provided related information in its content. For example:</p><div id="0b50"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonalDetails</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): job_title: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">str</span>] = Field( <span class="hljs-literal">None</span>, description=<span class="hljs-string">"The most recent job titles in the resume."</span>, ) ....</pre></div><p id="4777">Moreover, some features may consist of more than one item like technical skills, there is a <code>conlist</code> method that helps. The following code snippet shows how to construct the tech_skills list with an uncertain number of items.</p><div id="a6d0"><pre><span class="hljs-keyword">from</span> pydantic <span class="hljs-keyword">import</span> conlist

<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonalDetails</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): tech_skills: <span class="hljs-type">Optional</span>[conlist(<span class="hljs-built_in">str</span>, min_items=<span class="hljs-number">1</span>, max_items=<span class="hljs-number">7</span>)] = Field( <span class="hljs-literal">None</span>, ) ....</pre></div><h1 id="652a">4. Legal Document Analysis</h1><p id="d5bb">Reading a legal document is always a nightmare for me as well as other non-legal professionals because they always contain complex language, technical terms, and intricate clauses that can be overwhelming. The most common legal document for us is multiple kinds of “agreements” like Service Agreements, Purchase Agreements, or Lease Agreements. Using tagging chain to extract the key valuable information is not only saving our time but also improving our understanding of the document’s contents properly.</p><figure id="dcc4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bznIl5Xm6KtIjDrMIG9jFA.png"><figcaption>Photo by <a href="https://unsplash.com/@romaindancre?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Romain Dancre</a> on <a href="https://unsplash.com/photos/doplSDELX7E?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p id="482a">We can define the schema that includes the following tags:</p><ul><li>Service Description: scope, deliverables</li><li>Payment: fees, schedule, invoicing, penalties</li><li>Terms: start date, end date, renewal, termination</li></ul><h1 id="a721">5. Medical Record Annotation</h1><p id="3356">Reading a medical record is often another headache task for non-medical professionals due to the presence of complex terminology, technical jargon, and intricate details. Although in most cases, your medical record is only reviewed by medical doctors, for some mild illnesses, or routine check-ups for your children or your parents, non-medical professionals like you may need to navigate through medical records to order OTC medicine, prepare food or schedule the next clinic visit. In such cases, having a tagging chain can be incredibly helpful in extracting key valuable information and improving comprehension of the medical record’s contents.</p><figure id="e5ee"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_67jDrR6YOrlLiaBxwBibA.jpeg"><figcaption>Photo by <a href="https://unsplash.com/@spencerbdavis?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Spencer Davis</a> on <a href="https://unsplash.com/photos/s4_g2TCyNHM?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p id="39ef">By utilizing a tagging chain, we can define a schema that includes specific tags to extract key valuable information from medical records. Here’s an example of how the tagging chain can be applied to medical record annotation:</p><ul><li>Medical History — pre-existing conditions, allergies, surgeries, and chronic illnesses.</li><li>Medications — drug names, dosages, frequency, and duration.</li><li>Progress — response to treatment, and observations.</li><li>Follow-ups — new appointments, additional tests</li></ul><h1 id="3b5e">6. Extras</h1><p id="c7ec">Certainly, there are other usages of tagging chains like Review/Comment Management, Customer Support, Social Media Monitoring, and Survey Chatbots. This wonderful idea of a large language model application is transforming the traditional way we handle various tasks and processes.</p><p id="abad">Thanks for reading. If you think it’s helpful, please Clap 👏 for this article. Your encouragement and comments mean a lot to me, mentally and financially. 🍔</p><p id="f438"><b>Before you go:</b></p><p id="d60c">🎥 If you prefer video tutorials, please subscribe to my<b> <a href="https://www.youtube.com/@yeyulab?source=about_page-------------------------------------">YouTube</a></b> channel where I started to convert most of my articles to visual demonstrations.</p><p id="bbef">✍️ If you have any questions or requests, please leave me responses or find me on<b> <a href="https://twitter.com/Yeyu2HUANG/?source=about_page-------------------------------------">Twitter</a> (Now X) </b>and <a href="https://discord.gg/KPTCE4CEmp?source=about_page-------------------------------------"><b>Discord</b></a> where you can have my active support on development and deployment.</p><p id="7028">☕️ If you would like to have exclusive resources and technical services, joining the membership of my<b> <a href="https://ko-fi.com/yeyuh?source=about_page-------------------------------------">Ko-fi</a> </b>will be a good choice.</p><h1 id="9b06">Level Up Coding</h1><p id="2633">Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>💰 Free coding interview course ⇒ <a href="https://skilled.dev/?utm_source=luc&amp;utm_medium=article">View Course</a></li><li>🧠 AI Tools ⇒ <a href="https://www.aimind.so/">View Now</a></li></ul><p id="0837">🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev/">Newsletter</a></p><p id="9860">🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><b>Join the Level Up talent collective and find an amazing job</b></a></p></article></body>

5 Most Practical Usages of Langchain’s Tagging Chain That You May Not Be Aware Of

An Implementation Guide for a List of AI-driven Tagging Applications

Image generated by Midjourney

Using the OpenAI’s “function” parameters, Langchain allows users to specify a schema for tagging documents called Tagging Chain, that ensures the model produces precise tags aligned with the desired categories. From my perspective, the tagging feature means more than it looks because generating structured text rather than imaginative chat has much more practical usage that can really help individuals and businesses.

One of my previous articles about building an interview chatbot was also using tactics leveraging tagging chains.

From analyzing sentiment in customer feedback to identifying the intensity of language use, the potential applications of this technology are vast. This article concludes with the most practical uses of Langchain’s Tagging Chain that you may not be aware of. From the simple approach of defining properties and expected types, to a more complicated control over the model’s output with the use of enumerations, descriptions, and required properties, let’s see how it works.

1. Content Recommendation System

Designing a content recommendation system to serve the audience can be done in a few steps with Tagging Chain today. The general idea is to use the tagging system to categorize the content, and then use these tags to match the content with user preferences.

Each piece of content (e.g., text, voice streaming, or video) is passed through the tagging chain. The tagging chain is configured with a schema that is relevant to the content being tagged. For instance, if you are managing an aggregating media of IT technology news, you absolutely never push all the news in the same order to your various type of subscribers, instead, you might pick up relevant content to the proper audience based on the tags. By using tagging, your system can automatically tag every piece of news based on categories like ‘Topics’, ‘Company’, ‘Sentiment’, ‘Region’, ‘Event’ etc.

Prototyping

Step 1— Import the modules

from langchain.chains import LLMChain, create_tagging_chain_pydantic
from langchain.chat_models import ChatOpenAI
from enum import Enum
from pydantic import BaseModel, Field

import os

Step 2— Define the source text

You can define a function to handle the coming news one by one. In this prototype, we simply copy/paste the news body from the open Internet like https://www.technologyreview.com/2023/07/18/1076479/metas-latest-ai-model-is-free-for-all/ which talks about the new release of LLaMA 2.

text_input = """
Meta is going all in on open-source AI. The company is today unveiling LLaMA 2,
...
...
"""

Step 3 — Tag definition by Pydantic class

class NewsFeatures(BaseModel):
    topic: str = Field(
        ...,
        enum=[ "Artificial Intelligence",
                    "Cybersecurity",
                    "Data Science",
                    "Internet of Things",
                    "Blockchain",
                    "Quantum Computing",
                    "Augmented Reality / Virtual Reality",
                    "Machine Learning",
                    "5G",
                    "others"],
    )
    region: str = Field(
        ...,
        enum=["North America", "Europe", "Asia", "Africa", "Latin America"],
        description="Sense from the article details about which region does the story happened",
    )
    event: str = Field(
        ...,
        enum=["new product", "new technology", "earnings report", "Interview", "company operations", "startup", "others"],
    )
    sentiment: str = Field(
        ...,
        description="whether the news story has a positive, negative, or neutral sentiment.",
    )

Using enum type as much as possible to let the tagging chain to generated text that is under your control. The description is also quite critical to prompt language models to understand the meaning of tags.

Step 4 — Create tagging chain and generate tags

Simply call create_tagging_chain_pydantic() by loading the tag definition and gpt-3.5-turbo-0613 model which has the latest “function calling” update.

os.environ["OPENAI_API_KEY"] = "{Your_API_Key}"

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
tagging_chain = create_tagging_chain_pydantic(NewsFeatures, llm)
res = tagging_chain.run(text_input)

print(res)

The feature of the news loaded has quickly generated by tagging chain which could save tons of your content managing workload. After being embedded with a code snippet, your recommendation system is capable of understanding the categories of each input content and dispatching it to a certain group of audiences with relevant preferences close to the tags.

Generated output

2. Email Classification

I know every email service provider has a mature toolset to filter, classify and prioritize incoming emails for users. Unfortunately, these tool is mostly based on traditional rule-based filtering and classification techniques, which may not always capture the nuances and context of emails effectively. However, if you want to enhance the email classification capabilities using more advanced techniques like the tagging chain, you can integrate it as a complementary tool alongside the existing tools provided by the email service provider.

Prototype

Assuming you are a sales manager and your email box is full of different various topics of emails from the internal organization to external communities, from key customer complaints to normal user inquiries, without reading, it’s hard for you to prioritize them based on the normal tool before tagging chain step in.

Step 1— Import the modules

from langchain.chains import LLMChain, create_tagging_chain_pydantic
from langchain.chat_models import ChatOpenAI
from enum import Enum
from pydantic import BaseModel, Field
import os

Step 2 — Define the source text

The fake email has come from Amazon’s engineering director about a complaint about the quality of the product “GuessWhat”. let’s copy it to text_input.

text_input = """
Subject: Urgent Concerns about the Quality of GuessWhat AI Product

Dear Yeyu,

I am Hulk, the Engineering Director at Amazon. I am writing to express my deep disappointment and frustration with the quality of our GuessWhat AI product. We have identified several critical issues that compromise its performance and accuracy.

We value our longstanding partnership, and it is crucial that we address these concerns promptly. I request your immediate attention to this matter and ask that you assign a team of experts to collaborate with our engineering team. Together, we can identify the root causes and work towards a swift resolution.

Please provide a comprehensive plan outlining the steps you will take to improve the quality of GuessWhat. We need to establish a roadmap for enhancement to meet the highest standards of performance and reliability.

I appreciate your partnership and trust that, by working together, we can overcome these challenges and continue delivering exceptional products to our customers. I look forward to your prompt response.

Sincerely,

Hulk
Engineering Director
Amazon
"""

Step 3— Tag definition by Pydantic class

class EmailFeatures(BaseModel):
    topic: str = Field(
        ...,
        enum=[
            "Customer Inquiry",
            "Customer Complaint",
            "Follow-up",
            "Proposal",
            "Demo/Product Presentation",
            "Deal Negotiation",
            "Order Processing",
            "Customer Testimonials/Reviews",
            "Product Information Request",
            "Account Support",
            "Shipping and Delivery",
            "Payment and Billing",
            "Return and Refund",
            "Product Support",
            "Technical Support",
            "Partnership/Cooperation",
            "Event/Conference Invitation",
            "Webinar/Training Registration",
            "Other"
        ],
    )
    sender_weighs: int = Field(
        ...,
        description="The importance of sender based on their title and company, score 1-10, 10 means Senior leadership from high ranking company",
    )
    urgent: int = Field(
        ...,
        description="Score 1-10, 10 is the highest level of urgency",
    )
    sentiment: str = Field(
        ...,
        description="whether the email has a positive, negative, or neutral sentiment.",
    )

I defined four tags which indicate what type of topic in a very detailed definition, how important is the sender, the sender’s urgency, and his/her sentiment, which helps the receiver to manage their email much more efficiently.

Step 4 — Create tagging chain and generate tags

As usual, simply call create_tagging_chain_pydantic() by loading the tag definition and gpt-3.5-turbo-0613 model which has the latest “function calling” update.

os.environ["OPENAI_API_KEY"] = "{Your_API_Key}"

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
tagging_chain = create_tagging_chain_pydantic(EmailFeatures, llm)
res = tagging_chain.run(text_input)
print(res)

The result shows what I am expecting.

Generated output

You are able to integrate the code snippet into any popular email service provider by using their APIs like Gmail API and Azure Outlook API to organize your mailbox for handling email with high efficiency.

3. Resume Screening

As a management leader or HR specialist, resume screening is so necessary but so frustrating work that wastes 80% of your time reading mismatched resumes. Traditional keyword filtering always falls short in accurately identifying qualified candidates. To alleviate these challenges and optimize resume screening, the tagging chain is naturally perfect for this task.

Compared to the last two examples, for resume screening, you may be interested in more tags for completely extracting the essential personal details for evaluation. Here is my recommendation for tags:

  • Education Background — degree, field of study, institution, certifications
  • Professional Experience — job title, industry experience, years, leadership roles
  • Publications and Research — papers, conference presentations
  • Skills and Expertise — tech skills, language proficiency

Since the contents of resumes are so varied, we have to use the Optional type for the schema to tolerate the None value if the resume has not been provided related information in its content. For example:

class PersonalDetails(BaseModel):
    job_title: Optional[str] = Field(
        None,
        description="The most recent job titles in the resume.",
    )
    ....

Moreover, some features may consist of more than one item like technical skills, there is a conlist method that helps. The following code snippet shows how to construct the tech_skills list with an uncertain number of items.

from pydantic import conlist

class PersonalDetails(BaseModel):
    tech_skills: Optional[conlist(str, min_items=1, max_items=7)] = Field(
        None,
    )
    ....

4. Legal Document Analysis

Reading a legal document is always a nightmare for me as well as other non-legal professionals because they always contain complex language, technical terms, and intricate clauses that can be overwhelming. The most common legal document for us is multiple kinds of “agreements” like Service Agreements, Purchase Agreements, or Lease Agreements. Using tagging chain to extract the key valuable information is not only saving our time but also improving our understanding of the document’s contents properly.

Photo by Romain Dancre on Unsplash

We can define the schema that includes the following tags:

  • Service Description: scope, deliverables
  • Payment: fees, schedule, invoicing, penalties
  • Terms: start date, end date, renewal, termination

5. Medical Record Annotation

Reading a medical record is often another headache task for non-medical professionals due to the presence of complex terminology, technical jargon, and intricate details. Although in most cases, your medical record is only reviewed by medical doctors, for some mild illnesses, or routine check-ups for your children or your parents, non-medical professionals like you may need to navigate through medical records to order OTC medicine, prepare food or schedule the next clinic visit. In such cases, having a tagging chain can be incredibly helpful in extracting key valuable information and improving comprehension of the medical record’s contents.

Photo by Spencer Davis on Unsplash

By utilizing a tagging chain, we can define a schema that includes specific tags to extract key valuable information from medical records. Here’s an example of how the tagging chain can be applied to medical record annotation:

  • Medical History — pre-existing conditions, allergies, surgeries, and chronic illnesses.
  • Medications — drug names, dosages, frequency, and duration.
  • Progress — response to treatment, and observations.
  • Follow-ups — new appointments, additional tests

6. Extras

Certainly, there are other usages of tagging chains like Review/Comment Management, Customer Support, Social Media Monitoring, and Survey Chatbots. This wonderful idea of a large language model application is transforming the traditional way we handle various tasks and processes.

Thanks for reading. If you think it’s helpful, please Clap 👏 for this article. Your encouragement and comments mean a lot to me, mentally and financially. 🍔

Before you go:

🎥 If you prefer video tutorials, please subscribe to my YouTube channel where I started to convert most of my articles to visual demonstrations.

✍️ If you have any questions or requests, please leave me responses or find me on Twitter (Now X) and Discord where you can have my active support on development and deployment.

☕️ If you would like to have exclusive resources and technical services, joining the membership of my Ko-fi will be a good choice.

Level Up Coding

Thanks for being a part of our community! Before you go:

🔔 Follow us: Twitter | LinkedIn | Newsletter

🚀👉 Join the Level Up talent collective and find an amazing job

Artificial Intelligence
Chatbots
Gpt 4
Python Programming
Langchain
Recommended from ReadMedium