diff options
| author | diogo464 <[email protected]> | 2025-07-24 09:35:15 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-07-24 09:35:15 +0100 |
| commit | 4220266ceae7b4fa018f4d5c7375d445bf9f7d49 (patch) | |
| tree | b5e4ada7f8d462f9c644dc1b3dcfe0a27e363ca8 /fetch-oneline-description | |
| parent | 346efd69254b9f5835241da5838f051ae7a6f2e3 (diff) | |
use openrouter api instead of llm cli tool
Diffstat (limited to 'fetch-oneline-description')
| -rwxr-xr-x | fetch-oneline-description | 60 |
1 files changed, 24 insertions, 36 deletions
diff --git a/fetch-oneline-description b/fetch-oneline-description index 731a34b84..8985ffce5 100755 --- a/fetch-oneline-description +++ b/fetch-oneline-description | |||
| @@ -2,38 +2,12 @@ | |||
| 2 | import os | 2 | import os |
| 3 | import sys | 3 | import sys |
| 4 | import json | 4 | import json |
| 5 | import subprocess | 5 | import requests |
| 6 | 6 | ||
| 7 | MODEL = "openrouter/anthropic/claude-3.5-haiku" | 7 | KEY = os.environ["OPENROUTER_KEY"] |
| 8 | MODEL = "anthropic/claude-3.5-haiku" | ||
| 8 | 9 | ||
| 9 | 10 | SYSTEM_PROMPT = """És um assistente especializado em condensar descrições de eventos de corrida em resumos de uma linha em português de Portugal. Deves extrair e resumir apenas a informação mais importante e relevante da descrição fornecida. | |
| 10 | class LLMClient: | ||
| 11 | """Client for LLM description generation.""" | ||
| 12 | |||
| 13 | def __init__(self, model: str): | ||
| 14 | self.model = model | ||
| 15 | |||
| 16 | def llm_call( | ||
| 17 | self, | ||
| 18 | system_prompt: str, | ||
| 19 | user_prompt: str, | ||
| 20 | ) -> str: | ||
| 21 | proc = subprocess.run( | ||
| 22 | ["llm", "-m", self.model, "-s", system_prompt, user_prompt], | ||
| 23 | timeout=30, | ||
| 24 | capture_output=True, | ||
| 25 | text=True, | ||
| 26 | ) | ||
| 27 | stdout = proc.stdout.strip() | ||
| 28 | stderr = proc.stderr.strip() | ||
| 29 | if proc.returncode != 0: | ||
| 30 | print(stderr) | ||
| 31 | proc.check_returncode() | ||
| 32 | return stdout | ||
| 33 | |||
| 34 | def generate_description(self, text: str) -> str: | ||
| 35 | """Generate short description using LLM.""" | ||
| 36 | system_prompt = """És um assistente especializado em condensar descrições de eventos de corrida em resumos de uma linha em português de Portugal. Deves extrair e resumir apenas a informação mais importante e relevante da descrição fornecida. | ||
| 37 | 11 | ||
| 38 | Exemplos de resumos que deves gerar: | 12 | Exemplos de resumos que deves gerar: |
| 39 | + Corrida histórica pelas ruas de Lisboa com vista para o Tejo | 13 | + Corrida histórica pelas ruas de Lisboa com vista para o Tejo |
| @@ -52,13 +26,27 @@ IMPORTANTE: | |||
| 52 | - Não menciones distâncias se já estão implícitas no tipo de evento | 26 | - Não menciones distâncias se já estão implícitas no tipo de evento |
| 53 | - Foca-te no que torna este evento único ou interessante""" | 27 | - Foca-te no que torna este evento único ou interessante""" |
| 54 | 28 | ||
| 55 | return self.llm_call( | 29 | |
| 56 | system_prompt, | 30 | def prompt(key: str, model: str, system: str, user: str) -> str: |
| 57 | text, | 31 | response = requests.post( |
| 58 | ) | 32 | url="https://openrouter.ai/api/v1/chat/completions", |
| 33 | headers={ | ||
| 34 | "Authorization": f"Bearer {key}", | ||
| 35 | }, | ||
| 36 | data=json.dumps( | ||
| 37 | { | ||
| 38 | "model": model, | ||
| 39 | "messages": [ | ||
| 40 | {"role": "system", "content": system}, | ||
| 41 | {"role": "user", "content": user}, | ||
| 42 | ], | ||
| 43 | } | ||
| 44 | ), | ||
| 45 | ).json() | ||
| 46 | print(response) | ||
| 47 | return response["choices"][0]["message"]["content"] | ||
| 59 | 48 | ||
| 60 | 49 | ||
| 61 | client = LLMClient(MODEL) | ||
| 62 | for slug in sys.argv[1:]: | 50 | for slug in sys.argv[1:]: |
| 63 | data_path = os.path.join("events", slug, "data.json") | 51 | data_path = os.path.join("events", slug, "data.json") |
| 64 | if not os.path.exists(data_path): | 52 | if not os.path.exists(data_path): |
| @@ -70,7 +58,7 @@ for slug in sys.argv[1:]: | |||
| 70 | oneline_path = os.path.join("events", slug, "oneline-description") | 58 | oneline_path = os.path.join("events", slug, "oneline-description") |
| 71 | if os.path.exists(oneline_path): | 59 | if os.path.exists(oneline_path): |
| 72 | continue | 60 | continue |
| 73 | oneline = client.generate_description(description) | 61 | oneline = prompt(KEY, MODEL, SYSTEM_PROMPT, description) |
| 74 | with open(oneline_path, "w") as f: | 62 | with open(oneline_path, "w") as f: |
| 75 | f.write(oneline) | 63 | f.write(oneline) |
| 76 | 64 | ||
