File size: 1,736 Bytes
2f12302
 
 
 
 
c6d3d04
2f12302
 
c6d3d04
2f12302
 
 
 
 
 
 
 
 
 
 
 
 
229f176
2f12302
 
 
229f176
 
 
 
 
 
2f12302
 
c6d3d04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from langchain.chains.llm import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.docstore.document import Document
import time
from typing import List
from langchain.chains.summarize import load_summarize_chain


async def async_generate_llmchain(
    llm: ChatOpenAI, docs: List[Document], llm_kwargs: dict, k: str
) -> dict:
    """Asyncronous LLMChain function.

    Args:
        llm (ChatOpenAI): Language model to use.
        docs (List[Document]): List of documents.
        llm_kwargs (dict): Keyword arguments for the LLMChain.
        k (str): Key for a dictionary under which the output is returned.

    Returns:
        dict: Dictionary with the summarization.
    """

    print(f"Starting summarization for {k}")
    now = time.time()

    chain = load_summarize_chain(
        llm=llm,
        **llm_kwargs,
    )

    resp = await chain.arun(docs)
    print(f"Time taken for {k}: ", time.time() - now)
    return {k: resp}


async def async_generate_summary_chain(
    llm: ChatOpenAI, docs: List[Document], summarization_kwargs: dict, k: str
) -> dict:
    """Asyncronous LLMChain function.

    Args:
        llm (ChatOpenAI): Language model to use.
        docs (List[Document]): List of documents.
        summarization_kwargs (dict): Keyword arguments for the load_summarize_chain.
        k (str): Key for a dictionary under which the output is returned.

    Returns:
        dict: Dictionary with the summarization.
    """
    print(f"Starting summarization for {k}")
    now = time.time()
    chain = load_summarize_chain(
        llm=llm,
        **summarization_kwargs,
    )
    resp = await chain.arun(docs)
    print(f"Time taken for {k}: ", time.time() - now)
    return {k: resp}