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: default answer to unparsed jsonAnswer #41

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"configurations": [
{
"name": "entrypoint (extended scraper)",
"type": "pwa-node",
"request": "launch",
"cwd": "${workspaceFolder}/actors/extended-gpt-scraper",
"program": "src/main.ts",
"runtimeArgs": [
"--no-warnings",
"--experimental-specifier-resolution=node",
"--loader",
"../../module_loader.js"
],
"runtimeExecutable": "node",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**"
]
}
]
}
4 changes: 3 additions & 1 deletion packages/gpt-scraper-core/src/models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { GeneralModelHandler } from './model.js';

// TODO: refactor: make this error modular with other non-OpenAI models
export const tryWrapInOpenaiError = (error: any) => {

Check warning on line 14 in packages/gpt-scraper-core/src/models/openai.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (error?.response?.data?.error) {
return new OpenaiAPIError(error.response.data.error.message || error.response.data.error.code);
}
Expand Down Expand Up @@ -47,7 +47,9 @@
const possibleJsonAnswer = functionArguments || answer;
const jsonAnswer = possibleJsonAnswer ? tryToParseJsonFromString(possibleJsonAnswer) : null;

return { answer, jsonAnswer, usage };
// it may return just function arguments (jsonAnswer), but if jsonAnswer is present, then `answer` should be too,
// so default `answer` to the unparsed value of `jsonAnswer`
return { answer: answer ?? possibleJsonAnswer, jsonAnswer, usage };
}

/**
Expand All @@ -56,10 +58,10 @@
* // TODO: refactor: perhaps we can achieve this with only crawlee?
*/
processInstructionsWithRetry = (options: ProcessInstructionsOptions<OpenAIModelSettings>) => {
const process: RetryFunction<any> = async (stopTrying: (e: Error) => void, attempt: number) => {

Check warning on line 61 in packages/gpt-scraper-core/src/models/openai.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
try {
return await this.processInstructions(options);
} catch (error: any) {

Check warning on line 64 in packages/gpt-scraper-core/src/models/openai.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
// NOTE: OpenAI API returns 429 with insufficient_quota, user needs to buy a plan.
if (error?.response?.status === 429 && error.response?.data?.error?.type === 'insufficient_quota') {
stopTrying(error);
Expand Down Expand Up @@ -102,7 +104,7 @@
* Parses the function arguments from the OpenAI LLM's output.
*/
private parseFunctionArguments = (functionOutput: LLMResult): string | null => {
const generations = functionOutput.generations as any;

Check warning on line 107 in packages/gpt-scraper-core/src/models/openai.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const firstFunction = generations?.[0]?.[0]?.message;
const functionArguments = firstFunction?.lc_kwargs?.additional_kwargs?.function_call?.arguments;

Expand Down
Loading