-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mendableai/main
Fix FIRECRAWL_API_URL bug, also various PyLint fixes
- Loading branch information
Showing
26 changed files
with
1,726 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,38 @@ | ||
import OpenAI from "openai"; | ||
import { Document } from "../../lib/entities"; | ||
import { numTokensFromString } from "./helpers"; | ||
|
||
export type ScraperCompletionResult = { | ||
data: any | null; | ||
url: string; | ||
}; | ||
|
||
const maxTokens = 32000; | ||
const modifier = 4; | ||
const defaultPrompt = | ||
"You are a professional web scraper. Extract the contents of the webpage"; | ||
|
||
function prepareOpenAIDoc( | ||
document: Document | ||
): OpenAI.Chat.Completions.ChatCompletionContentPart[] { | ||
// Check if the markdown content exists in the document | ||
if (!document.markdown) { | ||
): [OpenAI.Chat.Completions.ChatCompletionContentPart[], number] { | ||
let markdown = document.markdown; | ||
|
||
// Check if the markdown content exists in the document | ||
if (!markdown) { | ||
throw new Error( | ||
"Markdown content is missing in the document. This is likely due to an error in the scraping process. Please try again or reach out to [email protected]" | ||
); | ||
} | ||
|
||
return [{ type: "text", text: document.markdown }]; | ||
// count number of tokens | ||
const numTokens = numTokensFromString(document.markdown, "gpt-4"); | ||
|
||
if (numTokens > maxTokens) { | ||
// trim the document to the maximum number of tokens, tokens != characters | ||
markdown = markdown.slice(0, (maxTokens * modifier)); | ||
} | ||
|
||
return [[{ type: "text", text: markdown }], numTokens]; | ||
} | ||
|
||
export async function generateOpenAICompletions({ | ||
|
@@ -38,7 +51,7 @@ export async function generateOpenAICompletions({ | |
temperature?: number; | ||
}): Promise<Document> { | ||
const openai = client as OpenAI; | ||
const content = prepareOpenAIDoc(document); | ||
const [content, numTokens] = prepareOpenAIDoc(document); | ||
|
||
const completion = await openai.chat.completions.create({ | ||
model, | ||
|
@@ -72,6 +85,7 @@ export async function generateOpenAICompletions({ | |
return { | ||
...document, | ||
llm_extraction: llmExtraction, | ||
warning: numTokens > maxTokens ? `Page was trimmed to fit the maximum token limit defined by the LLM model (Max: ${maxTokens} tokens, Attemped: ${numTokens} tokens). If results are not good, email us at [email protected] so we can help you.` : undefined, | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { scrapWithFireEngine } from "../../src/scraper/WebScraper/single_url"; | ||
|
||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const scrapInBatches = async ( | ||
urls: string[], | ||
batchSize: number, | ||
delayMs: number | ||
) => { | ||
let successCount = 0; | ||
let errorCount = 0; | ||
|
||
for (let i = 0; i < urls.length; i += batchSize) { | ||
const batch = urls | ||
.slice(i, i + batchSize) | ||
.map((url) => scrapWithFireEngine(url)); | ||
try { | ||
const results = await Promise.all(batch); | ||
results.forEach((data, index) => { | ||
if (data.trim() === "") { | ||
errorCount++; | ||
} else { | ||
successCount++; | ||
console.log( | ||
`Scraping result ${i + index + 1}:`, | ||
data.trim().substring(0, 20) + "..." | ||
); | ||
} | ||
}); | ||
} catch (error) { | ||
console.error("Error during scraping:", error); | ||
} | ||
await delay(delayMs); | ||
} | ||
|
||
console.log(`Total successful scrapes: ${successCount}`); | ||
console.log(`Total errored scrapes: ${errorCount}`); | ||
}; | ||
function run() { | ||
const urls = Array.from({ length: 200 }, () => "https://scrapethissite.com"); | ||
scrapInBatches(urls, 10, 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.