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

Add an after model validator ensuring temp=1 for o1 models #649

Merged
merged 8 commits into from
Oct 29, 2024
14 changes: 14 additions & 0 deletions paperqa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,20 @@ def _deprecated_field(self) -> Self:
setattr(self.agent.index, new_name, value) # Propagate to new location
return self

@model_validator(mode="after")
def _validate_temperature_for_o1_preview(self) -> Self:
dakoner marked this conversation as resolved.
Show resolved Hide resolved
"""Ensures temperature is 1 if the LLM is 'o1-preview' or 'o1-mini'.

o1 reasoning models only support temperature = 1. See
https://platform.openai.com/docs/guides/reasoning/quickstart
"""
if self.llm.startswith("o1-") and self.temperature != 1:
raise ValueError(
"When dealing with OpenAI o1 models, the temperature"
f" must be set to 1, {self.temperature} was specified."
)
return self

@computed_field # type: ignore[prop-decorator]
@property
def md5(self) -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ def test_router_kwargs_present_in_models() -> None:
settings = Settings()
assert settings.get_llm().config["router_kwargs"] is not None
assert settings.get_summary_llm().config["router_kwargs"] is not None


def test_o1_requires_temp_equals_1() -> None:
with pytest.raises(ValidationError):
_ = Settings(llm="o1-thismodeldoesnotexist", temperature=0)
_ = Settings(llm="o1-thismodeldoesnotexist", temperature=1)
Loading