Skip to content

Generate embeddings for DC core schema#6046

Open
ajaits wants to merge 2 commits intodatacommonsorg:masterfrom
ajaits:dc-schema-index
Open

Generate embeddings for DC core schema#6046
ajaits wants to merge 2 commits intodatacommonsorg:masterfrom
ajaits:dc-schema-index

Conversation

@ajaits
Copy link
Contributor

@ajaits ajaits commented Mar 2, 2026

Generated embedding for DC core schema nodes for propriety, class, enums excluding schemaless properties using an agent.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces the necessary infrastructure to generate and utilize embeddings for the Data Commons core schema. It provides a new Python utility to transform schema definitions into a structured CSV format, which can then be used to create machine learning embeddings. Concurrently, it updates the deployment configuration to integrate this new schema embedding index, enabling advanced natural language processing capabilities that leverage the structured knowledge of the DC schema.

Highlights

  • Schema Embedding Generation Script: A new Python script (build_schema_csv.py) has been introduced to process Data Commons (DC) core schema files (MCF format) and extract relevant schema nodes (Classes, Properties, Enumerations) into a CSV file. This CSV file serves as input for generating embeddings.
  • Schema Embedding Index Configuration: The catalog.yaml Helm chart configuration has been updated to include a new schema_index entry. This configures a memory-based index for schema embeddings, specifying its source path, GCS embeddings path, and the uae-large-v1-model for health checks.
  • Unit Tests for Schema CSV Builder: A dedicated test file (build_schema_csv_test.py) has been added to ensure the correctness and robustness of the build_schema_csv.py script, covering MCF parsing, multi-file handling, and the main logic for CSV generation and camelCase splitting.
Changelog
  • deploy/helm_charts/dc_website/nl/catalog.yaml
    • Added a new schema_index configuration under indexes, specifying store_type, source_path, embeddings_path, model, and healthcheck_query.
  • tools/nl/embeddings/build_schema_csv.py
    • Added a new Python script to parse MCF files, extract schema nodes (Classes, Properties, Enums), and output them to a CSV file.
    • Implemented logic to handle multiple values for the same key, remove dcid: prefixes, and convert camelCase names to space-separated words.
  • tools/nl/embeddings/build_schema_csv_test.py
    • Added a new Python test file to validate the build_schema_csv.py script.
    • Included tests for MCF parsing, handling multiple MCF files, and the end-to-end CSV generation logic.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new script to generate embeddings for the DC core schema, along with the necessary configuration and tests. The script parses MCF files to extract relevant schema nodes and their descriptions, creating a CSV for the embeddings pipeline. The tests cover the basic functionality. My review includes suggestions to improve the robustness of the MCF parser and the logic for identifying enumeration-related nodes to ensure correctness and better maintainability.

Comment on lines +31 to +71
def parse_mcf(file_path):
"""Parses an MCF file and yields nodes."""
# Read nodes for one file at a time
files = file_path.split(',')
for file in files:
with open(file.strip(), 'r') as f:
content = f.read()

# Split by empty lines to get blocks, but handle multiple newlines
blocks = re.split(r'\n\s*\n', content)

for i, block in enumerate(blocks):
node = {}
lines = block.strip().split('\n')
for line in lines:
if not line:
continue
# Handle multi-line descriptions if necessary, but for now assume single line or simple structure
# A simple regex to capture key: value
match = re.match(r'^([^:]+):\s*(.*)$', line)
if match:
key = match.group(1).strip()
value = match.group(2).strip()
# Remove quotes if present
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]

# Handle multiple values for same key (e.g. typeOf)
if key in node:
if isinstance(node[key], list):
node[key].append(value)
else:
node[key] = [node[key], value]
else:
node[key] = value
# else:
# print(f"DEBUG: No match for line: '{line}'")

if 'Node' in node:
yield node

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The parse_mcf function is not robust against multi-line values, which are valid in the MCF format. Continuation lines (starting with whitespace) will be silently ignored by the current line-by-line regex matching, which could lead to incomplete or incorrect data being parsed. The comment on line 48 indicates this is a known assumption, but it makes the script fragile to future changes in core_schema.mcf.

To improve robustness, I recommend updating the parser to handle multi-line values. This would typically involve iterating through lines of a block and concatenating indented lines to the value of the preceding line.

Comment on lines +106 to +123
# Check if it's a Class, Property, or Enum
is_schema_node = False
for t in type_of:
if t in ['dcid:Class', 'dcid:Property', 'Class', 'Property']:
is_schema_node = True
break
if 'Enum' in t: # Heuristic
is_schema_node = True

# Also check subClassOf for Enumeration
sub_class_of = node.get('subClassOf')
if isinstance(sub_class_of, str):
sub_class_of = [sub_class_of]
if sub_class_of:
for s in sub_class_of:
if s in ['dcid:Enumeration', 'Enumeration']:
is_schema_node = True
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for identifying schema nodes, particularly for enumerations, is based on a fragile heuristic (if 'Enum' in t:). This could lead to incorrect classifications if a node's type contains "Enum" for other reasons.

A more robust approach would be to first perform a pass over the MCF data to explicitly identify all enumeration classes (i.e., nodes with subClassOf: dcid:Enumeration). Then, during the main processing pass, a node can be identified as a schema node if it is a Class, a Property, an enumeration class itself, or an instance of an enumeration class (i.e., its typeOf is one of the identified enumeration classes). This would make the logic more reliable and less dependent on naming conventions.

@ajaits ajaits requested review from clincoln8 and keyurva March 2, 2026 12:33
Copy link
Contributor

@keyurva keyurva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go! Thanks Ajai.

'Path to the output CSV file.')


def parse_mcf(file_path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we install and use the mcf lib for parsing like we do here instead of writing a new parse function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we separate the PR into 2? One for yaml + csv files and another for the code? The former can be submitted right away while we iterate on the latter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants