Generate embeddings for DC core schema#6046
Generate embeddings for DC core schema#6046ajaits wants to merge 2 commits intodatacommonsorg:masterfrom
Conversation
Summary of ChangesHello, 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
Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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 | ||
|
|
There was a problem hiding this comment.
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.
| # 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 |
There was a problem hiding this comment.
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.
| 'Path to the output CSV file.') | ||
|
|
||
|
|
||
| def parse_mcf(file_path): |
There was a problem hiding this comment.
Can we install and use the mcf lib for parsing like we do here instead of writing a new parse function?
There was a problem hiding this comment.
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.
Generated embedding for DC core schema nodes for propriety, class, enums excluding schemaless properties using an agent.