forked from AIML4OS/funathon-project2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag_model.py
More file actions
247 lines (201 loc) · 6.9 KB
/
Copy pathrag_model.py
File metadata and controls
247 lines (201 loc) · 6.9 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
from dotenv import load_dotenv
from qdrant_client import QdrantClient
from openai import OpenAI
import duckdb
from dataclasses import dataclass, field
from typing import Optional, List
from qdrant_client.models import Distance, VectorParams
from uuid import uuid5, NAMESPACE_DNS
from qdrant_client.models import PointStruct
import json
from more_itertools import chunked
from tqdm import tqdm
COLLECTION_NAME = "nace-collection"
NACE_NAMESPACE = uuid5(NAMESPACE_DNS, "nace-rev2")
load_dotenv()
EMB_MODEL_NAME = "qwen3-embedding-8b"
emb_dim = 4096
sample_size = 10
client_llmlab = OpenAI(
base_url=os.environ["LLMLAB_URL"],
api_key=os.environ["LLMLAB_API_KEY"],
)
client_qdrant = QdrantClient(
url=os.environ["QDRANT_URL"],
api_key=os.environ["QDRANT_API_KEY"],
port=os.environ["QDRANT_API_PORT"],
check_compatibility=False
)
collections = client_qdrant.get_collections()
for collection in collections.collections:
print("Hello DB:" + collection.name)
# Delete the collection if necessary
if client_qdrant.collection_exists(collection_name=COLLECTION_NAME):
client_qdrant.delete_collection(collection_name=COLLECTION_NAME)
# Create the collection
client_qdrant.create_collection(
collection_name=COLLECTION_NAME,
vectors_config=VectorParams(
size=emb_dim,
distance=Distance.COSINE
)
)
con = duckdb.connect(database=":memory:")
con.execute("INSTALL httpfs;")
con.execute("LOAD httpfs;")
path_nace = 'https://minio.lab.sspcloud.fr/projet-formation/diffusion/funathon/2026/project2/NACE_Rev2.1_Structure_Explanatory_Notes_EN.tsv'
query_definition = f"SELECT * FROM read_csv('{path_nace}')"
table = con.execute(query_definition).to_arrow_table()
nace = table.to_pylist()
def _clean(value) -> Optional[str]:
"""Normalize to stripped single-line string, or None if empty/missing."""
if value is None:
return None
# str() handles non-string values (int, float...) from raw dicts
# replace("\n", " ") flattens multiline strings to a single line
# split() tokenizes on any whitespace, join(" ") rebuilds with single spaces
cleaned = " ".join(str(value).replace("\n", " ").split())
# Empty string is falsy in Python — return None instead for consistency
return cleaned or None
@dataclass
class NaceDocument:
code: str
heading: str
level: int
parent_code: Optional[str] = None
includes: Optional[str] = None
includes_also: Optional[str] = None
excludes: Optional[str] = None
text: str = field(init=False)
vector: Optional[List[float]] = field(default=None, init=False)
@classmethod
def from_raw(
cls,
raw: dict,
with_includes_also=True,
with_excludes=False,
) -> "NaceDocument":
for key in ("CODE", "HEADING", "LEVEL"):
if not raw.get(key):
raise ValueError(f"Missing required field: {key}")
level = int(raw["LEVEL"])
if not (1 <= level <= 4):
raise ValueError(f"Invalid level: {level}")
obj = cls(
code=str(raw["CODE"]).strip(),
heading=_clean(raw["HEADING"]),
level=level,
parent_code=_clean(raw.get("PARENT_CODE")),
includes=_clean(raw.get("Includes")),
includes_also=_clean(raw.get("IncludesAlso")),
excludes=_clean(raw.get("Excludes")),
)
obj.text = obj.to_embedding_text(
with_includes_also=with_includes_also,
with_excludes=with_excludes,
)
return obj
def to_embedding_text(
self,
*,
with_includes_also: bool = False,
with_excludes: bool = False,
) -> str:
parts = []
parts.append(f"# Code: {self.code}")
parts.append(f"# Title: {self.heading}")
if self.includes:
parts.append("")
parts.append("## Includes:")
parts.append(self.includes.strip())
if with_includes_also and self.includes_also:
parts.append("")
parts.append("## Also includes:")
parts.append(self.includes_also.strip())
if with_excludes and self.excludes:
parts.append("")
parts.append("## Excludes:")
parts.append(self.excludes.strip())
output = "\n".join(parts)
output = output.replace("\\n", "\n")
return output.strip()
def get_embeddings(
self,
client_llmlab,
emb_model: str,
verbose = False,
) -> List[float]:
try:
response = client_llmlab.embeddings.create(
model=EMB_MODEL_NAME,
input=self.text
)
self.vector = response.data[0].embedding
if verbose:
return self.vector
except Exception as e:
raise RuntimeError(f"Embedding failed for doc {self.code}: {str(e)}")
def to_qdrant_point(
self,
) -> PointStruct:
if not hasattr(self, "vector") or self.vector is None:
raise ValueError("vector is missing or Null")
return PointStruct(
# uuid5 is deterministic: same namespace + code always yields the same UUID
# stable across runs, valid for Qdrant, no hacky string manipulation needed
id=str(uuid5(NACE_NAMESPACE, self.code)),
vector=self.vector,
payload={
"code": self.code,
"level": self.level,
"parent_code": self.parent_code,
# Storing the text used for embedding enables inspection and debugging
"text": self.text,
}
)
nace_documents = []
for nace_code in nace[:sample_size]:
nace_documents.append(
NaceDocument.from_raw(
raw=nace_code,
with_includes_also=True,
with_excludes=True
)
)
for nace_doc in nace_documents:
nace_doc.get_embeddings(
client_llmlab,
EMB_MODEL_NAME,
)
nace_points = []
for nace_code in nace:
nace_doc = NaceDocument.from_raw(
raw=nace_code,
with_includes_also=True,
with_excludes=True
)
nace_doc.get_embeddings(
client_llmlab,
EMB_MODEL_NAME,
)
nace_points.append(
nace_doc.to_qdrant_point()
)
point = nace_points[0]
point_dict = point.model_dump()
# Truncate the vector for readability (full vector is hundreds of floats)
vector = point_dict["vector"]
point_dict["vector"] = f"[{vector[0]:.4f}, {vector[1]:.4f}, ..., {vector[-1]:.4f}] ({len(vector)} dims)"
print("Check the first PointStruct:\n")
print(json.dumps(point_dict, indent=2, ensure_ascii=False))
BATCH_SIZE = 16
batches = list(chunked(nace_points, BATCH_SIZE))
for batch in tqdm(batches, desc="Uploading to Qdrant", unit="batch"):
try:
client_qdrant.upsert(
collection_name=COLLECTION_NAME,
points=batch,
)
except Exception as e:
tqdm.write(f"✗ Batch failed: {e}")