Repository navigation
Add POST /text/topics: suggest a topic for each questionnaire item - #36
Open
ronnyTodgers wants to merge 3 commits into
Open
ronnyTodgers wants to merge 3 commits into
ronnyTodgers wants to merge 3 commits into
Conversation
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>
8 of 13 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
POST /text/topics, which suggests a topic for each questionnaire item, andGET /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/matchcome 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 asagreement, and at 0.8 or more the label is markedauto, 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/, notdata/, which.gitignoreexcludes):Results on 520 items from 32 families of standard questionnaires, with the reference items as shipped:
autoautolabels rightFor 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.pyhas no API dependencies and could move there.Type of change
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 existingtests/local_tests/test_negate_local.py, against the API run locally with uvicorn. I did not run the other existing local tests.Test Configuration
main(7a621e1)requirements.txt. The 8-second start-up figure was measured on Linux, inside an existing Harmony API container, from a 10% sample.Checklist
test_negate_local.py, which passes)🤖 Generated with Claude Code