Support Fibers for collaborative multitasking on LLM io waiting
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3538027. -->
Reported by: [andrewbelcher](https://www.drupal.org/user/655282)
Related to !792
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>LLM calls are typically slow and present a bottle neck in processing, leaving the main thread idling. Fibers present a mechanism for collaborative multi tasking, allowing us to support multiple concurrent LLM calls where appropriate. </p>
<p>Fibers are already supported in the render pipeline (in particular big pipe), and may be coming in other areas of Drupal Core.</p>
<p>Fibers could be implemented for specific use cases, e.g. a drush command that processes background AI tasks, or something like automators where we may have multiple non-chained LLM calls that could be processed concurrently rather than sequentially.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Support Fiber suspension in <code>OpenAiBasedProviderClientBase</code>, allowing LLM calls to collaboratively multi task.</p>
<p>Note, the PHP OpenAI SDK doesn't currently support async API requests, but we can work around that by using a streamed response which <em>is</em> effectively asynchronous as it uses a generator with an underlying async io.</p>
<p>We should also include some test coverage for it!</p>
<h3>Impact</h3>
<p>See the attached video for a PoC implemented directly in the OpenAI module (as <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai_provider_openai/issues/3531260" title="Status: Closed (fixed)">#3531260: Remove OpenAI SDK dependency and extend it from Drupal AI core module</a></span> isn't merged yet). The highlight is that 4 AI requests which totalled 20s consecutive execution instead ran in 10s. The requests are deliberately of different durations, 10s represents the longest request.</p>
> Related issue: [Issue #3541471](https://www.drupal.org/node/3541471)
> Related issue: [Issue #3541470](https://www.drupal.org/node/3541470)
> Related issue: [Issue #3541472](https://www.drupal.org/node/3541472)
> Related issue: [Issue #3541473](https://www.drupal.org/node/3541473)
issue