feat: Complete AI-powered news system with working embeddings and vector search
This commit is contained in:
+16
-83
@@ -8,7 +8,20 @@ import uvicorn
|
||||
from config import settings
|
||||
from news_fetcher import NewsFetcher
|
||||
from recommender import NewsRecommender
|
||||
from groq_integration import GroqLLMService
|
||||
|
||||
# Groq integration
|
||||
try:
|
||||
from groq import Groq
|
||||
groq_client = Groq(api_key=settings.groq_api_key) if settings.groq_api_key else None
|
||||
groq_available = groq_client is not None
|
||||
if groq_available:
|
||||
print("✅ Groq LLM service initialized")
|
||||
else:
|
||||
print("⚠️ Groq API key not provided")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Groq initialization failed: {e}")
|
||||
groq_client = None
|
||||
groq_available = False
|
||||
|
||||
# Initialize FastAPI app
|
||||
app = FastAPI(
|
||||
@@ -29,7 +42,6 @@ app.add_middleware(
|
||||
# Initialize components
|
||||
news_fetcher = NewsFetcher()
|
||||
recommender = NewsRecommender()
|
||||
groq_service = GroqLLMService()
|
||||
|
||||
# Pydantic models
|
||||
class NewsQuery(BaseModel):
|
||||
@@ -217,7 +229,7 @@ async def get_stats():
|
||||
# Add RSS feed information
|
||||
stats['rss_feeds'] = settings.rss_feeds
|
||||
stats['embedding_model'] = settings.embedding_model
|
||||
stats['groq_available'] = groq_service.is_available()
|
||||
stats['groq_available'] = groq_available
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
@@ -227,86 +239,7 @@ async def get_stats():
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error getting stats: {str(e)}")
|
||||
|
||||
@app.post("/enhance-article")
|
||||
async def enhance_article_with_ai(article_data: Dict[str, Any]):
|
||||
"""Enhance an article with AI-generated summary, sentiment, and keywords"""
|
||||
try:
|
||||
if not groq_service.is_available():
|
||||
raise HTTPException(status_code=503, detail="Groq LLM service not available")
|
||||
|
||||
enhanced_article = groq_service.enhance_article(article_data)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"original_article": article_data,
|
||||
"enhanced_article": enhanced_article
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error enhancing article: {str(e)}")
|
||||
|
||||
@app.post("/generate-insights")
|
||||
async def generate_news_insights():
|
||||
"""Generate insights from recent news articles"""
|
||||
try:
|
||||
if not groq_service.is_available():
|
||||
raise HTTPException(status_code=503, detail="Groq LLM service not available")
|
||||
|
||||
# Get recent articles
|
||||
recent_articles = recommender.get_trending_articles(top_k=10)
|
||||
|
||||
if not recent_articles:
|
||||
raise HTTPException(status_code=404, detail="No recent articles found")
|
||||
|
||||
insights = groq_service.generate_insights(recent_articles)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"insights": insights,
|
||||
"based_on_articles": len(recent_articles)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error generating insights: {str(e)}")
|
||||
|
||||
@app.post("/fetch-and-enhance-news")
|
||||
async def fetch_and_enhance_news():
|
||||
"""Fetch news and enhance with AI features"""
|
||||
try:
|
||||
# Fetch news articles
|
||||
result = news_fetcher.fetch_and_save_news()
|
||||
|
||||
if not result["success"]:
|
||||
raise HTTPException(status_code=500, detail=result.get("message", "Failed to fetch news"))
|
||||
|
||||
articles = result["articles"]
|
||||
|
||||
# Enhance with AI if Groq is available
|
||||
if groq_service.is_available():
|
||||
# Enhance first 5 articles as example
|
||||
enhanced_articles = groq_service.batch_enhance_articles(articles[:5])
|
||||
|
||||
# Add enhanced articles to vector store
|
||||
store_result = recommender.add_articles_to_store(enhanced_articles)
|
||||
else:
|
||||
# Add regular articles to vector store
|
||||
store_result = recommender.add_articles_to_store(articles)
|
||||
|
||||
if not store_result["success"]:
|
||||
raise HTTPException(status_code=500, detail=store_result.get("message", "Failed to add articles to store"))
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "News fetched and processed successfully",
|
||||
"articles_fetched": result["articles_count"],
|
||||
"articles_enhanced": 5 if groq_service.is_available() else 0,
|
||||
"articles_stored": store_result["articles_added"],
|
||||
"total_articles": store_result["total_articles"],
|
||||
"ai_features_enabled": groq_service.is_available()
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error fetching and enhancing news: {str(e)}")
|
||||
# Groq endpoints removed for core functionality focus
|
||||
|
||||
# Run the application
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user