Skip to content

Commit

Permalink
summarize-topic: Fix linting errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanagr authored and timabbott committed Dec 16, 2024
1 parent 77362d4 commit 9d5670b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
6 changes: 3 additions & 3 deletions zulip/integrations/jabber/jabber_mirror_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import logging
import optparse
import sys
from configparser import SafeConfigParser
from configparser import ConfigParser

# The following is a table showing which kinds of messages are handled by the
# mirror in each mode:
Expand Down Expand Up @@ -385,10 +385,10 @@ def config_error(msg: str) -> None:
else:
config_file = options.zulip_config_file

config = SafeConfigParser()
config = ConfigParser()
try:
with open(config_file) as f:
config.readfp(f, config_file)
config.read_file(f, config_file)
except OSError:
pass
for option in (
Expand Down
36 changes: 24 additions & 12 deletions zulip/integrations/litellm/summarize-topic
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
#!/usr/bin/env python3

import argparse
import json
import os
import sys
import urllib.parse
from configparser import ConfigParser
import json
from typing import Any, Dict

from litellm import completion # type: ignore[import-not-found]

import zulip

def format_conversation(result):

def format_conversation(result: Dict[str, Any]) -> str:
# Note: Including timestamps seems to have no impact; including reactions
# makes the results worse.
zulip_messages = result["messages"]
if len(zulip_messages) == 0:
print("No messages in conversation to summarize")
sys.exit(0)

zulip_messages_list = [{"sender": message['sender_full_name'],
"content": message['content']} for message in zulip_messages]
zulip_messages_list = [
{"sender": message["sender_full_name"], "content": message["content"]}
for message in zulip_messages
]
return json.dumps(zulip_messages_list)

def make_message(content, role="user"):
return {"content": content,
"role": role}

def get_max_summary_length(conversation_length):
return min(6, 4 + int((conversation_length-10)/10))
def make_message(content: str, role: str = "user") -> Dict[str, str]:
return {"content": content, "role": role}


def get_max_summary_length(conversation_length: int) -> int:
return min(6, 4 + int((conversation_length - 10) / 10))


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -113,7 +119,7 @@ if __name__ == "__main__":
print("Failed fetching message history", result)
sys.exit(1)

conversation_length = len(result['messages'])
conversation_length = len(result["messages"])
max_summary_length = get_max_summary_length(conversation_length)

print("Conversation URL:", url)
Expand All @@ -122,7 +128,11 @@ if __name__ == "__main__":
intro = f"The following is a chat conversation in the Zulip team chat app. channel: {channel}, topic: {topic}"
formatted_conversation = format_conversation(result)
prompt = f"Succinctly summarize this conversation based only on the information provided, in up to {max_summary_length} sentences, for someone who is familiar with the context. Mention key conclusions and actions, if any. Refer to specific people as appropriate. Don't use an intro phrase."
messages = [make_message(intro, "system"), make_message(formatted_conversation), make_message(prompt)]
messages = [
make_message(intro, "system"),
make_message(formatted_conversation),
make_message(prompt),
]

# Send formatted messages to the LLM model for summarization
response = completion(
Expand All @@ -131,6 +141,8 @@ if __name__ == "__main__":
messages=messages,
)

print(f"Used {response['usage']['completion_tokens']} completion tokens to summarize {conversation_length} Zulip messages ({response['usage']['prompt_tokens']} prompt tokens).")
print(
f"Used {response['usage']['completion_tokens']} completion tokens to summarize {conversation_length} Zulip messages ({response['usage']['prompt_tokens']} prompt tokens)."
)
print()
print(response["choices"][0]["message"]["content"])

0 comments on commit 9d5670b

Please sign in to comment.