|
25 | 25 | from concurrent.futures import ThreadPoolExecutor, as_completed |
26 | 26 |
|
27 | 27 | import requests |
28 | | -from utils import USER_AGENT |
29 | | - |
30 | | -SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
31 | | -REPO_ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR)) |
32 | | -LLMS_SH = os.path.join(REPO_ROOT, "llms.sh") |
33 | | -LLMS_MODEL = os.getenv("LLMS_MODEL", "moonshotai/kimi-k2.5") |
| 28 | +from utils import SCRIPT_DIR, REPO_ROOT, LLMS_SH, LLMS_ANALYTICS_MODEL, USER_AGENT, parse_json_response |
34 | 29 |
|
35 | 30 | HN_API = "https://hacker-news.firebaseio.com/v0/item/{}.json" |
36 | 31 | SESSION = requests.Session() |
@@ -200,25 +195,6 @@ def comments_to_text(comments: list[dict], max_comments: int = 200, max_chars: i |
200 | 195 | - Keep the total output under 500 words |
201 | 196 | - Return ONLY valid JSON""" |
202 | 197 |
|
203 | | - |
204 | | -def parse_json_response(text: str) -> dict: |
205 | | - """Parse JSON from an LLM response.""" |
206 | | - try: |
207 | | - return json.loads(text) |
208 | | - except json.JSONDecodeError: |
209 | | - pass |
210 | | - cleaned = re.sub(r"^```(?:json)?\s*", "", text.strip()) |
211 | | - cleaned = re.sub(r"\s*```$", "", cleaned) |
212 | | - try: |
213 | | - return json.loads(cleaned) |
214 | | - except json.JSONDecodeError: |
215 | | - pass |
216 | | - match = re.search(r"(\{[\s\S]*\})", text) |
217 | | - if match: |
218 | | - return json.loads(match.group(1)) |
219 | | - raise ValueError("Could not parse JSON from LLM response") |
220 | | - |
221 | | - |
222 | 198 | def analyze_sentiment(post_title: str, comments_text: str, model: str) -> str: |
223 | 199 | """Use LLM to generate sentiment analysis markdown.""" |
224 | 200 | user_message = f"Post Title: {post_title}\n\n--- COMMENTS ---\n{comments_text}" |
@@ -260,7 +236,7 @@ def analyze_sentiment(post_title: str, comments_text: str, model: str) -> str: |
260 | 236 | def main(): |
261 | 237 | parser = argparse.ArgumentParser(description="Analyze comments from a Hacker News post.") |
262 | 238 | parser.add_argument("url", help="HN comments URL or item ID (e.g. https://news.ycombinator.com/item?id=46978710)") |
263 | | - parser.add_argument("--model", default=LLMS_MODEL, help=f"Model name (default: {LLMS_MODEL})") |
| 239 | + parser.add_argument("--model", default=LLMS_ANALYTICS_MODEL, help=f"Model name (default: {LLMS_ANALYTICS_MODEL})") |
264 | 240 | parser.add_argument( |
265 | 241 | "--max-chars", type=int, default=30000, help="Max chars of comments to send to LLM (default: 30000)" |
266 | 242 | ) |
|
0 commit comments