-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummary.py
37 lines (31 loc) · 1.38 KB
/
summary.py
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
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
def generate_summary(llm, input_text):
# Chunk the input_text
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=500, chunk_overlap=50)
chunks = text_splitter.split_text(input_text.replace('\n', ''))
# Map
map_template = """[INST] <<SYS>>
You are a helpful assistant. Complete the task below. Output the answer only. Do not include any greetings or instructions.
<</SYS>>
Summarize the below text from a podcast episode:
{docs}[/INST]
"""
map_prompt = PromptTemplate.from_template(map_template)
map_chain = LLMChain(llm=llm, prompt=map_prompt)
# Process each chunk through the map_chain
summaries = []
for chunk in chunks:
summary = map_chain.run(chunk)
cleaned_summary = summary.replace(
"Sure! Here is a summary of the text:\n\n", "")
summaries.append(cleaned_summary)
# Concatenate summaries into one string
concatenated_summaries = ' '.join(summaries)
output = concatenated_summaries.replace(
"Sure! Here is the summary of the podcast episode:\n\n", "")
output = concatenated_summaries.replace(
"Sure! Here is a summary of the podcast episode:\n\n", "")
return output