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
13 changes: 13 additions & 0 deletions terminal/serve/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3405,6 +3405,19 @@ def __init__(self, thinking: bool = True, tools: list[dict] | None = None, strea
self.mode = "content" if opened else "between"
self.header = "" # the block's header: a call's tool name

# The state the server reads (Service._run) for each mode: "reasoning" in a thinking block, "content" between
# blocks (a forced call's opening goes there once the thinking is closed), "text" inside a text block
STATES = {"between": "content", "header": "header", "reasoning": "reasoning", "content": "text", "call": "call"}

@property
def state(self) -> str:
mode = getattr(self, "mode", None)
return self.STATES[mode] if mode is not None else self._state

@state.setter
def state(self, value: str):
self._state = value # OutputParser.__init__'s; this parser's state is its mode

@property
def clean(self) -> bool:
return not self.ink
Expand Down
2 changes: 2 additions & 0 deletions terminal/serve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6622,6 +6622,8 @@ def replica_selected(index: int) -> None:
if wrap:
budget = st["thinking_budget"] = None
text = closed_with(REASONING_WRAP_UP, parser.think_end) + (force or "")
elif self.call_format == "inkling": # a block of its own: no blank line before it
text = force
else:
text = "\n" * (2 - (len(tail) - len(tail.rstrip("\n")))) + force
force = None
Expand Down
26 changes: 26 additions & 0 deletions terminal/serve/test_inkling.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ def test_a_reply_goes_on_in_the_text_block_a_prompt_opened(self):
if kind == "event"]
self.assertEqual((said(evs, "reasoning"), said(evs, "content")), (reasoning, "Rome"))

def test_a_named_tool_choice_is_opened_once_the_thinking_is_closed(self):
# the model thinks; the server writes the call's opening right after the closed thinking block, in place of
# the text block the model would open, and the model goes on with the arguments
tools = [{"name": "get_weather", "parameters": {"properties": {"city": {"type": "string"}}}}]
force = self.svc.force_text(FUNC_START + "get_weather>\n")
ids = self.svc.encode_prompt(self.chat, tools, {})
self.svc.engine = MockEngine(self.tok, [THINK + "<|message_model|><|content_text|>No tool needed.",
'{"city":"Rome"}}' + END + STOP])
evs = [e for kind, e in self.svc.run(ids, True, tools, 256, {}, threading.Event(), force) if kind == "event"]
forced = self.tok.encode(force, parse_special=True)
self.assertEqual(self.svc.engine.last_prompt[-len(forced):], forced)
self.assertEqual(self.tok.decode(self.svc.engine.last_prompt[len(ids):]), THINK + force)
self.assertEqual(said(evs, "reasoning"), "the capital of Italy")
self.assertEqual([(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"],
[("get_weather", {"city": "Rome"})])
self.assertEqual(said(evs, "content"), "")

def test_the_parser_says_where_a_forced_call_can_go(self):
p = InklingOutputParser(tools=SCHEMA)
p.feed("<|message_model|><|content_thinking|>x")
self.assertEqual(p.state, "reasoning")
p.feed(END)
self.assertEqual((p.state, p.clean), ("content", True)) # between blocks
p.feed("<|message_model|><|content_text|>y")
self.assertEqual(p.state, "text") # not inside a text block


if __name__ == "__main__":
unittest.main()
Loading