Skip to content
Open
3 changes: 2 additions & 1 deletion docs/examples/generative_slots/generative_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def generate_summary(text: str) -> str:
print("Output sentiment is : ", sentiment_component)

summary = generate_summary(
m=m,
text="""
The eagle rays are a group of cartilaginous fishes in the family Myliobatidae,
consisting mostly of large species living in the open ocean rather than on the sea bottom.
Expand All @@ -28,6 +29,6 @@ def generate_summary(text: str) -> str:
surface. Compared with other rays, they have long tails, and well-defined, rhomboidal bodies.
They are ovoviviparous, giving birth to up to six young at a time. They range from 0.48 to
5.1 m (1.6 to 16.7 ft) in length and 7 m (23 ft) in wingspan.
"""
""",
)
print("Generated summary is :", summary)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Literal

from mellea import generative, start_session
from mellea.stdlib.genslot import PreconditionException
from mellea.stdlib.requirement import Requirement, simple_validate
from mellea.stdlib.sampling.base import RejectionSamplingStrategy


@generative
def classify_sentiment(text: str) -> Literal["positive", "negative", "unknown"]:
"""Classify the sentiment of the text."""
...


if __name__ == "__main__":
m = start_session()

# Add preconditions and requirements.
sentiment_component = classify_sentiment(
m,
text="I love this!",
# Preconditions are only checked with basic validation. Don't use the strategy.
precondition_requirements=["the text arg should be less than 100 words"],
Copy link
Contributor

Choose a reason for hiding this comment

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

That's cool. Is there a scenario where a precondition_check would be helpful for m.instruct as well?

Copy link
Contributor Author

@jakelorocco jakelorocco Nov 10, 2025

Choose a reason for hiding this comment

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

I think most of the functionality that we add to a function like @generative or m.instruct, can / should be exposed at the level of m.act at least. However, I think we would need to figure out all the hooks / processing / validation steps we want before doing that.

# Reqs to use with the strategy. You could also just remove "unknown" from the structured output for this.
requirements=["avoid classifying the sentiment as unknown"],
strategy=RejectionSamplingStrategy(), # Must specify a strategy for gen slots
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking of these parameters as parameters to the decorator. Was. there a reason to add it to the function call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, there were some python type hinting limitations that could not be easily overcome: #190 (comment)


print(
f"Prompt to the model looked like:\n```\n{m.last_prompt()[0]['content']}\n```"
) # type: ignore
# Prompt to the model looked like:
# ```
# Your task is to imitate the output of the following function for the given arguments.
# Reply Nothing else but the output of the function.

# Function:
# def classify_sentiment(text: str) -> Literal['positive', 'negative', 'unknown']:
# """Classify the sentiment of the text.

# Postconditions:
# - avoid classifying the sentiment as unknown
# """

# Arguments:
# - text: "I love this!" (type: <class 'str'>)
# ```

print("\nOutput sentiment is:", sentiment_component)

# We can also force a precondition failure.
try:
sentiment_component = classify_sentiment(
m,
text="I hate this!",
# Requirement always fails to validate given the lambda.
precondition_requirements=[
Requirement(
"the text arg should be only one word",
validation_fn=simple_validate(lambda x: (False, "Forced to fail!")),
)
],
)
except PreconditionException as e:
print(f"exception: {str(e)}")

# Look at why the precondition validation failed.
print("Failure reasons:")
for val_result in e.validation:
print("-", val_result.reason)
Loading