Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text and embedding type to neo4j enhanced schema #17289

Merged
merged 4 commits into from
Dec 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def remove_empty_values(input_dict):
DISTINCT_VALUE_LIMIT = 10
CHUNK_SIZE = 1000
VECTOR_INDEX_NAME = "entity"
LONG_TEXT_THRESHOLD = 52

node_properties_query = """
CALL apoc.meta.data()
Expand Down Expand Up @@ -281,6 +282,19 @@ def refresh_schema(self) -> None:
)
enhanced_info = self.structured_query(enhanced_cypher)[0]["output"]
for prop in node_props:
# Map to custom types
# Text
if prop["type"] == "STRING" and any(
len(value) >= LONG_TEXT_THRESHOLD
for value in enhanced_info[prop["property"]]["values"]
):
enhanced_info[prop["property"]]["type"] = "TEXT"
# Embedding
if (
prop["type"] == "LIST"
and enhanced_info[prop["property"]]["max_size"] > LIST_LIMIT
):
enhanced_info[prop["property"]]["type"] = "EMBEDDING"
if prop["property"] in enhanced_info:
prop.update(enhanced_info[prop["property"]])
# Update rel info
Expand Down Expand Up @@ -754,7 +768,7 @@ def _enhanced_schema_cypher(
prop_type = prop["type"]
if prop_type == "STRING":
with_clauses.append(
f"collect(distinct substring(toString(coalesce(n.`{prop_name}`, '')), 0, 50)) "
f"collect(distinct substring(toString(coalesce(n.`{prop_name}`, '')), 0, {LONG_TEXT_THRESHOLD})) "
f"AS `{prop_name}_values`"
)
return_clauses.append(
Expand All @@ -781,11 +795,14 @@ def _enhanced_schema_cypher(
elif prop_type == "LIST":
with_clauses.append(
f"min(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_min`, "
f"max(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_max`"
f"max(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_max`, "
# Get first 3 sub-elements of the first element as sample values
f"collect(n.`{prop_name}`)[0][..3] AS `{prop_name}_values`"
)
return_clauses.append(
f"min_size: `{prop_name}_size_min`, "
f"max_size: `{prop_name}_size_max`"
f"max_size: `{prop_name}_size_max`, "
f"values:`{prop_name}_values`"
)
elif prop_type in ["BOOLEAN", "POINT", "DURATION"]:
continue
Expand Down Expand Up @@ -821,7 +838,7 @@ def _enhanced_schema_cypher(
)
else:
with_clauses.append(
f"collect(distinct substring(n.`{prop_name}`, 0, 50)) "
f"collect(distinct substring(n.`{prop_name}`, 0, {LONG_TEXT_THRESHOLD})) "
f"AS `{prop_name}_values`"
)
return_clauses.append(f"values: `{prop_name}_values`")
Expand Down Expand Up @@ -857,11 +874,14 @@ def _enhanced_schema_cypher(
elif prop_type == "LIST":
with_clauses.append(
f"min(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_min`, "
f"max(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_max`"
f"max(size(coalesce(n.`{prop_name}`, []))) AS `{prop_name}_size_max`, "
# Get first 3 sub-elements of the first element as sample values
f"collect(n.`{prop_name}`)[0][..3] AS `{prop_name}_values`"
)
return_clauses.append(
f"min_size: `{prop_name}_size_min`, "
f"max_size: `{prop_name}_size_max`"
f"max_size: `{prop_name}_size_max`, "
f"values:`{prop_name}_values`"
)
elif prop_type in ["BOOLEAN", "POINT", "DURATION"]:
continue
Expand Down Expand Up @@ -934,7 +954,12 @@ def filter_func(x: str) -> bool:
if prop["values"]
else ""
)

elif prop["type"] == "TEXT":
example = (
f'Example: "{clean_string_values(prop["values"][0])}"'
if prop["values"]
else ""
)
elif prop["type"] in [
"INTEGER",
"FLOAT",
Expand All @@ -952,9 +977,17 @@ def filter_func(x: str) -> bool:
)
elif prop["type"] == "LIST":
# Skip embeddings
if not prop.get("min_size") or prop["min_size"] > LIST_LIMIT:
continue
example = f'Min Size: {prop["min_size"]}, Max Size: {prop["max_size"]}'
# if not prop.get("min_size") or prop["min_size"] > LIST_LIMIT:
# continue
example = (
f'Min Size: {prop.get("min_size", "N/A")}, '
f'Max Size: {prop.get("max_size", "N/A")}, '
+ (
f'Example: [{prop["values"][0]}]'
if prop.get("values") and len(prop["values"]) > 0
else ""
)
)
formatted_node_props.append(
f" - `{prop['property']}`: {prop['type']} {example}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-graph-stores-neo4j"
readme = "README.md"
version = "0.4.3"
version = "0.4.4"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
Expand Down
Loading