Spaces:
Runtime error
Runtime error
Update summarize.py
Browse files- summarize.py +24 -1
summarize.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
from langchain.chains.summarize import load_summarize_chain
|
| 3 |
from utils import openai_llm
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
|
| 6 |
from langfuse.callback import CallbackHandler
|
| 7 |
load_dotenv()
|
|
@@ -14,13 +15,35 @@ os.environ["LANGFUSE_HOST"] = os.getenv("LANGFUSE_HOST")
|
|
| 14 |
|
| 15 |
langfuse_handler = CallbackHandler()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def setup_summary_chain(api_key):
|
| 21 |
"""Set up a summary chain with a specified LLM."""
|
| 22 |
llm = openai_llm(api_key=api_key)
|
| 23 |
-
return load_summarize_chain(llm=llm, chain_type='map_reduce'
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
def summarize_documents(documents, api_key):
|
|
|
|
| 2 |
from langchain.chains.summarize import load_summarize_chain
|
| 3 |
from utils import openai_llm
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from langchain_core.prompts import PromptTemplate
|
| 6 |
|
| 7 |
from langfuse.callback import CallbackHandler
|
| 8 |
load_dotenv()
|
|
|
|
| 15 |
|
| 16 |
langfuse_handler = CallbackHandler()
|
| 17 |
|
| 18 |
+
map_prompt = """
|
| 19 |
+
Write a detailed summary of the following text.
|
| 20 |
+
The summary should be in the language of the text.
|
| 21 |
+
Do not miss any important details.
|
| 22 |
+
Mention all the important entities at the end of the summary with a heading 'IMPORTANT ENTITIES' and bullets underneath the heading
|
| 23 |
|
| 24 |
|
| 25 |
+
"{text}"
|
| 26 |
+
DETAILED SUMMARY:
|
| 27 |
+
"""
|
| 28 |
+
map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text"])
|
| 29 |
+
|
| 30 |
+
combine_prompt = """
|
| 31 |
+
Write a detailed summary of the following text delimited by triple backquotes.
|
| 32 |
+
Return your response as a detailed paragraph.
|
| 33 |
+
The Response should be in the language of the text
|
| 34 |
+
Mention all the important entities at the end of the summary with a heading 'IMPORTANT ENTITIES' and bullets underneath the heading
|
| 35 |
+
|
| 36 |
+
```{text}```
|
| 37 |
+
PARAGRAPH SUMMARY:
|
| 38 |
+
"""
|
| 39 |
+
combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["text"])
|
| 40 |
|
| 41 |
def setup_summary_chain(api_key):
|
| 42 |
"""Set up a summary chain with a specified LLM."""
|
| 43 |
llm = openai_llm(api_key=api_key)
|
| 44 |
+
return load_summarize_chain(llm=llm, chain_type='map_reduce',
|
| 45 |
+
map_prompt=map_prompt_template,
|
| 46 |
+
combine_prompt=combine_prompt_template)
|
| 47 |
|
| 48 |
|
| 49 |
def summarize_documents(documents, api_key):
|