for any queries contact Ajit Kumar Panda (ajit.kumar.panda@sap.com)
This sample repository contains sourcecode for summarization of texts using Langchain summarization chain.
Langchain's summarization chain supports 3 techniques i.e. Stuff, Refine, Map-Reduce.
- Stuff: The chain will take a list of documents, insert them all into a prompt, and pass that prompt to an LLM for summarization.
- Map-Reduce: The chain will first summarize each document using LLM, then combine the summaries into a prompt , and pass that prompt to an LLM for final summarization.
- Refine: The chain updates a rolling summary by iterating over the documents in a sequence. In each iteration, the current document and the previously generated summary are passed as prompt for summarization.
Note: Here, document refers to langchain module: Document.
In Js, it is an object with 2 properties: pageContent, metadata
Example:
{
"pageContent":"The Marvel Cinematic Universe (MCU) is a media franchise and shared universe centered around a series of superhero films and television series produced by Marvel Studios. It features interconnected stories based on characters from Marvel Comics, including iconic heroes like Iron Man, Captain America, and Spider-Man. The MCU has become one of the highest-grossing film franchises, renowned for its expansive world-building and cross-character plotlines",
"metadata":{"lines":{"from":1, "to":3}}
}More details can be found here:
- Text content which needs to be summarized is read from a file: state_of_the_union.txt
- Different text splitters can be used to convert large content into chuks (documents). In this sample RecursiveCharacterTextSplitter is used. More Info
- CAP service has 3 methods for 3 different techniques:
getSummaryByStuff: Summarization by Stuff techniquegetSummaryByReduce: Summarization by MapReduce techniquegetSummaryByRefine: Summarization by Refine technique
- Custom chat model is created as a util (here:utilGenAIHub.js) where cap-llm-plugin is used to make LLM call via SAP GenAI Hub. (How to Create Custom Chat Model)
- Summary logic is added in a util as well where prompts can be changed according to your choice (here: utilSummary.js)
- Clone repo and install all packages:
npm i - update .cdsrc.json with your deployment id, resource group, destination etc.
Note: destination will point to AI core api with its credentials.
{ "requires": { "db": "hana", "cap-llm-plugin": { "impl": "cap-llm-plugin/srv/cap-llm-plugin.js" }, "GENERATIVE_AI_HUB": { "CHAT_MODEL_DESTINATION_NAME": "AICoreAzureOpenAIDestination", "CHAT_MODEL_DEPLOYMENT_URL": "/v2/inference/deployments/<deployment id>", "CHAT_MODEL_RESOURCE_GROUP": "<resource group>", "CHAT_MODEL_API_VERSION": "2023-05-15" }, "AICoreAzureOpenAIDestination": { "kind": "rest", "credentials": { "destination": "<destination name>", "requestTimeout": "300000" } } } } - Login into cloud foundry and run following commands:
cf cs destination lite cap-summarization-chain-destcds bind --to cap-summarization-chain-dest --for developmentcf cs xsuaa application cap-summarization-chain-authcds bind --to cap-summarization-chain-auth --for development
- Run application:
cds watch - Use test.http to trigger calls