LangChain 实现文本分类任务

LangChain 实现文本分类任务

任务背景

  • 利用 LangChain 的结构化输出能力,将 LLM(此处为 DashScope 兼容的 Qwen 系列模型)封装为可控的“信息抽取器”。
  • 针对输入文本自动输出结构化情感标签、攻击性评分与语言类别,便于后续分析或自动化处理。

任务目标

  1. 连接 Qwen 模型,确保能以结构化格式返回结果。
  2. 基于 Pydantic 定义分类 Schema,限制返回字段的类型和取值。
  3. 构建 Prompt → LLM → 结构化输出的 LangChain Chain。
  4. 演示两个不同 Schema(自由输出与枚举约束)的分类流程。

关键依赖库

  • langchain_core:提供 ChatPromptTemplate,用于构建提示模板。
  • langchain_openai:封装 DashScope 兼容接口,负责与 Qwen 模型通信。
  • pydantic.v1:通过 BaseModelField 描述结构化 Schema,驱动模型输出校验。

解决问题的核心逻辑

1. 初始化 LLM

通过 ChatOpenAI 连接 DashScope 提供的 Qwen 模型,配置 modelapi_keybase_url

model = ChatOpenAI(
    model='qwen-turbo',
    api_key=qwen_api_key,
    base_url='https://dashscope.aliyuncs.com/compatible-mode/v1'
)

2. 定义结构化输出 Schema

使用 Pydantic BaseModel 声明分类任务需要的字段,并通过 Field 提供描述或枚举约束。LangChain 会返回符合 Schema 的对象。

class Classification(BaseModel):
	"""
         定义一个Pydantic的数据模型,未来需要根据该类型,完成文本的分类
    """
	# 文本的情感倾向,预期为字符串类型
	sentiment: str = Field(description="文本的情感")
	# 文本的攻击性,预期为1到10的整数
	aggressiveness: int = Field(
		description="描述文本的攻击性,数字越大表示越攻击性"
	)
	# 文本使用的语言,预期为字符串类型
	language: str = Field(description="文本使用的语言")
class Classification1(BaseModel):
    """
        定义一个Pydantic的数据模型,未来需要根据该类型,完成文本的分类
    """
    # 文本的情感倾向,预期为字符串类型
    # ...没有默认值
    # 枚举限制:这个字段的值只能是列表里的字符
	# description告诉模型这个属性是干什么的
    sentiment: str = Field(..., enum=["happy", "neutral", "sad"], description="文本的情感")

    # 文本的攻击性,预期为1到5的整数
    aggressiveness: int = Field(..., enum=[1, 2, 3, 4, 5], description="描述文本的攻击性,数字越大表示越攻击性")

    # 文本使用的语言,预期为字符串类型
    language: str = Field(..., enum=["spanish", "english", "french", "中文", "italian"], description="文本使用的语言")
 

3. 构建 Prompt 与 Chain

ChatPromptTemplate 负责把输入文本嵌入提示词;model.with_structured_output 让 LLM 根据 Schema 自动解析输出;最后通过 | 组合成 Chain。

tagging_prompt = ChatPromptTemplate.from_template(
    """
    从以下段落中提取所需信息。
    只提取'Classification'类中提到的属性。
    段落:
    {input}
    """
)

chain = tagging_prompt | model.with_structured_output(Classification)
chain1 = tagging_prompt1 | model.with_structured_output(Classification1)

4. 调用 Chain 并解释结果

准备输入文本,执行 chain.invoke({'input': input_text}),即可获得 Pydantic 对象,并直接访问字段或打印。

input_text = "中国人民大学的王教授:我靠,真的是师德败坏..."
result: Classification = chain.invoke({'input': input_text})
result1: Classification = chain.invoke({'input': input_text})
print(result)
# sentiment='愤怒' aggressiveness=8 language='中文'
print(result1)
# sentiment='sad' aggressiveness=5 language='中文'

文本流程图

输入文本

ChatPromptTemplate 注入上下文

ChatOpenAI (Qwen) 生成结构化响应

with_structured_output 校验并转换为 Pydantic 模型

打印或消费分类结果

总结

  • 通过 LangChain 的结构化输出能力,可以让大模型直接返回经过 Schema 校验的数据对象。
  • Pydantic Schema 提供了类型与取值的硬约束,使模型输出更可控。
  • 该示例展示了自由 Schema 与严格枚举 Schema 的对比,为情感分析、内容审核等场景提供了模版。

完整代码

import os

from langchain_core.prompts import ChatPromptTemplate
from langchain_experimental.tabular_synthetic_data.prompts import SYNTHETIC_FEW_SHOT_PREFIX, SYNTHETIC_FEW_SHOT_SUFFIX
from langchain_openai import ChatOpenAI
from pydantic.v1 import BaseModel, Field

# Qwen(通义千问)API Key
qwen_api_key = 'sk-TripleH'  # 请替换为您的 DashScope API Key

# 创建 Qwen LLM 模型
# 可选模型:qwen-turbo, qwen-plus, qwen-max, qwen-max-longcontext
model = ChatOpenAI(
    model='qwen-turbo',  # 可以根据需要改为 qwen-plus 或 qwen-max
    api_key=qwen_api_key,
    base_url='https://dashscope.aliyuncs.com/compatible-mode/v1'
)

class Classification(BaseModel):
	"""
         定义一个Pydantic的数据模型,未来需要根据该类型,完成文本的分类
    """
	# 文本的情感倾向,预期为字符串类型
	sentiment: str = Field(description="文本的情感")
	# 文本的攻击性,预期为1到10的整数
	aggressiveness: int = Field(
		description="描述文本的攻击性,数字越大表示越攻击性"
	)
	# 文本使用的语言,预期为字符串类型
	language: str = Field(description="文本使用的语言")

# 创建一个用于提取信息的提示模板
tagging_prompt = ChatPromptTemplate.from_template(
    """
    从以下段落中提取所需信息。
    只提取'Classification'类中提到的属性。
    段落:
    {input}
    """
)


chain = tagging_prompt | model.with_structured_output(Classification)

input_text = "中国人民大学的王教授:我靠,真的是师德败坏,做出的事情实在让我生气!"
# input_text = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"

result: Classification = chain.invoke({'input' : input_text})

print(result)
# sentiment='愤怒' aggressiveness=8 language='中文'



class Classification1(BaseModel):
    """
        定义一个Pydantic的数据模型,未来需要根据该类型,完成文本的分类
    """
    # 文本的情感倾向,预期为字符串类型
    # ...没有默认值
    # 枚举限制:这个字段的值只能是列表里的字符
	# description告诉模型这个属性是干什么的
    sentiment: str = Field(..., enum=["happy", "neutral", "sad"], description="文本的情感")

    # 文本的攻击性,预期为1到5的整数
    aggressiveness: int = Field(..., enum=[1, 2, 3, 4, 5], description="描述文本的攻击性,数字越大表示越攻击性")

    # 文本使用的语言,预期为字符串类型
    language: str = Field(..., enum=["spanish", "english", "french", "中文", "italian"], description="文本使用的语言")
    
# 创建一个用于提取信息的提示模板
tagging_prompt1 = ChatPromptTemplate.from_template(
    """
    从以下段落中提取所需信息。
    只提取'Classification'类中提到的属性。
    段落:
    {input}
    """
)

chain = tagging_prompt1 | model.with_structured_output(Classification1)

input_text1 = "中国人民大学的王教授:我靠,真的是师德败坏,做出的事情实在让我生气!"
# input_text1 = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"

result1: Classification1 = chain.invoke({'input' : input_text1})

print(result1)
# sentiment='sad' aggressiveness=5 language='中文'
### 如何使用LangChain进行文本分类 在自然语言处理(NLP)领域,文本分类是一项核心任务。通过给文档分配特定的标签(如情感分析、主题识别等),可以更深入地理解文本内容。以下是基于LangChain框架实现文本分类的最佳实践以及示例代码。 #### 使用LangChain中的OpenAI工具进行文本分类 LangChain 提供了一种简单而强大的方式来集成 OpenAI 的模型用于文本分类任务。具体来说,可以通过定义提示模板(Prompt Template)并将输入数据传递到大语言模型中完成分类操作[^1]。 下面展示了一个完整的 Python 实现过程: ```python from langchain.prompts import PromptTemplate from langchain.llms import OpenAI from langchain.chains import LLMChain # 初始化LLM实例 (假设已设置OPENAI_API_KEY环境变量) llm = OpenAI(temperature=0) # 创建一个针对文本分类任务专用提示模板 prompt_template = """You are an AI assistant that classifies text into predefined categories. Classify the following piece of text based on its sentiment as either 'Positive', 'Negative' or 'Neutral'. Text: {input_text} Sentiment Classification:""" classification_prompt = PromptTemplate(template=prompt_template, input_variables=["input_text"]) # 构建链以执行实际推理工作流 sentiment_chain = LLMChain(prompt=classification_prompt, llm=llm) # 测试样本输入及其对应的输出结果 test_input = "I absolutely loved this movie! The acting was superb." result = sentiment_chain.run(input_text=test_input) print(f"Input Text: {test_input}\nClassification Result: {result}") ``` 上述脚本展示了如何利用 LangChain 和 OpenAI 工具快速搭建起一个简易的情感分类器。它接受一段文字作为输入,并返回该段落所属的情绪类别(正面/负面/中立)。此方法同样适用于其他类型的多类别的标注需求。 #### 解决常见问题 当尝试部署此类应用程序时可能会遇到一些挑战,比如性能优化或者错误调试等问题。以下是一些可能有用的技巧: - **提高效率**:如果需要批量处理大量请求,则考虑引入缓存机制减少重复计算开销。 - **增强鲁棒性**:对于复杂场景下的异常情况增加额外校验逻辑确保最终预测质量稳定可靠。 另外值得注意的是,在某些情况下单纯依赖预训练好的大型语言模型未必能够满足业务上的精确度要求;此时则需进一步微调专属领域的定制化版本[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值