Skip to content

Summarize

1. Chain Types

  1. stuff: すべてのDocumentをcombineしてLLMに送る
  2. map_reduce: それぞれの要約をつくってからまとめる
  3. refine:最初にようやくを作り、次のDocと合わせてようやくを作っていく (Incremental)

2. Implementaion

  1. load_summarize_chain: llmとChain Typeを指定してchainを返す。

    引数: 1. llm: 1. chain_type: stuffがデフォルト 1. verbose: bool 1. **kwargs:

    返り値: BaseCombineDocumentsChain

    内部では、chain_typeによってそれぞれ対象となるChainをLoadしている

    loader_mapping: Mapping[str, LoadingCallable] = {
        "stuff": _load_stuff_chain,
        "map_reduce": _load_map_reduce_chain,
        "refine": _load_refine_chain,
    }
    
    それぞれは、それぞれのChainを初期化する。 1. _load_stuff_chain: デフォルトでstuff_prompt.PROMPT指定される。自分で指定したい場合には、load_summarize_chainに引数でpromptを指定すれば良い。prompt内のvariableもdocument_variable_nameで指定することができる
    def _load_stuff_chain(
        llm: BaseLanguageModel,
        prompt: BasePromptTemplate = stuff_prompt.PROMPT,
        document_variable_name: str = "text",
        verbose: Optional[bool] = None,
        **kwargs: Any,
    ) -> StuffDocumentsChain:
        llm_chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
        # TODO: document prompt
        return StuffDocumentsChain(
            llm_chain=llm_chain,
            document_variable_name=document_variable_name,
            verbose=verbose,
            **kwargs,
        )
    
    1. _load_map_reduce_chain: デフォルトではmap_reduce_prompt.PROMPTmap_promptcombine_promptに指定されるが、自分でそれぞれ指定することができる。また、variableもcombine_document_variable_namemap_reduce_document_variable_nameで指定することができる。
    def _load_map_reduce_chain(
        llm: BaseLanguageModel,
        map_prompt: BasePromptTemplate = map_reduce_prompt.PROMPT,
        combine_prompt: BasePromptTemplate = map_reduce_prompt.PROMPT,
        combine_document_variable_name: str = "text",
        map_reduce_document_variable_name: str = "text",
        collapse_prompt: Optional[BasePromptTemplate] = None,
        reduce_llm: Optional[BaseLanguageModel] = None,
        collapse_llm: Optional[BaseLanguageModel] = None,
        verbose: Optional[bool] = None,
        token_max: int = 3000,
        **kwargs: Any,
    ) -> MapReduceDocumentsChain:
        map_chain = LLMChain(llm=llm, prompt=map_prompt, verbose=verbose)
        _reduce_llm = reduce_llm or llm
        reduce_chain = LLMChain(llm=_reduce_llm, prompt=combine_prompt, verbose=verbose)
        ...
    
    1. _load_refine_chain: デフォルトでは、question_promptには、refine_prompts.REFINE_PROMPTが、refine_promptには、refine_prompts.REFINE_PROMPTが指定されるが自分で指定することができる。variableもdocument_variable_nameで指定することができる。
    def _load_refine_chain(
        llm: BaseLanguageModel,
        question_prompt: BasePromptTemplate = refine_prompts.PROMPT,
        refine_prompt: BasePromptTemplate = refine_prompts.REFINE_PROMPT,
        document_variable_name: str = "text",
        initial_response_name: str = "existing_answer",
        refine_llm: Optional[BaseLanguageModel] = None,
        verbose: Optional[bool] = None,
        **kwargs: Any,
    ) -> RefineDocumentsChain:
        initial_chain = LLMChain(llm=llm, prompt=question_prompt, verbose=verbose)
        _refine_llm = refine_llm or llm
        refine_chain = LLMChain(llm=_refine_llm, prompt=refine_prompt, verbose=verbose)
        return RefineDocumentsChain(
            initial_llm_chain=initial_chain,
            refine_llm_chain=refine_chain,
            document_variable_name=document_variable_name,
            initial_response_name=initial_response_name,
            verbose=verbose,
            **kwargs,
        )
    
    1. BaseCombineDocumentsChain: Stuff, MapReduce, Refine Documents ChainはすべてBaseCombineDocumentsChainを実装している 1. combine_docs: Documentsを一つのstringに変換する 1. prompt_length: 与えられたDocumentsに対してのPromptの長さを返す

  2. StuffDocumentsChain: combine_docs: function to stuff all documents into one prompt and pass to LLM.

    prompt:

    """Write a concise summary of the following:
    
    
    "{text}"
    
    
    CONCISE SUMMARY:"""
    
  3. MapReduceDocumentsChain: prompt

    """Write a concise summary of the following:
    
    
    "{text}"
    
    
    CONCISE SUMMARY:"""
    

  4. RefineDocumentsChain: init prompt:
    """Write a concise summary of the following:
    
    
    "{text}"
    
    
    CONCISE SUMMARY:"""
    
    refine prompt:
    "Your job is to produce a final summary\n"
    "We have provided an existing summary up to a certain point: {existing_answer}\n"
    "We have the opportunity to refine the existing summary"
    "(only if needed) with some more context below.\n"
    "------------\n"
    "{text}\n"
    "------------\n"
    "Given the new context, refine the original summary"
    "If the context isn't useful, return the original summary."
    

Basic Usage

  1. Import

    from langchain import OpenAI, PromptTemplate, LLMChain
    from langchain.text_splitter import CharacterTextSplitter
    from langchain.chains.mapreduce import MapReduceChain
    from langchain.prompts import PromptTemplate
    
    llm = OpenAI(temperature=0)
    

  2. docの準備 (state_of_the_union.txt (723行))

    text_splitter = CharacterTextSplitter()
    
    with open("../../state_of_the_union.txt") as f:
        state_of_the_union = f.read()
    texts = text_splitter.split_text(state_of_the_union)
    
    from langchain.docstore.document import Document
    
    docs = [Document(page_content=t) for t in texts[:3]]
    

  3. Summarize

    from langchain.chains.summarize import load_summarize_chain
    
    chain = load_summarize_chain(llm, chain_type="map_reduce")
    chain.run(docs)
    
    1. 結果 In response to Russian aggression in Ukraine, the US and its allies are taking action to hold Putin accountable, including economic sanctions, cutting off access to technology, and a dedicated task force to go after the crimes of Russian oligarchs. The US is also providing economic and military assistance to Ukraine and mobilizing ground forces, air squadrons, and ship deployments to protect NATO countries. President Biden's American Rescue Plan and Bipartisan Infrastructure Law have provided economic relief and created jobs for millions of Americans.

Advanced

Tip1: Prompt

{text}が必要になる

prompt_template = """Write a concise summary of the following:


{text}


CONCISE SUMMARY IN JAPANESE:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
chain = load_summarize_chain(llm, chain_type="stuff", prompt=PROMPT)
chain.run(docs)
{
    'intermediate_steps':
        [
            '\n今夜、私たちはアメリカ人として一緒に集まりました。ロシアのプーチンがウクライナに侵攻しようとしたとき、ウクライナの人々は勇敢に立ち向かいました。NATO加盟国はロシアを制裁し、犯罪を追求しています。プーチンは今よりもっと孤立しています。私たちはプーチンの豊かな資産を取り上げるために準備しています。',
            '\nアメリカはヨーロッパの仲間と共に、ロシアの豪華なヨット、アパート、プライベートジェットを押収するために取り組んでいます。空港を閉鎖し、ロシアの経済を押しつぶすために、米国は仲間と共に、ウクライナに軍事支援、経済支援、人道支援を提供しています。米国はウクライナに10億ドル以上の支援を行っています。米国はNATO加盟国を守るために、地上部隊、航空部隊',
            '\n\nアメリカは2年間で最も厳しい時期を迎えました。パンデミックは厳しく、多くの家族が食料、ガソリン、住宅などの費用を支払うのに苦労しています。私は理解しています。私は父がスコートン、ペンシルベニアから仕事を探すために家を出たのを覚えています。私が大統領になった最初のことの1つは、アメリカ救済計画を通すために戦うことでした。この計画は、国民を救済し、COVID-19と戦うために'
        ],
    'output_text': '\n\nアメリカはNATO加盟国を守るために、ウクライナへの軍事支援、経済支援、人道支援を行い、プーチンの豊かな資産を取り上げる準備をしています。また、パンデミックにより多くの家族が苦労していることを理解し、国民を救済するために戦っています。'
}

課題

  • [ ] 日本語が切れてしまう

Ref

  1. LangChainのSummarizationについて調べたまとめ