-
-
Notifications
You must be signed in to change notification settings - Fork 364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
summarize-topic: Add a tool to summarize topic. #834
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
amanagr
force-pushed
the
llm_summaries
branch
3 times, most recently
from
October 24, 2024 06:56
0166f56
to
9eef6a4
Compare
timabbott
reviewed
Oct 24, 2024
timabbott
reviewed
Oct 24, 2024
timabbott
reviewed
Oct 24, 2024
amanagr
force-pushed
the
llm_summaries
branch
from
October 25, 2024 10:14
9eef6a4
to
051690f
Compare
amanagr
force-pushed
the
llm_summaries
branch
from
October 25, 2024 10:33
051690f
to
f0f8c59
Compare
I did some quick reworking of the output and README, fixed an extra diff --git a/zulip/integrations/litellm/README.md b/zulip/integrations/litellm/README.md
index 81fd645a..9d66fcb4 100644
--- a/zulip/integrations/litellm/README.md
+++ b/zulip/integrations/litellm/README.md
@@ -2,28 +2,31 @@
Generate a short summary of the last 100 messages in the provided topic URL.
-
### API Keys
-For testing you need access token from https://huggingface.co/settings/tokens (or set the correct env variable with the access token if using a different model)
+For testing you need access token from
+https://huggingface.co/settings/tokens (or set the correct env
+variable with the access token if using a different model)
+
+In `~/.zuliprc` add a section named `litellm` and set the api key for
+the model you are trying to use. For example:
-In `~/.zuliprc` add a section named `LITELLM_API_KEYS` and set the api key for the model you are trying to use.
-For example: -[LITELLM_API_KEYS]
-Just run -$ zulip/integrations/litelllm/summarize-topic --help
+$ zulip/integrations/litellm/summarize-topic --help
usage: summarize-topic [-h] [--url URL] [--model MODEL]
options:
diff --git a/zulip/integrations/litellm/summarize-topic b/zulip/integrations/litellm/summarize-topic
index 5d536cbd..901017b0 100755
--- a/zulip/integrations/litellm/summarize-topic
+++ b/zulip/integrations/litellm/summarize-topic
@@ -10,27 +10,6 @@ from litellm import completion # type: ignore[import-not-found]
import zulip
-config_file = zulip.get_default_config_filename()
-if not config_file:
- print("Could not find the Zulip configuration file. Please read the provided README.")
- sys.exit()
-
-client = zulip.Client(config_file=config_file)
-
-config = ConfigParser()
-# Make config parser case sensitive otherwise API keys will be lowercased
-# which is not supported by litellm.
-# https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform
-config.optionxform = str # type: ignore[assignment, method-assign]
-
-with open(config_file) as f:
- config.read_file(f, config_file)
-
-# Set all the keys in `LITELLM_API_KEYS` as environment variables.
-for key in config["LITELLM_API_KEYS"]:
- print("Setting key:", key)
- os.environ[key] = config["LITELLM_API_KEYS"][key]
-
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
@@ -45,8 +24,48 @@ if __name__ == "__main__":
help="The model name to use for summarization",
default="huggingface/meta-llama/Meta-Llama-3-8B-Instruct",
)
+ parser.add_argument(
+ "--max-tokens",
+ type=int,
+ help="The maximum tokens permitted in the response",
+ default=100,
+ )
+ parser.add_argument(
+ "--max-messages",
+ type=int,
+ help="The maximum number of messages fetched from the server",
+ default=100,
+ )
+ parser.add_argument(
+ "--verbose",
+ type=bool,
+ help="Print verbose debugging output",
+ default=False,
+ )
args = parser.parse_args()
+ config_file = zulip.get_default_config_filename()
+ if not config_file:
+ print("Could not find the Zulip configuration file. Please read the provided README.")
+ sys.exit()
+
+ client = zulip.Client(config_file=config_file)
+
+ config = ConfigParser()
+ # Make config parser case sensitive otherwise API keys will be lowercased
+ # which is not supported by litellm.
+ # https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.optionxform
+ config.optionxform = str # type: ignore[assignment, method-assign]
+
+ with open(config_file) as f:
+ config.read_file(f, config_file)
+
+ # Set all the keys in `litellm` as environment variables.
+ for key in config["litellm"]:
+ if args.verbose:
+ print("Setting key:", key)
+ os.environ[key] = config["litellm"][key]
+
url = args.url
model = args.model
@@ -64,33 +83,48 @@ if __name__ == "__main__":
request = {
"anchor": "newest",
- "num_before": 100,
+ "num_before": args.max_messages,
"num_after": 0,
"narrow": narrow,
+ # Fetch raw Markdown, not HTML
"apply_markdown": False,
}
result = client.get_messages(request)
+ if result["result"] == "error":
+ print("Failed fetching message history", result)
+ sys.exit(1)
messages = result["messages"]
+ if len(messages) == 0:
+ print("No messages in conversation to summarize")
+ sys.exit(0)
+
formatted_messages = [
{"content": f"{message['sender_full_name']}: {message['content']}", "role": "user"}
for message in messages
]
# Provide a instruction if using an `Instruct` model.
- # There is a 100 token output limit by hugging face.
if "Instruct" in model:
formatted_messages.append(
- {"content": "Summarize the above content within 90 words.", "role": "user"}
+ {
+ "content": """
+Summarize the above content within 90 words.
+""",
+ "role": "user",
+ }
)
# Send formatted messages to the LLM model for summarization
response = completion(
+ max_tokens=args.max_tokens,
model=model,
messages=formatted_messages,
)
- print("Server response:\n", response)
- print("\n\nGenerated summary for URL:", url)
- print("Summary:")
+ print("Summarized conversation URL:", url)
+ print(
+ f"Used {response['usage']['total_tokens']} tokens to summarize {len(formatted_messages)} Zulip messages."
+ )
+ print()
print(response["choices"][0]["message"]["content"]) |
timabbott
force-pushed
the
llm_summaries
branch
from
October 30, 2024 20:44
f0f8c59
to
c04a172
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Copy pasted from zulip/zulip#31897
For testing you need access token from https://huggingface.co/settings/tokens (or set the correct env variable with the access token if using a different model)
Then set
os.environ["HUGGINGFACE_API_KEY"] = "YOUR_API_KEY"
intools/summarize-topic
.Just run
tools/summarize-topic
to generate same summary.NOTE: only topic links are supported right now.