标记
将文本分类为标签
标记是指用类别对文档进行标记,例如:
- 情感
- 语言
- 风格(正式、非正式等)
- 涉及主题
- 政治倾向
概述
标记有几个组成部分:
function
: 像 extraction 一样,标记使用 functions 来指定模型应如何标记文档schema
: 定义我们希望如何标记文档
快速入门
让我们看看一个非常简单的例子,展示如何在 LangChain 中使用 OpenAI 工具调用进行标记。我们将使用 OpenAI 模型支持的 with_structured_output
方法:
%pip install --upgrade --quiet langchain langchain-openai
# 设置环境变量 OPENAI_API_KEY 或从 .env 文件加载:
# import dotenv
# dotenv.load_dotenv()
接下来,我们将指定一个 Pydantic 模型,包含一些属性及其预期类型。
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
tagging_prompt = ChatPromptTemplate.from_template(
"""
从以下段落中提取所需的信息。
仅提取“Classification”函数中提到的属性。
段落:
{input}
"""
)
class Classification(BaseModel):
sentiment: str = Field(description="文本的情感")
aggressiveness: int = Field(
description="文本的攻击性,按 1 到 10 的比例"
)
language: str = Field(description="文本所写的语言")
# LLM
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125").with_structured_output(
Classification
)
tagging_chain = tagging_prompt | llm
inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
tagging_chain.invoke({"input": inp})
Classification(sentiment='positive', aggressiveness=1, language='Spanish')
如果我们想要 JSON 输出,只需调用 .dict()
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
res = tagging_chain.invoke({"input": inp})
res.dict()
{'sentiment': 'negative', 'aggressiveness': 8, 'language': 'Spanish'}
如我们在示例中所见,它正确地解释了我们的需求。
结果会有所不同,例如,我们可能会得到不同语言的情感(如 'positive'、'enojado' 等)。
我们将在下一节中看到如何控制这些结果。
更精细的控制
仔细的模式定义使我们对模型的输出有了更多的控制。
具体来说,我们可以定义:
- 每个属性的可能值
- 描述以确保模型理解该属性
- 需要返回的属性
让我们重新声明我们的 Pydantic 模型,以控制之前提到的每个方面,使用枚举:
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}
"""
)
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125").with_structured_output(
Classification
)
chain = tagging_prompt | llm
现在,答案将以我们期望的方式受到限制!
inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
chain.invoke({"input": inp})
Classification(sentiment='happy', aggressiveness=1, language='spanish')
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
chain.invoke({"input": inp})
Classification(sentiment='sad', aggressiveness=5, language='spanish')
inp = "Weather is ok here, I can go outside without much more than a coat"
chain.invoke({"input": inp})
Classification(sentiment='neutral', aggressiveness=2, language='english')
LangSmith trace 让我们窥视内部工作:
深入探讨
- 您可以使用 metadata tagger 文档转换器从 LangChain
Document
中提取元数据。 - 这涵盖了与标签链相同的基本功能,只是应用于 LangChain
Document
。