File size: 782 Bytes
bf06fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate

def get_summary_prompt():
    """Get the prompt template for document summarization"""
    return ChatPromptTemplate.from_template("""
You are a helpful assistant. Summarize the following document clearly and accurately:
<context>
{context}
</context>
""")

def summarize_document(llm, documents):
    """
    Summarize the uploaded document(s)
    
    Args:
        llm: Language model instance
        documents: List of document chunks
        
    Returns:
        str: Document summary
    """
    summary_prompt = get_summary_prompt()
    chain = create_stuff_documents_chain(llm, summary_prompt)
    return chain.invoke({"context": documents})