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: Key-value extraction #320

Merged
merged 8 commits into from
Dec 31, 2024
Merged

feat: Key-value extraction #320

merged 8 commits into from
Dec 31, 2024

Conversation

remi-guan
Copy link
Collaborator

@remi-guan remi-guan commented Dec 28, 2024

Description

Our new feature to extract data from PDF by key-value. Integrated with our async backend API.

Code Structure

  1. Modify app/components/playground/ActionContainer.tsx to add a new tab. The enum PlaygroundTabs is also required to edit.
  2. Implement a container app/components/playground/ExtractKeyValuePairContainer.tsx. I've put the upload logic within, and use our store app/hooks/usePlaygroundStore.ts to preserve the input/output, so you don't lose the data while you're working with the file with the other features. (Like you can do both extracting text while you're extracting the key-value) Also add types of the store file app/types/PlaygroundTypes.ts.
  3. I used a tutorial with a free, easy-to-use library React Joyride. By adding steps to each dom element you can easily define the tutorial steps. Create the component and put it inside the container, then you should get a tutorial. app/components/tutorials/ExtractKeyValuePairTutorial.tsx
  4. Implement a test.

API Integration

I'm using async extract_key_value API by uploading to a preassigned s3 URL by /async/upload API with parameters to trigger an extract key value action. And continue to fetch the result with /async/fetch API. This logic has already been implemented by runAsyncRequestJob function.

I also have a file implemented this feature with the sync API endpoint /extract_key_value. This logic is not being used, since sync API usually reach 30s timeout limit causing we get an error.

However, the file has still been preserved since I saw that we can use it to implement a retry mechanism like this module MarkdownExtractContainer.tsx.

But implementing such logic require more time, and our current code is able to use now. So I haven't scheduled time to do that yet.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement

How Has This Been Tested?

Tested:

  1. Upload a file, enter a key and description to extract.
  2. Tutor should automatically pop over.(runs only once, unless you clear your local storage)
  3. Inputs and the extract result should be preserved if you switch tabs.
  4. “Back to file” and “Download” button.
  5. Toast is working fine.
  6. Scrolling is working fine.

Screenshots (if applicable)

image
image
image

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 15 changed files in this pull request and generated no comments.

Files not reviewed (9)
  • .vscode/settings.json: Language not supported
  • package.json: Language not supported
  • test-results/.last-run.json: Language not supported
  • app/components/playground/KeyValueInputs.tsx: Evaluated as low risk
  • app/components/tutorials/ExtractKeyValuePairTutorial.tsx: Evaluated as low risk
  • app/hooks/usePlaygroundStore.ts: Evaluated as low risk
  • app/types/PlaygroundTypes.ts: Evaluated as low risk
  • tests/coreTests.ts: Evaluated as low risk
  • app/components/playground/ActionContainer.tsx: Evaluated as low risk
Comments suppressed due to low confidence (2)

app/components/playground/ExtractKeyValuePairContainer.tsx:205

  • The 'formattedExtractResult' memoization logic assumes the structure of the 'extractResult'. If the structure changes, it could break. Consider adding a check or comment to clarify the assumption.
const content = typeof selectedFile.extractResult === 'string' ? JSON.parse(selectedFile.extractResult) : selectedFile.extractResult;

app/actions/apiInterface.ts:54

  • The new parameter 'vqaExtractInstruction' should be documented properly in the interface.
vqaExtractInstruction?: Record<string, string>;
@lingjiekong
Copy link
Member

Suggestion: for this kind of complex feature, it will be great to record a video to ease the effort on the reviewer side.

@lingjiekong lingjiekong requested a review from boqiny December 29, 2024 19:01
@lingjiekong
Copy link
Member

@remi-guan Good progress on implementing this feature and I tested after pulling your code locally and still have couple problems that I want you to work with @boqiny to resolve.

  1. @remi-guan make sure you add a bit more details regarding what is the backend api that you are integrating with, so it ease the review for the backend engineers in the future.
  2. @remi-guan after extracting key value is done, I move back to the file by clicking "back to file", I feel we miss a feature to move to the extract key value without re-rerunning it again by clicking "extract key-value". I feel we can add a "back to key-value result"-ish button without re-running.
  3. @boqiny it looks like the quota update is not right and no matter the page of files, we are always decrement 1 page.
  4. @boqiny what is the extract key value strategy for now?

@lingjiekong
Copy link
Member

Description

Our new feature to extract data from PDF by key-value. Integrated with our async backend API.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement

How Has This Been Tested?

Tested:

  1. Upload a file, enter a key and description to extract.
  2. Tutor should automatically pop over.(runs only once, unless you clear your local storage)
  3. Inputs and the extract result should be preserved if you switch tabs.
  4. “Back to file” and “Download” button.
  5. Toast is working fine.
  6. Scrolling is working fine.

Screenshots (if applicable)

image image image

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Forgive me for my ignorance and what does toast mean for toast is working fine.

@lingjiekong
Copy link
Member

@remi-guan, we used to have two frontend dev setup.

You can take a look for the current setup and please feel free to comment and modify the notion page regarding how to better setup future frontend or ts related project.

@@ -8,4 +8,7 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"cSpell.words": [
"cambio"
],
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: new line at the end of file.

Copy link
Member

Choose a reason for hiding this comment

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

it looks like you are using async extract_key_value api by uploading to a preasigned s3 url to trigger a extract key value and continue to fetch the result. Therefore, shall we name this to async instead of sync in the file name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, this is previous code. First, I implemented the code with sync API, that is the file you're commenting on.

After a few tests, Charles told me that sync API usually reaches the 30s timeout, so I better to use the async version.

Which has been currently implemented in app/components/playground/ExtractKeyValuePairContainer.tsx#136~197. I didn't extract that logic to a separated function because I've seen we have similar code in the other modules.

So the sync API has not being used, I'm still preserving this file is because it seems the other pages are using this logic as some retry mechanism.

Reply to your comment:

  1. No, I didn't mix the async and sync code up.
  2. I preserved this file for later use. Although I seem better to mention this in my PR description. I'll update my PR, so this file is more reasonable to you later today.

Copy link
Member

Choose a reason for hiding this comment

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

Yes. our current backend is not very well implemented using AWS API gateway which has a tight 30 seconds timeout for sync api.

@lingjiekong
Copy link
Member

@remi-guan , I am not a frontend expert, could you please take this opportunity to explain regarding what are the changes in each file, so future developer will have a sense on how to add similar frontend features in the future.

@remi-guan
Copy link
Collaborator Author

@remi-guan Good progress on implementing this feature and I tested after pulling your code locally and still have couple problems that I want you to work with @boqiny to resolve.

  1. @remi-guan make sure you add a bit more details regarding what is the backend api that you are integrating with, so it ease the review for the backend engineers in the future.
  2. @remi-guan after extracting key value is done, I move back to the file by clicking "back to file", I feel we miss a feature to move to the extract key value without re-rerunning it again by clicking "extract key-value". I feel we can add a "back to key-value result"-ish button without re-running.
  3. @boqiny it looks like the quota update is not right and no matter the page of files, we are always decrement 1 page.
  4. @boqiny what is the extract key value strategy for now?

@lingjiekong

  1. Okay, I'll edit my PR to include that details.
  2. Sounds good, it's easy to add, I can implement that feature today.

@remi-guan
Copy link
Collaborator Author

Description

Our new feature to extract data from PDF by key-value. Integrated with our async backend API.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement

How Has This Been Tested?

Tested:

  1. Upload a file, enter a key and description to extract.
  2. Tutor should automatically pop over.(runs only once, unless you clear your local storage)
  3. Inputs and the extract result should be preserved if you switch tabs.
  4. “Back to file” and “Download” button.
  5. Toast is working fine.
  6. Scrolling is working fine.

Screenshots (if applicable)

image image image

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Forgive me for my ignorance and what does toast mean for toast is working fine.

toast in frontend means the little message on top of the screen, aka message. E.g. this one:
image

Normally, we differ toast with notification, the latter one is usually to be bigger, slide from aside and requires user interaction.

In my PR comment, I was mentioning that the toast should be working, by which I mean the toast should display the current status of the feature properly.

@remi-guan
Copy link
Collaborator Author

The new commits:

  • Add a button to back to result after back to file.
  • Fix an issue of the extraction result was conflicted with the another modules.
  • Refactor the code of state management, approximately removed 50 lines and bringing better logic structure and code quality.
  • Reformat the code with prettier to pass the lint.

@lingjiekong lingjiekong requested review from Copilot and lingjiekong and removed request for boqiny December 31, 2024 04:19

Choose a reason for hiding this comment

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

Copilot reviewed 7 out of 21 changed files in this pull request and generated no comments.

Files not reviewed (14)
  • .vscode/settings.json: Language not supported
  • app/globals.css: Language not supported
  • package.json: Language not supported
  • test-results.json: Language not supported
  • test-results/.last-run.json: Language not supported
  • app/components/playground/ActionContainer.tsx: Evaluated as low risk
  • app/components/playground/KeyValueInputs.tsx: Evaluated as low risk
  • app/actions/apiInterface.ts: Evaluated as low risk
  • app/actions/runSyncExtractKeyValue.ts: Evaluated as low risk
  • app/actions/uploadFile.ts: Evaluated as low risk
  • app/components/Button.tsx: Evaluated as low risk
  • README.md: Evaluated as low risk
  • app/components/playground/ExtractKeyValuePairContainer.tsx: Evaluated as low risk
  • app/hooks/usePlaygroundStore.ts: Evaluated as low risk
Copy link
Member

@lingjiekong lingjiekong left a comment

Choose a reason for hiding this comment

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

LGTM.

@lingjiekong lingjiekong merged commit eb678e7 into main Dec 31, 2024
2 checks passed
@remi-guan remi-guan self-assigned this Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants