-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl_pydantic_ai_docs.py
More file actions
330 lines (278 loc) · 11.3 KB
/
crawl_pydantic_ai_docs.py
File metadata and controls
330 lines (278 loc) · 11.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
import os
import sys
import json
import asyncio
import requests
from xml.etree import ElementTree
from typing import List, Dict, Any
from dataclasses import dataclass
from datetime import datetime, timezone
from urllib.parse import urlparse
from dotenv import load_dotenv
from crawl4ai import AsyncWebCrawler, BrowserConfig, CrawlerRunConfig, CacheMode
# from openai import AsyncOpenAI
from groq import AsyncGroq
from supabase import create_client, Client
import google.generativeai as genai
load_dotenv()
# Initialize OpenAI and Supabase clients
groq_client = AsyncGroq(api_key=os.getenv("GROQ_API_KEY"))
# openai_client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
GEMINI_EMBED_MODEL = os.getenv("GEMINI_EMBED_MODEL", "models/text-embedding-004")
EMBED_DIM = 768 # adjust if you change models
supabase: Client = create_client(
os.getenv("SUPABASE_URL"),
os.getenv("SUPABASE_SERVICE_KEY")
)
@dataclass
class ProcessedChunk:
url: str
chunk_number: int
title: str
summary: str
content: str
metadata: Dict[str, Any]
embedding: List[float]
def chunk_text(text: str, chunk_size: int = 5000) -> List[str]:
"""Split text into chunks, respecting code blocks and paragraphs."""
chunks = []
start = 0
text_length = len(text)
while start < text_length:
# Calculate end position
end = start + chunk_size
# If we're at the end of the text, just take what's left
if end >= text_length:
chunks.append(text[start:].strip())
break
# Try to find a code block boundary first (```)
chunk = text[start:end]
code_block = chunk.rfind('```')
if code_block != -1 and code_block > chunk_size * 0.3:
end = start + code_block
# If no code block, try to break at a paragraph
elif '\n\n' in chunk:
# Find the last paragraph break
last_break = chunk.rfind('\n\n')
if last_break > chunk_size * 0.3: # Only break if we're past 30% of chunk_size
end = start + last_break
# If no paragraph break, try to break at a sentence
elif '. ' in chunk:
# Find the last sentence break
last_period = chunk.rfind('. ')
if last_period > chunk_size * 0.3: # Only break if we're past 30% of chunk_size
end = start + last_period + 1
# Extract chunk and clean it up
chunk = text[start:end].strip()
if chunk:
chunks.append(chunk)
# Move start position for next chunk
start = max(start + 1, end)
return chunks
# async def get_title_and_summary(chunk: str, url: str) -> Dict[str, str]:
# """Extract title and summary using GPT-4."""
# system_prompt = """You are an AI that extracts titles and summaries from documentation chunks.
# Return a JSON object with 'title' and 'summary' keys.
# For the title: If this seems like the start of a document, extract its title. If it's a middle chunk, derive a descriptive title.
# For the summary: Create a concise summary of the main points in this chunk.
# Keep both title and summary concise but informative."""
# try:
# response = await openai_client.chat.completions.create(
# model=os.getenv("LLM_MODEL", "gpt-4o-mini"),
# messages=[
# {"role": "system", "content": system_prompt},
# {"role": "user", "content": f"URL: {url}\n\nContent:\n{chunk[:1000]}..."} # Send first 1000 chars for context
# ],
# response_format={ "type": "json_object" }
# )
# return json.loads(response.choices[0].message.content)
# except Exception as e:
# print(f"Error getting title and summary: {e}")
# return {"title": "Error processing title", "summary": "Error processing summary"}
# ===============================================
# Update get_title_and_summary function (around line 90):
async def get_title_and_summary(chunk: str, url: str) -> Dict[str, str]:
"""Extract title and summary using Groq."""
system_prompt = """You are an AI that extracts titles and summaries from documentation chunks.
Return a JSON object with 'title' and 'summary' keys.
For the title: If this seems like the start of a document, extract its title. If it's a middle chunk, derive a descriptive title.
For the summary: Create a concise summary of the main points in this chunk.
Keep both title and summary concise but informative."""
try:
response = await groq_client.chat.completions.create(
model=os.getenv("LLM_MODEL", "llama-3.1-8b-instant"),
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"URL: {url}\n\nContent:\n{chunk[:1000]}..."}
],
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
except Exception as e:
print(f"Error getting title and summary: {e}")
return {"title": "Error processing title", "summary": "Error processing summary"}
# ===============================================
# async def get_embedding(text: str) -> List[float]:
# """Get embedding vector from OpenAI."""
# try:
# response = await openai_client.embeddings.create(
# model="text-embedding-3-small",
# input=text
# )
# return response.data[0].embedding
# except Exception as e:
# print(f"Error getting embedding: {e}")
# return [0] * 1536 # Return zero vector on error
# ===============================================
# NEW:
async def get_embedding(text: str) -> List[float]:
try:
result = await asyncio.to_thread(
genai.embed_content,
model=GEMINI_EMBED_MODEL,
content=text,
task_type="retrieval_document",
)
return result["embedding"]
except Exception as e:
print(f"Error getting embedding: {e}")
return [0.0] * EMBED_DIM
# ===============================================
async def process_chunk(chunk: str, chunk_number: int, url: str) -> ProcessedChunk:
"""Process a single chunk of text."""
# Get title and summary
extracted = await get_title_and_summary(chunk, url)
# Get embedding
embedding = await get_embedding(chunk)
# Create metadata
metadata = {
"source": "pydantic_ai_docs",
"chunk_size": len(chunk),
"crawled_at": datetime.now(timezone.utc).isoformat(),
"url_path": urlparse(url).path
}
return ProcessedChunk(
url=url,
chunk_number=chunk_number,
title=extracted['title'],
summary=extracted['summary'],
content=chunk, # Store the original chunk content
metadata=metadata,
embedding=embedding
)
async def insert_chunk(chunk: ProcessedChunk):
"""Insert a processed chunk into Supabase."""
try:
data = {
"url": chunk.url,
"chunk_number": chunk.chunk_number,
"title": chunk.title,
"summary": chunk.summary,
"content": chunk.content,
"metadata": chunk.metadata,
"embedding": chunk.embedding
}
result = supabase.table("site_pages").insert(data).execute()
print(f"Inserted chunk {chunk.chunk_number} for {chunk.url}")
return result
except Exception as e:
print(f"Error inserting chunk: {e}")
return None
async def process_and_store_document(url: str, markdown: str):
"""Process a document and store its chunks in parallel."""
# Split into chunks
chunks = chunk_text(markdown)
# Process chunks in parallel
tasks = [
process_chunk(chunk, i, url)
for i, chunk in enumerate(chunks)
]
processed_chunks = await asyncio.gather(*tasks)
# Store chunks in parallel
insert_tasks = [
insert_chunk(chunk)
for chunk in processed_chunks
]
await asyncio.gather(*insert_tasks)
async def crawl_parallel(urls: List[str], max_concurrent: int = 5):
"""Crawl multiple URLs in parallel with a concurrency limit."""
browser_config = BrowserConfig(
headless=True,
verbose=False,
extra_args=["--disable-gpu", "--disable-dev-shm-usage", "--no-sandbox"],
)
crawl_config = CrawlerRunConfig(cache_mode=CacheMode.BYPASS)
# Create the crawler instance
crawler = AsyncWebCrawler(config=browser_config)
await crawler.start()
try:
# Create a semaphore to limit concurrency
semaphore = asyncio.Semaphore(max_concurrent)
async def process_url(url: str):
async with semaphore:
result = await crawler.arun(
url=url,
config=crawl_config,
session_id="session1"
)
if result.success:
print(f"Successfully crawled: {url}")
await process_and_store_document(url, result.markdown_v2.raw_markdown)
else:
print(f"Failed: {url} - Error: {result.error_message}")
# Process all URLs in parallel with limited concurrency
await asyncio.gather(*[process_url(url) for url in urls])
finally:
await crawler.close()
# def get_pydantic_ai_docs_urls() -> List[str]:
# """Get URLs from Pydantic AI docs sitemap."""
# sitemap_url = "https://ai.pydantic.dev/sitemap.xml"
# try:
# response = requests.get(sitemap_url)
# response.raise_for_status()
# # Parse the XML
# root = ElementTree.fromstring(response.content)
# # Extract all URLs from the sitemap
# namespace = {'ns': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
# urls = [loc.text for loc in root.findall('.//ns:loc', namespace)]
# return urls
# except Exception as e:
# print(f"Error fetching sitemap: {e}")
# return []
# async def main():
# # Get URLs from Pydantic AI docs
# urls = get_pydantic_ai_docs_urls()
# if not urls:
# print("No URLs found to crawl")
# return
# print(f"Found {len(urls)} URLs to crawl")
# await crawl_parallel(urls)
def get_sitemap_urls(sitemap_url: str) -> List[str]:
"""Fetch all URLs from sitemap or sitemap index."""
urls = []
try:
response = requests.get(sitemap_url)
response.raise_for_status()
root = ElementTree.fromstring(response.content)
ns = {'ns': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
# If it's a sitemap index, recurse into each sitemap
sitemap_tags = root.findall('.//ns:sitemap/ns:loc', ns)
if sitemap_tags:
for sitemap in sitemap_tags:
urls += get_sitemap_urls(sitemap.text)
else:
urls = [loc.text for loc in root.findall('.//ns:url/ns:loc', ns)]
except Exception as e:
print(f"Error fetching sitemap: {e}")
return urls
async def main():
sitemap_url = "https://dignizant.com/sitemap.xml"
urls = get_sitemap_urls(sitemap_url)
if not urls:
print("No URLs found to crawl")
return
print(f"Found {len(urls)} URLs to crawl")
await crawl_parallel(urls)
if __name__ == "__main__":
asyncio.run(main())