Skip to content
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

Better example code for models using chains framework #1347

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

nnarayen
Copy link
Contributor

@nnarayen nnarayen commented Jan 28, 2025

🚀 What

Small followup to #1346, but purely focused on better example code in error messages.

There are still some references to chains, but I think this is better overall.

Before:

$ truss push my_model.py --remote baseten-staging
ERROR ChainsUsageError: The user defined code does not comply with the required spec, please fix below:
/Users/nnarayen/Desktop/repos/baseten-local-deploy/my_model.py:26 (PassthroughModel.__init__) : If `Model` uses context for initialization, it must have `context` argument of type `<class 'truss_chains.definitions.DeploymentContext'>` as the last argument.
Got arguments: `[]`.
Example of correct `__init__` with context:
class HelloWorld(chains.ChainletBase):
    # chains.depends indicates that the HelloWorld chainlet depends on the RandInt Chainlet
    # this enables the HelloWorld chainlet to call the RandInt chainlet
    def __init__(self, rand_int=chains.depends(RandInt, retries=3)) -> None:
        self._rand_int = rand_int

    def run_remote(self, max_value: int) -> str:
        num_repetitions = self._rand_int.run_remote(max_value)
        return "Hello World! " * num_repetitions

After:

$ truss push my_model.py --remote baseten-staging
ERROR ChainsUsageError: The user defined code does not comply with the required spec, please fix below:
/Users/nnarayen/Desktop/repos/baseten-local-deploy/my_model.py:26 (PassthroughModel.__init__) : If `Model` uses context for initialization,
it must have `context` argument of type `<class 'truss_chains.definitions.DeploymentContext'>` as the last argument.
Got arguments: `[]`.
Example of correct `__init__` with context:
class HelloWorld(chains.ModelBase):
    def __init__(self, context: chains.DeploymentContext = chains.depends_context()):
        self._call_count = 0

    def predict(self, call_count_increment: int) -> int:
        self._call_count += call_count_increment
        return self._call_count

Next steps:

  • Context marker still invokes _example_chainlet_code() directly, since we've mainly fixed the InitValidator callsites here.

💻 How

🔬 Testing

@nnarayen nnarayen force-pushed the nikhil/error-message-improvements branch from bb5b569 to e3ea6a2 Compare January 28, 2025 21:43
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put both examples into a subdir e.g. truss_chains/reference_code/?

@@ -224,15 +224,31 @@ def _example_chainlet_code() -> str:
# called as a result of that, we have a circular import ("partially initialized
# module 'truss_chains.example_chainlet' ...").
except AttributeError:
logging.error("example_chainlet` is broken.", exc_info=True, stack_info=True)
logging.error("`example_chainlet` is broken.", exc_info=True, stack_info=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call it "reference" (to highlight it's for showcasing correct API) in distinction from truss-chains/examples which are actual use case examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

@nnarayen nnarayen force-pushed the nikhil/python-dx-improvements branch from 2568a20 to d88e360 Compare January 29, 2025 15:31
Base automatically changed from nikhil/python-dx-improvements to main January 29, 2025 20:13
@nnarayen nnarayen force-pushed the nikhil/error-message-improvements branch from 6ac40eb to c2e58c5 Compare January 29, 2025 20:15
@nnarayen nnarayen merged commit f26589c into main Jan 29, 2025
5 checks passed
@nnarayen nnarayen deleted the nikhil/error-message-improvements branch January 29, 2025 22:00
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.

2 participants