-
Notifications
You must be signed in to change notification settings - Fork 0
Improve tutor syllabus quality: single-pass generation, deterministic references #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| from src.app.tutor.service.models import ( | ||
| DraftAssessment, | ||
| DraftCompetency, | ||
| DraftOutcome, | ||
| DraftSession, | ||
| DraftSyllabus, | ||
| ) | ||
| from src.app.tutor.service.syllabus import ( | ||
| compute_limits, | ||
| detect_syllabus_lang, | ||
| render_markdown, | ||
| render_references, | ||
| split_references, | ||
| trim_chatter, | ||
| ) | ||
|
|
||
|
|
||
| def make_draft() -> DraftSyllabus: | ||
| return DraftSyllabus( | ||
| course_title="Économie circulaire", | ||
| description="Desc", | ||
| objectives=["O1", "O2", "O3", "O4", "O5"], | ||
| outcomes=[ | ||
| DraftOutcome(text=f"Out {i}", objective_numbers=[1]) for i in range(1, 10) | ||
| ], | ||
| competencies=[ | ||
| DraftCompetency(text="Travail en équipe", outcome_numbers=[1]), | ||
| DraftCompetency( | ||
| text="Systémique", greencomp_code="2.1", outcome_numbers=[2] | ||
| ), | ||
| DraftCompetency(text="Non liée", greencomp_code="2.2", outcome_numbers=[]), | ||
| DraftCompetency(text="Critique", greencomp_code="2.2", outcome_numbers=[3]), | ||
| DraftCompetency(text="Doublon", greencomp_code="2.2", outcome_numbers=[1]), | ||
| DraftCompetency( | ||
| text="Faux code", greencomp_code="9.9", outcome_numbers=[1] | ||
| ), | ||
| ], | ||
| assessment=[ | ||
| DraftAssessment(method="Projet", weight="100%", outcome_numbers=[1, 99]) | ||
| ], | ||
| schedule=[DraftSession(topics="A | B", outcome_numbers=[1], class_plan="x\ny")], | ||
| ) | ||
|
|
||
|
|
||
| def test_compute_limits(): | ||
| assert compute_limits(None).sessions is None | ||
| assert compute_limits(None).outcomes == 5 # sized like a semester course | ||
| assert compute_limits("4 semaines").outcomes == 3 | ||
| assert compute_limits("8 semaines").outcomes == 4 # grows with duration | ||
| assert compute_limits("8 semaines").objectives == 3 | ||
| assert compute_limits("12 semaines").sessions == 12 | ||
| assert compute_limits("30h").sessions == 10 | ||
| assert compute_limits("1 semestre").sessions == 12 | ||
| assert compute_limits("40 weeks").outcomes == 7 # ceiling | ||
| assert compute_limits("40 weeks").objectives == 4 | ||
|
|
||
|
|
||
| def test_render_markdown_fr(): | ||
| md = render_markdown(make_draft(), "fr", compute_limits("12 semaines")) | ||
| assert md.startswith("# Économie circulaire") | ||
| assert "## 3. Résultats d'apprentissage" in md and "Learning" not in md | ||
| assert "**RA5**" in md and "**RA6**" not in md # capped at 5 outcomes | ||
| assert "4. O4" not in md # capped at 3 objectives | ||
| assert "Non liée" not in md and "Doublon" not in md # unlinked / duplicate code | ||
| # official name in the syllabus language, GreenComp listed first | ||
| assert "**GreenComp 2.1 – Pensée systémique** : Systémique *(RA2)*" in md | ||
| assert "**GreenComp 2.2 – Pensée critique** : Critique" in md | ||
| assert md.index("GreenComp 2.1") < md.index("Travail en équipe") | ||
| assert "- Faux code" in md # unknown code -> plain competency | ||
| assert "RA99" not in md | ||
| assert "| 1 | A / B | RA1 | x<br>y |" in md | ||
|
|
||
|
|
||
| def test_references_are_deterministic(): | ||
| refs = render_references( | ||
| [ | ||
| {"title": "Doc A", "url": "https://a.org"}, | ||
| {"title": "Doc A", "url": "https://a.org"}, # second slice of same doc | ||
| {"title": "Doc B", "url": "https://b.org"}, | ||
| ], | ||
| "fr", | ||
| ) | ||
| assert refs.startswith("## 7. Références") | ||
| assert refs.count("https://a.org") == 1 | ||
| assert '<a href="https://b.org" target="_blank">[lien]</a>' in refs | ||
| assert "Aucune" in render_references([], "fr") | ||
| # a resource without title or url is never rendered | ||
| assert "Aucune" in render_references( | ||
| [{"title": "", "url": "https://c.org"}, {"title": "Doc C", "url": ""}], "fr" | ||
| ) | ||
|
|
||
|
|
||
| def test_split_and_trim(): | ||
| md = ( | ||
| "Voici votre syllabus :\n# T\n## 6. Programme\n| a |\n|---|\n" | ||
| "Ce syllabus est aligné avec GreenComp.\n## 7. Références\n- Doc <a>x</a>" | ||
| ) | ||
| body, refs = split_references(md) | ||
| assert refs == "## 7. Références\n- Doc <a>x</a>" | ||
| assert trim_chatter(body) == "# T\n## 6. Programme\n| a |\n|---|" | ||
| assert split_references("# T\nno refs") == ("# T\nno refs", "") | ||
|
|
||
|
|
||
| def test_detect_syllabus_lang(): | ||
| assert ( | ||
| detect_syllabus_lang(render_markdown(make_draft(), "fr", compute_limits(None))) | ||
| == "fr" | ||
| ) | ||
| assert ( | ||
| detect_syllabus_lang(render_markdown(make_draft(), "en", compute_limits(None))) | ||
| == "en" | ||
| ) | ||
| assert detect_syllabus_lang("# free text") == "unknown" | ||
|
|
||
|
|
||
| def test_generate_syllabus_retries_once_then_raises(): | ||
| import asyncio | ||
|
|
||
| import pytest | ||
| from langchain_core.runnables import RunnableConfig, RunnableLambda | ||
|
|
||
| from src.app.tutor.service.models import MessageWithResources | ||
| from src.app.tutor.service.syllabus import generate_syllabus | ||
|
|
||
| answers = [None, make_draft()] # first answer invalid, second valid | ||
|
|
||
| class FakeModel: | ||
| def with_structured_output(self, schema): | ||
| return RunnableLambda(lambda _: answers.pop(0)) | ||
|
|
||
| msg = MessageWithResources( | ||
| lang="fr", content=[], themes=[], summary=[], resources=[], duration=None | ||
| ) | ||
| md = asyncio.run(generate_syllabus(msg, FakeModel(), [], RunnableConfig())) # type: ignore | ||
| assert md.startswith("# Économie circulaire") and not answers | ||
|
|
||
| answers[:] = [None, None] | ||
| with pytest.raises(Exception): | ||
| asyncio.run(generate_syllabus(msg, FakeModel(), [], RunnableConfig())) # type: ignore |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be added to the values.yaml in the k8s folder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8600ba8