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

Update dataclasses.replace calls in DefaultRetryStrategy #112

Merged
merged 8 commits into from
Sep 5, 2024
4 changes: 3 additions & 1 deletion spice/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def get_model_from_name(model_name: str) -> Model:
else:
input_cost = None
output_cost = None
model = dataclasses.replace(model, name=model_name, input_cost=input_cost, output_cost=output_cost)
model = model.model_copy(
update={"name": model_name, "input_cost": input_cost, "output_cost": output_cost}
)
Copy link
Member

Choose a reason for hiding this comment

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

this is still a dataclass, so shouldn't be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I apologize for the confusion. The Model class is indeed still a dataclass. The change from dataclasses.replace to model_copy was only necessary for the SpiceCallArgs class, which has been converted to a Pydantic model. I'll update the PR to keep the Model class as a dataclass and only change the SpiceCallArgs related code.

return model

return UnknownModel(model_name, None)
5 changes: 2 additions & 3 deletions spice/retry_strategy/default_strategy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dataclasses
from typing import Any, Callable, Optional

from spice.call_args import SpiceCallArgs
Expand All @@ -17,9 +16,9 @@ def decide(
self, call_args: SpiceCallArgs, attempt_number: int, model_output: str, name: str
) -> tuple[Behavior, SpiceCallArgs, Any, str]:
if attempt_number == 1 and call_args.temperature is not None:
call_args = dataclasses.replace(call_args, temperature=max(0.2, call_args.temperature))
call_args = call_args.model_copy(update={"temperature": max(0.2, call_args.temperature)})
elif attempt_number > 1 and call_args.temperature is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
call_args = call_args.model_copy(update={"temperature": max(0.2, call_args.temperature)})
call_args = call_args.model_copy(update={"temperature": max(0.2, call_args.temperature or 0)})

It's a good practice to provide a default value when using or with None. This ensures that max() always receives two numeric values, even if call_args.temperature is None.

call_args = dataclasses.replace(call_args, temperature=max(0.5, call_args.temperature))
call_args = call_args.model_copy(update={"temperature": max(0.5, call_args.temperature)})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
call_args = call_args.model_copy(update={"temperature": max(0.5, call_args.temperature)})
call_args = call_args.model_copy(update={"temperature": max(0.5, call_args.temperature or 0)})

Similar to the previous comment, it's safer to provide a default value when call_args.temperature might be None.

if self.validator and not self.validator(model_output):
if attempt_number < self.retries:
Expand Down
Loading