MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / _advance_tool_call_state

Method _advance_tool_call_state

examples/server/server.py:6003–6315  ·  view source on GitHub ↗
(
        self,
        item_state: Dict[str, Any],
        text: str,
        item_plan: Dict[str, Any],
    )

Source from the content-addressed store, hash-verified

6001 return False, deltas
6002
6003 def _advance_tool_call_state(
6004 self,
6005 item_state: Dict[str, Any],
6006 text: str,
6007 item_plan: Dict[str, Any],
6008 ) -> Tuple[bool, List[Dict[str, Any]]]:
6009 deltas: List[Dict[str, Any]] = []
6010 if item_plan["kind"] == "buffered":
6011 item_state["buffer"] = item_state["buffer"] + text
6012 return True, deltas
6013 if item_plan["kind"] == "json-message":
6014 buffer = item_state["pending"] + text
6015 item_state["pending"] = ""
6016 while True:
6017 mode = item_state["mode"]
6018 if mode == "function-name":
6019 name_capture = item_plan["name_capture"]
6020 name_prefix = name_capture["start"]
6021 if buffer.startswith(name_prefix):
6022 remainder = buffer[len(name_prefix) :]
6023 elif name_prefix.startswith(buffer):
6024 item_state["pending"] = buffer
6025 return True, deltas
6026 else:
6027 return False, deltas
6028 name_end = 0
6029 while name_end < len(remainder) and (
6030 remainder[name_end].isalnum() or remainder[name_end] == "_"
6031 ):
6032 name_end += 1
6033 if name_end == 0:
6034 if not remainder:
6035 item_state["pending"] = buffer
6036 return True, deltas
6037 return False, deltas
6038 if name_end == len(remainder):
6039 item_state["pending"] = buffer
6040 return True, deltas
6041 function_name = remainder[:name_end]
6042 item_state["tool_call"]["function"]["name"] = function_name
6043 tool_call_index = cast(int, item_state["tool_call_index"])
6044 deltas.append(
6045 {
6046 "tool_calls": [
6047 {
6048 "index": tool_call_index,
6049 "id": (
6050 f"call_{self._choice_index}_{function_name}_"
6051 f"{self._completion_id}_{tool_call_index}"
6052 ),
6053 "type": "function",
6054 "function": {"name": function_name},
6055 }
6056 ]
6057 }
6058 )
6059 buffer = remainder[name_end:]
6060 item_state["mode"] = "seek-arguments"

Callers 1

_advance_stream_stateMethod · 0.95

Calls 5

_advance_json_scannerMethod · 0.95
_coerce_tool_argumentMethod · 0.95
appendMethod · 0.80

Tested by

no test coverage detected