Skip to content

perf Mistral.py#13761

Closed
PVBLIC-F wants to merge 3 commits into
open-webui:devfrom
PVBLIC-F:dev
Closed

perf Mistral.py#13761
PVBLIC-F wants to merge 3 commits into
open-webui:devfrom
PVBLIC-F:dev

Conversation

@PVBLIC-F
Copy link
Copy Markdown
Contributor

Pull Request Checklist

Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.

Before submitting, make sure you've checked the following:

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests to validate the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

I’ve completely overhauled the Mistral OCR loader for maximum performance and resilience: I replaced raw requests calls with a single aiohttp.ClientSession configured with a 30 s ClientTimeout so no request can hang indefinitely; converted every network interaction (_upload_file, _get_signed_url, _process_ocr, and _delete_file) to non-blocking async def methods; wrapped file uploads in a with open(...) context manager to guarantee the PDF handle is always closed; parallelized page-to-Document construction using asyncio.get_event_loop().run_in_executor so multi-page docs process in parallel; hardened the OCR step with a Tenacity @Retry decorator for exponential-backoff retries; throttled overall concurrency with an asyncio.Semaphore(5) around the entire load() method; and added aenter/aexit to make the loader a proper async context manager—plus a helper to shut down the session cleanly.

Added

  • [List any new features, functionalities, or additions]

Changed

  • [List any changes, updates, refactorings, or optimizations]

Deprecated

  • [List any deprecated functionality or features that have been removed]

Removed

  • [List any removed features, files, or functionalities]

Fixed

  • [List any fixes, corrections, or bug fixes]

Security

  • [List any new or updated security-related changes, including vulnerability fixes]

Breaking Changes

  • BREAKING CHANGE: [List any breaking changes affecting compatibility or functionality]

Additional Information

  • [Insert any additional context, notes, or explanations for the changes]
    • [Reference any related issues, commits, or other relevant information]

Screenshots or Videos

  • [Attach any relevant screenshots or videos demonstrating the changes]

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

PVBLIC-F added 3 commits May 10, 2025 12:43
I kicked off by streamlining the HTTP layer—replacing individual requests calls with a shared aiohttp.ClientSession and streaming PDF uploads to cut down on TCP/TLS handshakes. From there, I converted every network interaction (_upload_file, _get_signed_url, _process_ocr, and _delete_file) into async def methods so nothing blocks the event loop. To speed up CPU-bound work, I introduced a helper _build_document and used asyncio.get_event_loop().run_in_executor combined with asyncio.gather to parse all pages in parallel. Recognizing that OCR calls can sometimes hiccup, I wrapped _process_ocr in a Tenacity @Retry decorator for exponential-backoff retries. Finally, to prevent overloading the OCR service or my own resources, I added an asyncio.Semaphore to throttle concurrent load() operations to five at a time—and provided a shutdown_loader helper to cleanly close the session when I’m done.
Safe file handling
In _upload_file, I replaced the manual open/close calls with a with open(..., "rb") context manager so the file descriptor is always closed—even if an exception occurs during upload.

Request timeouts
I imported ClientTimeout from aiohttp and wrapped our ClientSession in a 30 s total timeout. Now any stalled upload/GET/POST/DELETE will raise a timeout error instead of hanging indefinitely.
Switched to httpx.AsyncClient, replacing the previous aiohttp setup with a single, unified client that supports HTTP/2 out of the box, simpler timeout configuration, and a familiar requests-style API in both sync and async contexts.
@tjbck
Copy link
Copy Markdown
Contributor

tjbck commented May 12, 2025

using aiohttp is the convention, feel free to reopen!

@tjbck tjbck closed this May 12, 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