-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathembed_module.py
More file actions
168 lines (144 loc) · 4.97 KB
/
embed_module.py
File metadata and controls
168 lines (144 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Hamilton embedding DAG.
Fetches Movie nodes from Neo4j, computes OpenAI embeddings over
title + overview text, writes embeddings back to each node, and
creates a Neo4j vector index for use during retrieval.
Run this once after ingestion and before querying.
DAG flow:
neo4j_driver + openai_api_key
-> movie_texts
-> embedding_client
-> movie_embeddings
-> vector_index
-> embedding_summary
"""
import logging
import openai
from neo4j import Driver
logger = logging.getLogger(__name__)
# Shared constants — also imported by retrieval_module.py
VECTOR_INDEX_NAME = "movie_embeddings"
EMBEDDING_MODEL = "text-embedding-3-small"
EMBEDDING_DIMENSIONS = 1536
BATCH_SIZE = 100
def movie_texts(neo4j_driver: Driver) -> list[dict]:
"""
Fetch all Movie nodes and build embedding text from title + overview.
Returns list of dicts with keys: id, text.
"""
query = """
MATCH (m:Movie)
WHERE m.title IS NOT NULL
RETURN m.id AS id,
m.title AS title,
coalesce(m.overview, '') AS overview
"""
with neo4j_driver.session() as session:
rows = session.run(query).data()
texts = [
{
"id": row["id"],
"text": f"{row['title']}. {row['overview']}".strip(),
}
for row in rows
if row["id"] is not None
]
logger.info("Fetched %d movie texts for embedding", len(texts))
return texts
def embedding_client(openai_api_key: str) -> openai.OpenAI:
"""Initialise the OpenAI client for embedding calls."""
return openai.OpenAI(api_key=openai_api_key)
def movie_embeddings(
movie_texts: list[dict],
embedding_client: openai.OpenAI,
) -> list[dict]:
"""
Compute OpenAI embeddings for all movie texts in batches of BATCH_SIZE.
Returns list of dicts with keys: id, embedding.
"""
results = []
total = len(movie_texts)
for i in range(0, total, BATCH_SIZE):
batch = movie_texts[i : i + BATCH_SIZE]
response = embedding_client.embeddings.create(
model=EMBEDDING_MODEL,
input=[item["text"] for item in batch],
)
for item, emb_obj in zip(batch, response.data, strict=False):
results.append({"id": item["id"], "embedding": emb_obj.embedding})
logger.info("Embedded batch %d-%d of %d", i, min(i + BATCH_SIZE, total), total)
logger.info("Computed %d embeddings", len(results))
return results
def vector_index(
movie_embeddings: list[dict],
neo4j_driver: Driver,
) -> str:
"""
Write embeddings to Movie nodes in Neo4j and create a cosine
vector index named VECTOR_INDEX_NAME over the embedding property.
Returns the index name.
"""
write_query = """
UNWIND $batch AS row
MATCH (m:Movie {id: row.id})
SET m.embedding = row.embedding
"""
total = len(movie_embeddings)
with neo4j_driver.session() as session:
for i in range(0, total, BATCH_SIZE):
batch = movie_embeddings[i : i + BATCH_SIZE]
session.run(write_query, {"batch": batch})
logger.info(
"Wrote embeddings to nodes %d-%d of %d",
i,
min(i + BATCH_SIZE, total),
total,
)
session.run(f"DROP INDEX {VECTOR_INDEX_NAME} IF EXISTS")
session.run(
f"""
CREATE VECTOR INDEX {VECTOR_INDEX_NAME}
FOR (m:Movie)
ON m.embedding
OPTIONS {{
indexConfig: {{
`vector.dimensions`: {EMBEDDING_DIMENSIONS},
`vector.similarity_function`: 'cosine'
}}
}}
"""
)
logger.info("Created vector index '%s'", VECTOR_INDEX_NAME)
return VECTOR_INDEX_NAME
def embedding_summary(
movie_embeddings: list[dict],
vector_index: str,
) -> dict:
"""
Collect embedding statistics and return a summary.
Terminal node of the embedding DAG.
"""
summary = {
"embeddings_written": len(movie_embeddings),
"vector_index": vector_index,
"model": EMBEDDING_MODEL,
"dimensions": EMBEDDING_DIMENSIONS,
}
logger.info("Embedding complete: %s", summary)
return summary