aki-008 commited on
Commit
331e3ee
·
1 Parent(s): a8e2a77

feat: chromadb integration

Browse files
Backend/app/api/deps.py CHANGED
@@ -8,6 +8,7 @@ from app.models import User
8
  from app.config import settings
9
  from fastapi import Request
10
  from chromadb import AsyncHttpClient
 
11
 
12
  security = HTTPBearer()
13
 
@@ -56,4 +57,10 @@ async def get_chroma_client(request: Request) -> AsyncHttpClient:
56
  client = getattr(request.app.state, "chroma_client", None)
57
  if client is None:
58
  raise RuntimeError("ChromaDB client is not initialized in App State.")
59
- return client
 
 
 
 
 
 
 
8
  from app.config import settings
9
  from fastapi import Request
10
  from chromadb import AsyncHttpClient
11
+ from chromadb.api.models.Collection import Collection
12
 
13
  security = HTTPBearer()
14
 
 
57
  client = getattr(request.app.state, "chroma_client", None)
58
  if client is None:
59
  raise RuntimeError("ChromaDB client is not initialized in App State.")
60
+ return client
61
+
62
+ def get_chroma_collection(request: Request) -> Collection:
63
+ collection = getattr(request.app.state, "chroma_collection", None)
64
+ if collection is None:
65
+ raise RuntimeError("ChromaDB Collection not loaded during application startup.")
66
+ return collection
Backend/app/api/v1/endpoints/quiz.py CHANGED
@@ -3,11 +3,30 @@ from chromadb import AsyncHttpClient
3
  from app.models import User
4
  from app.api.deps import get_db, get_current_user, get_chroma_client
5
  from app.schema import Quiz_input
6
- from prompts import SYSTEM_PROMPT
7
-
8
- router = APIRouter()
9
-
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
 
 
3
  from app.models import User
4
  from app.api.deps import get_db, get_current_user, get_chroma_client
5
  from app.schema import Quiz_input
6
+ from .prompts import SYSTEM_PROMPT
 
 
 
7
 
8
+ from fastapi import APIRouter, Depends, HTTPException
9
+ from chromadb.api.models.Collection import Collection # Import Collection type
10
+ from app.api.deps import get_chroma_collection
11
+
12
+ router = APIRouter(prefix="/search")
13
+
14
+
15
+ @router.get("/")
16
+ async def search_documents(
17
+ query: str,
18
+ # Inject the pre-loaded Collection object directly
19
+ collection: Collection = Depends(get_chroma_collection)
20
+ ):
21
+ try:
22
+ # The Collection object is ready to use immediately.
23
+ results = await collection.query(
24
+ query_texts=[query],
25
+ n_results=5
26
+ )
27
+ return results
28
+ except Exception as e:
29
+ raise HTTPException(status_code=500, detail=f"ChromaDB Query Error: {e}")
30
 
31
 
32
 
Backend/app/main.py CHANGED
@@ -6,7 +6,7 @@ from app.config import settings
6
  from app.database import engine, Base
7
  from app.api.v1.api import api_router
8
  import chromadb
9
-
10
 
11
 
12
  @asynccontextmanager
@@ -18,10 +18,20 @@ async def lifespan(app: FastAPI):
18
  async with engine.begin() as conn:
19
  await conn.run_sync(Base.metadata.create_all)
20
 
21
- app.state.chroma_client = await chromadb.AsyncHttpClient(
22
  host=settings.chroma_host,
23
  port=settings.chroma_port
24
  )
 
 
 
 
 
 
 
 
 
 
25
 
26
  print("✅ Tables ready!")
27
  yield
 
6
  from app.database import engine, Base
7
  from app.api.v1.api import api_router
8
  import chromadb
9
+ from chromadb.api.models.Collection import Collection
10
 
11
 
12
  @asynccontextmanager
 
18
  async with engine.begin() as conn:
19
  await conn.run_sync(Base.metadata.create_all)
20
 
21
+ client = await chromadb.AsyncHttpClient(
22
  host=settings.chroma_host,
23
  port=settings.chroma_port
24
  )
25
+ app.state.chroma_client = client
26
+
27
+ try:
28
+ collection: Collection = await client.get_or_create_collection(settings.chroma_collection)
29
+ app.state.chroma_collection = collection
30
+
31
+ count = await collection.count()
32
+ print(f"Successfully loaded collection '{settings.chroma_collection}' with {count} documents.")
33
+ except Exception as e:
34
+ print(f"Failed to load ChromaDB collection: {e}")
35
 
36
  print("✅ Tables ready!")
37
  yield
Backend/requirements.txt CHANGED
@@ -8,3 +8,4 @@ passlib[argon2]
8
  python-jose[cryptography]
9
  email-validator
10
 
 
 
8
  python-jose[cryptography]
9
  email-validator
10
 
11
+