-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
287 lines (227 loc) · 9.35 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import os
import tiktoken
import re
import logging
from gql import gql
from datetime import datetime, timedelta
from omnivoreql import OmnivoreQL
from dotenv import load_dotenv
from colorama import Fore, Style
from langchain_community.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
load_dotenv()
# Logger
logging.basicConfig(
filename="logs/main.log",
filemode="w",
format="%(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger("logger")
# LLM
summarization_prompt = """\
<role>
Act as a professional summarizer and assistant.
</role>
<context>
I will provide you with an article.
</context>
<task>
- Summarize the article into bullet points.
- Start with one sentence describing the article from high-level, then provide bullet points.
- Highlight the most important points using markdown bold.
- Use markdown syntax.
- Think step by step.
</task>
<constraints>
- Make sure you follow 80/20 rule: provide 80% of essential value using 20% or less volume of text.
- Be as consise and comprehensive as possible.
</constraints>
<article>
{content}
</article>
"""
def initialize_llm(temperature: float = 0.0):
model_id = os.getenv("OLLAMA_MODEL_ID")
base_url = os.getenv("OLLAMA_BASE_URL")
llm = ChatOllama(model=model_id, temperature=temperature, base_url=base_url)
return llm
class Summarizer:
def __init__(self,temperature: float = 0.0) -> None:
self.llm = initialize_llm(temperature=temperature)
self.tokenizer = tiktoken.get_encoding("cl100k_base") # ~Llama tokenizer
self.prompt_template = ChatPromptTemplate.from_messages(
[("human", summarization_prompt)]
)
def get_summary(self, content: str):
print(f"Tokens: approx. {len(self.tokenizer.encode(content))}")
# Get the summary from LLM
prompt = self.prompt_template.invoke({"content": content})
response = self.llm.invoke(prompt)
# Parse Ollama completion
summary = response.content
total_duration = response.response_metadata["total_duration"]
completion_tokens = (
response.response_metadata["eval_count"]
if "eval_count" in response.response_metadata
else 0
)
prompt_tokens = (
response.response_metadata["prompt_eval_count"]
if "prompt_eval_count" in response.response_metadata
else 0
)
total_tokens = prompt_tokens + completion_tokens
stats = {
"time_taken": total_duration * 1e-9, # nanoseconds to seconds
"total_tokens": total_tokens,
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
}
return summary, stats
# Omnivore
class OmnivoreClient:
def __init__(self):
self.client = OmnivoreQL(os.getenv("OMNIVORE_API_KEY"))
self.username = self.get_username()
self.summ_label_id, self.read_later_label_id = self.get_label_ids()
self.onmivore_link_template = "https://omnivore.app/me/"
print("Profile:", self.username)
print("'summarized' label ID:", self.summ_label_id)
print("'read later' label ID:", self.read_later_label_id)
def get_username(self) -> str:
username = self.client.get_profile()["me"]["profile"]["username"]
return username
def get_label_ids(self):
labels = self.client.get_labels()["labels"]["labels"]
summ_label_id = None
read_later_label_id = None
for label in labels:
if label["name"] == "summarized":
summ_label_id = label["id"]
elif label["name"] == "read later":
read_later_label_id = label["id"]
if summ_label_id and read_later_label_id:
break
if summ_label_id is None or read_later_label_id is None:
raise ValueError("Label 'summarized' or 'read later' not found!")
return summ_label_id, read_later_label_id
def set_article_summarized(self, aid: str, label_ids: list) -> list[str]:
return self.set_article_new_label(aid, label_ids, [self.summ_label_id])
def set_article_read_later(self, aid: str, label_ids: list) -> list[str]:
return self.set_article_new_label(aid, label_ids, [self.read_later_label_id])
def set_article_new_label(
self, aid: str, old_label_ids: list, new_label_ids: list[str]
):
mutation = gql(
"""
mutation SetLabels($input: SetLabelsInput!) {
setLabels(input: $input) {
... on SetLabelsSuccess {
labels {
id
name
}
}
... on SetLabelsError {
errorCodes
}
}
}
"""
)
label_ids = old_label_ids + new_label_ids
new_lebels = self.client.client.execute(
mutation, variable_values={"input": {"pageId": aid, "labelIds": label_ids}}
)
new_label_ids = [label["id"] for label in new_lebels["setLabels"]["labels"]]
new_label_names = [label["name"] for label in new_lebels["setLabels"]["labels"]]
print(Fore.RESET + f"New labels: {new_label_names}")
return new_label_ids
def get_articles(self, query: str, limit: int = None):
articles = self.client.get_articles(
query=query, include_content=False, limit=limit
)
# For each article get its slug
results = []
for article in articles["search"]["edges"]:
# llama3 constraint, ignore large articles for now
if article["node"]["wordsCount"] < 8192:
slug = article["node"]["slug"]
result = self.client.get_article(
self.username, slug, format="markdown"
)["article"]["article"]
results.append(result)
else:
print(Fore.RED + "\nSkipping large article!")
print("Title:", article["node"]["title"])
print("Words count:", article["node"]["wordsCount"])
print(
f"Omnivore link: {self.onmivore_link_template + article['node']['slug']}"
)
print(Style.RESET_ALL)
return results
def parse_article(self, article: dict):
aid = article["id"]
title = article["title"]
author = article["author"]
words_count = article["wordsCount"]
description = article["description"]
label_names = [label["name"] for label in article["labels"]]
label_ids = [label["id"] for label in article["labels"]]
omnivore_link = self.onmivore_link_template + article["slug"]
logger.info(f"Content before preprocessing:\n\n{article['content']}")
link_pattern = r"\[(.*?)\]\(.+?\)"
content = re.sub(link_pattern, r"\1", article["content"]) # removes links
content = re.sub(link_pattern, r"\1", content) # for nested links
logger.info(f"Content after preprocessing:\n\n{content}")
print(Fore.MAGENTA + f"Processing article AID: {aid}")
print(Fore.RESET + f"Title: {title}")
print(f"Author: {author}")
print(f"Current labels: {label_names}")
print(f"Description: {description}")
print(f"Words count: {words_count}")
return aid, label_ids, content, omnivore_link
def archive_article(self, aid: str):
return self.client.archive_article(aid)
def main():
# Initialize LLM
summarizer = Summarizer()
# Initialize Omnivore
omnivore = OmnivoreClient()
# Get posts from subscriptions from the last 24 hours (RSS/Newsletter)
date_one_week_ago = datetime.now() - timedelta(days=1)
query = (
f"in:inbox has:subscriptions saved:{date_one_week_ago.strftime('%Y-%m-%d')}..*"
)
# Get recent newsletter/feed article (from the last 24 hours)
# For now, skips large articles, that exceed 8192 words
articles = omnivore.get_articles(query=query, limit=None)
# Parse the articles
for article in articles:
aid, label_ids, content, omnivore_link = omnivore.parse_article(article)
# Get the summary from LLM
response, stats = summarizer.get_summary(content)
print("Summary:\n")
print(Fore.MAGENTA + response)
print(Style.RESET_ALL)
print(f"Omnivore link: {omnivore_link}\n")
print(f"Stats: {stats}\n")
logger.info(f"Summary:\n{response}")
logger.info(f"Stats: {stats}")
# Add label 'summarized' to article if satisfied
if input("Are you satisfied with this summary? (y/n): ") == "y":
label_ids = omnivore.set_article_summarized(aid, label_ids)
print(Fore.GREEN + "Label 'summarized' added to article.")
if input(Fore.RESET + "Do you want to read full article later? (y/n): ") == "y":
omnivore.set_article_read_later(aid, label_ids)
print(Fore.GREEN + "Label 'read later' added to article.")
else:
omnivore.archive_article(aid)
print(Fore.GREEN + "Article archived.")
else:
print(Fore.RED + "Label 'summarized' not added to article. Proceeding...")
print(Style.RESET_ALL)
print(Fore.RESET + "Finished processing articles. Exiting...")
if __name__ == "__main__":
main()