File size: 1,889 Bytes
bbe5b39
dd9d8a6
 
bbe5b39
7e5d956
bbe5b39
 
 
 
 
 
 
 
 
 
 
 
7e5d956
 
 
 
 
bbe5b39
dd9d8a6
7e5d956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd9d8a6
 
 
 
7e5d956
 
 
dd9d8a6
bbe5b39
dd9d8a6
 
 
bbe5b39
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
import os
from langchain.chains.summarize import load_summarize_chain
from utils import openai_llm
from dotenv import load_dotenv
from langchain_core.prompts import PromptTemplate

from langfuse.callback import CallbackHandler
load_dotenv()

os.environ["LANGFUSE_PUBLIC_KEY"] = os.getenv("LANGFUSE_PUBLIC_KEY")
os.environ["LANGFUSE_SECRET_KEY"] = os.getenv("LANGFUSE_SECRET_KEY")
os.environ["LANGFUSE_HOST"] = os.getenv("LANGFUSE_HOST") 


 
langfuse_handler = CallbackHandler()

map_prompt = """
Write a detailed summary of the following text.  
The summary should be in the language of the text.
Do not miss any important details.
Mention all the important entities at the end of the summary with a heading 'IMPORTANT ENTITIES' and bullets underneath the heading


"{text}"
DETAILED SUMMARY:
"""
map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text"])

combine_prompt = """
Write a detailed summary of the following text delimited by triple backquotes.
Return your response as a detailed paragraph.
The Response should be in the language of the text
Mention all the important entities at the end of the summary with a heading 'IMPORTANT ENTITIES' and bullets underneath the heading

```{text}```
PARAGRAPH SUMMARY:
"""
combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["text"])

def setup_summary_chain(api_key):
    """Set up a summary chain with a specified LLM."""
    llm = openai_llm(api_key=api_key)
    return load_summarize_chain(llm=llm, chain_type='map_reduce',
                                map_prompt=map_prompt_template,
                                combine_prompt=combine_prompt_template)


def summarize_documents(documents, api_key):
    """Generate summaries for provided documents."""
    summary_chain = setup_summary_chain(api_key)
    return summary_chain.run(documents, callbacks=[langfuse_handler])