Spaces:
Sleeping
Sleeping
File size: 7,399 Bytes
266d7bc 804054e 266d7bc 804054e 266d7bc 804054e 266d7bc 804054e 266d7bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
import os
from typing import ClassVar
import yaml
from pydantic import BaseModel, Field, SecretStr, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from src.models.article_models import FeedItem
# -----------------------------
# Supabase database settings
# -----------------------------
class SupabaseDBSettings(BaseModel):
table_name: str = Field(default="feed_articles", description="Supabase table name")
host: str = Field(default="localhost", description="Database host")
name: str = Field(default="postgres", description="Database name")
user: str = Field(default="postgres", description="Database user")
password: SecretStr = Field(default=SecretStr("password"), description="Database password")
port: int = Field(default=6543, description="Database port")
test_database: str = Field(default="feed_test", description="Test database name")
# -----------------------------
# RSS settings
# -----------------------------
class RSSSettings(BaseModel):
feeds: list[FeedItem] = Field(
default_factory=list[FeedItem], description="List of RSS feed items"
)
default_start_date: str = Field(default="2025-09-15", description="Default cutoff date")
batch_size: int = Field(
default=5, description="Number of articles to parse and ingest in a batch"
)
# -----------------------------
# Qdrant settings
# -----------------------------
# BAAI/bge-large-en-v1.5 (1024), BAAI/bge-base-en-v1.5 (HF, 768). BAAI/bge-base-en (Fastembed, 768)
class QdrantSettings(BaseModel):
url: str = Field(default="", description="Qdrant API URL")
api_key: str = Field(default="", description="Qdrant API key")
timeout: int = Field(default=30, description="Qdrant client timeout")
collection_name: str = Field(
default="feed_collection", description="Qdrant collection name"
)
dense_model_name: str = Field(default="BAAI/bge-base-en", description="Dense model name")
sparse_model_name: str = Field(
default="Qdrant/bm25", description="Sparse model name"
) # prithivida/Splade_PP_en_v1 (larger)
vector_dim: int = Field(
default=768,
description="Vector dimension", # 768, 1024 with Jina or large HF
)
article_batch_size: int = Field(
default=5, description="Number of articles to parse and ingest in a batch"
)
sparse_batch_size: int = Field(default=32, description="Sparse batch size")
embed_batch_size: int = Field(default=50, description="Dense embedding batch")
upsert_batch_size: int = Field(default=25, description="Batch size for Qdrant upsert")
max_concurrent: int = Field(default=2, description="Maximum number of concurrent tasks")
# -----------------------------
# Text splitting
# -----------------------------
class TextSplitterSettings(BaseModel):
chunk_size: int = Field(default=4000, description="Size of text chunks")
chunk_overlap: int = Field(default=200, description="Size of text chunks")
separators: list[str] = Field(
default_factory=lambda: [
"\n---\n",
"\n\n",
"\n```\n",
"\n## ",
"\n# ",
"\n**",
"\n",
". ",
"! ",
"? ",
" ",
"",
],
description="List of separators for text splitting. The order or separators matter",
)
# -----------------------------
# Jina Settings
# -----------------------------
class JinaSettings(BaseModel):
api_key: str = Field(default="", description="Jina API key")
url: str = Field(default="https://api.jina.ai/v1/embeddings", description="Jina API URL")
model: str = Field(default="jina-embeddings-v3", description="Jina model name") # 1024
# -----------------------------
# Hugging Face Settings
# -----------------------------
# BAAI/bge-large-en-v1.5 (1024), BAAI/bge-base-en-v1.5 (768)
class HuggingFaceSettings(BaseModel):
api_key: str = Field(default="", description="Hugging Face API key")
model: str = Field(default="BAAI/bge-base-en-v1.5", description="Hugging Face model name")
# -----------------------------
# Openai Settings
# -----------------------------
class OpenAISettings(BaseModel):
api_key: str | None = Field(default="", description="OpenAI API key")
# model: str = Field(default="gpt-4o-mini", description="OpenAI model name")
# -----------------------------
# OpenRouter Settings
# -----------------------------
class OpenRouterSettings(BaseModel):
api_key: str = Field(default="", description="OpenRouter API key")
api_url: str = Field(default="https://openrouter.ai/api/v1", description="OpenRouter API URL")
# -----------------------------
# Opik Observability Settings
# -----------------------------
class OpikObservabilitySettings(BaseModel):
api_key: str = Field(default="", description="Opik Observability API key")
project_name: str = Field(default="feed-pipeline", description="Opik project name")
# -----------------------------
# YAML loader
# -----------------------------
def load_yaml_feeds(path: str) -> list[FeedItem]:
"""
Load RSS feed items from a YAML file.
If the file does not exist or is empty, returns an empty list.
Args:
path (str): Path to the YAML file.
Returns:
list[FeedItem]: List of FeedItem instances loaded from the file.
"""
if not os.path.exists(path):
return []
with open(path, encoding="utf-8") as f:
data = yaml.safe_load(f)
feed_list = data.get("feeds", [])
return [FeedItem(**feed) for feed in feed_list]
# -----------------------------
# Main Settings
# -----------------------------
class Settings(BaseSettings):
supabase_db: SupabaseDBSettings = Field(default_factory=SupabaseDBSettings)
qdrant: QdrantSettings = Field(default_factory=QdrantSettings)
rss: RSSSettings = Field(default_factory=RSSSettings)
text_splitter: TextSplitterSettings = Field(default_factory=TextSplitterSettings)
jina: JinaSettings = Field(default_factory=JinaSettings)
hugging_face: HuggingFaceSettings = Field(default_factory=HuggingFaceSettings)
openai: OpenAISettings = Field(default_factory=OpenAISettings)
openrouter: OpenRouterSettings = Field(default_factory=OpenRouterSettings)
opik: OpikObservabilitySettings = Field(default_factory=OpikObservabilitySettings)
rss_config_yaml_path: str = "src/configs/feeds_rss.yaml"
# Pydantic v2 model config
model_config: ClassVar[SettingsConfigDict] = SettingsConfigDict(
env_file=[".env"],
env_file_encoding="utf-8",
extra="ignore",
env_nested_delimiter="__",
case_sensitive=False,
frozen=True,
)
@model_validator(mode="after")
def load_yaml_rss_feeds(self) -> "Settings":
"""
Load RSS feeds from a YAML file after model initialization.
If the file does not exist or is empty, the feeds list remains unchanged.
Args:
self (Settings): The settings instance.
Returns:
Settings: The updated settings instance.
"""
yaml_feeds = load_yaml_feeds(self.rss_config_yaml_path)
if yaml_feeds:
self.rss.feeds = yaml_feeds
return self
# -----------------------------
# Instantiate settings
# -----------------------------
settings = Settings()
|