💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
fix(workflow): write a URL without a filename to a file, not to the directory (#1400)
PerryLink8e7a33c54c0c
diff --git a/src/python/txtai/workflow/task/retrieve.py b/src/python/txtai/workflow/task/retrieve.pyindex 622fab36..37fc1513 100644--- a/src/python/txtai/workflow/task/retrieve.py+++ b/src/python/txtai/workflow/task/retrieve.py@@ -52,7 +52,8 @@ class RetrieveTask(Task):if url:if self.flatten:# Flatten directory structure (default)- path = os.path.join(self.directory, os.path.basename(path))+ # A URL with no filename falls back to its hostname, then to "download"+ path = os.path.join(self.directory, os.path.basename(path) or urlparse(element).hostname or "download")directory = Noneelse:# Derive output pathdiff --git a/test/python/testworkflow.py b/test/python/testworkflow.pyindex ce193a57..df35c610 100644--- a/test/python/testworkflow.py+++ b/test/python/testworkflow.py@@ -23,6 +23,7 @@ from croniter import croniterfrom txtai.api import APIfrom txtai.app import Applicationfrom txtai.embeddings import Documents, Embeddings+from txtai.pipeline.data.urlretrieve import URLRetrievefrom txtai.pipeline import Nop, Segmentation, Summary, Translation, Textractorfrom txtai.workflow import (Workflow,@@ -485,6 +486,21 @@ class TestWorkflow(unittest.TestCase):results = list(workflow(["file://" + Utils.PATH + "/books.jpg"]))self.assertTrue(results[0].endswith("books.jpg") and "txtai" in results[0])+ # A URL with no filename must still write to a file, not to the output directory+ with patch.object(URLRetrieve, "__call__", return_value=b"data"):+ with tempfile.TemporaryDirectory() as directory:+ path = RetrieveTask(directory=directory).prepare("https://example.com/")+ self.assertTrue(os.path.isfile(path))+ with open(path, "rb") as handle:+ self.assertEqual(handle.read(), b"data")++ # Distinct bare host URLs must not resolve to the same file+ first = RetrieveTask(directory=directory).prepare("https://a.example.com/")+ second = RetrieveTask(directory=directory).prepare("https://b.example.com/")+ self.assertNotEqual(first, second)+ self.assertTrue(first.endswith("a.example.com"))+ self.assertTrue(second.endswith("b.example.com"))+def testRetrieveLocalFilenames(self):"""Test that local filenames are preserved instead of parsed as URLs