Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Only resolve transform functions when env variable set, closes #1155

davidmezzettid8ae6776f86f
diff --git a/src/python/txtai/vectors/dense/external.py b/src/python/txtai/vectors/dense/external.py
index cf1525dd..2467c931 100644
--- a/src/python/txtai/vectors/dense/external.py
+++ b/src/python/txtai/vectors/dense/external.py
@@ -2,7 +2,7 @@
External module
"""
-import random
+import os
import types
from ...util import Library, Resolver
@@ -48,18 +48,20 @@ class External(Vectors):
"""
if transform:
- # Save transform name
- name = transform
+ # Check if transform function resolution is allowed
+ if isinstance(transform, str) and os.environ.get("ALLOW_RESOLVE_TRANSFORM", "False") not in ("True", "1"):
+ raise ImportError(
+ (
+ "External transform function resolution is disabled. "
+ "Set the env variable `ALLOW_RESOLVE_TRANSFORM=True` to enable transform function resolution. "
+ "This should only be done for trusted and/or reviewed code. "
+ )
+ )
# Resolve transform instance, if necessary
- transform = Resolver()(transform) if transform and isinstance(transform, str) else transform
+ transform = Resolver()(transform) if isinstance(transform, str) else transform
# Get function or callable instance
transform = transform if isinstance(transform, types.FunctionType) else transform()
- # Validate transform function - must return a np.array or list of float arrays
- result = transform([str(random.randint(1_000, 100_000))])
- if len(result) == 0 or (not isinstance(result[0], np.ndarray) and not isinstance(result[0][0], float)):
- raise ImportError(f"Invalid transform function {name}")
-
return transform
diff --git a/test/python/testvectors/testdense/testexternal.py b/test/python/testvectors/testdense/testexternal.py
index 1dcba329..b4496b23 100644
--- a/test/python/testvectors/testdense/testexternal.py
+++ b/test/python/testvectors/testdense/testexternal.py
@@ -5,6 +5,8 @@ External module tests
import os
import unittest
+from unittest.mock import patch
+
import numpy as np
from txtai.vectors import External, VectorsFactory
@@ -16,7 +18,7 @@ class Transform:
"""
def __call__(self, data):
- return ["invalid"]
+ return [[0.0, 1.0]]
class TestExternal(unittest.TestCase):
@@ -32,6 +34,14 @@ class TestExternal(unittest.TestCase):
cls.model = VectorsFactory.create({"method": "external"}, None)
+ def testDisabled(self):
+ """
+ Test that transforms are disabled by default
+ """
+
+ with self.assertRaises(ImportError):
+ VectorsFactory.create({"transform": "testvectors.testdense.testexternal.Transform"}, None)
+
def testIndex(self):
"""
Test indexing with external vectors
@@ -54,13 +64,14 @@ class TestExternal(unittest.TestCase):
with open(stream, "rb") as queue:
self.assertEqual(np.load(queue).shape, (500, 768))
- def testInvalid(self):
+ @patch.dict(os.environ, {"ALLOW_RESOLVE_TRANSFORM": "True"})
+ def testResolution(self):
"""
- Test invalid transform function
+ Test resolving an external transform function
"""
- with self.assertRaises(ImportError):
- VectorsFactory.create({"transform": "testvectors.testdense.testexternal.Transform"}, None)
+ transform = VectorsFactory.create({"transform": "testvectors.testdense.testexternal.Transform"}, None)
+ self.assertTrue(np.array_equal(transform.encode(["test"]), np.array([[0.0, 1.0]])))
def testMethod(self):
"""