Skip to content

Commit

Permalink
add option to define own quote prompts (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fleinen authored Nov 21, 2024
1 parent a965443 commit 0b6c7d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ STRAVA_WIDGET_URL="<url>"
STRAVA_CLUB_URL="<url>"
SLACK_BOT_TOKEN="<token>"
SLACK_CHANNEL="<channel>"
GROQ_API_KEY="<key>"
GROQ_API_KEY="<key>"
QUOTE_PROMPTS_PATH="<path>"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ There will be a quote at the end of every message. If a Groq API key is provided

You can generte an API key [here](https://console.groq.com/keys). The key must be set as an environment variable with the key `GROQ_API_KEY` which can be set in the `.env` file.

By default, the predefined prompts from the file `src/weeklt_strava_stats/data/quote_prompts.csv` will be used. If you want to use your own prompts, you can set the environment variable `QUOTE_PROMPTS_PATH` in the `.env` file.

### Optional: Add a link to the Strava Club
If you want to include a link to the Strava club in the message, you can set the environment variable `STRAVA_CLUB_URL` in the `.env` file.
10 changes: 6 additions & 4 deletions src/weekly_strava_stats/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import os
import logging
import click
from dotenv import load_dotenv

Expand Down Expand Up @@ -74,9 +74,11 @@ def post_last_weeks_stats():
logging.error("No stats found for last week")
return

groq_api_key = os.getenv('GROQ_API_KEY')
strava_club_url = os.getenv('STRAVA_CLUB_URL')
message_builder = MessageBuilder(groq_api_key, strava_club_url)
message_builder = MessageBuilder(
groq_api_key=os.getenv('GROQ_API_KEY'),
quote_prompts_path=os.getenv('QUOTE_PROMPTS_PATH'),
club_url=os.getenv('STRAVA_CLUB_URL')
)
message = message_builder.build(last_weeks_stats, compare_stats)

slack_connector = SlackChannelConnector(slack_token, slack_channel)
Expand Down
27 changes: 14 additions & 13 deletions src/weekly_strava_stats/utils/message_builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import random
from typing import Optional
from weekly_strava_stats.storage.week_stats import WeekStats
Expand All @@ -7,17 +8,22 @@


class MessageBuilder:
def __init__(self, groq_api_key: Optional[str], club_url: Optional[str] = None):
def __init__(self, groq_api_key: Optional[str], quote_prompts_path: Optional[Path] = None, club_url: Optional[str] = None):
"""
Initialize the MessageBuilder with the given GROQ API key.
Initialize the MessageBuilder.
Args:
groq_api_key (Optional[str]): The API key for accessing the GROQ service. Can be None if not required.
club_url (Optional[str], optional): The link to the Strava club. Defaults to None.
groq_api_key (str, optional): The API key for accessing the GROQ service. Can be None if not required.
quote_prompts_path (Path, optional): The path to the quote prompts file. If not provided, the default prompts are used. Defaults to None.
club_url (str, optional): The link to the Strava club. Defaults to None.
"""
self.groq_api_key = groq_api_key
self.club_url = club_url

data_path = resources.files(weekly_strava_stats).joinpath("data")
self.quote_prompts_path = quote_prompts_path if quote_prompts_path else data_path / "quote_prompts.csv"
self.default_prompt_path = data_path / "quotes.txt"

def build(self, stats: WeekStats, last_week_stats: Optional[WeekStats] = None) -> str:
"""
Builds a message string containing the current week's statistics and a quote.
Expand Down Expand Up @@ -118,7 +124,7 @@ def get_quote(self, original_message: str) -> str:
str: The retrieved or generated quote.
"""
if not self.groq_api_key:
return MessageBuilder.get_default_quote()
return self.get_default_quote()
return MessageBuilder.get_generated_quote(self, original_message)

def get_generated_quote(self, original_message: str) -> str:
Expand All @@ -129,8 +135,7 @@ def get_generated_quote(self, original_message: str) -> str:
Returns:
str: A generated quote formatted with the author's name and the quote in italics.
"""
with resources.path(weekly_strava_stats, 'data') as data_dir:
quotes_path = data_dir / "quote_prompts.csv"
quotes_path = self.quote_prompts_path

with open(quotes_path, 'r') as quotes_file:
# read without header (first line)
Expand All @@ -156,18 +161,14 @@ def get_generated_quote(self, original_message: str) -> str:
message = f"{author}: _{fake_quote}_"
return message

@staticmethod
def get_default_quote() -> str:
def get_default_quote(self) -> str:
"""
Retrieves a random quote from a predefined list of quotes stored in a text file.
Returns:
str: A randomly selected quote from the quotes file.
"""
with resources.path(weekly_strava_stats, 'data') as data_dir:
quotes_path = data_dir / "quotes.txt"

with open(quotes_path, 'r') as quotes_file:
with open(self.default_quote_path, 'r') as quotes_file:
default_quotes = quotes_file.readlines()

return f"_{random.choice(default_quotes)}_"

0 comments on commit 0b6c7d4

Please sign in to comment.