Spaces:
Sleeping
Sleeping
ping98k commited on
Commit ·
6d50a75
1
Parent(s): 3404ee0
Switch to chat_template_kwargs for thinking
Browse files- README.md +3 -4
- main.py +0 -4
- tests/test_main.py +1 -1
- tests/test_tournament_utils.py +3 -3
- tournament_utils.py +3 -18
README.md
CHANGED
|
@@ -24,12 +24,11 @@ This project provides a small interface for running "tournaments" between langua
|
|
| 24 |
- `ENABLE_GENERATE_THINKING`
|
| 25 |
- `ENABLE_SCORE_THINKING`
|
| 26 |
- `ENABLE_PAIRWISE_THINKING`
|
| 27 |
-
|
| 28 |
-
|
| 29 |
When any of the thinking flags are enabled, the app sends
|
| 30 |
-
`
|
| 31 |
`litellm.completion` call for that model. Otherwise it sends
|
| 32 |
-
`
|
| 33 |
2. Install dependencies (example with `pip`):
|
| 34 |
```bash
|
| 35 |
pip install gradio litellm python-dotenv tqdm matplotlib
|
|
|
|
| 24 |
- `ENABLE_GENERATE_THINKING`
|
| 25 |
- `ENABLE_SCORE_THINKING`
|
| 26 |
- `ENABLE_PAIRWISE_THINKING`
|
| 27 |
+
|
|
|
|
| 28 |
When any of the thinking flags are enabled, the app sends
|
| 29 |
+
`chat_template_kwargs={"enable_thinking": True}` with each
|
| 30 |
`litellm.completion` call for that model. Otherwise it sends
|
| 31 |
+
`chat_template_kwargs={"enable_thinking": False}`.
|
| 32 |
2. Install dependencies (example with `pip`):
|
| 33 |
```bash
|
| 34 |
pip install gradio litellm python-dotenv tqdm matplotlib
|
main.py
CHANGED
|
@@ -48,7 +48,6 @@ PAIRWISE_WITH_INSTRUCTION_DEFAULT = os.getenv("PASS_INSTRUCTION_TO_PAIRWISE", "t
|
|
| 48 |
GENERATE_THINKING_DEFAULT = os.getenv("ENABLE_GENERATE_THINKING", "false").lower() == "true"
|
| 49 |
SCORE_THINKING_DEFAULT = os.getenv("ENABLE_SCORE_THINKING", "false").lower() == "true"
|
| 50 |
PAIRWISE_THINKING_DEFAULT = os.getenv("ENABLE_PAIRWISE_THINKING", "false").lower() == "true"
|
| 51 |
-
THINKING_BUDGET_TOKENS_DEFAULT = int(os.getenv("THINKING_BUDGET_TOKENS", "1024"))
|
| 52 |
CRITERIA_DEFAULT = "Factuality,Instruction Following,Precision"
|
| 53 |
def _clean_json(txt):
|
| 54 |
txt = re.sub(r"^```.*?\n|```$", "", txt, flags=re.DOTALL).strip()
|
|
@@ -164,7 +163,6 @@ def run_tournament(
|
|
| 164 |
api_key=api_token,
|
| 165 |
temperature=generate_temperature,
|
| 166 |
thinking=generate_thinking,
|
| 167 |
-
budget_tokens=THINKING_BUDGET_TOKENS_DEFAULT,
|
| 168 |
return_usage=True,
|
| 169 |
)
|
| 170 |
add_usage(usage)
|
|
@@ -190,7 +188,6 @@ def run_tournament(
|
|
| 190 |
temperature=score_temperature,
|
| 191 |
include_instruction=score_with_instruction,
|
| 192 |
thinking=score_thinking,
|
| 193 |
-
budget_tokens=THINKING_BUDGET_TOKENS_DEFAULT,
|
| 194 |
return_usage=True,
|
| 195 |
)
|
| 196 |
add_usage(usage)
|
|
@@ -230,7 +227,6 @@ def run_tournament(
|
|
| 230 |
temperature=pairwise_temperature,
|
| 231 |
include_instruction=pairwise_with_instruction,
|
| 232 |
thinking=pairwise_thinking,
|
| 233 |
-
budget_tokens=THINKING_BUDGET_TOKENS_DEFAULT,
|
| 234 |
return_usage=True,
|
| 235 |
)
|
| 236 |
add_usage(usage)
|
|
|
|
| 48 |
GENERATE_THINKING_DEFAULT = os.getenv("ENABLE_GENERATE_THINKING", "false").lower() == "true"
|
| 49 |
SCORE_THINKING_DEFAULT = os.getenv("ENABLE_SCORE_THINKING", "false").lower() == "true"
|
| 50 |
PAIRWISE_THINKING_DEFAULT = os.getenv("ENABLE_PAIRWISE_THINKING", "false").lower() == "true"
|
|
|
|
| 51 |
CRITERIA_DEFAULT = "Factuality,Instruction Following,Precision"
|
| 52 |
def _clean_json(txt):
|
| 53 |
txt = re.sub(r"^```.*?\n|```$", "", txt, flags=re.DOTALL).strip()
|
|
|
|
| 163 |
api_key=api_token,
|
| 164 |
temperature=generate_temperature,
|
| 165 |
thinking=generate_thinking,
|
|
|
|
| 166 |
return_usage=True,
|
| 167 |
)
|
| 168 |
add_usage(usage)
|
|
|
|
| 188 |
temperature=score_temperature,
|
| 189 |
include_instruction=score_with_instruction,
|
| 190 |
thinking=score_thinking,
|
|
|
|
| 191 |
return_usage=True,
|
| 192 |
)
|
| 193 |
add_usage(usage)
|
|
|
|
| 227 |
temperature=pairwise_temperature,
|
| 228 |
include_instruction=pairwise_with_instruction,
|
| 229 |
thinking=pairwise_thinking,
|
|
|
|
| 230 |
return_usage=True,
|
| 231 |
)
|
| 232 |
add_usage(usage)
|
tests/test_main.py
CHANGED
|
@@ -115,7 +115,7 @@ def test_run_tournament_full_loop():
|
|
| 115 |
assert 'Done' in process_log
|
| 116 |
assert hist_fig == 'fig'
|
| 117 |
assert top_picks.strip() in {'p1', 'p2'}
|
| 118 |
-
mock_gen.assert_called_once_with('instr', 4, model='gm', api_base='b', api_key='k', temperature=1, thinking=True,
|
| 119 |
assert 'Score completion' in process_log
|
| 120 |
assert 'Pairwise completion' in process_log
|
| 121 |
assert 'Prompt tokens' in usage
|
|
|
|
| 115 |
assert 'Done' in process_log
|
| 116 |
assert hist_fig == 'fig'
|
| 117 |
assert top_picks.strip() in {'p1', 'p2'}
|
| 118 |
+
mock_gen.assert_called_once_with('instr', 4, model='gm', api_base='b', api_key='k', temperature=1, thinking=True, return_usage=True)
|
| 119 |
assert 'Score completion' in process_log
|
| 120 |
assert 'Pairwise completion' in process_log
|
| 121 |
assert 'Prompt tokens' in usage
|
tests/test_tournament_utils.py
CHANGED
|
@@ -26,7 +26,7 @@ def test_generate_players():
|
|
| 26 |
resp = make_response([" player1 ", "player2\n"])
|
| 27 |
with patch('tournament_utils.completion', return_value=resp) as mock_comp:
|
| 28 |
players = tu.generate_players('instr', 2, model='m', api_base='b', api_key='k', temperature=0.5)
|
| 29 |
-
mock_comp.assert_called_once_with(model='m', messages=[{'role': 'user', 'content': 'instr'}], n=2, api_base='b', api_key='k', temperature=0.5,
|
| 30 |
assert players == ['player1', 'player2']
|
| 31 |
|
| 32 |
|
|
@@ -60,7 +60,7 @@ def test_thinking_passed_to_completion():
|
|
| 60 |
tu.prompt_pairwise('i', 'block', 'a', 'b', thinking=True)
|
| 61 |
assert mock_comp.call_count == 3
|
| 62 |
for call in mock_comp.call_args_list:
|
| 63 |
-
assert call.kwargs['
|
| 64 |
|
| 65 |
|
| 66 |
def test_thinking_disabled_by_default():
|
|
@@ -71,4 +71,4 @@ def test_thinking_disabled_by_default():
|
|
| 71 |
tu.prompt_pairwise('i', 'block', 'a', 'b')
|
| 72 |
assert mock_comp.call_count == 3
|
| 73 |
for call in mock_comp.call_args_list:
|
| 74 |
-
assert call.kwargs['
|
|
|
|
| 26 |
resp = make_response([" player1 ", "player2\n"])
|
| 27 |
with patch('tournament_utils.completion', return_value=resp) as mock_comp:
|
| 28 |
players = tu.generate_players('instr', 2, model='m', api_base='b', api_key='k', temperature=0.5)
|
| 29 |
+
mock_comp.assert_called_once_with(model='m', messages=[{'role': 'user', 'content': 'instr'}], n=2, api_base='b', api_key='k', temperature=0.5, chat_template_kwargs={'enable_thinking': False})
|
| 30 |
assert players == ['player1', 'player2']
|
| 31 |
|
| 32 |
|
|
|
|
| 60 |
tu.prompt_pairwise('i', 'block', 'a', 'b', thinking=True)
|
| 61 |
assert mock_comp.call_count == 3
|
| 62 |
for call in mock_comp.call_args_list:
|
| 63 |
+
assert call.kwargs['chat_template_kwargs'] == {'enable_thinking': True}
|
| 64 |
|
| 65 |
|
| 66 |
def test_thinking_disabled_by_default():
|
|
|
|
| 71 |
tu.prompt_pairwise('i', 'block', 'a', 'b')
|
| 72 |
assert mock_comp.call_count == 3
|
| 73 |
for call in mock_comp.call_args_list:
|
| 74 |
+
assert call.kwargs['chat_template_kwargs'] == {'enable_thinking': False}
|
tournament_utils.py
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
-
import os
|
| 2 |
from litellm import completion
|
| 3 |
|
| 4 |
-
BUDGET_TOKENS_DEFAULT = int(os.getenv("THINKING_BUDGET_TOKENS", "1024"))
|
| 5 |
-
|
| 6 |
|
| 7 |
def _completion_kwargs(
|
| 8 |
api_base: str | None,
|
|
@@ -29,7 +26,6 @@ def generate_players(
|
|
| 29 |
api_key: str | None = None,
|
| 30 |
temperature: float | None = None,
|
| 31 |
thinking: bool = False,
|
| 32 |
-
budget_tokens: int = BUDGET_TOKENS_DEFAULT,
|
| 33 |
return_usage: bool = False,
|
| 34 |
) -> list[str] | tuple[list[str], object]:
|
| 35 |
"""Request ``n`` completions for the instruction using the given model.
|
|
@@ -39,10 +35,7 @@ def generate_players(
|
|
| 39 |
"""
|
| 40 |
messages = [{"role": "user", "content": instruction}]
|
| 41 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 42 |
-
kwargs["
|
| 43 |
-
"type": "enabled" if thinking else "disabled",
|
| 44 |
-
"budget_tokens": budget_tokens if thinking else 0,
|
| 45 |
-
}
|
| 46 |
response = completion(
|
| 47 |
model=model,
|
| 48 |
messages=messages,
|
|
@@ -67,7 +60,6 @@ def prompt_score(
|
|
| 67 |
temperature: float | None = None,
|
| 68 |
include_instruction: bool = True,
|
| 69 |
thinking: bool = False,
|
| 70 |
-
budget_tokens: int = BUDGET_TOKENS_DEFAULT,
|
| 71 |
return_usage: bool = False,
|
| 72 |
) -> str | tuple[str, object]:
|
| 73 |
"""Return a JSON score string evaluating `player` on the criteria."""
|
|
@@ -80,10 +72,7 @@ Return JSON exactly like: {{"scores": [{example_scores}]}}."""
|
|
| 80 |
prompt += f"\n\nInstruction:\n{instruction}"
|
| 81 |
prompt += f"\n\nOutput:\n{player}"
|
| 82 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 83 |
-
kwargs["
|
| 84 |
-
"type": "enabled" if thinking else "disabled",
|
| 85 |
-
"budget_tokens": budget_tokens if thinking else 0,
|
| 86 |
-
}
|
| 87 |
response = completion(
|
| 88 |
model=model,
|
| 89 |
messages=[{"role": "system", "content": prompt}],
|
|
@@ -107,7 +96,6 @@ def prompt_pairwise(
|
|
| 107 |
temperature: float | None = None,
|
| 108 |
include_instruction: bool = True,
|
| 109 |
thinking: bool = False,
|
| 110 |
-
budget_tokens: int = BUDGET_TOKENS_DEFAULT,
|
| 111 |
return_usage: bool = False,
|
| 112 |
) -> str | tuple[str, object]:
|
| 113 |
"""Return which player wins in JSON using the given criteria."""
|
|
@@ -119,10 +107,7 @@ Return ONLY JSON {{"winner": "A"}} or {{"winner": "B"}}."""
|
|
| 119 |
prompt += f"\n\nInstruction:\n{instruction}"
|
| 120 |
prompt += f"\n\nPlayers:\n<A>{a}</A>\n<B>{b}</B>"
|
| 121 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 122 |
-
kwargs["
|
| 123 |
-
"type": "enabled" if thinking else "disabled",
|
| 124 |
-
"budget_tokens": budget_tokens if thinking else 0,
|
| 125 |
-
}
|
| 126 |
response = completion(
|
| 127 |
model=model,
|
| 128 |
messages=[{"role": "system", "content": prompt}],
|
|
|
|
|
|
|
| 1 |
from litellm import completion
|
| 2 |
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def _completion_kwargs(
|
| 5 |
api_base: str | None,
|
|
|
|
| 26 |
api_key: str | None = None,
|
| 27 |
temperature: float | None = None,
|
| 28 |
thinking: bool = False,
|
|
|
|
| 29 |
return_usage: bool = False,
|
| 30 |
) -> list[str] | tuple[list[str], object]:
|
| 31 |
"""Request ``n`` completions for the instruction using the given model.
|
|
|
|
| 35 |
"""
|
| 36 |
messages = [{"role": "user", "content": instruction}]
|
| 37 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 38 |
+
kwargs["chat_template_kwargs"] = {"enable_thinking": thinking}
|
|
|
|
|
|
|
|
|
|
| 39 |
response = completion(
|
| 40 |
model=model,
|
| 41 |
messages=messages,
|
|
|
|
| 60 |
temperature: float | None = None,
|
| 61 |
include_instruction: bool = True,
|
| 62 |
thinking: bool = False,
|
|
|
|
| 63 |
return_usage: bool = False,
|
| 64 |
) -> str | tuple[str, object]:
|
| 65 |
"""Return a JSON score string evaluating `player` on the criteria."""
|
|
|
|
| 72 |
prompt += f"\n\nInstruction:\n{instruction}"
|
| 73 |
prompt += f"\n\nOutput:\n{player}"
|
| 74 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 75 |
+
kwargs["chat_template_kwargs"] = {"enable_thinking": thinking}
|
|
|
|
|
|
|
|
|
|
| 76 |
response = completion(
|
| 77 |
model=model,
|
| 78 |
messages=[{"role": "system", "content": prompt}],
|
|
|
|
| 96 |
temperature: float | None = None,
|
| 97 |
include_instruction: bool = True,
|
| 98 |
thinking: bool = False,
|
|
|
|
| 99 |
return_usage: bool = False,
|
| 100 |
) -> str | tuple[str, object]:
|
| 101 |
"""Return which player wins in JSON using the given criteria."""
|
|
|
|
| 107 |
prompt += f"\n\nInstruction:\n{instruction}"
|
| 108 |
prompt += f"\n\nPlayers:\n<A>{a}</A>\n<B>{b}</B>"
|
| 109 |
kwargs = _completion_kwargs(api_base, api_key, temperature)
|
| 110 |
+
kwargs["chat_template_kwargs"] = {"enable_thinking": thinking}
|
|
|
|
|
|
|
|
|
|
| 111 |
response = completion(
|
| 112 |
model=model,
|
| 113 |
messages=[{"role": "system", "content": prompt}],
|