feat: Implement AI-powered embeddings and vector similarity search system

This commit is contained in:
Aherobo Ovie Victor
2025-07-07 18:45:10 +01:00
parent e188af8b17
commit 86d14ef472
3 changed files with 419 additions and 3 deletions
+87 -3
View File
@@ -8,6 +8,7 @@ import uvicorn
from config import settings
from news_fetcher import NewsFetcher
from recommender import NewsRecommender
from groq_integration import GroqLLMService
# Initialize FastAPI app
app = FastAPI(
@@ -28,6 +29,7 @@ app.add_middleware(
# Initialize components
news_fetcher = NewsFetcher()
recommender = NewsRecommender()
groq_service = GroqLLMService()
# Pydantic models
class NewsQuery(BaseModel):
@@ -211,19 +213,101 @@ async def get_stats():
"""Get system statistics"""
try:
stats = recommender.get_store_stats()
# Add RSS feed information
stats['rss_feeds'] = settings.rss_feeds
stats['embedding_model'] = settings.embedding_model
stats['groq_available'] = groq_service.is_available()
return {
"success": True,
"statistics": 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)}")
# Run the application
if __name__ == "__main__":
uvicorn.run(