Skip to content

Add Context Chat Multi: process a batch of questions in one task - #351

Open
MichelleAntunes wants to merge 7 commits into
nextcloud:masterfrom
MichelleAntunes:feat/context-chat-multi
Open

Add Context Chat Multi: process a batch of questions in one task#351
MichelleAntunes wants to merge 7 commits into
nextcloud:masterfrom
MichelleAntunes:feat/context-chat-multi

Conversation

@MichelleAntunes

@MichelleAntunes MichelleAntunes commented Sep 7, 2026

Copy link
Copy Markdown

Backend implementation for nextcloud/assistant#637.

Problem

Context Chat currently answers one question per task. For workflows that need answers to a fixed batch of related questions against the same knowledge base, this means submitting each question as a separate task, waiting for each one, and manually reassembling the results afterwards.

Solution

Adds a new task type, "Context Chat Multi", that accepts a list of questions instead of a single prompt. Each question is processed through the existing process_context_query() logic — the same code path already used for a single question, just looped, so no query/retrieval logic is duplicated. Sources are grouped per question rather than merged across the whole batch, since the same source can be relevant to more than one question.

Changes

  • context_chat_backend/chain/types.py: new MultiOutput type for the batch result shape
  • context_chat_backend/controller.py: registers the new Multi task type
  • context_chat_backend/task_fetcher.py: processes Multi tasks by looping the existing single-question logic, groups sources per question instead of merging them, and exposes the configured question cap (MAX_MULTI_QUESTIONS) to the frontend via input_shape_defaults so the UI can enforce the same limit

Testing

Tested end-to-end on a real Nextcloud instance with the companion UI branch: submitted a batch of questions against real indexed documents and confirmed the Assistant UI returns correctly answered, source-attributed results for each question.

Companion PR

This is the backend half of the feature. UI branch: nextcloud/assistant#646

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Adds a new TaskType/TaskProcessingProvider registration in
enabled_handler(), alongside the existing context_chat and
context_chat_search ones.

This new task type ('context_chat:context_chat_multi') accepts the
same input as a regular question (prompt, scopeType, scopeList,
scopeListMeta), but is meant to receive several questions at once,
one per line.

Its output_shape is different from the single-question task type:
instead of a single 'output' text field, it declares 'questions'
and 'answers' as lists (LIST_OF_TEXTS), so the caller gets back one
answer per question, in order, plus a combined 'sources' list.

expected_runtime is set to 30 * MAX_MULTI_QUESTIONS, since answering
multiple questions sequentially takes proportionally longer than a
single question.

Also unregisters the new provider in the disabled branch, mirroring
the existing search/normal providers.

Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
MichelleAntunes added 6 commits September 7, 2026 10:34
Teaches the request processing loop to recognize the new
'context_chat:context_chat_multi' task type (added to the
next_task() polling call) and route it to a new process_multi_task()
function.

process_multi_task():
- Splits the task's 'prompt' input into individual questions, one
  per non-empty line, via the new _split_questions() helper.
- Caps the number of questions at MAX_MULTI_QUESTIONS (20) to avoid
  unbounded processing time on a single task.
- Calls the existing process_context_query() once per question,
  reusing the same vector search + LLM logic used for single
  questions, instead of duplicating that logic.
- Collects all answers in order, and deduplicates sources across
  answers (the same document may be relevant to more than one
  question) before returning them.
- Raises ValueError if no valid question was found in the prompt,
  so the task fails clearly instead of silently returning nothing.

The result (questions, answers, sources) is returned to Nextcloud
via return_result_to_nextcloud(), matching the output_shape declared
for this task type in controller.py.

Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
…all answers

Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
…tcher.py

Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
Signed-off-by: MichelleAntunes <miichelleantunes@outlook.com>
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.

1 participant