-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsummarize_mail.py
49 lines (42 loc) · 1.53 KB
/
summarize_mail.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
import asyncio
from nodetool.dsl.google.mail import GmailSearch, EmailFlag
from nodetool.dsl.graph import graph, run_graph
from nodetool.dsl.nodetool.output import StringOutput
from nodetool.dsl.nodetool.text import Join, Concat
from nodetool.dsl.ollama.text import Ollama
from nodetool.dsl.nodetool.list import MapTemplate
from nodetool.metadata.types import LlamaModel
# Search Gmail for recent emails
emails = GmailSearch(
date_filter=GmailSearch.DateFilter.SINCE_ONE_DAY,
max_results=2,
)
# Format emails into a template
formatted_emails = MapTemplate(
values=emails,
template="==================\nFrom: {sender}\nSubject: {subject}\nBody: {body}\n==================",
)
# Join all formatted emails
joined_emails = Join(strings=formatted_emails, separator="")
# Add summarization prompt
prompt = Concat(
a=joined_emails,
b="Create a concise and well-structured summary of the emails above. Prioritize important topics at the top, group related emails together, and include a separate section for newsletters. Summarize each email briefly, highlighting key details, and organize the digest for easy scanning and action.",
)
# Generate summary using Gemma
summary = Ollama(
prompt=prompt,
model=LlamaModel(repo_id="llama3.2:3b"),
system_prompt="You are a summarizer.",
context_window=65536,
temperature=0,
top_k=50,
top_p=0.95,
)
output = StringOutput(
name="summary",
description="Summary of the emails",
value=summary,
)
summary_str = asyncio.run(run_graph(graph(output)))
print(summary_str)