Skip to content

Commit

Permalink
feat: add GPT-4o-mini as default PPR model (#69)
Browse files Browse the repository at this point in the history
* feat: add GPT-4o-mini model

* feat: add generation token limit

* feat: set GPT-4o-mini as default PPR scraper model

* docs: update CHANGELOG.md
  • Loading branch information
Patai5 authored Jul 30, 2024
1 parent b34526c commit f0bb799
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
6 changes: 4 additions & 2 deletions actors/extended-gpt-scraper/.actor/input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"gpt-4-32k",
"text-davinci-003",
"gpt-4-turbo",
"gpt-4o"
"gpt-4o",
"gpt-4o-mini"
],
"enumTitles": [
"GPT-3.5 Turbo",
Expand All @@ -48,7 +49,8 @@
"GPT-4 32k",
"GTP-3 (davinci)",
"GPT-4 Turbo (Preview)",
"GPT-4o"
"GPT-4o",
"gpt-4o-mini"
]
},
"includeUrlGlobs": {
Expand Down
2 changes: 1 addition & 1 deletion code/src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function adjustPprScraperInput(input: Input) {
return {
...input,
skipGptGlobs: [],
model: 'gpt-3.5-turbo',
model: 'DEFAULT_PPR_SCRAPER',
openaiApiKey: getOpenAiApiKeyEnvOrFail(),
actorType: ACTORS.PPR_PRICING,
};
Expand Down
23 changes: 23 additions & 0 deletions code/src/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,27 @@ const OPEN_AI_MODELS: { [modelKey: string]: ModelConfig } = {
output: 0.015,
},
},
'gpt-4o-mini': {
modelName: 'gpt-4o-mini',
maxTokens: 128_000,
interface: 'chat',
cost: {
input: 0.00015,
output: 0.0006,
},
},
/**
* Default scraper model that we use for the PPR scraper.
* - We limit the max tokens here so the price is capped.
*/
DEFAULT_PPR_SCRAPER: {
modelName: 'gpt-4o-mini',
limitGenerationTokens: true,
maxTokens: 10_000,
interface: 'chat',
cost: {
input: 0.00015,
output: 0.0006,
},
},
} as const;
7 changes: 5 additions & 2 deletions code/src/models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ const wrapInOpenaiError = (error: LangchainError): OpenaiAPIError => {

export class OpenAIModelHandler extends GeneralModelHandler<OpenAIModelSettings> {
async processInstructions(options: ProcessInstructionsOptions<OpenAIModelSettings>) {
const { instructions, content, schema, schemaDescription, modelSettings } = options;
const { instructions, content, schema, schemaDescription, modelSettings, remainingTokens } = options;
const { modelName, limitGenerationTokens, maxTokens } = this.modelConfig;

log.debug(`Calling Openai API with model ${this.modelConfig.modelName}`);

const handleLLMEndCallback = this.handleLLMEndCallbackHandler();
const modelOptions = {
...modelSettings,
modelName: this.modelConfig.modelName,
modelName,
maxTokens: limitGenerationTokens ? remainingTokens : maxTokens,
callbacks: [{ handleLLMEnd: handleLLMEndCallback.handleLLMEnd }],
};

Expand Down
2 changes: 2 additions & 0 deletions code/src/routes/crawl-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const crawlRoute = async (context: PlaywrightCrawlingContext) => {
} else {
log.info(`Processing page ${url} with GPT instruction...`, { contentLength: pageContent.length });
}
const remainingTokens = getNumberOfTextTokens(pageContent) + instructionTokenLength;

try {
const answerResult = await model.processInstructionsWithRetry({
Expand All @@ -160,6 +161,7 @@ export const crawlRoute = async (context: PlaywrightCrawlingContext) => {
schema,
schemaDescription,
modelSettings,
remainingTokens,
apifyClient: Actor.apifyClient,
});
answer = answerResult.answer;
Expand Down
3 changes: 3 additions & 0 deletions code/src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface ModelConfig {
maxOutputTokens?: number;
interface: 'chat' | 'text';
cost?: Cost;
/** Only used for cases where we need to limit the token limit earlier than the actual maximum (e.g. our PPR Actor) */
limitGenerationTokens?: true;
}

export interface Usage {
Expand All @@ -29,6 +31,7 @@ export interface ProcessInstructionsOptions<ModelSettings extends object> {
modelSettings: ModelSettings;
schema?: AnySchema;
schemaDescription: string;
remainingTokens: number;
}

export interface ProcessedInstructions {
Expand Down
6 changes: 6 additions & 0 deletions shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
This changelog tracks updates to both GTP Scraper and Extended GPT Scraper actors.

# 2024-07-30
*Features*
- Added support for GPT-4o-mini model. (Extended GPT scraper)
- Set this model as the default one for the the *Pay Per Result* scraper with a set token limit.
- With this, the maximum token limit for the *Pay Per Result* scraper was increased by 150%.

# 2024-05-20
*Features*
- Added support for GPT-4o model. (Extended GPT Scraper only)
Expand Down

0 comments on commit f0bb799

Please sign in to comment.