-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathkubeflow-pipeline.py
More file actions
473 lines (395 loc) · 17.3 KB
/
kubeflow-pipeline.py
File metadata and controls
473 lines (395 loc) · 17.3 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import kfp
from kfp import dsl
from kfp.dsl import *
from typing import *
@dsl.component(
base_image="python:3.9",
packages_to_install=["requests", "beautifulsoup4"]
)
def download_github_directory(
repo_owner: str,
repo_name: str,
directory_path: str,
github_token: str,
github_data: dsl.Output[dsl.Dataset]
):
import requests
import json
import base64
from bs4 import BeautifulSoup
headers = {"Authorization": f"token {github_token}"} if github_token else {}
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents/{directory_path}"
def validate_github_response(response, url):
# Handle GitHub API rate limit errors with a clearer message
if response.status_code == 403:
remaining = response.headers.get("X-RateLimit-Remaining")
if remaining == "0":
raise RuntimeError(
"GitHub API rate limit exceeded while downloading documentation files. "
"Provide a GitHub token to increase the rate limit."
)
raise RuntimeError(
f"GitHub API returned 403 Forbidden for {url}. "
"Check that your GitHub token is valid and has the required access."
)
# Handle missing repository path with a specific error
if response.status_code == 404:
raise FileNotFoundError(
f"GitHub path not found: {url}. "
"Check repo_owner, repo_name, and directory_path."
)
# Keep default behavior for other HTTP errors
response.raise_for_status()
def get_files_recursive(url):
files = []
try:
response = requests.get(url, headers=headers)
validate_github_response(response, url)
items = response.json()
for item in items:
if item['type'] == 'file' and (item['name'].endswith('.md') or item['name'].endswith('.html')):
file_response = requests.get(item['url'], headers=headers)
validate_github_response(file_response, item['url'])
file_data = file_response.json()
content = base64.b64decode(file_data['content']).decode('utf-8')
# Extract text from HTML files
if item['name'].endswith('.html'):
soup = BeautifulSoup(content, 'html.parser')
content = soup.get_text(separator=' ', strip=True)
files.append({
'path': item['path'],
'content': content,
'file_name': item['name']
})
elif item['type'] == 'dir':
files.extend(get_files_recursive(item['url']))
except Exception as e:
print(f"Error fetching {url}: {e}")
return files
files = get_files_recursive(api_url)
print(f"Downloaded {len(files)} files")
with open(github_data.path, 'w', encoding='utf-8') as f:
for file_data in files:
f.write(json.dumps(file_data, ensure_ascii=False) + '\n')
@dsl.component(
base_image="python:3.9",
packages_to_install=["requests"]
)
def download_github_issues(
repos: str,
labels: str,
state: str,
max_issues_per_repo: int,
github_token: str,
issues_data: dsl.Output[dsl.Dataset]
):
"""Fetch GitHub issues and comments from multiple repos for RAG indexing.
Args:
repos: Comma-separated list of repos (e.g., "kubeflow/kubeflow,kubeflow/pipelines")
labels: Comma-separated labels to filter (e.g., "kind/bug,kind/question")
state: Issue state - "open", "closed", or "all"
max_issues_per_repo: Maximum issues to fetch per repository
github_token: GitHub personal access token for API authentication
issues_data: Output dataset path
"""
import requests
import json
import time
headers = {"Authorization": f"token {github_token}"} if github_token else {}
all_issues = []
def api_request(url, params=None):
"""Make GitHub API request with rate limit handling."""
max_retries = 3
for attempt in range(max_retries):
try:
resp = requests.get(url, params=params, headers=headers)
# Handle rate limiting
if resp.status_code == 403:
remaining = resp.headers.get("X-RateLimit-Remaining", "0")
if remaining == "0":
reset_time = int(resp.headers.get("X-RateLimit-Reset", 0))
wait_time = max(reset_time - int(time.time()), 60)
print(f"Rate limited. Waiting {wait_time}s...")
time.sleep(min(wait_time, 300)) # Max 5 min wait
continue
if resp.status_code == 200:
return resp.json()
else:
print(f"API error: HTTP {resp.status_code}")
return None
except Exception as e:
print(f"Request failed (attempt {attempt+1}): {e}")
time.sleep(2 ** attempt) # Exponential backoff
return None
def fetch_comments(owner, name, issue_number):
"""Fetch all comments for a single issue."""
comments_url = f"https://api.github.com/repos/{owner}/{name}/issues/{issue_number}/comments"
comments_text = ""
page = 1
while True:
comments = api_request(comments_url, {"per_page": 100, "page": page})
if not comments:
break
for comment in comments:
author = comment.get("user", {}).get("login", "unknown")
created = comment.get("created_at", "")[:10]
body = comment.get("body", "") or ""
comments_text += f"\n\n---\n**Comment by @{author}** ({created}):\n{body}"
if len(comments) < 100:
break
page += 1
return comments_text
for repo in repos.split(","):
repo = repo.strip()
if "/" not in repo:
print(f"Skipping invalid repo format: {repo}")
continue
owner, name = repo.split("/", 1)
print(f"Fetching issues from {owner}/{name}...")
page = 1
repo_issues = []
while len(repo_issues) < max_issues_per_repo:
url = f"https://api.github.com/repos/{owner}/{name}/issues"
params = {
"state": state,
"labels": labels,
"per_page": 100,
"page": page
}
issues = api_request(url, params)
if not issues:
break
for issue in issues:
if "pull_request" in issue:
continue
labels_str = ", ".join([l["name"] for l in issue.get("labels", [])])
issue_url = issue.get("html_url", "")
created_at = issue.get("created_at", "")[:10]
updated_at = issue.get("updated_at", "")[:10]
# Build issue content with full metadata
content = f"# {issue['title']}\n\n"
content += f"**Repository:** {repo}\n"
content += f"**Issue:** #{issue['number']}\n"
content += f"**URL:** {issue_url}\n"
content += f"**Labels:** {labels_str}\n"
content += f"**State:** {issue['state']}\n"
content += f"**Created:** {created_at}\n"
content += f"**Updated:** {updated_at}\n\n"
content += issue.get("body", "") or ""
# Fetch and append comments
if issue.get("comments", 0) > 0:
comments = fetch_comments(owner, name, issue["number"])
content += comments
repo_issues.append({
"path": f"issues/{name}/{issue['number']}",
"content": content,
"file_name": f"issue-{name}-{issue['number']}.md",
"url": issue_url
})
if len(repo_issues) >= max_issues_per_repo:
break
page += 1
all_issues.extend(repo_issues)
print(f" Fetched {len(repo_issues)} issues from {repo}")
print(f"Total issues fetched: {len(all_issues)}")
with open(issues_data.path, 'w', encoding='utf-8') as f:
for issue_data in all_issues:
f.write(json.dumps(issue_data, ensure_ascii=False) + '\n')
@dsl.component(
base_image="pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime",
packages_to_install=["sentence-transformers", "langchain"]
)
def chunk_and_embed(
github_data: dsl.Input[dsl.Dataset],
repo_name: str,
base_url: str,
chunk_size: int,
chunk_overlap: int,
embedded_data: dsl.Output[dsl.Dataset]
):
import json
import os
import re
import torch
from sentence_transformers import SentenceTransformer
from langchain.text_splitter import RecursiveCharacterTextSplitter
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2', device=device)
print(f"Model loaded on {device}")
records = []
with open(github_data.path, 'r', encoding='utf-8') as f:
for line in f:
file_data = json.loads(line)
content = file_data['content']
# AGGRESSIVE CLEANING FOR BETTER EMBEDDINGS
# Remove Hugo frontmatter (both --- and +++ styles)
content = re.sub(r'^\s*[+\-]{3,}.*?[+\-]{3,}\s*', '', content, flags=re.DOTALL | re.MULTILINE)
# Remove Hugo template syntax
content = re.sub(r'\{\{.*?\}\}', '', content, flags=re.DOTALL)
# Remove HTML comments and tags
content = re.sub(r'<!--.*?-->', '', content, flags=re.DOTALL)
content = re.sub(r'<[^>]+>', ' ', content)
# Remove navigation/menu artifacts
content = re.sub(r'\b(Get Started|Contribute|GenAI|Home|Menu|Navigation)\b', '', content, flags=re.IGNORECASE)
# Clean up URLs and links
content = re.sub(r'https?://[^\s]+', '', content)
content = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', content) # Convert [text](url) to text
# Remove excessive whitespace and normalize
content = re.sub(r'\s+', ' ', content) # Multiple spaces to single
content = re.sub(r'\n\s*\n\s*\n+', '\n\n', content) # Multiple newlines to double
content = content.strip()
# Skip files that are too short after cleaning
if len(content) < 50:
print(f"Skipping file after cleaning: {file_data['path']} ({len(content)} chars)")
continue
# Build citation URL (same as before)
path_parts = file_data['path'].split('/')
if 'content/en/docs' in file_data['path']:
docs_index = path_parts.index('docs')
url_path = '/'.join(path_parts[docs_index+1:])
url_path = os.path.splitext(url_path)[0]
citation_url = f"{base_url}/{url_path}"
else:
citation_url = f"{base_url}/{file_data['path']}"
file_unique_id = f"{repo_name}:{file_data['path']}"
# Create splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
length_function=len,
separators=["\n\n", "\n", ". ", " ", ""]
)
# Split into chunks
chunks = text_splitter.split_text(content)
print(f"File: {file_data['path']} -> {len(chunks)} chunks (avg: {sum(len(c) for c in chunks)/len(chunks):.0f} chars)")
# Create embeddings
for chunk_idx, chunk in enumerate(chunks):
embedding = model.encode(chunk).tolist()
records.append({
'file_unique_id': file_unique_id,
'repo_name': repo_name,
'file_path': file_data['path'],
'file_name': file_data['file_name'],
'citation_url': citation_url[:1024],
'chunk_index': chunk_idx,
'content_text': chunk[:2000],
'embedding': embedding
})
print(f"Created {len(records)} total chunks")
with open(embedded_data.path, 'w', encoding='utf-8') as f:
for record in records:
f.write(json.dumps(record, ensure_ascii=False) + '\n')
@dsl.component(
base_image="python:3.9",
packages_to_install=["pymilvus", "numpy"]
)
def store_milvus(
embedded_data: dsl.Input[dsl.Dataset],
milvus_host: str,
milvus_port: str,
collection_name: str
):
from pymilvus import connections, utility, FieldSchema, CollectionSchema, DataType, Collection
import json
from datetime import datetime
connections.connect("default", host=milvus_host, port=milvus_port)
# DROP existing collection to fix schema mismatch
if utility.has_collection(collection_name):
utility.drop_collection(collection_name)
print(f"Dropped existing collection: {collection_name}")
# Enhanced schema with 768 dimensions
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
FieldSchema(name="file_unique_id", dtype=DataType.VARCHAR, max_length=512),
FieldSchema(name="repo_name", dtype=DataType.VARCHAR, max_length=256),
FieldSchema(name="file_path", dtype=DataType.VARCHAR, max_length=512),
FieldSchema(name="file_name", dtype=DataType.VARCHAR, max_length=256),
FieldSchema(name="citation_url", dtype=DataType.VARCHAR, max_length=1024),
FieldSchema(name="chunk_index", dtype=DataType.INT64),
FieldSchema(name="content_text", dtype=DataType.VARCHAR, max_length=2000),
FieldSchema(name="vector", dtype=DataType.FLOAT_VECTOR, dim=768), # Updated for all-mpnet-base-v2
FieldSchema(name="last_updated", dtype=DataType.INT64)
]
# Create new collection with correct schema
schema = CollectionSchema(fields, "RAG collection for documentation")
collection = Collection(collection_name, schema)
print(f"Created new collection: {collection_name}")
# Rest of your existing code remains the same...
records = []
timestamp = int(datetime.now().timestamp())
with open(embedded_data.path, 'r', encoding='utf-8') as f:
for line in f:
record = json.loads(line)
records.append({
"file_unique_id": record["file_unique_id"],
"repo_name": record["repo_name"],
"file_path": record["file_path"],
"file_name": record["file_name"],
"citation_url": record["citation_url"],
"chunk_index": record["chunk_index"],
"content_text": record["content_text"],
"vector": record["embedding"],
"last_updated": timestamp
})
if records:
batch_size = 1000
for i in range(0, len(records), batch_size):
batch = records[i:i + batch_size]
collection.insert(batch)
collection.flush()
# Create index
index_params = {
"metric_type": "COSINE",
"index_type": "IVF_FLAT",
"params": {"nlist": min(1024, len(records))}
}
collection.create_index("vector", index_params)
collection.load()
print(f"✅ Inserted {len(records)} records. Total: {collection.num_entities}")
@dsl.pipeline(
name="github-rag",
description="RAG pipeline for processing GitHub documentation"
)
def github_rag_pipeline(
repo_owner: str = "kubeflow",
repo_name: str = "website",
directory_path: str = "content/en",
github_token: str = "",
base_url: str = "https://www.kubeflow.org/docs",
chunk_size: int = 1000,
chunk_overlap: int = 100,
milvus_host: str = "milvus-standalone-final.docs-agent.svc.cluster.local",
milvus_port: str = "19530",
collection_name: str = "docs_rag"
):
# Download GitHub directory
download_task = download_github_directory(
repo_owner=repo_owner,
repo_name=repo_name,
directory_path=directory_path,
github_token=github_token
)
# Chunk and embed the content
chunk_task = chunk_and_embed(
github_data=download_task.outputs["github_data"],
repo_name=repo_name,
base_url=base_url,
chunk_size=chunk_size,
chunk_overlap=chunk_overlap
)
# Store in Milvus
store_task = store_milvus(
embedded_data=chunk_task.outputs["embedded_data"],
milvus_host=milvus_host,
milvus_port=milvus_port,
collection_name=collection_name
)
if __name__ == "__main__":
import os
# Set environment variable to disable caching by default
os.environ['KFP_DISABLE_EXECUTION_CACHING_BY_DEFAULT'] = 'true'
# Compile the pipeline with caching disabled by default
kfp.compiler.Compiler().compile(
pipeline_func=github_rag_pipeline,
package_path="github_rag_pipeline.yaml"
)