Skip to content

feat: simplify intrinsics (code and examples)#946

Open
jakelorocco wants to merge 9 commits intomainfrom
jal/simplify-intrinsics
Open

feat: simplify intrinsics (code and examples)#946
jakelorocco wants to merge 9 commits intomainfrom
jal/simplify-intrinsics

Conversation

@jakelorocco
Copy link
Copy Markdown
Contributor

@jakelorocco jakelorocco commented Apr 27, 2026

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

  • Link to Issue: Fixes N/A

I will likely have to fix merge conflicts between this and the other granite switch pr. I would ideally like to merge this first.

Changes:

  • Add changes to start_session to simplify setting context_type.
  • Added start_backend to do the same thing that start_session does but not wrap it in a session
  • simplified the document parameters for intrinsic functions
  • simplified the intrinsic examples by removing unnecessary mellea imports
  • ensured existing intrinsic examples work with new easy use parameters, etc...

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
@github-actions github-actions Bot added the enhancement New feature or request label Apr 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

The PR description has been updated. Please fill out the template for your PR to be reviewed.

Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
@jakelorocco jakelorocco marked this pull request as ready for review April 27, 2026 20:36
@jakelorocco jakelorocco requested a review from a team as a code owner April 27, 2026 20:36
Copy link
Copy Markdown
Contributor

@ajbozarth ajbozarth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three inline comments below. General notes:

Circular import pattern (start_backend.pysession.py): session.py does a top-level import of start_backend, while start_backend.py defers its import of _resolve_backend_and_context inside the function body to avoid the cycle. Works at runtime, but fragile — the impl file depends on a private helper from another module. Cleaner to move _resolve_backend_and_context / _resolve_context into a _session_util.py that both files import independently.

Merge surface with #881: Both PRs touch _util.py, rag.py, and all the docs/examples/intrinsics/ files. The changes are orthogonal (this PR adds the coerce/resolve helpers; #881 extends call_intrinsic to support OpenAIBackend) but the merge will need care to keep both. Also: citations.py here drops the explicit doc_id="1" / doc_id="2" in favour of auto_doc_id=True (0-indexed). #881 also modifies that file — verify the final merged example has consistent IDs and matches whatever the intrinsic test fixtures expect.

Return type fixesrewrite_question and check_context_relevance were both typed -> float; now correctly -> str. Good catches.

Comment thread mellea/stdlib/session.py Outdated
Comment thread mellea/stdlib/session.py Outdated
Comment thread mellea/stdlib/session.py Outdated
)
else:
model_id_str = str(model_id)
resolved_ctx, backend, model_id_str = _resolve_backend_and_context(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression: this constructs the backend (including passing model_options to the constructor) before the SESSION_PRE_INIT hook runs. In the original code the hook ran first and the backend was built with the hook's (potentially modified) model_options. Now the hook updates the local model_options variable on line 291 but the backend never sees it.

Fix: split this helper — call _resolve_context here for early argument validation, then construct the backend after the hook block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved; split functionality up and calling the backend creation post hook.

…ssage for intrinsics

Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com>
Assisted-by: CLAUDE:OPUS
@jakelorocco
Copy link
Copy Markdown
Contributor Author

@ajbozarth

Circular import pattern (start_backend.pysession.py): session.py does a top-level import of start_backend, while start_backend.py defers its import of _resolve_backend_and_context inside the function body to avoid the cycle. Works at runtime, but fragile — the impl file depends on a private helper from another module. Cleaner to move _resolve_backend_and_context / _resolve_context into a _session_util.py that both files import independently.

Fixed the circular imports. Split up functionality and moved everything to start backend. We don't need a separate utils function. session can just import from start_backend.

Merge surface with #881: Both PRs touch _util.py, rag.py, and all the docs/examples/intrinsics/ files. The changes are orthogonal (this PR adds the coerce/resolve helpers; #881 extends call_intrinsic to support OpenAIBackend) but the merge will need care to keep both. Also: citations.py here drops the explicit doc_id="1" / doc_id="2" in favour of auto_doc_id=True (0-indexed). #881 also modifies that file — verify the final merged example has consistent IDs and matches whatever the intrinsic test fixtures expect.

I tested all intrinsic examples. Functionality is the same. I will resolve merge conflicts in the other PR. This PR takes precedence.

@jakelorocco jakelorocco requested a review from ajbozarth April 28, 2026 15:57
Copy link
Copy Markdown
Contributor

@ajbozarth ajbozarth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three issues from the previous round are addressed — hook ordering fixed, unreachable assert removed, misleading comment removed. One remaining concern with no specific line to attach: the hook ordering fix has no regression test. A unit test that patches invoke_hook to mutate model_options and asserts the backend was constructed with the updated value would lock this in.

Comment thread mellea/stdlib/session.py
_resolve_context,
_resolve_model_id_str,
backend_name_to_class,
start_backend,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session.py imports start_backend here but never calls it. The only effect is exposing mellea.stdlib.session.start_backend as an importable path, which test_session_unit.py relies on. Remove this import and update the test to import from the canonical location (mellea.stdlib.start_backend or mellea) instead.

if backend_class is None:
raise Exception(
f"Backend name {backend_name} unknown. Please see the docstring for "
"`mellea.stdlib.session.start_session` for a list of options."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error references start_session's docstring, but this is start_backend. Either point to start_backend or list the valid backend names inline — they're short enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants