Skip to content

Commit 3ecfd0d

Browse files
committed
summarize-topic: Add a tool to summarize topic.
1 parent 75bea9f commit 3ecfd0d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ types-pytz
1616
types-requests
1717
gitlint>=0.13.0
1818
-r ./zulip/integrations/bridge_with_matrix/requirements.txt
19+
litellm

tools/summarize-topic

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import urllib.parse
6+
7+
from litellm import completion
8+
9+
import zulip
10+
11+
os.environ["HUGGINGFACE_API_KEY"] = "YOUR_API_KEY"
12+
client = zulip.Client(config_file="~/zuliprc")
13+
14+
if __name__ == "__main__":
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument(
17+
"--url",
18+
type=str,
19+
help="The URL to fetch content from",
20+
default="https://chat.zulip.org/#narrow/stream/101-design/topic/more.20user.20indicators",
21+
)
22+
parser.add_argument(
23+
"--model",
24+
type=str,
25+
help="The model name to use for summarization",
26+
default="huggingface/meta-llama/Meta-Llama-3-8B-Instruct",
27+
)
28+
args = parser.parse_args()
29+
30+
url = args.url
31+
model = args.model
32+
33+
base_url, narrow_hash = url.split("#")
34+
narrow_hash_terms = narrow_hash.split("/")
35+
channel = narrow_hash_terms[2].split("-")[1]
36+
topic = narrow_hash_terms[4]
37+
channel = urllib.parse.unquote(channel.replace(".", "%"))
38+
topic = urllib.parse.unquote(topic.replace(".", "%"))
39+
40+
narrow = [
41+
{"operator": "channel", "operand": channel},
42+
{"operator": "topic", "operand": topic},
43+
]
44+
45+
request = {
46+
"anchor": "newest",
47+
"num_before": 100,
48+
"num_after": 0,
49+
"narrow": narrow,
50+
"apply_markdown": False,
51+
}
52+
result = client.get_messages(request)
53+
messages = result["messages"]
54+
55+
formatted_messages = [
56+
{"content": f"{message['sender_full_name']}: {message['content']}", "role": "user"}
57+
for message in messages
58+
]
59+
60+
# Provide a instruction if using an `Instruct` model.
61+
# There is a 100 token output limit by hugging face.
62+
if "Instruct" in model:
63+
formatted_messages.append(
64+
{"content": "Summarize the above content within 90 words.", "role": "user"}
65+
)
66+
67+
# Send formatted messages to the LLM model for summarization
68+
response = completion(
69+
model=model,
70+
messages=formatted_messages,
71+
)
72+
73+
print("Server response:\n", response)
74+
print("\n\nSummary:\n", response["choices"][0]["message"]["content"])

0 commit comments

Comments
 (0)