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