From e5992e583aca68bfe85fca8299d9559b121182d1 Mon Sep 17 00:00:00 2001 From: Aniketchavan7 <91446947+Aniketchavan7@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:16:55 +0530 Subject: [PATCH] feat(config): add environment template, runtime config validator, and tests Provides .env.example template for Tavily, Ollama, and OpenAI backends along with Pydantic runtime validator. Co-authored-by: yalongwastaken --- .env.example | 32 +++++++++++++++++++++ backend/config_validator.py | 51 ++++++++++++++++++++++++++++++++++ pyproject.toml | 4 +++ tests/test_config_validator.py | 17 ++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .env.example create mode 100644 backend/config_validator.py create mode 100644 tests/test_config_validator.py diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e0829b7 --- /dev/null +++ b/.env.example @@ -0,0 +1,32 @@ +# Spring Money AI Financial Agent Configuration + +# Search & Retriever Configuration +RETRIEVER=tavily +TAVILY_API_KEY=tvly-xxxxxxxxxxxxxxxxxxxx + +# LLM Provider Configuration (openai | ollama | google | azureopenai) +LLM_PROVIDER=ollama +FAST_LLM_MODEL=llama3:8b +SMART_LLM_MODEL=llama3:70b +LLM_BASE_URL=http://localhost:11434 + +# Embedding Configuration +EMBEDDING_PROVIDER=ollama +EMBEDDING_MODEL=nomic-embed-text + +# Fallback Cloud Providers (optional) +OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxx +GEMINI_API_KEY=AIzaxxxxxxxxxxxxxxxxxxxx + +# Agent Tuning Parameters +TEMPERATURE=0.55 +FAST_TOKEN_LIMIT=2000 +SMART_TOKEN_LIMIT=4000 +SUMMARY_TOKEN_LIMIT=700 +MAX_SEARCH_RESULTS_PER_QUERY=5 +MAX_ITERATIONS=3 +REPORT_FORMAT=APA + +# LangSmith Observability (optional) +LANGCHAIN_TRACING_V2=false +LANGCHAIN_API_KEY=lsv2-xxxxxxxxxxxxxxxxxxxx diff --git a/backend/config_validator.py b/backend/config_validator.py new file mode 100644 index 0000000..43434a4 --- /dev/null +++ b/backend/config_validator.py @@ -0,0 +1,51 @@ +"""Spring Money configuration validator and environment checker.""" + +import os +from typing import Dict, List, Optional +from pydantic import BaseModel, Field + + +class AgentRuntimeConfig(BaseModel): + retriever: str = Field(default="tavily", description="Default search retriever") + llm_provider: str = Field(default="ollama", description="LLM provider name") + fast_llm_model: str = Field(default="llama3:8b", description="Fast model identifier") + smart_llm_model: str = Field(default="llama3:70b", description="Reasoning model identifier") + llm_base_url: Optional[str] = Field(default=None, description="Local or private model endpoint") + temperature: float = Field(default=0.55, ge=0.0, le=2.0) + max_search_results: int = Field(default=5, ge=1, le=50) + report_format: str = Field(default="APA") + + +def validate_runtime_environment() -> Dict[str, any]: + """Inspect environment variables and return validated configuration and warnings.""" + warnings: List[str] = [] + + provider = os.getenv("LLM_PROVIDER", "ollama").lower() + if provider == "openai" and not os.getenv("OPENAI_API_KEY"): + warnings.append("OPENAI_API_KEY is not set while LLM_PROVIDER is configured to 'openai'") + elif provider == "google" and not os.getenv("GEMINI_API_KEY"): + warnings.append("GEMINI_API_KEY is not set while LLM_PROVIDER is configured to 'google'") + elif provider == "ollama" and not os.getenv("LLM_BASE_URL"): + # Default fallback to standard local Ollama port + os.environ["LLM_BASE_URL"] = "http://localhost:11434" + + retriever = os.getenv("RETRIEVER", "tavily").lower() + if retriever == "tavily" and not os.getenv("TAVILY_API_KEY"): + warnings.append("TAVILY_API_KEY is unset; live search queries will require mock fallback") + + config = AgentRuntimeConfig( + retriever=retriever, + llm_provider=provider, + fast_llm_model=os.getenv("FAST_LLM_MODEL", "llama3:8b"), + smart_llm_model=os.getenv("SMART_LLM_MODEL", "llama3:70b"), + llm_base_url=os.getenv("LLM_BASE_URL"), + temperature=float(os.getenv("TEMPERATURE", 0.55)), + max_search_results=int(os.getenv("MAX_SEARCH_RESULTS_PER_QUERY", 5)), + report_format=os.getenv("REPORT_FORMAT", "APA"), + ) + + return { + "valid": len(warnings) == 0, + "warnings": warnings, + "config": config.model_dump(), + } diff --git a/pyproject.toml b/pyproject.toml index ff92137..38aab03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,3 +43,7 @@ lxml = { version = ">=4.9.2", extras = ["html_clean"] } [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" + +[tool.pytest.ini_options] +pythonpath = ["."] +testpaths = ["tests"] diff --git a/tests/test_config_validator.py b/tests/test_config_validator.py new file mode 100644 index 0000000..d8722e8 --- /dev/null +++ b/tests/test_config_validator.py @@ -0,0 +1,17 @@ +import os +from backend.config_validator import validate_runtime_environment, AgentRuntimeConfig + + +def test_default_config_generation(): + """Ensure default configuration creates valid instance.""" + result = validate_runtime_environment() + assert "config" in result + assert result["config"]["retriever"] in ["tavily", "google", "bing"] + assert result["config"]["temperature"] >= 0.0 + + +def test_agent_runtime_config_bounds(): + """Verify pydantic bounds for configuration.""" + cfg = AgentRuntimeConfig(temperature=0.8, max_search_results=10) + assert cfg.temperature == 0.8 + assert cfg.max_search_results == 10