Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Raise an error when attempting to update a read-only index #235

davidmezzetti378fb4e2faa2
diff --git a/src/python/txtai/api/routers/embeddings.py b/src/python/txtai/api/routers/embeddings.py
index c08eefe8..4a0f9792 100644
--- a/src/python/txtai/api/routers/embeddings.py
+++ b/src/python/txtai/api/routers/embeddings.py
@@ -4,9 +4,10 @@ Defines API paths for embeddings endpoints.
from typing import List
-from fastapi import APIRouter, Body, Request
+from fastapi import APIRouter, Body, HTTPException, Request
from .. import application
+from ...app import ReadOnlyError
router = APIRouter()
@@ -56,7 +57,10 @@ def add(documents: List[dict] = Body(...)):
documents: list of {id: value, text: value}
"""
- application.get().add(documents)
+ try:
+ application.get().add(documents)
+ except ReadOnlyError as e:
+ raise HTTPException(status_code=403, detail=e.args[0]) from e
@router.get("/index")
@@ -65,7 +69,10 @@ def index():
Builds an embeddings index for previously batched documents.
"""
- application.get().index()
+ try:
+ application.get().index()
+ except ReadOnlyError as e:
+ raise HTTPException(status_code=403, detail=e.args[0]) from e
@router.get("/upsert")
@@ -74,7 +81,10 @@ def upsert():
Runs an embeddings upsert operation for previously batched documents.
"""
- application.get().upsert()
+ try:
+ application.get().upsert()
+ except ReadOnlyError as e:
+ raise HTTPException(status_code=403, detail=e.args[0]) from e
@router.post("/delete")
@@ -89,7 +99,10 @@ def delete(ids: List = Body(...)):
ids deleted
"""
- return application.get().delete(ids)
+ try:
+ return application.get().delete(ids)
+ except ReadOnlyError as e:
+ raise HTTPException(status_code=403, detail=e.args[0]) from e
@router.get("/count")
diff --git a/src/python/txtai/app/__init__.py b/src/python/txtai/app/__init__.py
index 0edc46d2..95768fb6 100644
--- a/src/python/txtai/app/__init__.py
+++ b/src/python/txtai/app/__init__.py
@@ -2,4 +2,4 @@
App imports
"""
-from .base import Application
+from .base import Application, ReadOnlyError
diff --git a/src/python/txtai/app/base.py b/src/python/txtai/app/base.py
index 760eb3d8..f04ae38c 100644
--- a/src/python/txtai/app/base.py
+++ b/src/python/txtai/app/base.py
@@ -270,9 +270,12 @@ class Application:
unmodified input documents
"""
- if self.embeddings and self.config.get("writable"):
+ # Raise error if index is not writable
+ if not self.config.get("writable"):
+ raise ReadOnlyError("Attempting to add documents to a read-only index (writable != True)")
+
+ if self.embeddings:
with self.lock:
- # Only add batch if index is marked writable
# Create documents file if not already open
if not self.documents:
self.documents = Documents()
@@ -305,7 +308,11 @@ class Application:
Builds an embeddings index for previously batched documents.
"""
- if self.embeddings and self.config.get("writable") and self.documents:
+ # Raise error if index is not writable
+ if not self.config.get("writable"):
+ raise ReadOnlyError("Attempting to index a read-only index (writable != True)")
+
+ if self.embeddings and self.documents:
with self.lock:
# Build scoring index if scoring method provided
if self.embeddings.scoring:
@@ -327,7 +334,11 @@ class Application:
Runs an embeddings upsert operation for previously batched documents.
"""
- if self.embeddings and self.config.get("writable") and self.documents:
+ # Raise error if index is not writable
+ if not self.config.get("writable"):
+ raise ReadOnlyError("Attempting to upsert a read-only index (writable != True)")
+
+ if self.embeddings and self.documents:
with self.lock:
# Run upsert
self.embeddings.upsert(self.documents)
@@ -351,7 +362,11 @@ class Application:
ids deleted
"""
- if self.embeddings and self.config.get("writable"):
+ # Raise error if index is not writable
+ if not self.config.get("writable"):
+ raise ReadOnlyError("Attempting to delete from a read-only index (writable != True)")
+
+ if self.embeddings:
with self.lock:
return self.embeddings.delete(ids)
@@ -530,3 +545,9 @@ class Application:
self.pool.close()
self.pool.join()
self.pool = None
+
+
+class ReadOnlyError(Exception):
+ """
+ Error raised when trying to modify a read-only index
+ """
diff --git a/test/python/testapi/testembeddings.py b/test/python/testapi/testembeddings.py
index d043677d..3578b71d 100644
--- a/test/python/testapi/testembeddings.py
+++ b/test/python/testapi/testembeddings.py
@@ -119,7 +119,7 @@ class TestEmbeddings(unittest.TestCase):
Test empty API configuration
"""
- api = API({})
+ api = API({"writable": True})
self.assertIsNone(api.search("test", None))
self.assertIsNone(api.batchsearch(["test"], None))
@@ -259,5 +259,10 @@ class TestEmbeddings(unittest.TestCase):
# Test similarity
uid = self.client.post("similarity", json={"query": "feel good story", "texts": self.data}).json()[0]["id"]
-
self.assertEqual(uid, 4)
+
+ # Test errors raised for write operations
+ self.assertEqual(self.client.post("add", json=[{"id": 0, "text": "test"}]).status_code, 403)
+ self.assertEqual(self.client.get("index").status_code, 403)
+ self.assertEqual(self.client.get("upsert").status_code, 403)
+ self.assertEqual(self.client.post("delete", json=[0]).status_code, 403)