Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain[minor]: Firecrawl Document Loader #5180

Merged
merged 18 commits into from
Apr 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update firecrawl.ts
  • Loading branch information
nickscamara committed Apr 22, 2024
commit 6aa8895c44f102aa2fa5c4505399a62724e6b819
36 changes: 33 additions & 3 deletions langchain/src/document_loaders/web/firecrawl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ interface FirecrawlLoaderParameters {
mode?: "crawl" | "scrape";
params?: Record<string, unknown>;
}
interface FirecrawlDocument {
markdown: string;
metadata: Record<string, unknown>;
}
interface ScrapeResponse {
success: boolean;
data?: FirecrawlDocument;
error?: string;
}
/**
* Response interface for crawling operations.
*/
interface CrawlResponse {
success: boolean;
jobId?: string;
data?: FirecrawlDocument[];
error?: string;
}

/**
* Class representing a document loader for loading data from
Expand All @@ -28,8 +46,11 @@ interface FirecrawlLoaderParameters {
*/
export class FirecrawlLoader extends BaseDocumentLoader {
private apiKey: string;

private url: string;

private mode: "crawl" | "scrape";

private params?: Record<string, unknown>;

constructor(loaderParams: FirecrawlLoaderParameters) {
Expand Down Expand Up @@ -59,15 +80,24 @@ export class FirecrawlLoader extends BaseDocumentLoader {
*/
public async load(): Promise<Document[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer DocumentInterface

Suggested change
public async load(): Promise<Document[]> {
public async load(): Promise<DocumentInterface[]> {

const app = new FirecrawlApp({ apiKey: this.apiKey });
let firecrawlDocs: any[];
let firecrawlDocs: FirecrawlDocument[];

const processResponse = (response: any) => {
const processResponse = (
response: ScrapeResponse | CrawlResponse
): FirecrawlDocument[] => {
if (!response.success) {
throw new Error(
`Failed to ${this.mode} the URL using FirecrawlLoader. Error: ${response.error}`
);
}
return this.mode === "scrape" ? [response.data] : response.data;
if (!response.data) {
throw new Error(
`Failed to ${this.mode} the URL using FirecrawlLoader. No data returned.`
);
}
return this.mode === "scrape"
? ([response.data] as FirecrawlDocument[])
: (response.data as FirecrawlDocument[]);
};

if (this.mode === "scrape") {
Expand Down