mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 15:55:19 +00:00
I work at Firecrawl. This adds a Firecrawl page to Integrations > Data
Platforms & Frameworks: a Python recipe that crawls a site with
Firecrawl, stores the pages as Markdown chunks with their source URL in
a LanceDB table that embeds them through the embedding registry,
searches the table, and then adds web search results to the same table.
It follows the docs workflow in CONTRIBUTING.md:
- The example is a test, `docs/web-tests/py/test_firecrawl.py`, split
into five `--8<--` snippets. Like the other integration snippets that
need a provider key, it skips unless `FIRECRAWL_API_KEY` is set.
- `docs/web/snippets/firecrawl.mdx` is generated from it with `uv run
docs/web-tests/mdx_snippets_gen.py -s docs/web-tests/py -s
docs/web-tests/ts -s docs/web-tests/rs -o docs/web/snippets`. The
generator also rewrites other snippet files on current `main` without
this change, so I left those out.
- `docs/web/integrations/data/firecrawl.mdx` imports the snippets with
`<CodeBlock>` like the dlt page, and every heading has an explicit
`{#anchor}`.
- `docs/web/docs.json` adds the page at the end of the Data Platforms &
Frameworks group.
The embedding function is `sentence-transformers` with
`all-MiniLM-L6-v2`, so the recipe needs no API key besides Firecrawl's.
Each Firecrawl call passes `integration="_lancedb"`, a label that tells
Firecrawl a request came from this guide; the page says it is optional.
It needs `firecrawl-py` 4.3.3 or later.
## Testing
```
$ FIRECRAWL_API_KEY=... pytest docs/web-tests/py/test_firecrawl.py -v
test_firecrawl.py::test_firecrawl_crawl_and_search PASSED (lancedb 0.40.0, Python 3.12)
$ pytest docs/web-tests/py/test_firecrawl.py -rs (no key)
SKIPPED [1] Set FIRECRAWL_API_KEY to run this integration snippet
$ ruff format --check docs/web-tests/py/test_firecrawl.py && ruff check docs/web-tests/py/test_firecrawl.py
1 file already formatted
All checks passed!
```
Regenerating the snippets from the test produces the same
`docs/web/snippets/firecrawl.mdx`. I have not run the Mintlify site
checks (`mint validate`, broken-links), so please run them in CI or on
your side.
I drafted this with an AI coding assistant and ran the test above
myself.
202 lines
6.1 KiB
Python
202 lines
6.1 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
def require_env(var_name: str) -> str:
|
|
"""Skip the test unless the required environment variable is provided."""
|
|
value = os.environ.get(var_name)
|
|
if not value:
|
|
pytest.skip(f"Set {var_name} to run this integration snippet")
|
|
return value
|
|
|
|
|
|
def test_firecrawl_crawl_and_search() -> None:
|
|
require_env("FIRECRAWL_API_KEY")
|
|
pytest.importorskip("firecrawl")
|
|
pytest.importorskip("sentence_transformers")
|
|
|
|
# --8<-- [start:firecrawl_crawl]
|
|
import os
|
|
|
|
from firecrawl import Firecrawl
|
|
|
|
firecrawl = Firecrawl(api_key=os.environ["FIRECRAWL_API_KEY"])
|
|
|
|
crawl = firecrawl.crawl(
|
|
"https://docs.lancedb.com/enterprise/v1-14-0/integrations",
|
|
limit=10,
|
|
scrape_options={"formats": ["markdown"], "only_main_content": True},
|
|
integration="_lancedb",
|
|
)
|
|
pages = [page for page in crawl.data if page.markdown]
|
|
print(f"Crawled {len(pages)} pages")
|
|
# --8<-- [end:firecrawl_crawl]
|
|
|
|
assert pages
|
|
|
|
# --8<-- [start:firecrawl_chunk]
|
|
def chunk_markdown(markdown: str, max_chars: int = 1000) -> list[str]:
|
|
chunks, current = [], ""
|
|
for paragraph in markdown.split("\n\n"):
|
|
if current and len(current) + len(paragraph) > max_chars:
|
|
chunks.append(current.strip())
|
|
current = ""
|
|
current += paragraph + "\n\n"
|
|
if current.strip():
|
|
chunks.append(current.strip())
|
|
return chunks
|
|
|
|
# --8<-- [end:firecrawl_chunk]
|
|
|
|
# --8<-- [start:firecrawl_table]
|
|
import tempfile
|
|
|
|
import lancedb
|
|
from lancedb.embeddings import get_registry
|
|
from lancedb.pydantic import LanceModel, Vector
|
|
|
|
embedder = (
|
|
get_registry().get("sentence-transformers").create(name="all-MiniLM-L6-v2")
|
|
)
|
|
|
|
class WebChunk(LanceModel):
|
|
text: str = embedder.SourceField()
|
|
vector: Vector(embedder.ndims()) = embedder.VectorField()
|
|
source_url: str
|
|
title: str
|
|
|
|
rows = [
|
|
{
|
|
"text": chunk,
|
|
"source_url": page.metadata.source_url,
|
|
"title": page.metadata.title or "",
|
|
}
|
|
for page in pages
|
|
for chunk in chunk_markdown(page.markdown)
|
|
]
|
|
|
|
db = lancedb.connect(tempfile.mkdtemp(prefix="lancedb-firecrawl-"))
|
|
table = db.create_table("firecrawl_docs", schema=WebChunk)
|
|
table.add(rows)
|
|
print(f"Added {table.count_rows()} chunks")
|
|
# --8<-- [end:firecrawl_table]
|
|
|
|
assert table.count_rows() == len(rows) > 0
|
|
|
|
# --8<-- [start:firecrawl_query]
|
|
results = (
|
|
table.search("How do I query a LanceDB table with SQL?")
|
|
.select(["title", "source_url", "text", "_distance"])
|
|
.limit(3)
|
|
.to_list()
|
|
)
|
|
for row in results:
|
|
print(f"{row['_distance']:.3f}", row["source_url"])
|
|
# --8<-- [end:firecrawl_query]
|
|
|
|
assert len(results) == 3
|
|
assert all(row["source_url"].startswith("https://") for row in results)
|
|
|
|
# --8<-- [start:firecrawl_web_search]
|
|
search = firecrawl.search(
|
|
"lancedb hybrid search reranking",
|
|
limit=5,
|
|
scrape_options={"formats": ["markdown"], "only_main_content": True},
|
|
integration="_lancedb",
|
|
)
|
|
table.add(
|
|
[
|
|
{
|
|
"text": chunk,
|
|
"source_url": result.metadata.source_url,
|
|
"title": result.metadata.title or "",
|
|
}
|
|
for result in search.web or []
|
|
if getattr(result, "markdown", None)
|
|
for chunk in chunk_markdown(result.markdown)
|
|
]
|
|
)
|
|
print(f"Table now holds {table.count_rows()} chunks")
|
|
# --8<-- [end:firecrawl_web_search]
|
|
|
|
assert table.count_rows() > len(rows)
|
|
|
|
|
|
def test_firecrawl_search_with_mixed_results(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Run the examples on canned responses where search mixes plain and scraped results."""
|
|
firecrawl = pytest.importorskip("firecrawl")
|
|
pytest.importorskip("sentence_transformers")
|
|
from firecrawl.v2.types import (
|
|
CrawlJob,
|
|
Document,
|
|
DocumentMetadata,
|
|
SearchData,
|
|
SearchResultWeb,
|
|
)
|
|
|
|
def page(url: str, title: str, markdown: str) -> Document:
|
|
return Document(
|
|
markdown=markdown,
|
|
metadata=DocumentMetadata(source_url=url, title=title),
|
|
)
|
|
|
|
class FakeFirecrawl:
|
|
def __init__(self, api_key: str) -> None:
|
|
pass
|
|
|
|
def crawl(self, url: str, **kwargs) -> CrawlJob:
|
|
return CrawlJob(
|
|
status="completed",
|
|
total=3,
|
|
completed=3,
|
|
data=[
|
|
page(
|
|
f"https://docs.lancedb.com/{slug}",
|
|
slug.title(),
|
|
f"# {slug.title()}\n\nLanceDB {slug} guide.",
|
|
)
|
|
for slug in ("sql", "search", "indexing")
|
|
],
|
|
)
|
|
|
|
def search(self, query: str, **kwargs) -> SearchData:
|
|
return SearchData(
|
|
web=[
|
|
SearchResultWeb(
|
|
url="https://example.com/unscraped",
|
|
title="Unscraped result",
|
|
),
|
|
page(
|
|
"https://example.com/hybrid-search",
|
|
"Hybrid search",
|
|
"# Hybrid search\n\nReranking LanceDB results.",
|
|
),
|
|
]
|
|
)
|
|
|
|
import lancedb
|
|
|
|
connections = []
|
|
connect = lancedb.connect
|
|
|
|
def recording_connect(*args, **kwargs):
|
|
connections.append(connect(*args, **kwargs))
|
|
return connections[-1]
|
|
|
|
monkeypatch.setenv("FIRECRAWL_API_KEY", "fc-test")
|
|
monkeypatch.setattr(firecrawl, "Firecrawl", FakeFirecrawl)
|
|
monkeypatch.setattr(lancedb, "connect", recording_connect)
|
|
|
|
test_firecrawl_crawl_and_search()
|
|
|
|
source_urls = (
|
|
connections[0].open_table("firecrawl_docs").to_pandas()["source_url"].tolist()
|
|
)
|
|
assert len(source_urls) == 4
|
|
assert source_urls.count("https://example.com/hybrid-search") == 1
|
|
assert "https://example.com/unscraped" not in source_urls
|