将文本分类为标签
标签是指用诸如以下类别的标签对文档进行标记:
- 情绪
- 语言
- 风格(正式、非正式等)
- 涵盖的主题
- 政治倾向
概述
标签包含几个组件
快速入门
让我们看看一个非常简单的示例,说明我们如何在 LangChain 中使用 OpenAI 工具调用进行标签。我们将使用 OpenAI 模型支持的 .withStructuredOutput()
方法
- npm
- yarn
- pnpm
npm i langchain @langchain/openai @langchain/core zod
yarn add langchain @langchain/openai @langchain/core zod
pnpm add langchain @langchain/openai @langchain/core zod
让我们使用Zod 定义一个具有几个属性及其预期类型的架构。
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
const taggingPrompt = ChatPromptTemplate.fromTemplate(
`Extract the desired information from the following passage.
Only extract the properties mentioned in the 'Classification' function.
Passage:
{input}
`
);
const classificationSchema = z.object({
sentiment: z.string().describe("The sentiment of the text"),
aggressiveness: z
.number()
.int()
.min(1)
.max(10)
.describe("How aggressive the text is on a scale from 1 to 10"),
language: z.string().describe("The language the text is written in"),
});
// LLM
const llm = new ChatOpenAI({
temperature: 0,
model: "gpt-3.5-turbo-0125",
});
// Name is optional, but gives the models more clues as to what your schema represents
const llmWihStructuredOutput = llm.withStructuredOutput(classificationSchema, {
name: "extractor",
});
const taggingChain = taggingPrompt.pipe(llmWihStructuredOutput);
const input =
"Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!";
await taggingChain.invoke({ input });
{ sentiment: "positive", aggressiveness: 1, language: "Spanish" }
正如我们在示例中看到的那样,它正确地解释了我们的意图。
结果会有所不同,因此我们可能会得到不同语言的情绪(例如,“积极”、“enojado” 等)。
我们将在下一节中了解如何控制这些结果。
更精细的控制
仔细定义架构使我们能够更好地控制模型的输出。
具体来说,我们可以定义
- 每个属性的可能值
- 描述以确保模型理解该属性
- 要返回的必需属性
让我们使用枚举重新声明 Zod 架构,以控制之前提到的每个方面
import { z } from "zod";
const classificationSchema = z.object({
sentiment: z
.enum(["happy", "neutral", "sad"])
.describe("The sentiment of the text"),
aggressiveness: z
.number()
.int()
.min(1)
.max(5)
.describe(
"describes how aggressive the statement is, the higher the number the more aggressive"
),
language: z
.enum(["spanish", "english", "french", "german", "italian"])
.describe("The language the text is written in"),
});
const taggingPrompt = ChatPromptTemplate.fromTemplate(
`Extract the desired information from the following passage.
Only extract the properties mentioned in the 'Classification' function.
Passage:
{input}
`
);
// LLM
const llm = new ChatOpenAI({
temperature: 0,
model: "gpt-3.5-turbo-0125",
});
const llmWihStructuredOutput = llm.withStructuredOutput(classificationSchema, {
name: "extractor",
});
const chain = taggingPrompt.pipe(llmWihStructuredOutput);
现在答案将以我们期望的方式受到限制!
const input =
"Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!";
await chain.invoke({ input });
{ sentiment: "happy", aggressiveness: 3, language: "spanish" }
const input = "Estoy muy enojado con vos! Te voy a dar tu merecido!";
await chain.invoke({ input });
{ sentiment: "sad", aggressiveness: 5, language: "spanish" }
const input =
"Weather is ok here, I can go outside without much more than a coat";
await chain.invoke({ input });
{ sentiment: "neutral", aggressiveness: 3, language: "english" }
该LangSmith 跟踪 让我们得以窥视幕后