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

feat: add 'skip GPT processing' Globs #38

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions actors/extended-gpt-scraper/.actor/input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
"default": 10,
"unit": "pages"
},
"skipGptGlobs": {
"title": "Skip GPT processing for Globs",
"type": "array",
"description": "This setting allows you to specify certain page URLs to skip GPT instructions for. Pages matching these glob patterns will only be crawled for links, excluding them from GPT processing. Useful for intermediary pages used for navigation or undesired content.",
"editor": "globs",
"default": [],
"prefill": []
},
"useStructureOutput": {
"sectionCaption": "Formatted output",
"sectionDescription": "By default, the scraper outputs text answers for each page. If you want to get data in a structured format, you can define a JSON schema. The scraper uses [function](https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions), which is called for each page. The function receives the page content and returns the answer in the defined JSON format.",
Expand Down
18 changes: 10 additions & 8 deletions actors/gpt-scraper/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ if (process.env.ACTOR_MAX_PAID_DATASET_ITEMS) {
await updateDeprecatedInput(input);

if (process.env.OPENAI_API_KEY) {
const crawler = await createCrawler({
input: {
...input,
maxPagesPerCrawl: maxRequestsPerCrawl,
model: DEFAULT_PEY_PER_RESULT_OPENAI_MODEL,
openaiApiKey: process.env.OPENAI_API_KEY,
},
});
/** Input for the PPR Actor. We explicitly override some of these values, so that users don't change them. */
const adjustedPayPerResultInput = {
...input,
maxPagesPerCrawl: maxRequestsPerCrawl,
skipGptGlobs: [],
Patai5 marked this conversation as resolved.
Show resolved Hide resolved
model: DEFAULT_PEY_PER_RESULT_OPENAI_MODEL,
openaiApiKey: process.env.OPENAI_API_KEY,
};

const crawler = await createCrawler({ input: adjustedPayPerResultInput });

// We explicitly remove it so we are sure the key is only passed through params to remove double source of truth
delete process.env.OPENAI_API_KEY;
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/gpt-scraper-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"gpt-3-encoder": "^1.1.4",
"joplin-turndown-plugin-gfm": "^1.0.12",
"langchain": "^0.0.209",
"minimatch": "^9.0.3",
"openai": "^3.3.0",
"playwright": "*",
"turndown": "^7.1.2"
Expand Down
9 changes: 7 additions & 2 deletions packages/gpt-scraper-core/src/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import { getModelByName } from './models/models.js';
import { getNumberOfTextTokens, htmlToMarkdown, maybeShortsTextByTokenLength, shrinkHtml } from './processors.js';
import { Input, PAGE_FORMAT } from './types/input.js';
import { parseInput } from './input.js';
import { NonRetryableOpenaiAPIError } from './errors.js';
import { parseInput, validateInput, validateInputCssSelectors } from './input.js';
import { NonRetryableOpenaiAPIError } from './errors.js';
import { OpenAIModelSettings } from './types/models.js';
import { doesUrlMatchGlobs } from './utils.js';

interface State {
pageOutputted: number;
Expand All @@ -30,7 +30,7 @@
addFormats(validator);
validator.compile(schema);
return schema;
} catch (e: any) {

Check warning on line 33 in packages/gpt-scraper-core/src/crawler.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
log.error(`Schema is not valid: ${e.message}`, { error: e });
await Actor.fail('Schema is not valid. Go to Actor run log, '
+ 'where you can find error details or disable "Use JSON schema to format answer" option.');
Expand Down Expand Up @@ -118,6 +118,11 @@
);
}

const skipGptProcessing = input.skipGptGlobs && doesUrlMatchGlobs(url, input.skipGptGlobs);
if (skipGptProcessing) {
log.info(`Skipping page from GPT processing because it matched 'skipGptGlobs', crawling only.`, { url });
}

// A function to be evaluated by Playwright within the browser context.
let originContentHtml;
if (input.targetSelector) {
Expand Down Expand Up @@ -183,7 +188,7 @@
answer = answerResult.answer;
jsonAnswer = answerResult.jsonAnswer;
model.updateApiCallUsage(answerResult.usage);
} catch (error: any) {

Check warning on line 191 in packages/gpt-scraper-core/src/crawler.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error instanceof NonRetryableOpenaiAPIError) {
throw await Actor.fail(error.message);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gpt-scraper-core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { HTML_TAGS_TO_IGNORE, type Input, PAGE_FORMAT } from './input.js';
export { type Input, PAGE_FORMAT } from './input.js';
export { type OpenAIModelSettings } from './models.js';
1 change: 1 addition & 0 deletions packages/gpt-scraper-core/src/types/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Input extends OpenAIModelSettings {
useStructureOutput?: boolean;
pageFormatInRequest?: PAGE_FORMAT;
saveSnapshots?: boolean;
skipGptGlobs?: GlobInput[];
initialCookies?: Cookie[];
removeElementsCssSelector?: string;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/gpt-scraper-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GlobInput } from 'crawlee';
import { minimatch } from 'minimatch';

export const doesUrlMatchGlobs = (url: string, globs: GlobInput[]): boolean => {
return globs.some((glob) => doesUrlMatchGlob(url, glob));
};

const doesUrlMatchGlob = (url: string, glob: GlobInput): boolean => {
const globString = typeof glob === 'string' ? glob : glob.glob;

return minimatch(url, globString, { nocase: true });
};
Loading