将文本分类为标签
标签意味着使用诸如以下类别标记文档:
- 情绪
- 语言
- 风格(正式、非正式等)
- 涵盖的主题
- 政治倾向
概述
标签有几个组成部分
快速入门
让我们看一个非常简单的示例,说明如何在 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 跟踪 使我们能够窥探幕后