Refactor backend configuration and enhance news fetching functionality
- Introduced a Config dataclass in config.py to manage API keys, RSS feeds, and directory paths more effectively. - Updated the NewsFetcher class to include retry logic for fetching articles from RSS feeds. - Modified the EmbeddingGenerator and NewsRecommender classes to utilize the new configuration structure. - Enhanced main.py to implement API token verification for secure access to news fetching and recommendations.
This commit is contained in:
+9
-14
@@ -1,16 +1,11 @@
|
||||
from pinecone import Pinecone, ServerlessSpec
|
||||
from typing import List, Dict, Any
|
||||
from config import (
|
||||
PINECONE_API_KEY,
|
||||
PINECONE_INDEX_NAME,
|
||||
VECTOR_DIMENSION,
|
||||
TOP_K_RESULTS
|
||||
)
|
||||
from typing import List, Dict, Any, Optional
|
||||
from config import config
|
||||
|
||||
class VectorStore:
|
||||
def __init__(self):
|
||||
self.pinecone = Pinecone(api_key=PINECONE_API_KEY)
|
||||
self.index_name = PINECONE_INDEX_NAME
|
||||
def __init__(self, pinecone_client: Optional[Pinecone] = None):
|
||||
self.pinecone = pinecone_client or Pinecone(api_key=config.pinecone_api_key)
|
||||
self.index_name = config.pinecone_index_name
|
||||
self._ensure_index()
|
||||
|
||||
def _ensure_index(self):
|
||||
@@ -20,11 +15,11 @@ class VectorStore:
|
||||
# Create a new index with the correct dimension
|
||||
self.pinecone.create_index(
|
||||
name=self.index_name,
|
||||
dimension=VECTOR_DIMENSION,
|
||||
dimension=config.vector_dimension,
|
||||
metric="cosine",
|
||||
spec=ServerlessSpec(cloud="aws", region="us-east-1")
|
||||
)
|
||||
print(f"Created new index '{self.index_name}' with dimension {VECTOR_DIMENSION}")
|
||||
print(f"Created new index '{self.index_name}' with dimension {config.vector_dimension}")
|
||||
|
||||
self.index = self.pinecone.Index(self.index_name)
|
||||
|
||||
@@ -57,12 +52,12 @@ class VectorStore:
|
||||
print(f"Error upserting articles: {str(e)}")
|
||||
return False
|
||||
|
||||
def search_similar(self, query_embedding: List[float], top_k: int = TOP_K_RESULTS) -> List[Dict[str, Any]]:
|
||||
def search_similar(self, query_embedding: List[float], top_k: int = None) -> List[Dict[str, Any]]:
|
||||
"""Search for similar articles using the query embedding."""
|
||||
try:
|
||||
results = self.index.query(
|
||||
vector=query_embedding,
|
||||
top_k=top_k,
|
||||
top_k=top_k or config.top_k_results,
|
||||
include_metadata=True
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user