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 endpoint to allow passing files when running flows #5588

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

ogabrielluiz
Copy link
Contributor

This pull request introduces several enhancements, including an update method in the Tweaks class for dynamic dictionary merging. A new POST endpoint /run/upload/{flow_id_or_name} has been added to facilitate flow execution with file uploads, supporting both JSON and form data inputs. The SimplifiedAPIRequest model has been enhanced with form handling and validation, allowing for better usability. Additionally, comprehensive tests for file uploads and flow execution have been implemented to ensure robust functionality and error handling. Overall, these changes improve the flexibility and reliability of the API.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request labels Jan 8, 2025
@github-actions github-actions bot removed the enhancement New feature or request label Jan 8, 2025
@ogabrielluiz ogabrielluiz changed the title feat: Add file upload support and enhance Tweaks and API request handling feat: Add endpoint to allow passing files when running flows Jan 8, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
@ogabrielluiz
Copy link
Contributor Author

Hey @philnash , @mieslep

Do you mind taking a look at this?

@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
Copy link

codspeed-hq bot commented Jan 8, 2025

CodSpeed Performance Report

Merging #5588 will degrade performances by 17.39%

Comparing add-upload-in-run (2753133) with main (249f000)

Summary

⚡ 1 improvements
❌ 1 regressions
✅ 12 untouched benchmarks
🆕 4 new benchmarks
⁉️ 1 dropped benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main add-upload-in-run Change
🆕 test_successful_run_no_payload[/api/v1/run/] N/A 281.3 ms N/A
🆕 test_successful_run_no_payload[/api/v1/run/upload/] N/A 245.8 ms N/A
test_successful_run_with_input_type_text 218.6 ms 264.6 ms -17.39%
test_successful_run_with_output_type_any 4,143.6 ms 246 ms ×17
⁉️ test_successful_run_with_output_type_debug 249.6 ms N/A N/A
🆕 test_successful_run_with_output_type_debug[/api/v1/run/] N/A 257.2 ms N/A
🆕 test_successful_run_with_output_type_debug[/api/v1/run/upload/] N/A 256.6 ms N/A

@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
Copy link
Collaborator

@erichare erichare left a comment

Choose a reason for hiding this comment

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

Looks awesome, left a couple small NITs but i love this.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 8, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 8, 2025
@ogabrielluiz ogabrielluiz added the DO NOT MERGE Don't Merge this PR label Jan 9, 2025
- Introduced an `update` method in the `Tweaks` class to allow merging of a dictionary into the `root` attribute.
- This enhancement improves the functionality of the `Tweaks` class by enabling dynamic updates to its internal state.
- Introduced a new POST endpoint `/run/upload/{flow_id_or_name}` to execute flows with support for file uploads and streaming responses.
- Enhanced input handling to accept both JSON body and form data, allowing for flexible request formats.
- Implemented file processing logic to validate and store uploaded files, integrating them into the flow execution context.
- Added telemetry logging for flow execution metrics, improving monitoring and debugging capabilities.
- Updated dependencies and imports to accommodate new functionality.
…ation

- Added form data validation to the SimplifiedAPIRequest model, allowing for JSON string input and ensuring proper parsing.
- Introduced a new model_config with JSON schema examples to guide API users on expected input formats.
- Implemented an as_form method to facilitate the creation of model instances from form data, improving usability in web contexts.
- Updated imports for better organization and clarity in the schemas.py file.
- Added a fixture for creating and cleaning up file flow components in tests.
- Improved assertions in existing tests for version and config endpoints to provide clearer error messages.
- Introduced new tests for successful file uploads, handling invalid filename formats, and uploading multiple files.
- Enhanced tests for running flows with file uploads, ensuring robust validation of responses and session management.
- Updated imports and organized test structure for better readability and maintainability.
…and update SimplifiedAPIRequest model

- Removed JSON parsing for tweaks in the simplified_run_flow_with_upload function, directly using the input data.
- Updated the SimplifiedAPIRequest model to handle tweaks as a JSON string, ensuring proper parsing during validation.
- Enhanced unit tests to reflect changes in input handling, ensuring compatibility with the new structure.
…to use Tweaks class

- Changed the initialization of `input_request.tweaks` from an empty dictionary to an instance of the `Tweaks` class when no tweaks are provided.
- This change ensures that the tweaks are handled consistently as objects, improving the overall structure and functionality of the input handling in the flow execution context.
- Added a new file `run_utils.py` to handle flow execution, including support for file uploads and streaming responses.
- Introduced `simple_run_flow` and `execute_flow` functions to manage flow execution and telemetry logging.
- Implemented file processing logic to validate and store uploaded files, integrating them into the flow execution context.
- Enhanced input validation with `validate_input_and_tweaks` to ensure proper handling of input values and tweaks.
- Added asynchronous event consumption with `consume_and_yield` to track and yield events to clients.
- Established a streaming response setup with `setup_streaming_response` to facilitate real-time updates during flow execution.
- Improved error handling and telemetry logging for better monitoring of flow execution metrics.
- Integrated `execute_flow` and `setup_streaming_response` functions to enhance flow execution handling.
- Removed extensive inline documentation and error handling from `simplified_run_flow`, delegating responsibilities to the new utility functions for improved readability and maintainability.
- Updated the flow execution logic to support both streaming and non-streaming responses more efficiently.
- Enhanced error handling by centralizing telemetry logging and exception management, ensuring consistent behavior across flow executions.
… empty dictionary

- Changed the initialization of `input_request.tweaks` from an instance of the `Tweaks` class to an empty dictionary when no tweaks are provided.
- This adjustment ensures that tweaks are handled as a standard dictionary, allowing for more flexible updates and integration with file processing logic.
…cumentation

- Updated `simplified_run_flow` and `simplified_run_flow_with_upload` functions to include detailed docstrings for better clarity on parameters and return types.
- Integrated `process_uploaded_files` to streamline file handling during flow execution.
- Removed unused imports and improved overall code organization for better maintainability.
- Enhanced error handling to provide more specific HTTP status codes for various failure scenarios.
- Improved input request handling to support both JSON and form-data submissions, ensuring flexibility in API usage.
- Updated the validation logic in the SimplifiedAPIRequest model to ensure that the "tweaks" field is only parsed from a JSON string if it is provided as a string within a dictionary.
- Enhanced error handling to provide more specific messages for JSON decoding errors related to the "tweaks" field, improving the clarity of error reporting.
- Enhanced error handling in the execute_flow function to provide more specific HTTP exceptions for different error types, including detailed messages for "not found" and "invalid chat input" scenarios.
- Centralized telemetry logging for both successful and failed executions, ensuring consistent tracking of flow execution metrics.
- Improved clarity of error messages returned to the client, aiding in debugging and user feedback.
- Refactored the error handling for "not found" scenarios to streamline the HTTPException raising process, improving clarity and consistency in error responses.
- Enhanced the detail of the error message returned, aiding in debugging and user feedback.
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Jan 9, 2025
@ogabrielluiz ogabrielluiz requested a review from NadirJ January 10, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DO NOT MERGE Don't Merge this PR enhancement New feature or request lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants