Add Extractive Question Answering operation type
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3584371. -->
Reported by: [ahmad khader](https://www.drupal.org/user/3727855)
Related to !1481
>>>
<p>[Tracker]<br>
<strong>Update Summary: </strong>[One-line status update for stakeholders]<br>
<strong>Short Description: </strong>[One-line issue summary for stakeholders]<br>
<strong>Check-in Date: </strong>MM/DD/YYYY<br>
<em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br>
[/Tracker]</p>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>The AI module currently has no operation type for extractive question answering — a task where the model receives a question and a context passage and extracts the answer span directly from the context, along with a confidence score and the character positions where it found the answer.</p>
<p>This is a well-established NLP task supported by Hugging Face's <code>question-answering</code> pipeline with popular models like <code>deepset/roberta-base-squad2</code> and <code>distilbert/distilbert-base-cased-distilled-squad</code> (both have active inference endpoints). Unlike chat-based QA, extractive QA is deterministic, fast, and grounded — the answer comes directly from the source text with no hallucination risk.</p>
<p>There is currently no way for Drupal modules to call extractive QA models through the AI module's normalized provider interface.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Add a new <code>extractive_question_answering</code> operation type following the same pattern as <code>text_classification</code> and <code>image_classification</code>.</p>
<h3>Example usage</h3>
<pre>use Drupal\ai\OperationType\ExtractiveQuestionAnswering\ExtractiveQuestionAnsweringInput;<br><br>$input = new ExtractiveQuestionAnsweringInput(<br> 'What is the capital of France?',<br> 'The capital of France is Paris, which is also the largest city in the country.'<br>);<br><br>$result = \Drupal::service('ai.provider')<br> ->createInstance('huggingface')<br> ->extractiveQuestionAnswering($input, 'deepset/roberta-base-squad2', ['my_module']);<br><br>$items = $result->getNormalized();<br>foreach ($items as $item) {<br> $answer = $item->getAnswer(); // "Paris"<br> $score = $item->getScore(); // 0.98<br> $start = $item->getStart(); // 25<br> $end = $item->getEnd(); // 30<br>}</pre><h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
<h3 id="summary-ai-usage">AI usage (if applicable)</h3>
<p>[ ] AI Assisted Issue<br>
This issue was generated with AI assistance, but was reviewed and refined by the creator.</p>
<p>[] AI Assisted Code<br>
This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p>
<p>[x] AI Generated Code<br>
This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p>
<p>[ ] Vibe Coded<br>
This code was generated by an AI and has only been functionally tested.</p>
> Related issue: [Issue #3582819](https://www.drupal.org/node/3582819)
> Related issue: [Issue #3582766](https://www.drupal.org/node/3582766)
issue