func clean up
Browse files
components/generators/daily_feed.py
CHANGED
@@ -15,7 +15,7 @@ Settings.llm = None
|
|
15 |
# π Environment variables
|
16 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
17 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
18 |
-
|
19 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
20 |
|
21 |
# β
Redis client
|
@@ -46,32 +46,36 @@ def build_prompt(content: str, topic: str) -> str:
|
|
46 |
return f"<s>[INST]{base_instruction}\n\n{tail}[/INST]</s>"
|
47 |
|
48 |
# π Call Mistral using HF Inference Endpoint
|
49 |
-
def call_mistral(prompt: str) -> str:
|
|
|
|
|
|
|
|
|
50 |
payload = {
|
51 |
-
"inputs": prompt
|
52 |
-
"parameters": {
|
53 |
-
"max_new_tokens": 128,
|
54 |
-
"temperature": 0.7,
|
55 |
-
},
|
56 |
}
|
|
|
57 |
try:
|
58 |
-
response = requests.post(
|
59 |
response.raise_for_status()
|
60 |
data = response.json()
|
61 |
|
|
|
62 |
if isinstance(data, list) and data:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
print("β HF Endpoint error:", str(e))
|
69 |
-
if e.response is not None:
|
70 |
-
print("Endpoint said:", e.response.text[:300])
|
71 |
-
except Exception as e:
|
72 |
-
print("β Unknown error:", str(e))
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
# βοΈ Summarize top N documents
|
77 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
|
|
15 |
# π Environment variables
|
16 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
17 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
18 |
+
MISTRAL_URL = os.environ.get("MISTRAL_URL")
|
19 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
20 |
|
21 |
# β
Redis client
|
|
|
46 |
return f"<s>[INST]{base_instruction}\n\n{tail}[/INST]</s>"
|
47 |
|
48 |
# π Call Mistral using HF Inference Endpoint
|
49 |
+
def call_mistral(prompt: str) -> Optional[str]:
|
50 |
+
headers = {
|
51 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
52 |
+
"Content-Type": "application/json"
|
53 |
+
}
|
54 |
payload = {
|
55 |
+
"inputs": prompt
|
|
|
|
|
|
|
|
|
56 |
}
|
57 |
+
|
58 |
try:
|
59 |
+
response = requests.post(MISTRAL_URL, headers=headers, json=payload, timeout=20)
|
60 |
response.raise_for_status()
|
61 |
data = response.json()
|
62 |
|
63 |
+
# Get the generated text
|
64 |
if isinstance(data, list) and data:
|
65 |
+
raw_output = data[0].get("generated_text", "")
|
66 |
+
elif isinstance(data, dict):
|
67 |
+
raw_output = data.get("generated_text", "")
|
68 |
+
else:
|
69 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
# β
Extract only the portion after the [/INST]</s> marker
|
72 |
+
if "[/INST]</s>" in raw_output:
|
73 |
+
return raw_output.split("[/INST]</s>")[-1].strip()
|
74 |
+
return raw_output.strip()
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
print(f"β οΈ Mistral error: {e}")
|
78 |
+
return None
|
79 |
|
80 |
# βοΈ Summarize top N documents
|
81 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|