跳到主要内容

如何返回引文

先决条件

本指南假设你熟悉以下内容

我们如何让模型引用其响应中引用的源文档的哪些部分?

为了探索一些提取引文的技巧,让我们先创建一个简单的 RAG 链。首先,我们仅使用 TavilySearchAPIRetriever 从网络检索。

设置

依赖项

在本演练中,我们将使用 OpenAI 聊天模型和嵌入以及 Memory 矢量存储,但这里显示的所有内容都适用于任何 聊天模型LLM嵌入 以及 矢量存储检索器

我们将使用以下包

npm install --save langchain @langchain/community @langchain/openai

我们需要为 Tavily Search 和 OpenAI 设置环境变量

export OPENAI_API_KEY=YOUR_KEY
export TAVILY_API_KEY=YOUR_KEY

LangSmith

你使用 LangChain 构建的许多应用程序将包含多个步骤,并多次调用 LLM。当这些应用程序越来越复杂时,能够检查链或代理内部究竟发生了什么变得至关重要。使用 LangSmith 是最好的方法。

请注意,LangSmith 不是必需的,但它很有帮助。如果你确实想使用 LangSmith,在你在上面的链接中注册后,请确保设置你的环境变量以开始记录跟踪

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=YOUR_KEY

# Reduce tracing latency if you are not in a serverless environment
# export LANGCHAIN_CALLBACKS_BACKGROUND=true

初始设置

import { TavilySearchAPIRetriever } from "@langchain/community/retrievers/tavily_search_api";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});

const retriever = new TavilySearchAPIRetriever({
k: 6,
});

const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You're a helpful AI assistant. Given a user question and some web article snippets, answer the user question. If none of the articles answer the question, just say you don't know.\n\nHere are the web articles:{context}",
],
["human", "{question}"],
]);

现在我们已经有了模型、检索器和提示,让我们将它们全部链接在一起。我们需要添加一些逻辑来将我们检索到的 Document 格式化为可以传递给我们的提示的字符串。我们将使其成为我们的链返回答案和检索到的文档。

import { Document } from "@langchain/core/documents";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { RunnableMap, RunnablePassthrough } from "@langchain/core/runnables";

/**
* Format the documents into a readable string.
*/
const formatDocs = (input: Record<string, any>): string => {
const { docs } = input;
return (
"\n\n" +
docs
.map(
(doc: Document) =>
`Article title: ${doc.metadata.title}\nArticle Snippet: ${doc.pageContent}`
)
.join("\n\n")
);
};
// subchain for generating an answer once we've done retrieval
const answerChain = prompt.pipe(llm).pipe(new StringOutputParser());
const map = RunnableMap.from({
question: new RunnablePassthrough(),
docs: retriever,
});
// complete chain that calls the retriever -> formats docs to string -> runs answer subchain -> returns just the answer and retrieved docs.
const chain = map
.assign({ context: formatDocs })
.assign({ answer: answerChain })
.pick(["answer", "docs"]);

await chain.invoke("How fast are cheetahs?");
{
answer: "Cheetahs are the fastest land animals on Earth. They can reach speeds as high as 75 mph or 120 km/h."... 124 more characters,
docs: [
Document {
pageContent: "Contact Us − +\n" +
"Address\n" +
"Smithsonian's National Zoo & Conservation Biology Institute  3001 Connecticut"... 1343 more characters,
metadata: {
title: "Cheetah | Smithsonian's National Zoo and Conservation Biology Institute",
source: "https://nationalzoo.si.edu/animals/cheetah",
score: 0.96283,
images: null
}
},
Document {
pageContent: "Now, their only hope lies in the hands of human conservationists, working tirelessly to save the che"... 880 more characters,
metadata: {
title: "How Fast Are Cheetahs, and Other Fascinating Facts About the World's ...",
source: "https://www.discovermagazine.com/planet-earth/how-fast-are-cheetahs-and-other-fascinating-facts-abou"... 21 more characters,
score: 0.96052,
images: null
}
},
Document {
pageContent: "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely r"... 1048 more characters,
metadata: {
title: "Cheetah | Description, Speed, Habitat, Diet, Cubs, & Facts",
source: "https://www.britannica.com/animal/cheetah-mammal",
score: 0.93137,
images: null
}
},
Document {
pageContent: "The science of cheetah speed\n" +
"The cheetah (Acinonyx jubatus) is the fastest land animal on Earth, cap"... 738 more characters,
metadata: {
title: "How Fast Can a Cheetah Run? - ThoughtCo",
source: "https://www.thoughtco.com/how-fast-can-a-cheetah-run-4587031",
score: 0.91385,
images: null
}
},
Document {
pageContent: "One of two videos from National Geographic's award-winning multimedia coverage of cheetahs in the ma"... 60 more characters,
metadata: {
title: "The Science of a Cheetah's Speed | National Geographic",
source: "https://www.youtube.com/watch?v=icFMTB0Pi0g",
score: 0.90358,
images: null
}
},
Document {
pageContent: "If a lion comes along, the cheetah will abandon its catch -- it can't fight off a lion, and chances "... 911 more characters,
metadata: {
title: "What makes a cheetah run so fast? | HowStuffWorks",
source: "https://animals.howstuffworks.com/mammals/cheetah-speed.htm",
score: 0.87824,
images: null
}
}
]
}

查看展示内部的 LangSmith 跟踪这里

工具调用

引用文档

让我们尝试使用 工具调用 使模型指定它实际引用了哪些提供的文档来回答。LangChain 有一些实用程序可以将对象或 Zod 对象转换为 OpenAI 等提供商预期的 JSONSchema 格式。我们将使用 .withStructuredOutput() 方法让模型输出与我们所需模式匹配的数据

import { z } from "zod";

const llmWithTool1 = llm.withStructuredOutput(
z
.object({
answer: z
.string()
.describe(
"The answer to the user question, which is based only on the given sources."
),
citations: z
.array(z.number())
.describe(
"The integer IDs of the SPECIFIC sources which justify the answer."
),
})
.describe("A cited source from the given text"),
{
name: "cited_answers",
}
);

const exampleQ = `What is Brian's height?

Source: 1
Information: Suzy is 6'2"

Source: 2
Information: Jeremiah is blonde

Source: 3
Information: Brian is 3 inches shorter than Suzy`;

await llmWithTool1.invoke(exampleQ);
{
answer: `Brian is 6'2" - 3 inches = 5'11" tall.`,
citations: [ 1, 3 ]
}

查看展示内部的 LangSmith 跟踪这里

现在我们准备将我们的链组合在一起

import { Document } from "@langchain/core/documents";

const formatDocsWithId = (docs: Array<Document>): string => {
return (
"\n\n" +
docs
.map(
(doc: Document, idx: number) =>
`Source ID: ${idx}\nArticle title: ${doc.metadata.title}\nArticle Snippet: ${doc.pageContent}`
)
.join("\n\n")
);
};
// subchain for generating an answer once we've done retrieval
const answerChain1 = prompt.pipe(llmWithTool1);
const map1 = RunnableMap.from({
question: new RunnablePassthrough(),
docs: retriever,
});
// complete chain that calls the retriever -> formats docs to string -> runs answer subchain -> returns just the answer and retrieved docs.
const chain1 = map1
.assign({
context: (input: { docs: Array<Document> }) => formatDocsWithId(input.docs),
})
.assign({ cited_answer: answerChain1 })
.pick(["cited_answer", "docs"]);

await chain1.invoke("How fast are cheetahs?");
{
cited_answer: {
answer: "Cheetahs can reach speeds as high as 75 mph or 120 km/h.",
citations: [ 1, 2, 5 ]
},
docs: [
Document {
pageContent: "One of two videos from National Geographic's award-winning multimedia coverage of cheetahs in the ma"... 60 more characters,
metadata: {
title: "The Science of a Cheetah's Speed | National Geographic",
source: "https://www.youtube.com/watch?v=icFMTB0Pi0g",
score: 0.97858,
images: null
}
},
Document {
pageContent: "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely r"... 1048 more characters,
metadata: {
title: "Cheetah | Description, Speed, Habitat, Diet, Cubs, & Facts",
source: "https://www.britannica.com/animal/cheetah-mammal",
score: 0.97213,
images: null
}
},
Document {
pageContent: "The science of cheetah speed\n" +
"The cheetah (Acinonyx jubatus) is the fastest land animal on Earth, cap"... 738 more characters,
metadata: {
title: "How Fast Can a Cheetah Run? - ThoughtCo",
source: "https://www.thoughtco.com/how-fast-can-a-cheetah-run-4587031",
score: 0.95759,
images: null
}
},
Document {
pageContent: "Contact Us − +\n" +
"Address\n" +
"Smithsonian's National Zoo & Conservation Biology Institute  3001 Connecticut"... 1343 more characters,
metadata: {
title: "Cheetah | Smithsonian's National Zoo and Conservation Biology Institute",
source: "https://nationalzoo.si.edu/animals/cheetah",
score: 0.92422,
images: null
}
},
Document {
pageContent: "Now, their only hope lies in the hands of human conservationists, working tirelessly to save the che"... 880 more characters,
metadata: {
title: "How Fast Are Cheetahs, and Other Fascinating Facts About the World's ...",
source: "https://www.discovermagazine.com/planet-earth/how-fast-are-cheetahs-and-other-fascinating-facts-abou"... 21 more characters,
score: 0.91867,
images: null
}
},
Document {
pageContent: "The speeds attained by the cheetah may be only slightly greater than those achieved by the pronghorn"... 2527 more characters,
metadata: {
title: "Cheetah - Wikipedia",
source: "https://en.wikipedia.org/wiki/Cheetah",
score: 0.81617,
images: null
}
}
]
}

查看一个 LangSmith 跟踪这里,它展示了内部机制。

引用片段

如果我们想要引用实际的文本跨度呢?我们可以尝试让我们的模型也返回这些。

注意:请注意,如果我们将文档分解成许多包含一两句话的短文档,而不是几个长文档,那么引用文档就大致等同于引用片段,并且可能对模型来说更容易,因为模型只需要返回每个片段的标识符,而不是实际的文本。我们建议尝试这两种方法并进行评估。

import { Document } from "@langchain/core/documents";

const citationSchema = z.object({
sourceId: z
.number()
.describe(
"The integer ID of a SPECIFIC source which justifies the answer."
),
quote: z
.string()
.describe(
"The VERBATIM quote from the specified source that justifies the answer."
),
});

const llmWithTool2 = llm.withStructuredOutput(
z.object({
answer: z
.string()
.describe(
"The answer to the user question, which is based only on the given sources."
),
citations: z
.array(citationSchema)
.describe("Citations from the given sources that justify the answer."),
}),
{
name: "quoted_answer",
}
);

const answerChain2 = prompt.pipe(llmWithTool2);
const map2 = RunnableMap.from({
question: new RunnablePassthrough(),
docs: retriever,
});
// complete chain that calls the retriever -> formats docs to string -> runs answer subchain -> returns just the answer and retrieved docs.
const chain2 = map2
.assign({
context: (input: { docs: Array<Document> }) => formatDocsWithId(input.docs),
})
.assign({ quoted_answer: answerChain2 })
.pick(["quoted_answer", "docs"]);

await chain2.invoke("How fast are cheetahs?");
{
quoted_answer: {
answer: "Cheetahs can reach speeds of up to 120kph or 75mph, making them the world’s fastest land animals.",
citations: [
{
sourceId: 5,
quote: "Cheetahs can reach speeds of up to 120kph or 75mph, making them the world’s fastest land animals."
},
{
sourceId: 1,
quote: "The cheetah (Acinonyx jubatus) is the fastest land animal on Earth, capable of reaching speeds as hi"... 25 more characters
},
{
sourceId: 3,
quote: "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely r"... 72 more characters
}
]
},
docs: [
Document {
pageContent: "Contact Us − +\n" +
"Address\n" +
"Smithsonian's National Zoo & Conservation Biology Institute  3001 Connecticut"... 1343 more characters,
metadata: {
title: "Cheetah | Smithsonian's National Zoo and Conservation Biology Institute",
source: "https://nationalzoo.si.edu/animals/cheetah",
score: 0.95973,
images: null
}
},
Document {
pageContent: "The science of cheetah speed\n" +
"The cheetah (Acinonyx jubatus) is the fastest land animal on Earth, cap"... 738 more characters,
metadata: {
title: "How Fast Can a Cheetah Run? - ThoughtCo",
source: "https://www.thoughtco.com/how-fast-can-a-cheetah-run-4587031",
score: 0.92749,
images: null
}
},
Document {
pageContent: "Now, their only hope lies in the hands of human conservationists, working tirelessly to save the che"... 880 more characters,
metadata: {
title: "How Fast Are Cheetahs, and Other Fascinating Facts About the World's ...",
source: "https://www.discovermagazine.com/planet-earth/how-fast-are-cheetahs-and-other-fascinating-facts-abou"... 21 more characters,
score: 0.92417,
images: null
}
},
Document {
pageContent: "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely r"... 1048 more characters,
metadata: {
title: "Cheetah | Description, Speed, Habitat, Diet, Cubs, & Facts",
source: "https://www.britannica.com/animal/cheetah-mammal",
score: 0.92341,
images: null
}
},
Document {
pageContent: "One of two videos from National Geographic's award-winning multimedia coverage of cheetahs in the ma"... 60 more characters,
metadata: {
title: "The Science of a Cheetah's Speed | National Geographic",
source: "https://www.youtube.com/watch?v=icFMTB0Pi0g",
score: 0.90025,
images: null
}
},
Document {
pageContent: "In fact, they are more closely related to kangaroos…\n" +
"Read more\n" +
"Animals on the Galapagos Islands: A G"... 987 more characters,
metadata: {
title: "How fast can cheetahs run, and what enables their incredible speed?",
source: "https://wildlifefaq.com/cheetah-speed/",
score: 0.87121,
images: null
}
}
]
}

您可以查看一个 LangSmith 跟踪这里,它展示了内部机制。

直接提示

并非所有模型都支持工具调用。我们可以通过直接提示来实现类似的结果。让我们看看使用一个更旧的 Anthropic 聊天模型,它在处理 XML 方面特别熟练,会是什么样子。

设置

安装 LangChain Anthropic 集成包

npm install @langchain/anthropic

将您的 Anthropic API 密钥添加到您的环境中

export ANTHROPIC_API_KEY=YOUR_KEY
import { ChatAnthropic } from "@langchain/anthropic";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { XMLOutputParser } from "@langchain/core/output_parsers";
import { Document } from "@langchain/core/documents";
import {
RunnableLambda,
RunnablePassthrough,
RunnableMap,
} from "@langchain/core/runnables";

const anthropic = new ChatAnthropic({
model: "claude-instant-1.2",
temperature: 0,
});
const system = `You're a helpful AI assistant. Given a user question and some web article snippets,
answer the user question and provide citations. If none of the articles answer the question, just say you don't know.

Remember, you must return both an answer and citations. A citation consists of a VERBATIM quote that
justifies the answer and the ID of the quote article. Return a citation for every quote across all articles
that justify the answer. Use the following format for your final output:

<cited_answer>
<answer></answer>
<citations>
<citation><source_id></source_id><quote></quote></citation>
<citation><source_id></source_id><quote></quote></citation>
...
</citations>
</cited_answer>

Here are the web articles:{context}`;

const anthropicPrompt = ChatPromptTemplate.fromMessages([
["system", system],
["human", "{question}"],
]);

const formatDocsToXML = (docs: Array<Document>): string => {
const formatted: Array<string> = [];
docs.forEach((doc, idx) => {
const docStr = `<source id="${idx}">
<title>${doc.metadata.title}</title>
<article_snippet>${doc.pageContent}</article_snippet>
</source>`;
formatted.push(docStr);
});
return `\n\n<sources>${formatted.join("\n")}</sources>`;
};

const format3 = new RunnableLambda({
func: (input: { docs: Array<Document> }) => formatDocsToXML(input.docs),
});
const answerChain = anthropicPrompt
.pipe(anthropic)
.pipe(new XMLOutputParser())
.pipe(
new RunnableLambda({
func: (input: { cited_answer: any }) => input.cited_answer,
})
);
const map3 = RunnableMap.from({
question: new RunnablePassthrough(),
docs: retriever,
});
const chain3 = map3
.assign({ context: format3 })
.assign({ cited_answer: answerChain })
.pick(["cited_answer", "docs"]);

const res = await chain3.invoke("How fast are cheetahs?");

console.log(JSON.stringify(res, null, 2));
{
"cited_answer": [
{
"answer": "Cheetahs can reach top speeds of around 75 mph, but can only maintain bursts of speed for short distances before tiring."
},
{
"citations": [
{
"citation": [
{
"source_id": "1"
},
{
"quote": "Scientists calculate a cheetah's top speed is 75 mph, but the fastest recorded speed is somewhat slower."
}
]
},
{
"citation": [
{
"source_id": "3"
},
{
"quote": "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely reach velocities of 80–100 km (50–62 miles) per hour while pursuing prey."
}
]
}
]
}
],
"docs": [
{
"pageContent": "One of two videos from National Geographic's award-winning multimedia coverage of cheetahs in the magazine's November 2012 iPad edition. See the other: http:...",
"metadata": {
"title": "The Science of a Cheetah's Speed | National Geographic",
"source": "https://www.youtube.com/watch?v=icFMTB0Pi0g",
"score": 0.96603,
"images": null
}
},
{
"pageContent": "The science of cheetah speed\nThe cheetah (Acinonyx jubatus) is the fastest land animal on Earth, capable of reaching speeds as high as 75 mph or 120 km/h. Cheetahs are predators that sneak up on their prey and sprint a short distance to chase and attack.\n Key Takeaways: How Fast Can a Cheetah Run?\nFastest Cheetah on Earth\nScientists calculate a cheetah's top speed is 75 mph, but the fastest recorded speed is somewhat slower. The top 10 fastest animals are:\nThe pronghorn, an American animal resembling an antelope, is the fastest land animal in the Western Hemisphere. While a cheetah's top speed ranges from 65 to 75 mph (104 to 120 km/h), its average speed is only 40 mph (64 km/hr), punctuated by short bursts at its top speed. Basically, if a predator threatens to take a cheetah's kill or attack its young, a cheetah has to run.\n",
"metadata": {
"title": "How Fast Can a Cheetah Run? - ThoughtCo",
"source": "https://www.thoughtco.com/how-fast-can-a-cheetah-run-4587031",
"score": 0.96212,
"images": null
}
},
{
"pageContent": "Now, their only hope lies in the hands of human conservationists, working tirelessly to save the cheetahs, the leopards and all the other wildlife of the scattered savannas and other habitats of Africa and Asia.\n Their tough paw pads and grippy claws are made to grab at the ground, and their large nasal passages and lungs facilitate the flow of oxygen and allow their rapid intake of air as they reach their top speeds.\n And though the two cats share a similar coloration, a cheetah's spots are circular while a leopard's spots are rose-shaped \"rosettes,\" with the centers of their spots showing off the tan color of their coats.\n Also classified as \"vulnerable\" are two of the cheetah's foremost foes, the lion and the leopard, the latter of which is commonly confused for the cheetah thanks to its own flecked fur.\n The cats are also consumers of the smallest of the bigger, bulkier antelopes, such as sables and kudus, and are known to gnaw on the occasional rabbit or bird.\n",
"metadata": {
"title": "How Fast Are Cheetahs, and Other Fascinating Facts About the World's ...",
"source": "https://www.discovermagazine.com/planet-earth/how-fast-are-cheetahs-and-other-fascinating-facts-about-the-worlds-quickest",
"score": 0.95688,
"images": null
}
},
{
"pageContent": "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely reach velocities of 80–100 km (50–62 miles) per hour while pursuing prey.\ncheetah,\n(Acinonyx jubatus),\none of the world’s most-recognizable cats, known especially for its speed. Their fur is dark and includes a thick yellowish gray mane along the back, a trait that presumably offers better camouflage and increased protection from high temperatures during the day and low temperatures at night during the first few months of life. Cheetahs eat a variety of small animals, including game birds, rabbits, small antelopes (including the springbok, impala, and gazelle), young warthogs, and larger antelopes (such as the kudu, hartebeest, oryx, and roan).\n A cheetah eats a variety of small animals, including game birds, rabbits, small antelopes (including the springbok, impala, and gazelle), young warthogs, and larger antelopes (such as the kudu, hartebeest, oryx, and roan). Their faces are distinguished by prominent black lines that curve from the inner corner of each eye to the outer corners of the mouth, like a well-worn trail of inky tears.",
"metadata": {
"title": "Cheetah | Description, Speed, Habitat, Diet, Cubs, & Facts",
"source": "https://www.britannica.com/animal/cheetah-mammal",
"score": 0.95589,
"images": null
}
},
{
"pageContent": "Contact Us − +\nAddress\nSmithsonian's National Zoo & Conservation Biology Institute  3001 Connecticut Ave., NW  Washington, DC 20008\nAbout the Zoo\n−\n+\nCareers\n−\n+\nNews & Media\n−\n+\nFooter Donate\n−\n+\nShop\n−\n+\nFollow us on social media\nSign Up for Emails\nFooter - SI logo, privacy, terms Conservation Efforts\nHistorically, cheetahs ranged widely throughout Africa and Asia, from the Cape of Good Hope to the Mediterranean, throughout the Arabian Peninsula and the Middle East, from Israel, India and Pakistan north to the northern shores of the Caspian and Aral Seas, and west through Uzbekistan, Turkmenistan, Afghanistan, and Pakistan into central India. Header Links\nToday's hours: 8 a.m. to 4 p.m. (last entry 3 p.m.)\nMega menu\nAnimals Global Nav Links\nElephant Cam\nSee the Smithsonian's National Zoo's Asian elephants — Spike, Bozie, Kamala, Swarna and Maharani — both inside the Elephant Community Center and outside in their yards.\n Conservation Global Nav Links\nAbout the Smithsonian Conservation Biology Institute\nCheetah\nAcinonyx jubatus\nBuilt for speed, the cheetah can accelerate from zero to 45 in just 2.5 seconds and reach top speeds of 60 to 70 mph, making it the fastest land mammal! Fun Facts\nConservation Status\nCheetah News\nTaxonomic Information\nAnimal News\nNZCBI staff in Front Royal, Virginia, are mourning the loss of Walnut, a white-naped crane who became an internet sensation for choosing one of her keepers as her mate.\n",
"metadata": {
"title": "Cheetah | Smithsonian's National Zoo and Conservation Biology Institute",
"source": "https://nationalzoo.si.edu/animals/cheetah",
"score": 0.94744,
"images": null
}
},
{
"pageContent": "The speeds attained by the cheetah may be only slightly greater than those achieved by the pronghorn at 88.5 km/h (55.0 mph)[96] and the springbok at 88 km/h (55 mph),[97] but the cheetah additionally has an exceptional acceleration.[98]\nOne stride of a galloping cheetah measures 4 to 7 m (13 to 23 ft); the stride length and the number of jumps increases with speed.[60] During more than half the duration of the sprint, the cheetah has all four limbs in the air, increasing the stride length.[99] Running cheetahs can retain up to 90% of the heat generated during the chase. In December 2016 the results of an extensive survey detailing the distribution and demography of cheetahs throughout the range were published; the researchers recommended listing the cheetah as Endangered on the IUCN Red List.[25]\nThe cheetah was reintroduced in Malawi in 2017.[160]\nIn Asia\nIn 2001, the Iranian government collaborated with the CCF, the IUCN, Panthera Corporation, UNDP and the Wildlife Conservation Society on the Conservation of Asiatic Cheetah Project (CACP) to protect the natural habitat of the Asiatic cheetah and its prey.[161][162] Individuals on the periphery of the prey herd are common targets; vigilant prey which would react quickly on seeing the cheetah are not preferred.[47][60][122]\nCheetahs are one of the most iconic pursuit predators, hunting primarily throughout the day, sometimes with peaks at dawn and dusk; they tend to avoid larger predators like the primarily nocturnal lion.[66] Cheetahs in the Sahara and Maasai Mara in Kenya hunt after sunset to escape the high temperatures of the day.[123] Cheetahs use their vision to hunt instead of their sense of smell; they keep a lookout for prey from resting sites or low branches. This significantly sharpens the vision and enables the cheetah to swiftly locate prey against the horizon.[61][86] The cheetah is unable to roar due to the presence of a sharp-edged vocal fold within the larynx.[2][87]\nSpeed and acceleration\nThe cheetah is the world's fastest land animal.[88][89][90][91][92] Estimates of the maximum speed attained range from 80 to 128 km/h (50 to 80 mph).[60][63] A commonly quoted value is 112 km/h (70 mph), recorded in 1957, but this measurement is disputed.[93] The mouth can not be opened as widely as in other cats given the shorter length of muscles between the jaw and the skull.[60][65] A study suggested that the limited retraction of the cheetah's claws may result from the earlier truncation of the development of the middle phalanx bone in cheetahs.[77]\nThe cheetah has a total of 30 teeth; the dental formula is 3.1.3.13.1.2.1.",
"metadata": {
"title": "Cheetah - Wikipedia",
"source": "https://en.wikipedia.org/wiki/Cheetah",
"score": 0.81312,
"images": null
}
}
]
}

查看这个 LangSmith 跟踪这里,了解有关内部机制的更多信息。

检索后处理

另一种方法是对检索到的文档进行后处理,以压缩内容,这样源内容本身就足够简洁,我们不需要模型引用特定来源或跨度。例如,我们可以将每个文档分解成一两句话,嵌入它们并只保留最相关的句子。LangChain 有些内置组件可以实现这一点。在这里,我们将使用一个RecursiveCharacterTextSplitter,它通过在分隔符子字符串上分割来创建指定大小的块,以及一个EmbeddingsFilter,它只保留具有最相关嵌入的文本。

import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { EmbeddingsFilter } from "langchain/retrievers/document_compressors/embeddings_filter";
import { OpenAIEmbeddings } from "@langchain/openai";
import { DocumentInterface } from "@langchain/core/documents";
import { RunnableMap, RunnablePassthrough } from "@langchain/core/runnables";

const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 400,
chunkOverlap: 0,
separators: ["\n\n", "\n", ".", " "],
keepSeparator: false,
});

const compressor = new EmbeddingsFilter({
embeddings: new OpenAIEmbeddings(),
k: 10,
});

const splitAndFilter = async (input): Promise<Array<DocumentInterface>> => {
const { docs, question } = input;
const splitDocs = await splitter.splitDocuments(docs);
const statefulDocs = await compressor.compressDocuments(splitDocs, question);
return statefulDocs;
};

const retrieveMap = RunnableMap.from({
question: new RunnablePassthrough(),
docs: retriever,
});

const retriever = retrieveMap.pipe(splitAndFilter);
const docs = await retriever.invoke("How fast are cheetahs?");
for (const doc of docs) {
console.log(doc.pageContent, "\n\n");
}
The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely reach velocities of 80–100 km (50–62 miles) per hour while pursuing prey.
cheetah,
(Acinonyx jubatus),


The science of cheetah speed
The cheetah (Acinonyx jubatus) is the fastest land animal on Earth, capable of reaching speeds as high as 75 mph or 120 km/h. Cheetahs are predators that sneak up on their prey and sprint a short distance to chase and attack.
Key Takeaways: How Fast Can a Cheetah Run?
Fastest Cheetah on Earth


Built for speed, the cheetah can accelerate from zero to 45 in just 2.5 seconds and reach top speeds of 60 to 70 mph, making it the fastest land mammal! Fun Facts
Conservation Status
Cheetah News
Taxonomic Information
Animal News
NZCBI staff in Front Royal, Virginia, are mourning the loss of Walnut, a white-naped crane who became an internet sensation for choosing one of her keepers as her mate.


The speeds attained by the cheetah may be only slightly greater than those achieved by the pronghorn at 88.5 km/h (55.0 mph)[96] and the springbok at 88 km/h (55 mph),[97] but the cheetah additionally has an exceptional acceleration.[98]


The cheetah is the world's fastest land animal.[88][89][90][91][92] Estimates of the maximum speed attained range from 80 to 128 km/h (50 to 80 mph).[60][63] A commonly quoted value is 112 km/h (70 mph), recorded in 1957, but this measurement is disputed.[93] The mouth can not be opened as widely as in other cats given the shorter length of muscles between the jaw and the skull


Scientists calculate a cheetah's top speed is 75 mph, but the fastest recorded speed is somewhat slower. The top 10 fastest animals are:


One stride of a galloping cheetah measures 4 to 7 m (13 to 23 ft); the stride length and the number of jumps increases with speed.[60] During more than half the duration of the sprint, the cheetah has all four limbs in the air, increasing the stride length.[99] Running cheetahs can retain up to 90% of the heat generated during the chase


The pronghorn, an American animal resembling an antelope, is the fastest land animal in the Western Hemisphere. While a cheetah's top speed ranges from 65 to 75 mph (104 to 120 km/h), its average speed is only 40 mph (64 km/hr), punctuated by short bursts at its top speed. Basically, if a predator threatens to take a cheetah's kill or attack its young, a cheetah has to run.


A cheetah eats a variety of small animals, including game birds, rabbits, small antelopes (including the springbok, impala, and gazelle), young warthogs, and larger antelopes (such as the kudu, hartebeest, oryx, and roan). Their faces are distinguished by prominent black lines that curve from the inner corner of each eye to the outer corners of the mouth, like a well-worn trail of inky tears.


Cheetahs are one of the most iconic pursuit predators, hunting primarily throughout the day, sometimes with peaks at dawn and dusk; they tend to avoid larger predators like the primarily nocturnal lion.[66] Cheetahs in the Sahara and Maasai Mara in Kenya hunt after sunset to escape the high temperatures of the day

查看 LangSmith 跟踪这里,了解内部机制。

const chain4 = retrieveMap
.assign({ context: formatDocs })
.assign({ answer: answerChain })
.pick(["answer", "docs"]);

// Note the documents have an article "summary" in the metadata that is now much longer than the
// actual document page content. This summary isn't actually passed to the model.
const res = await chain4.invoke("How fast are cheetahs?");

console.log(JSON.stringify(res, null, 2));
{
"answer": [
{
"answer": "\nCheetahs are the fastest land animals. They can reach top speeds between 75-81 mph (120-130 km/h). \n"
},
{
"citations": [
{
"citation": [
{
"source_id": "Article title: How Fast Can a Cheetah Run? - ThoughtCo"
},
{
"quote": "The science of cheetah speed\nThe cheetah (Acinonyx jubatus) is the fastest land animal on Earth, capable of reaching speeds as high as 75 mph or 120 km/h."
}
]
},
{
"citation": [
{
"source_id": "Article title: Cheetah - Wikipedia"
},
{
"quote": "Scientists calculate a cheetah's top speed is 75 mph, but the fastest recorded speed is somewhat slower."
}
]
}
]
}
],
"docs": [
{
"pageContent": "The science of cheetah speed\nThe cheetah (Acinonyx jubatus) is the fastest land animal on Earth, capable of reaching speeds as high as 75 mph or 120 km/h. Cheetahs are predators that sneak up on their prey and sprint a short distance to chase and attack.\n Key Takeaways: How Fast Can a Cheetah Run?\nFastest Cheetah on Earth\nScientists calculate a cheetah's top speed is 75 mph, but the fastest recorded speed is somewhat slower. The top 10 fastest animals are:\nThe pronghorn, an American animal resembling an antelope, is the fastest land animal in the Western Hemisphere. While a cheetah's top speed ranges from 65 to 75 mph (104 to 120 km/h), its average speed is only 40 mph (64 km/hr), punctuated by short bursts at its top speed. Basically, if a predator threatens to take a cheetah's kill or attack its young, a cheetah has to run.\n",
"metadata": {
"title": "How Fast Can a Cheetah Run? - ThoughtCo",
"source": "https://www.thoughtco.com/how-fast-can-a-cheetah-run-4587031",
"score": 0.96949,
"images": null
}
},
{
"pageContent": "The speeds attained by the cheetah may be only slightly greater than those achieved by the pronghorn at 88.5 km/h (55.0 mph)[96] and the springbok at 88 km/h (55 mph),[97] but the cheetah additionally has an exceptional acceleration.[98]\nOne stride of a galloping cheetah measures 4 to 7 m (13 to 23 ft); the stride length and the number of jumps increases with speed.[60] During more than half the duration of the sprint, the cheetah has all four limbs in the air, increasing the stride length.[99] Running cheetahs can retain up to 90% of the heat generated during the chase. In December 2016 the results of an extensive survey detailing the distribution and demography of cheetahs throughout the range were published; the researchers recommended listing the cheetah as Endangered on the IUCN Red List.[25]\nThe cheetah was reintroduced in Malawi in 2017.[160]\nIn Asia\nIn 2001, the Iranian government collaborated with the CCF, the IUCN, Panthera Corporation, UNDP and the Wildlife Conservation Society on the Conservation of Asiatic Cheetah Project (CACP) to protect the natural habitat of the Asiatic cheetah and its prey.[161][162] Individuals on the periphery of the prey herd are common targets; vigilant prey which would react quickly on seeing the cheetah are not preferred.[47][60][122]\nCheetahs are one of the most iconic pursuit predators, hunting primarily throughout the day, sometimes with peaks at dawn and dusk; they tend to avoid larger predators like the primarily nocturnal lion.[66] Cheetahs in the Sahara and Maasai Mara in Kenya hunt after sunset to escape the high temperatures of the day.[123] Cheetahs use their vision to hunt instead of their sense of smell; they keep a lookout for prey from resting sites or low branches. This significantly sharpens the vision and enables the cheetah to swiftly locate prey against the horizon.[61][86] The cheetah is unable to roar due to the presence of a sharp-edged vocal fold within the larynx.[2][87]\nSpeed and acceleration\nThe cheetah is the world's fastest land animal.[88][89][90][91][92] Estimates of the maximum speed attained range from 80 to 128 km/h (50 to 80 mph).[60][63] A commonly quoted value is 112 km/h (70 mph), recorded in 1957, but this measurement is disputed.[93] The mouth can not be opened as widely as in other cats given the shorter length of muscles between the jaw and the skull.[60][65] A study suggested that the limited retraction of the cheetah's claws may result from the earlier truncation of the development of the middle phalanx bone in cheetahs.[77]\nThe cheetah has a total of 30 teeth; the dental formula is 3.1.3.13.1.2.1.",
"metadata": {
"title": "Cheetah - Wikipedia",
"source": "https://en.wikipedia.org/wiki/Cheetah",
"score": 0.96423,
"images": null
}
},
{
"pageContent": "One of two videos from National Geographic's award-winning multimedia coverage of cheetahs in the magazine's November 2012 iPad edition. See the other: http:...",
"metadata": {
"title": "The Science of a Cheetah's Speed | National Geographic",
"source": "https://www.youtube.com/watch?v=icFMTB0Pi0g",
"score": 0.96071,
"images": null
}
},
{
"pageContent": "Contact Us − +\nAddress\nSmithsonian's National Zoo & Conservation Biology Institute  3001 Connecticut Ave., NW  Washington, DC 20008\nAbout the Zoo\n−\n+\nCareers\n−\n+\nNews & Media\n−\n+\nFooter Donate\n−\n+\nShop\n−\n+\nFollow us on social media\nSign Up for Emails\nFooter - SI logo, privacy, terms Conservation Efforts\nHistorically, cheetahs ranged widely throughout Africa and Asia, from the Cape of Good Hope to the Mediterranean, throughout the Arabian Peninsula and the Middle East, from Israel, India and Pakistan north to the northern shores of the Caspian and Aral Seas, and west through Uzbekistan, Turkmenistan, Afghanistan, and Pakistan into central India. Header Links\nToday's hours: 8 a.m. to 4 p.m. (last entry 3 p.m.)\nMega menu\nAnimals Global Nav Links\nElephant Cam\nSee the Smithsonian's National Zoo's Asian elephants — Spike, Bozie, Kamala, Swarna and Maharani — both inside the Elephant Community Center and outside in their yards.\n Conservation Global Nav Links\nAbout the Smithsonian Conservation Biology Institute\nCheetah\nAcinonyx jubatus\nBuilt for speed, the cheetah can accelerate from zero to 45 in just 2.5 seconds and reach top speeds of 60 to 70 mph, making it the fastest land mammal! Fun Facts\nConservation Status\nCheetah News\nTaxonomic Information\nAnimal News\nNZCBI staff in Front Royal, Virginia, are mourning the loss of Walnut, a white-naped crane who became an internet sensation for choosing one of her keepers as her mate.\n",
"metadata": {
"title": "Cheetah | Smithsonian's National Zoo and Conservation Biology Institute",
"source": "https://nationalzoo.si.edu/animals/cheetah",
"score": 0.91577,
"images": null
}
},
{
"pageContent": "The maximum speed cheetahs have been measured at is 114 km (71 miles) per hour, and they routinely reach velocities of 80–100 km (50–62 miles) per hour while pursuing prey.\ncheetah,\n(Acinonyx jubatus),\none of the world’s most-recognizable cats, known especially for its speed. Their fur is dark and includes a thick yellowish gray mane along the back, a trait that presumably offers better camouflage and increased protection from high temperatures during the day and low temperatures at night during the first few months of life. Cheetahs eat a variety of small animals, including game birds, rabbits, small antelopes (including the springbok, impala, and gazelle), young warthogs, and larger antelopes (such as the kudu, hartebeest, oryx, and roan).\n A cheetah eats a variety of small animals, including game birds, rabbits, small antelopes (including the springbok, impala, and gazelle), young warthogs, and larger antelopes (such as the kudu, hartebeest, oryx, and roan). Their faces are distinguished by prominent black lines that curve from the inner corner of each eye to the outer corners of the mouth, like a well-worn trail of inky tears.",
"metadata": {
"title": "Cheetah | Description, Speed, Habitat, Diet, Cubs, & Facts",
"source": "https://www.britannica.com/animal/cheetah-mammal",
"score": 0.91163,
"images": null
}
},
{
"pageContent": "If a lion comes along, the cheetah will abandon its catch -- it can't fight off a lion, and chances are, the cheetah will lose its life along with its prey if it doesn't get out of there fast enough.\n Advertisement\nLots More Information\nMore Great Links\nSources\nPlease copy/paste the following text to properly cite this HowStuffWorks.com article:\nAdvertisement\nAdvertisement\nAdvertisement\nAdvertisement\nAdvertisement If confronted, a roughly 125-pound cheetah will always run rather than fight -- it's too weak, light and thin to have any chance against something like a lion, which can be twice as long as a cheetah and weigh more than 400 pounds (181.4 kg) Cheetah moms spend a lot of time teaching their cubs to chase, sometimes dragging live animals back to the den so the cubs can practice the chase-and-catch process.\n It's more like a bound at that speed, completing up to three strides per second, with only one foot on the ground at any time and several stages when feet don't touch the ground at all.",
"metadata": {
"title": "What makes a cheetah run so fast? | HowStuffWorks",
"source": "https://animals.howstuffworks.com/mammals/cheetah-speed.htm",
"score": 0.89019,
"images": null
}
}
]
}

查看 LangSmith 跟踪这里,了解内部机制。

下一步

您现在已经学习了几种从您的 QA 链中返回引用的方法。

接下来,查看本节中的其他一些指南,例如如何添加聊天历史记录


此页面是否有帮助?


您也可以留下详细的反馈 在 GitHub 上.