Download tests/integration/test_processing_with_embeddings.py from Necthrall/AI: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/spaces/Necthrall/AI/resolve/main/tests/integration/test_processing_with_embeddings.py
- Command line
-
hf download hf://spaces/Necthrall/AI/tests/integration/test_processing_with_embeddings.py
-
curl -L -o test_processing_with_embeddings.py https://huggingface.co/spaces/Necthrall/AI/resolve/main/tests/integration/test_processing_with_embeddings.py
5.59 kB
| import time | |
| from typing import List | |
| import numpy as np | |
| import pytest | |
| from agents.processing_agent import ProcessingAgent | |
| from models.state import Passage, State | |
| class DummyEmbedModel: | |
| """Deterministic dummy embedding model that returns 384-dim vectors.""" | |
| def embed_documents(self, texts: List[str]) -> List[List[float]]: | |
| out = [] | |
| for i, t in enumerate(texts): | |
| # produce simple pattern-based vector for determinism | |
| vec = [(i + 1) * 0.001 for _ in range(384)] | |
| out.append(vec) | |
| return out | |
| def _make_text(tokens: int) -> str: | |
| return "word " * tokens | |
| def test_three_papers_get_384_embeddings(): | |
| passages = [] | |
| for i in range(3): | |
| passages.append( | |
| Passage( | |
| paper_id=f"p{i}", | |
| text=_make_text(600), | |
| metadata={"title": f"Paper {i}"}, | |
| ) | |
| ) | |
| state = State(query="test", passages=passages) | |
| agent = ProcessingAgent(chunk_size=200, chunk_overlap=20) | |
| processed = agent.process(state, embedding_model=DummyEmbedModel(), batch_size=32) | |
| assert processed.chunks is not None | |
| assert len(processed.chunks) > 0 | |
| for c in processed.chunks: | |
| emb = c.metadata.get("embedding") | |
| assert emb is not None, "Missing embedding on chunk" | |
| arr = np.asarray(emb) | |
| assert arr.shape == (384,) | |
| def test_chunking_failure_skips_embedding(monkeypatch): | |
| passages = [ | |
| Passage( | |
| paper_id="bad", | |
| text=_make_text(600), | |
| metadata={"title": "Bad Paper"}, | |
| ), | |
| Passage( | |
| paper_id="good", | |
| text=_make_text(600), | |
| metadata={"title": "Good Paper"}, | |
| ), | |
| ] | |
| state = State(query="test2", passages=passages) | |
| agent = ProcessingAgent(chunk_size=200, chunk_overlap=20) | |
| orig_parser = agent.splitter | |
| class BrokenParser: | |
| def __init__(self, delegate): | |
| self._delegate = delegate | |
| self._first = True | |
| def get_nodes_from_documents(self, docs): | |
| if self._first: | |
| self._first = False | |
| raise RuntimeError("simulated chunker failure") | |
| return self._delegate.get_nodes_from_documents(docs) | |
| agent.splitter = BrokenParser(orig_parser) | |
| processed = agent.process(state, embedding_model=DummyEmbedModel(), batch_size=16) | |
| # Ensure we still processed the good paper's chunks | |
| assert processed.chunks is not None | |
| assert any(c.metadata.get("paper_id") == "good" for c in processed.chunks) | |
| # Ensure no exception propagated and state.errors captured a message (or at least empty list allowed) | |
| assert isinstance(processed.errors, list) | |
| def test_metadata_preserved_through_embedding(): | |
| passages = [ | |
| Passage( | |
| paper_id="meta1", | |
| text=_make_text(300), | |
| metadata={"title": "Meta Paper"}, | |
| ) | |
| ] | |
| state = State(query="meta", passages=passages) | |
| agent = ProcessingAgent(chunk_size=100, chunk_overlap=10) | |
| processed = agent.process(state, embedding_model=DummyEmbedModel(), batch_size=8) | |
| assert processed.chunks is not None | |
| for idx, c in enumerate(processed.chunks): | |
| assert c.metadata.get("paper_id") == "meta1" | |
| assert "chunk_index" in c.metadata | |
| # embedding still present | |
| emb = c.metadata.get("embedding") | |
| assert emb is not None | |
| def test_end_to_end_timing_for_five_papers_is_fast(): | |
| passages = [] | |
| for i in range(5): | |
| passages.append( | |
| Passage( | |
| paper_id=f"t{i}", | |
| text=_make_text(400), | |
| metadata={"title": f"T{i}"}, | |
| ) | |
| ) | |
| state = State(query="timer", passages=passages) | |
| agent = ProcessingAgent(chunk_size=200, chunk_overlap=20) | |
| start = time.perf_counter() | |
| processed = agent.process(state, embedding_model=DummyEmbedModel(), batch_size=64) | |
| elapsed = time.perf_counter() - start | |
| assert elapsed < 6.0, f"Processing too slow: {elapsed:.2f}s" | |
| assert processed.chunks is not None | |
| assert len(processed.chunks) > 0 | |
| def test_embedding_retry_succeeds_after_failures(): | |
| """Simulate an embedding backend that fails a couple of times before succeeding.""" | |
| class FlakyEmbedModel: | |
| def __init__(self, fail_times: int = 2): | |
| self.calls = 0 | |
| self.fail_times = fail_times | |
| def embed_documents(self, texts: List[str]) -> List[List[float]]: | |
| self.calls += 1 | |
| if self.calls <= self.fail_times: | |
| raise RuntimeError("simulated embed service outage") | |
| # On success, return deterministic vectors | |
| return [[0.1 for _ in range(384)] for _ in range(len(texts))] | |
| passages = [ | |
| Passage( | |
| paper_id="r1", | |
| text=_make_text(300), | |
| metadata={"title": "Retry Paper"}, | |
| ) | |
| for _ in range(1) | |
| ] | |
| state = State(query="retry", passages=passages) | |
| agent = ProcessingAgent(chunk_size=100, chunk_overlap=10) | |
| model = FlakyEmbedModel(fail_times=2) | |
| processed = agent.process(state, embedding_model=model, batch_size=8) | |
| assert processed.chunks is not None | |
| # Ensure model was called multiple times (retries) and eventually succeeded | |
| assert model.calls >= 2 | |
| for c in processed.chunks: | |
| emb = c.metadata.get("embedding") | |
| assert emb is not None | |
| assert len(emb) == 384 | |