Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,32 +19,40 @@ class TicketRequest(BaseModel):
|
|
| 19 |
class SetupRequest(BaseModel):
|
| 20 |
kb_file: str # path to KB.json
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# -------------------------------
|
| 23 |
# KB Setup Endpoint
|
| 24 |
# -------------------------------
|
| 25 |
@app.post("/setup")
|
| 26 |
async def setup_endpoint(req: SetupRequest):
|
| 27 |
-
"""Embed KB.json and store in ChromaDB"""
|
| 28 |
global kb_collection
|
|
|
|
| 29 |
if not os.path.exists(req.kb_file):
|
| 30 |
raise HTTPException(status_code=404, detail="KB.json file not found")
|
| 31 |
|
| 32 |
-
# Load KB
|
| 33 |
with open(req.kb_file, "r") as f:
|
| 34 |
kb_data = json.load(f)
|
| 35 |
|
| 36 |
-
# Create
|
| 37 |
chroma_client = chromadb.Client(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
try:
|
| 43 |
kb_collection = chroma_client.get_collection("kb_collection")
|
| 44 |
except:
|
| 45 |
kb_collection = chroma_client.create_collection("kb_collection")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 49 |
model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
| 50 |
)
|
|
@@ -63,7 +71,9 @@ async def setup_endpoint(req: SetupRequest):
|
|
| 63 |
embedding_function=embedding_func
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
kb_collection.persist()
|
|
|
|
| 67 |
return {"status": "KB embedded and stored successfully"}
|
| 68 |
|
| 69 |
# -------------------------------
|
|
|
|
| 19 |
class SetupRequest(BaseModel):
|
| 20 |
kb_file: str # path to KB.json
|
| 21 |
|
| 22 |
+
# -------------------------------
|
| 23 |
+
# Global variable for collection
|
| 24 |
+
# -------------------------------
|
| 25 |
+
kb_collection = None
|
| 26 |
+
|
| 27 |
# -------------------------------
|
| 28 |
# KB Setup Endpoint
|
| 29 |
# -------------------------------
|
| 30 |
@app.post("/setup")
|
| 31 |
async def setup_endpoint(req: SetupRequest):
|
|
|
|
| 32 |
global kb_collection
|
| 33 |
+
|
| 34 |
if not os.path.exists(req.kb_file):
|
| 35 |
raise HTTPException(status_code=404, detail="KB.json file not found")
|
| 36 |
|
| 37 |
+
# Load KB.json
|
| 38 |
with open(req.kb_file, "r") as f:
|
| 39 |
kb_data = json.load(f)
|
| 40 |
|
| 41 |
+
# Create Chroma client (new API)
|
| 42 |
chroma_client = chromadb.Client(
|
| 43 |
+
Settings(
|
| 44 |
+
persist_directory="/data/chroma_db", # make sure this dir exists and writable
|
| 45 |
+
anonymized_telemetry=False
|
| 46 |
+
)
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
# Create or get collection
|
| 50 |
try:
|
| 51 |
kb_collection = chroma_client.get_collection("kb_collection")
|
| 52 |
except:
|
| 53 |
kb_collection = chroma_client.create_collection("kb_collection")
|
| 54 |
|
| 55 |
+
# Embedding function
|
| 56 |
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 57 |
model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
| 58 |
)
|
|
|
|
| 71 |
embedding_function=embedding_func
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# Persist to disk
|
| 75 |
kb_collection.persist()
|
| 76 |
+
|
| 77 |
return {"status": "KB embedded and stored successfully"}
|
| 78 |
|
| 79 |
# -------------------------------
|