Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion guidance/models/_openai_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def rule(self, node: RuleNode, **kwargs) -> Iterator[OutputAttr]:
raise ValueError("Save stop text not yet supported for OpenAI")

kwargs = kwargs.copy()
if node.temperature:
if node.temperature is not None:
kwargs["temperature"] = node.temperature
if node.max_tokens:
kwargs["max_completion_tokens"] = node.max_tokens
Expand Down
2 changes: 1 addition & 1 deletion guidance/models/experimental/_litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def rule(self, node: RuleNode, **kwargs) -> Iterator[OutputAttr]:
raise ValueError(f"stop_capture not yet supported for {self.ep_type} endpoint")

kwargs = kwargs.copy()
if node.temperature:
if node.temperature is not None:
kwargs["temperature"] = node.temperature
if node.max_tokens:
kwargs["max_tokens"] = node.max_tokens
Expand Down
2 changes: 1 addition & 1 deletion guidance/models/experimental/_sglang.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def rule(self, node: RuleNode, **kwargs) -> Iterator[OutputAttr]:
raise ValueError("stop_capture not yet supported for sglang endpoint")

kwargs = kwargs.copy()
if node.temperature:
if node.temperature is not None:
kwargs["temperature"] = node.temperature
if node.max_tokens:
kwargs["max_tokens"] = node.max_tokens
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_openai_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from guidance._grammar import gen
from guidance.models._openai_base import OpenAIRuleMixin


def test_temperature_zero_is_passed_through(monkeypatch):
"""temperature=0.0 is the canonical deterministic/greedy setting and must
not be dropped by a truthiness check (it is falsy in Python).
"""
captured: dict = {}

def fake_run(self, node, **kwargs):
captured.update(kwargs)
return iter([])

monkeypatch.setattr(OpenAIRuleMixin, "run", fake_run)
interp = OpenAIRuleMixin.__new__(OpenAIRuleMixin)
node = gen(temperature=0.0)
list(interp.rule(node))
assert captured.get("temperature") == 0.0


def test_temperature_none_not_passed(monkeypatch):
captured: dict = {}

def fake_run(self, node, **kwargs):
captured.update(kwargs)
return iter([])

monkeypatch.setattr(OpenAIRuleMixin, "run", fake_run)
interp = OpenAIRuleMixin.__new__(OpenAIRuleMixin)
node = gen()
list(interp.rule(node))
assert "temperature" not in captured