forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…hain-ai#1080) * feat: added returnIntermediateSteps flag to MapReduceChain * docs: addded extra documentation around the intermediate steps * fix: linting issue * docs: set map_reduce in backticks * feat: added option to loadQAChain as well * Populate params via destructuring, adds unit test * Lint --------- Co-authored-by: Jacob Lee <[email protected]> Co-authored-by: Nuno Campos <[email protected]>
- Loading branch information
1 parent
0eccc85
commit 0fe7aac
Showing
8 changed files
with
139 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import CodeBlock from "@theme/CodeBlock"; | ||
import SummarizeExample from "@examples/chains/summarization_map_reduce.ts"; | ||
import SummarizeExampleIntermediateSteps from "@examples/chains/summarization_map_reduce_intermediate_steps.ts"; | ||
|
||
# Summarization | ||
|
||
A summarization chain can be used to summarize multiple documents. One way is to input multiple smaller documents, after they have been divided into chunks, and operate over them with a `MapReduceDocumentsChain`. You can also choose instead for the chain that does summarization to be a StuffDocumentsChain, or a RefineDocumentsChain. See more about the differences between them [here](../index_related_chains/document_qa) | ||
|
||
<CodeBlock language="typescript">{SummarizeExample}</CodeBlock> | ||
|
||
## Intermediate Steps | ||
|
||
We can also return the intermediate steps for `map_reduce` chains, should we want to inspect them. This is done with the `returnIntermediateSteps` parameter. | ||
|
||
<CodeBlock language="typescript">{SummarizeExampleIntermediateSteps}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
examples/src/chains/summarization_map_reduce_intermediate_steps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { loadSummarizationChain } from "langchain/chains"; | ||
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; | ||
import * as fs from "fs"; | ||
|
||
export const run = async () => { | ||
// In this example, we use a `MapReduceDocumentsChain` specifically prompted to summarize a set of documents. | ||
const text = fs.readFileSync("state_of_the_union.txt", "utf8"); | ||
const model = new OpenAI({ temperature: 0 }); | ||
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); | ||
const docs = await textSplitter.createDocuments([text]); | ||
|
||
// This convenience function creates a document chain prompted to summarize a set of documents. | ||
const chain = loadSummarizationChain(model, { | ||
type: "map_reduce", | ||
returnIntermediateSteps: true, | ||
}); | ||
const res = await chain.call({ | ||
input_documents: docs, | ||
}); | ||
console.log({ res }); | ||
/* | ||
{ | ||
res: { | ||
intermediateSteps: [ | ||
"In response to Russia's aggression in Ukraine, the United States has united with other freedom-loving nations to impose economic sanctions and hold Putin accountable. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs and seize their ill-gotten gains.", | ||
"The United States and its European allies are taking action to punish Russia for its invasion of Ukraine, including seizing assets, closing off airspace, and providing economic and military assistance to Ukraine. The US is also mobilizing forces to protect NATO countries and has released 30 million barrels of oil from its Strategic Petroleum Reserve to help blunt gas prices. The world is uniting in support of Ukraine and democracy, and the US stands with its Ukrainian-American citizens.", | ||
" President Biden and Vice President Harris ran for office with a new economic vision for America, and have since passed the American Rescue Plan and the Bipartisan Infrastructure Law to help struggling families and rebuild America's infrastructure. This includes creating jobs, modernizing roads, airports, ports, and waterways, replacing lead pipes, providing affordable high-speed internet, and investing in American products to support American jobs.", | ||
], | ||
text: "President Biden is taking action to protect Americans from the COVID-19 pandemic and Russian aggression, providing economic relief, investing in infrastructure, creating jobs, and fighting inflation. | ||
He is also proposing measures to reduce the cost of prescription drugs, protect voting rights, and reform the immigration system. The speaker is advocating for increased economic security, police reform, and the Equality Act, as well as providing support for veterans and military families. | ||
The US is making progress in the fight against COVID-19, and the speaker is encouraging Americans to come together and work towards a brighter future.", | ||
}, | ||
} | ||
*/ | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters