-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathingest_studio1.py
More file actions
319 lines (272 loc) · 11.1 KB
/
ingest_studio1.py
File metadata and controls
319 lines (272 loc) · 11.1 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
"""
Example ingestion script for the original Studio1 Customer Support demo.
Today, the recommended way to ingest docs is via the **Streamlit sidebar**
(paste URLs and click "Extract & store to Memori"). This script is kept as a
convenience for rebuilding the Studio1 knowledge base from its public
docs/marketing site.
It uses Firecrawl to crawl https://www.studio1hq.com/ and ingests the content
into Memori v3 as a searchable knowledge base.
"""
import os
from typing import List
from dotenv import load_dotenv
from firecrawl import FirecrawlApp
from memori import Memori
from openai import OpenAI
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
load_dotenv()
# Primary docs site root used for broad crawling
STUDIO1_URL = "https://docs.studio1hq.com/"
# High-value pages we always want to ingest explicitly, even if the crawler
# misses them due to depth/links. This includes services, legal, and marketing
# "About" pages.
STUDIO1_STATIC_URLS = [
"https://docs.studio1hq.com/about-us",
"https://docs.studio1hq.com/faq",
"https://docs.studio1hq.com/services/technical-content",
"https://docs.studio1hq.com/services/developer-advocacy",
"https://docs.studio1hq.com/services/tech-video-production",
"https://docs.studio1hq.com/services/audit-services",
"https://docs.studio1hq.com/services/organic-campaign",
"https://docs.studio1hq.com/services/product-launch",
"https://docs.studio1hq.com/services/influencer-management",
"https://docs.studio1hq.com/terms-of-use",
"https://docs.studio1hq.com/privacy-policy",
"https://www.studio1hq.com/about-us",
]
def _init_memori() -> tuple[Memori, OpenAI]:
"""Initialize Memori v3 with SQLAlchemy + OpenAI, mirroring ai_consultant_agent.
Returns (Memori instance, OpenAI client) so we can use the registered
OpenAI client to drive Memori's automatic ingestion.
"""
openai_key = os.getenv("OPENAI_API_KEY", "")
if not openai_key:
raise RuntimeError("OPENAI_API_KEY is not set.")
db_path = os.getenv("SQLITE_DB_PATH", "./memori.sqlite")
database_url = f"sqlite:///{db_path}"
engine = create_engine(
database_url,
pool_pre_ping=True,
connect_args={"check_same_thread": False},
)
# Optional connectivity check
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
client = OpenAI(api_key=openai_key)
mem = Memori(conn=SessionLocal).openai.register(client)
mem.attribution(entity_id="studio1-support-kb", process_id="studio1-ingest")
mem.config.storage.build()
return mem, client
def _crawl_studio1() -> List[dict]:
"""Use Firecrawl to crawl the Studio1 docs site and return extracted pages."""
firecrawl_key = os.getenv("FIRECRAWL_API_KEY", "")
if not firecrawl_key:
raise RuntimeError("FIRECRAWL_API_KEY is not set.")
app = FirecrawlApp(api_key=firecrawl_key)
# Basic crawl config; this can be tuned later.
# Modern Firecrawl Python SDK exposes a `crawl(url, *, limit, scrape_options, ...)`
# signature and returns a Pydantic model (e.g. CrawlStatus / CrawlJob).
limit = 50
# Firecrawl v2 expects `scrape_options.formats` to be a ScrapeFormats model
# or a list of *supported* format literals. "text" is not a valid format,
# so we just request markdown + HTML and derive plain text ourselves later.
scrape_options = {
"formats": ["markdown", "html"],
"onlyMainContent": True,
}
if hasattr(app, "crawl_url"):
# Some SDK variants expose `crawl_url`; keep this for backwards-compat.
job = app.crawl_url(
url=STUDIO1_URL,
limit=limit,
scrape_options=scrape_options,
)
elif hasattr(app, "crawl"):
# Preferred modern API, matching docs.firecrawl.dev.
job = app.crawl(
STUDIO1_URL,
limit=limit,
scrape_options=scrape_options,
)
else:
raise RuntimeError(
"Installed Firecrawl client has neither `crawl_url` nor `crawl` method. "
"Please check your `firecrawl-py` version and docs."
)
# Normalize Firecrawl response into a list of page dicts.
# - Newer SDKs return a Pydantic model (e.g. CrawlJob / CrawlStatus)
# - Older SDKs may return a plain dict with a 'data' key
if isinstance(job, dict):
pages = job.get("data") or job.get("pages") or job
else:
# Pydantic models don't support `.get`, but do support attributes and `.model_dump()`.
pages = (
getattr(job, "data", None)
or getattr(job, "pages", None)
or getattr(job, "results", None)
)
if pages is None:
if hasattr(job, "model_dump"):
# Pydantic v2
data = job.model_dump()
elif hasattr(job, "dict"):
# Backwards compat with Pydantic v1
data = job.dict()
else:
raise RuntimeError(
f"Unexpected Firecrawl response type (no data/pages/results): {type(job)}"
)
pages = data.get("data") or data.get("pages") or data.get("results") or data
if not isinstance(pages, list):
raise RuntimeError(f"Unexpected Firecrawl response format: {type(pages)}")
return pages
def _scrape_static_pages() -> List[dict]:
"""Use Firecrawl to scrape a fixed list of high-value Studio1 URLs."""
firecrawl_key = os.getenv("FIRECRAWL_API_KEY", "")
if not firecrawl_key:
raise RuntimeError("FIRECRAWL_API_KEY is not set.")
app = FirecrawlApp(api_key=firecrawl_key)
results: List[dict] = []
for url in STUDIO1_STATIC_URLS:
try:
if hasattr(app, "scrape"):
doc = app.scrape(
url=url,
formats=["markdown", "html"],
onlyMainContent=True,
)
elif hasattr(app, "scrape_url"):
# Backwards-compat for older SDKs
doc = app.scrape_url(
url=url,
scrape_options={
"formats": ["markdown", "html"],
"onlyMainContent": True,
},
)
else:
raise RuntimeError(
"Installed Firecrawl client has neither `scrape` nor `scrape_url`."
)
# Normalise into a dict similar to crawl results
if isinstance(doc, dict):
data = doc.get("data") or doc
else:
if hasattr(doc, "model_dump"):
data = doc.model_dump()
elif hasattr(doc, "dict"):
data = doc.dict()
else:
data = doc
# Some SDKs wrap the page document under "data"
if isinstance(data, dict) and "markdown" in data or "html" in data:
page = data
elif isinstance(data, dict) and isinstance(data.get("data"), dict):
page = data["data"]
else:
# Fallback – if it's already shaped like a page list, skip here
if isinstance(data, list):
# Append each if we somehow got multiple docs back
for p in data:
if isinstance(p, dict):
results.append(p)
continue
page = data
if isinstance(page, dict):
# Ensure URL is set for deduplication later
page.setdefault("url", url)
results.append(page)
except Exception as e:
print(f"[Firecrawl] Warning: could not scrape {url}: {e}")
return results
def ingest():
mem, client = _init_memori()
crawled_pages = _crawl_studio1()
static_pages = _scrape_static_pages()
# Deduplicate by URL to avoid double-ingesting the same page from crawl+scrape
pages: List[dict] = []
seen_urls = set()
def _add_pages(src_pages: List[dict]):
for p in src_pages:
url = None
if isinstance(p, dict):
meta = p.get("metadata") or {}
url = p.get("url") or meta.get("sourceURL")
if not url:
# Fallback to id of object to prevent accidental merge
key = id(p)
else:
key = url
if key in seen_urls:
continue
seen_urls.add(key)
pages.append(p)
_add_pages(crawled_pages)
_add_pages(static_pages)
print(f"Fetched {len(pages)} pages from Studio1 docs + static URLs")
for idx, page in enumerate(pages, start=1):
# Firecrawl v2 returns Pydantic models (e.g. Document) rather than plain dicts.
# Normalize each item into a dict first.
if isinstance(page, dict):
page_dict = page
else:
if hasattr(page, "model_dump"):
page_dict = page.model_dump()
elif hasattr(page, "dict"):
page_dict = page.dict()
else:
raise RuntimeError(f"Unexpected page type from Firecrawl: {type(page)}")
metadata = page_dict.get("metadata") or {}
url = page_dict.get("url") or metadata.get("sourceURL") or STUDIO1_URL
markdown = (
page_dict.get("markdown")
or page_dict.get("text")
or page_dict.get("content")
or ""
)
if not markdown:
continue
title = page_dict.get("title") or metadata.get("title") or f"Studio1 Page {idx}"
doc_text = f"""Studio1 Documentation Page
Title: {title}
URL: {url}
Content:
{markdown}
"""
print(f"Ingesting page {idx}: {url}")
# Use the registered OpenAI client so Memori can automatically
# capture this as a "conversation" / memory. We keep the prompt
# lightweight and ask the model to simply acknowledge storage.
try:
_ = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": (
"Store the following Studio1 documentation page in "
"memory for future retrieval. Respond with a short "
"acknowledgement only.\n\n"
f"{doc_text}"
),
}
],
)
except Exception as record_err:
# Don't abort ingestion if a single record fails; just log it.
print(f"[Memori] Could not index page {idx} ({url}): {record_err}")
# Ensure any buffered writes are flushed to the backing store.
try:
adapter = getattr(mem.config.storage, "adapter", None)
if adapter is not None:
adapter.commit()
adapter.close()
except Exception as final_err:
print(
f"[Memori] Warning: issue committing/closing storage adapter: {final_err}"
)
print("Ingestion complete.")
if __name__ == "__main__":
ingest()