Skip to content

Commit 7fe9d8c

Browse files
committed
update
1 parent 0c84edd commit 7fe9d8c

7 files changed

Lines changed: 98 additions & 100 deletions

File tree

‎Chapter09/customer-support/src/customer_support/__main__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Ingest(KnowledgeSettings):
2020
"""Sync the source folder into the knowledge base."""
2121

2222
def cli_cmd(self) -> None:
23-
"""Open the knowledge base and sync the folder."""
24-
KnowledgeBase(self).sync()
23+
"""Sync the folder and print the report as JSON."""
24+
print(KnowledgeBase(self).sync().model_dump_json())
2525

2626

2727
class ChatResult(Answer):
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
from fastapi import FastAPI
1+
from typing import Annotated
22

3+
from fastapi import Depends, FastAPI
4+
5+
from .config import Settings
6+
from .knowledge import KnowledgeBase, SyncReport
37
from .response_models import HealthResponse
48

59
app = FastAPI(title="Customer Support")
610

711

12+
def get_knowledge_base() -> KnowledgeBase:
13+
"""Open the knowledge base configured by the environment."""
14+
return KnowledgeBase(Settings().knowledge)
15+
16+
817
@app.get("/health")
918
def health() -> HealthResponse:
1019
"""Report that the service is up."""
1120
return HealthResponse(status="ok")
21+
22+
23+
@app.post("/knowledge/sync")
24+
def sync_knowledge(
25+
knowledge_base: Annotated[
26+
KnowledgeBase, Depends(get_knowledge_base)
27+
],
28+
) -> SyncReport:
29+
"""Sync the source folder into the knowledge base."""
30+
return knowledge_base.sync()

‎Chapter09/customer-support/src/customer_support/knowledge.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from llama_index.core.storage.docstore import SimpleDocumentStore
1717
from llama_index.embeddings.fastembed import FastEmbedEmbedding
1818
from llama_index.vector_stores.chroma import ChromaVectorStore
19-
from loguru import logger
2019
from pydantic import BaseModel
2120

2221
from .config import KnowledgeSettings
@@ -192,7 +191,6 @@ def sync(self) -> SyncReport:
192191
self._pipeline(docstore).run(documents=documents)
193192
docstore.persist(str(self._docstore_path))
194193
report = SyncReport.compare(stored, documents)
195-
logger.info("Knowledge base synced: {}", report)
196194
return report
197195

198196
def search(self, query: str, limit: int = 3) -> list[Chunk]:

‎Chapter09/customer-support/tests/conftest.py‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -287,30 +287,6 @@ def hosted_settings() -> LLMSettings:
287287
return LLMSettings(provider="openai", model="gpt-4o-mini")
288288

289289

290-
@pytest.fixture
291-
def changed_llm_environment(
292-
monkeypatch: pytest.MonkeyPatch,
293-
) -> LLMSettings:
294-
"""Non-default configuration read from environment variables."""
295-
expected = LLMSettings(
296-
provider="openai",
297-
model="gpt-4o-mini",
298-
retrieval_limit=5,
299-
request_limit=7,
300-
)
301-
prefix = "CUSTOMER_SUPPORT_LLM_"
302-
for field in (
303-
"provider",
304-
"model",
305-
"retrieval_limit",
306-
"request_limit",
307-
):
308-
monkeypatch.setenv(
309-
prefix + field.upper(), str(getattr(expected, field))
310-
)
311-
return expected
312-
313-
314290
@pytest.fixture
315291
def database_settings(tmp_path: Path) -> DatabaseSettings:
316292
"""On-disk SQLite database isolated to this test."""

‎Chapter09/customer-support/tests/test_api.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,21 @@ def test_health(client: TestClient) -> None:
99
assert response.json() == {"status": "ok"}
1010

1111

12+
@pytest.mark.usefixtures("ingestion_environment")
13+
def test_sync_endpoint_reports_changes(
14+
client: TestClient, sample_sources: list[str]
15+
) -> None:
16+
"""Sync twice through the API and read both reports."""
17+
first = client.post("/knowledge/sync")
18+
assert first.status_code == 200
19+
assert first.json()["added"] == sample_sources, (
20+
"The first sync must add every sample document."
21+
)
22+
second = client.post("/knowledge/sync").json()
23+
assert second["unchanged"] == sample_sources, (
24+
"The second sync must find nothing to re-embed."
25+
)
26+
27+
1228
if __name__ == "__main__":
1329
pytest.main([__file__, "-x", "-s"])

‎Chapter09/customer-support/tests/test_settings.py‎

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)