File tree Expand file tree Collapse file tree
Chapter09/customer-support Expand file tree Collapse file tree Original file line number Diff line number Diff 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
2727class ChatResult (Answer ):
Original file line number Diff line number Diff line change 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
37from .response_models import HealthResponse
48
59app = 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" )
918def 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 ()
Original file line number Diff line number Diff line change 1616from llama_index .core .storage .docstore import SimpleDocumentStore
1717from llama_index .embeddings .fastembed import FastEmbedEmbedding
1818from llama_index .vector_stores .chroma import ChromaVectorStore
19- from loguru import logger
2019from pydantic import BaseModel
2120
2221from .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 ]:
Original file line number Diff line number Diff 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
315291def database_settings (tmp_path : Path ) -> DatabaseSettings :
316292 """On-disk SQLite database isolated to this test."""
Original file line number Diff line number Diff 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+
1228if __name__ == "__main__" :
1329 pytest .main ([__file__ , "-x" , "-s" ])
Load diff This file was deleted.
You can’t perform that action at this time.
0 commit comments