From 63dd4995224db0540be1bca7a8f8d463483f8e43 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Wed, 1 Oct 2025 18:08:25 -0700 Subject: migrate summarizer, tl, wac modules to cache using db --- summarizer.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) (limited to 'summarizer.py') diff --git a/summarizer.py b/summarizer.py index 8fe86ae..25d1f8f 100644 --- a/summarizer.py +++ b/summarizer.py @@ -1,4 +1,5 @@ from dotenv import load_dotenv +from database import Database import openai import json import hashlib @@ -11,21 +12,6 @@ def summarization_is_possible() -> bool: return os.getenv("OPENAI_API_KEY") -def _load_cache(): - cache_file = "summarization_cache.json" - if not os.path.exists(cache_file): - with open(cache_file, "w") as file: - json.dump({}, file) - with open(cache_file, "r") as file: - return json.load(file) - - -def _save_cache(cache: dict): - cache_file = "summarization_cache.json" - with open(cache_file, "w") as file: - json.dump(cache, file) - - def _make_cache_key(game: str, img_urls: list[str]) -> str: normalized_game = game.strip().lower() img_data = json.dumps(sorted(img_urls), separators=(",", ":")) @@ -40,12 +26,11 @@ def generate_headline_and_content_from_images(img_urls: list[str], game: str, me # Limit message content to 500 characters if len(message_content) > MAX_CHAR_CONTENT_CONSIDERATION_LENGTH: message_content = message_content[:MAX_CHAR_CONTENT_CONSIDERATION_LENGTH] - - cache = _load_cache() + database = Database() cache_key = _make_cache_key(game, img_urls) - if cache_key in cache: - cached = cache[cache_key] - return cached["headline"], cached["content"] + cache_entry = database.get_summary(cache_key) + if cache_entry: + return cache_entry["headline"], cache_entry["content"] tools = [ { "type": "function", @@ -100,9 +85,10 @@ def generate_headline_and_content_from_images(img_urls: list[str], game: str, me parsed_result = json.loads(tool_result) headline = parsed_result["headline"] content = parsed_result["content"] - cache[cache_key] = {"headline": headline, "content": content} - _save_cache(cache) + database.add_new_summary(cache_key, headline, content) + database.close() except openai.OpenAIError as e: print(f"[ERROR] Function call to OpenAI for summarization failed ERROR -> {e} ") + database.close() return None, None return headline, content -- cgit v1.2.3