Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/trowel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def generate_embeddings(input_file, collection, db_path, text_fields, limit, ski
db_path,
collection,
export_csv,
include_embeddings=True,
)
logging.info(f"Exported {num_exported} embeddings to {export_csv}")
logging.info("You can now use these embeddings with other embedding commands:")
Expand Down
6 changes: 5 additions & 1 deletion src/trowel/utils/embedding_generation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ def export_embeddings_to_csv(
# Get field names for the collection. Some CurateGPT backends can return
# empty field_names even when rows exist, so fall back to inferring them.
field_names = store.field_names(collection=collection_name) or []
raw_results = list(store.find(where={}, collection=collection_name))
find_kwargs = {"where": {}, "collection": collection_name}
if include_embeddings:
find_kwargs["include"] = ["metadatas", "documents", "embeddings"]

raw_results = list(store.find(**find_kwargs))
docs = []
for result in raw_results:
normalized = _normalize_store_result(
Expand Down
7 changes: 6 additions & 1 deletion tests/test_cli_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def test_command_with_export(self, mock_generate, mock_export, runner, sample_cs
assert result.exit_code == 0
assert "Exporting embeddings to" in result.output
assert "Exported 3 embeddings" in result.output
assert mock_export.called
mock_export.assert_called_once_with(
db_path,
"embeddings",
export_path,
include_embeddings=True,
)

@patch("trowel.cli.generate_embeddings_with_curategpt")
def test_command_with_limit(self, mock_generate, runner, sample_csv, temp_dir):
Expand Down
46 changes: 46 additions & 0 deletions tests/test_embedding_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,49 @@ def test_csv_export_handles_duckdb_tuple_results(self, mock_get_store, temp_dir)
rows = list(reader)
assert len(rows) == 2
assert rows[0]["id"] == "BERVO:0000001"

@patch("trowel.utils.embedding_generation_utils._get_curategpt_store")
def test_csv_export_requests_duckdb_embeddings_when_included(
self,
mock_get_store,
temp_dir,
):
"""Test export requests embeddings when requested."""
db_path = os.path.join(temp_dir, "test.duckdb")
os.makedirs(db_path, exist_ok=True)

mock_store = MagicMock()
mock_get_store.return_value = mock_store
mock_store.field_names.return_value = []

def fake_find(where, collection, include=None):
embeddings = (
[0.1, 0.2]
if include and "embeddings" in include
else None
)
return [
(
{"id": "BERVO:0000001", "label": "Temperature"},
0.0,
{"_embeddings": embeddings, "documents": "Temperature"},
)
]

mock_store.find.side_effect = fake_find

output_path = os.path.join(temp_dir, "output.csv")
num_exported = export_embeddings_to_csv(
db_path,
"test_collection",
output_path,
include_embeddings=True,
)

assert num_exported == 1
with open(output_path, 'r') as f:
reader = csv.DictReader(f)
rows = list(reader)
assert len(rows) == 1
assert "embeddings" in reader.fieldnames
assert rows[0]["embeddings"] == "[0.1, 0.2]"
Loading