This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenai_summarize.py
118 lines (89 loc) · 3.54 KB
/
openai_summarize.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import openai
import tiktoken
class OpenAISummarize(object):
openai_key = ""
def __init__(self, openai_key) -> None:
self.openai_key = openai_key
def count_tokens(self, text):
encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
return len(encoding.encode(text))
def chunk_text(self, text, max_tokens=500):
"""
Breaks up a given text into chunks of at most `max_tokens` tokens.
Args:
text (str): The text to chunk.
max_tokens (int): The maximum number of tokens allowed in each chunk.
Returns:
list of str: The chunks of text.
"""
encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
tokens = encoding.encode(text)
chunks = []
current_chunk = []
current_token_count = 0
for token in tokens:
if current_token_count + 1 <= max_tokens:
current_chunk.append(token)
current_token_count += 1
else:
chunks.append(encoding.decode(current_chunk))
current_chunk = [token]
current_token_count = 1
if current_chunk:
chunks.append(encoding.decode(current_chunk))
return chunks
def summarize_text(self, text, max_chunk_size=500, max_combined_summary_size=4000):
"""
Generates a summary of a given text using OpenAI's text-davinci-003 model.
Args:
text (str): The text to summarize.
max_chunk_size (int, optional): The size of each chunk of text to summarize. Defaults to 500.
max_combined_summary_size (int, optional): The maximum size of the combined summary. Defaults to 4000.
Returns:
str: The generated summary of the text.
"""
openai.api_key = self.openai_key
model_engine = "text-davinci-003"
prompt_template = "{}\n\nTl;dr (max 200 words)"
def recursive_summarize(text):
"""
Recursively generates a summary of a given text.
Args:
text (str): The text to summarize.
Returns:
str: The generated summary of the text.
"""
chunks = self.chunk_text(text, max_chunk_size)
summaries = []
# Summarize each chunk separately using the OpenAI API
for chunk in chunks:
prompt = prompt_template.format(chunk)
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=150,
n=1,
stop=None,
temperature=0.5,
)
summary = response.choices[0].text.strip()
summaries.append(summary)
combined_summary = " ".join(summaries)
if self.count_tokens(combined_summary) > max_combined_summary_size:
return recursive_summarize(combined_summary)
else:
return combined_summary
final_summary = recursive_summarize(text)
cohesion_prompt = f"{final_summary}\n\nTl;dr (max 2 paragraphs)"
response = openai.Completion.create(
engine=model_engine,
prompt=cohesion_prompt,
temperature=0.7,
max_tokens=300,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=1,
)
rewritten_summary = response.choices[0].text.strip()
return rewritten_summary