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 sentence embeddings QM #537

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions python/embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import mgp
from sentence_transformers import SentenceTransformer

# Initialize default model
model = SentenceTransformer('all-MiniLM-L6-v2')

# Exclude certain properties from embedding computation
EXCLUDE_PROPERTIES = {"embedding"}

@mgp.read_proc
def compute_embeddings(ctx: mgp.ProcCtx) -> mgp.Record(node_id=int, success=bool):
try:
# Fetch all nodes from the graph
for vertex in ctx.graph.vertices:
# Combine node labels and properties into a single string, excluding specified properties
node_data = " ".join(vertex.labels) + " " + " ".join(
f"{key}: {value}"
for key, value in vertex.properties.items()
if key not in EXCLUDE_PROPERTIES
)

# Compute the embedding
node_embedding = model.encode(node_data).tolist()

# Update the node with the computed embedding
vertex.properties["embedding"] = node_embedding

return mgp.Record(node_id=vertex.id, success=True)

except Exception as e:
# Handle exceptions by returning failure status
return mgp.Record(node_id=None, success=False)
Loading