Skip to content

Add POST /text/topics: suggest a topic for each questionnaire item - #36

Open
ronnyTodgers wants to merge 3 commits into
harmonydata:mainfrom
delosis:feat/item-topics
Open

ronnyTodgers wants to merge 3 commits into
harmonydata:mainfrom
delosis:feat/item-topics

Conversation

@ronnyTodgers

Copy link
Copy Markdown
Contributor

Description

Adds POST /text/topics, which suggests a topic for each questionnaire item, and GET /text/topics, which lists the topics. This follows our email discussion of 2 December; there is no issue for it.

Why. The topics in /text/match come from each item's nearest Mental Health Catalogue item, and the catalogue tags whole questionnaires rather than items. Every SDQ item, for example, is tagged "emotional problems, behavioural problems", including "Restless, overactive" and "Considerate of other people's feelings". So the items of one questionnaire can't get different topics. This endpoint labels each item on its own, with one of 22 common topics (depression, anxiety, sleep, ADHD, trauma, ...).

How. The item is stripped of its question stem ("Over the last 2 weeks, how often have you been bothered by ...") and embedded with the requested model, as in /text/match. The 10 nearest of 5,605 reference items vote, after centring the vectors on the reference mean. The winner's share of the vote is returned as agreement, and at 0.8 or more the label is marked auto, meaning it can be applied without asking. The next distinct topics in order of nearness are returned as further suggestions. No language model runs at request time: a request costs one embedding per item.

The reference items are embedded once per model and kept in memory. For the default model this happens in a background thread at start-up, which took about 8 seconds on a 12-core server CPU. Any other model is embedded on its first request.

Reference items (harmony_api/resources/topics/, not data/, which .gitignore excludes):

  • 4,669 Mental Health Catalogue items, each labelled on its own by a language model.
  • 936 example items written by a language model for the thinner topics.
  • Duplicates are removed.

Results on 520 items from 32 families of standard questionnaires, with the reference items as shipped:

Model Right topic Right topic among 3 suggestions Marked auto auto labels right
MiniLM (default) 78% 95% 52% 92%
multilingual-e5-base 79% 94% 44% 96%

For questionnaires that are not in the catalogue, expect about 70% right and 89% among the suggestions. The test items' labels have not yet been reviewed by a researcher, and the topic list is a draft: both need someone on the research side. The test set and evaluation scripts are not part of this PR.

Dependencies. None added: the endpoint uses numpy and the existing vectorisation functions. The Harmony submodule is unchanged. If you would rather have the labelling logic in the Harmony library, harmony_api/services/topics.py has no API dependencies and could move there.

Type of change

  • New feature (non-breaking change which adds functionality)

Testing

  • pytest tests/unit_tests: 25 tests that need no model or server. They cover the labelling on fake vectors, stem stripping, an end-to-end run with a fake model, and the shipped data.
  • tests/local_tests/test_topics_local.py (6 tests) and the existing tests/local_tests/test_negate_local.py, against the API run locally with uvicorn. I did not run the other existing local tests.
  • The 520 test items above sent through the endpoint, with MiniLM and with multilingual-e5-base.
  • Start-up: the default model's reference vectors were ready about 15 seconds after the API came up on a laptop. The first request then took 0.4 seconds.

Test Configuration

  • Library version: Harmony submodule as pinned on main (7a621e1)
  • OS: macOS on Apple silicon, Python 3.12, requirements.txt. The 8-second start-up figure was measured on Linux, inside an existing Harmony API container, from a 10% sample.
  • Toolchain: pytest, Ruff

Checklist

  • My PR is for one issue, rather than for multiple unrelated fixes.
  • My code follows the style guidelines of this project. (Ruff with pyflakes, pycodestyle and bugbear rules passes on the new files; I did not run PyCharm's formatter.)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (README: a section under "Calling the Harmony API", and 3.5 and 3.6 in the API reference)
  • My changes generate no new warnings (checked by importing the router with warnings turned into errors)
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (the new ones pass; of the existing ones I ran only test_negate_local.py, which passes)
  • Any dependent changes have been merged and published in downstream modules (there are none)

🤖 Generated with Claude Code

John Rogers and others added 3 commits December 2, 2026 13:29
The topics in /text/match come from each item's nearest Mental Health
Catalogue item, and the catalogue tags whole questionnaires, so every item
of a questionnaire with several subscales gets the same topics. The new
endpoint labels each item on its own, with one of 22 common topics, by a
vote of its 10 nearest items in a reference set of 5,605 items that were
labelled one by one.

No language model runs at request time. The reference items are embedded
once per model and kept in memory: for the default model in a background
thread at start-up, for any other model on its first request.

- GET /text/topics lists the topics
- POST /text/topics labels a list of texts with the requested model
- harmony_api/resources/topics/ holds the topic list and reference items
- tests/unit_tests/test_topics.py needs no model or server;
  tests/local_tests/test_topics_local.py runs against a local server

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Model.construct

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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