once more
Browse files
components/generators/daily_feed.py
CHANGED
@@ -7,34 +7,34 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
7 |
|
8 |
import redis
|
9 |
from typing import List, Dict
|
10 |
-
from llama_index.core.settings import Settings
|
11 |
from llama_index.core import VectorStoreIndex
|
12 |
from llama_index.core.query_engine import RetrieverQueryEngine
|
13 |
from llama_index.core.schema import Document
|
14 |
-
from
|
15 |
|
|
|
16 |
Settings.llm = None
|
17 |
|
18 |
-
# Load environment variables
|
19 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
20 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
21 |
-
MISTRAL_URL = os.environ.get("MISTRAL_URL")
|
22 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
23 |
|
24 |
-
# Connect to Redis
|
25 |
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
26 |
|
27 |
-
# Topics to query
|
28 |
TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
|
29 |
|
30 |
-
# Prompt
|
31 |
def build_prompt(content: str, topic: str) -> str:
|
32 |
return (
|
33 |
f"You are a news summarizer. Summarize the following content in 25-30 words. "
|
34 |
f"Make it engaging and informative. Include appropriate emojis. Topic: {topic}\n\n{content}"
|
35 |
)
|
36 |
|
37 |
-
# Call Mistral
|
38 |
def call_mistral(prompt: str) -> str:
|
39 |
headers = {
|
40 |
"Authorization": f"Bearer {HF_TOKEN}",
|
@@ -53,7 +53,7 @@ def call_mistral(prompt: str) -> str:
|
|
53 |
print(f"β οΈ Mistral error: {e}")
|
54 |
return None
|
55 |
|
56 |
-
#
|
57 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
58 |
feed = []
|
59 |
for doc in docs[:5]:
|
@@ -67,10 +67,11 @@ def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
|
67 |
})
|
68 |
return feed
|
69 |
|
70 |
-
# Main
|
71 |
def generate_and_cache_daily_feed(documents: List[Document]):
|
72 |
index = VectorStoreIndex.from_documents(documents)
|
73 |
-
|
|
|
74 |
|
75 |
final_feed = []
|
76 |
for topic in TOPICS:
|
@@ -84,13 +85,12 @@ def generate_and_cache_daily_feed(documents: List[Document]):
|
|
84 |
"feed": topic_feed
|
85 |
})
|
86 |
|
87 |
-
# Cache to Redis
|
88 |
redis_client.set(REDIS_KEY, json.dumps(final_feed, ensure_ascii=False))
|
89 |
print(f"β
Cached daily feed under key '{REDIS_KEY}'")
|
90 |
return final_feed
|
91 |
|
92 |
-
# Redis fetch for
|
93 |
-
|
94 |
def get_cached_daily_feed():
|
95 |
cached = redis_client.get(REDIS_KEY)
|
96 |
return json.loads(cached) if cached else []
|
|
|
7 |
|
8 |
import redis
|
9 |
from typing import List, Dict
|
|
|
10 |
from llama_index.core import VectorStoreIndex
|
11 |
from llama_index.core.query_engine import RetrieverQueryEngine
|
12 |
from llama_index.core.schema import Document
|
13 |
+
from llama_index.core.settings import Settings
|
14 |
|
15 |
+
# β
Disable OpenAI LLM fallback
|
16 |
Settings.llm = None
|
17 |
|
18 |
+
# π Load environment variables
|
19 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
20 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
21 |
+
MISTRAL_URL = os.environ.get("MISTRAL_URL") # Mistral inference endpoint
|
22 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face access token
|
23 |
|
24 |
+
# β
Connect to Redis
|
25 |
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
26 |
|
27 |
+
# π Topics to query and summarize
|
28 |
TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
|
29 |
|
30 |
+
# π§ Prompt builder for summarization
|
31 |
def build_prompt(content: str, topic: str) -> str:
|
32 |
return (
|
33 |
f"You are a news summarizer. Summarize the following content in 25-30 words. "
|
34 |
f"Make it engaging and informative. Include appropriate emojis. Topic: {topic}\n\n{content}"
|
35 |
)
|
36 |
|
37 |
+
# π Call Mistral API
|
38 |
def call_mistral(prompt: str) -> str:
|
39 |
headers = {
|
40 |
"Authorization": f"Bearer {HF_TOKEN}",
|
|
|
53 |
print(f"β οΈ Mistral error: {e}")
|
54 |
return None
|
55 |
|
56 |
+
# βοΈ Summarize a list of documents into a short news feed
|
57 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
58 |
feed = []
|
59 |
for doc in docs[:5]:
|
|
|
67 |
})
|
68 |
return feed
|
69 |
|
70 |
+
# π Main pipeline
|
71 |
def generate_and_cache_daily_feed(documents: List[Document]):
|
72 |
index = VectorStoreIndex.from_documents(documents)
|
73 |
+
retriever = index.as_retriever()
|
74 |
+
query_engine = RetrieverQueryEngine(retriever=retriever)
|
75 |
|
76 |
final_feed = []
|
77 |
for topic in TOPICS:
|
|
|
85 |
"feed": topic_feed
|
86 |
})
|
87 |
|
88 |
+
# πΎ Cache to Redis
|
89 |
redis_client.set(REDIS_KEY, json.dumps(final_feed, ensure_ascii=False))
|
90 |
print(f"β
Cached daily feed under key '{REDIS_KEY}'")
|
91 |
return final_feed
|
92 |
|
93 |
+
# π§ͺ Redis fetch (for use in APIs)
|
|
|
94 |
def get_cached_daily_feed():
|
95 |
cached = redis_client.get(REDIS_KEY)
|
96 |
return json.loads(cached) if cached else []
|