Add RAG basic implementation
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from estimator.config.settings import Settings
|
||||
|
||||
CONFIG = None # filled by the fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def workdir(tmp_path, monkeypatch):
|
||||
from pathlib import Path
|
||||
|
||||
repo_config = Path(__file__).resolve().parents[3] / "config"
|
||||
monkeypatch.chdir(tmp_path) # a different working directory, so a local .env can be written
|
||||
monkeypatch.setenv("EST_CONFIG_DIR", str(repo_config))
|
||||
return tmp_path
|
||||
|
||||
|
||||
def write_env(path, **pairs):
|
||||
(path / ".env").write_text("".join(f"{k}={v}\n" for k, v in pairs.items()))
|
||||
|
||||
|
||||
def test_variables_for_other_tools_in_the_same_env_file_are_ignored(workdir):
|
||||
write_env(
|
||||
workdir,
|
||||
POSTGRES_PASSWORD="abc", APP_PORT="8000", MIGRATE_ON_START="true", # docker compose and the entrypoint
|
||||
EST_DATABASE__DSN="sqlite:///x.db", EST_LLM__ANTHROPIC__API_KEY="k",
|
||||
) # fmt: skip
|
||||
s = Settings()
|
||||
assert s.database.dsn.get_secret_value() == "sqlite:///x.db"
|
||||
|
||||
|
||||
def test_a_typo_with_the_prefix_is_still_rejected(workdir):
|
||||
write_env(workdir, EST_DATABASE__DSN="sqlite:///x.db", EST_LLM__ANTRHOPIC__API_KEY="k")
|
||||
with pytest.raises(ValidationError):
|
||||
Settings()
|
||||
|
||||
|
||||
def test_the_real_environment_beats_the_env_file(workdir, monkeypatch):
|
||||
write_env(workdir, EST_DATABASE__DSN="sqlite:///from-file.db", EST_LLM__ANTHROPIC__API_KEY="k")
|
||||
monkeypatch.setenv("EST_DATABASE__DSN", "sqlite:///from-environment.db")
|
||||
assert Settings().database.dsn.get_secret_value() == "sqlite:///from-environment.db"
|
||||
|
||||
|
||||
def test_prefix_matching_is_case_insensitive_like_the_environment(workdir):
|
||||
write_env(workdir, est_database__dsn="sqlite:///lower.db", EST_LLM__ANTHROPIC__API_KEY="k")
|
||||
assert Settings().database.dsn.get_secret_value() == "sqlite:///lower.db"
|
||||
|
||||
|
||||
def test_the_shipped_env_example_loads_as_is(workdir):
|
||||
"""A user copies .env.example to .env: it must work without edits to its structure."""
|
||||
from pathlib import Path
|
||||
|
||||
example = Path(__file__).resolve().parents[3] / ".env.example"
|
||||
(workdir / ".env").write_text(example.read_text())
|
||||
s = Settings()
|
||||
assert s.database.dsn.get_secret_value().startswith("postgresql+psycopg://")
|
||||
assert s.api.key_map == {"key1": "client_a", "key2": "client_b"}
|
||||
assert s.web.enabled is False # the optional web section is commented out
|
||||
|
||||
@@ -160,3 +160,38 @@ def test_vat_can_be_reconfigured_for_another_regime(monkeypatch):
|
||||
_env(monkeypatch, EST_PRICING__VAT__DEFAULT_RATES='{"ru": 0.0, "eu": 0.0}')
|
||||
assert Settings().pricing.vat.default_rates["ru"] == 0.0
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------- RAG
|
||||
|
||||
|
||||
def test_rag_defaults_are_off_and_conservative(monkeypatch):
|
||||
_env(monkeypatch)
|
||||
r = Settings().rag
|
||||
assert (r.enabled, r.provider, r.model, r.top_k, r.min_similarity) == (False, "openai", "text-embedding-3-small", 3, 0.55)
|
||||
|
||||
|
||||
def test_enabled_rag_with_openai_needs_a_key(monkeypatch):
|
||||
_env(monkeypatch, EST_RAG__ENABLED="true")
|
||||
with pytest.raises(ValidationError, match="no API key"):
|
||||
Settings()
|
||||
|
||||
|
||||
def test_the_rag_key_falls_back_to_the_llm_openai_key(monkeypatch):
|
||||
_env(monkeypatch, EST_RAG__ENABLED="true", EST_LLM__OPENAI__API_KEY="sk-from-llm")
|
||||
s = Settings()
|
||||
assert s.rag_api_key() == "sk-from-llm" and "sk-from-llm" not in repr(s)
|
||||
_env(monkeypatch, EST_RAG__API_KEY="sk-own")
|
||||
assert Settings().rag_api_key() == "sk-own"
|
||||
|
||||
|
||||
def test_the_hash_provider_needs_no_key(monkeypatch):
|
||||
_env(monkeypatch, EST_RAG__ENABLED="true", EST_RAG__PROVIDER="hash")
|
||||
assert Settings().rag.provider == "hash"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key,value", [("TOP_K", "0"), ("TOP_K", "99"), ("MIN_SIMILARITY", "1.5"), ("BATCH_SIZE", "0"), ("PROVIDER", "cohere")])
|
||||
def test_rag_values_are_validated(monkeypatch, key, value):
|
||||
_env(monkeypatch, **{f"EST_RAG__{key}": value})
|
||||
with pytest.raises(ValidationError):
|
||||
Settings()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user